Advertisement
Guest User

Untitled

a guest
Sep 16th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.86 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Mindscape.LightSpeed;
  4. using Mindscape.LightSpeed.Logging;
  5. using NUnit.Framework;
  6.  
  7. namespace LightSpeedRepro
  8. {
  9.  
  10.     public class FooBarDTO
  11.     {
  12.         public int Id { get; set; }
  13.         public string Name { get; set; }
  14.         public IEnumerable<string> Bars { get; set; }
  15.     }
  16.  
  17.     [TestFixture]
  18.     public class Class1
  19.     {
  20.  
  21.         public IQueryable<FooBarDTO> getQuery(IQueryable<Foo> foos, IQueryable<Bar> bars)
  22.         {
  23.             return
  24.                 (from foo in foos
  25.                 join bar in bars on foo.Id equals bar.FooId into foobar
  26.                 let barlist = foobar.OrderBy(x => x.Name).Select(x => x.Name)
  27.                 select new FooBarDTO()
  28.                             {
  29.                                 Id = foo.Id,
  30.                                 Name = foo.Name,
  31.                                 Bars = barlist
  32.                             }).Take(1);
  33.         }
  34.  
  35.         [Test]
  36.         public void TestLightSpeed()
  37.         {
  38.             //Arrange
  39.             var context = new LightSpeedContext<LightSpeedModel1UnitOfWork>("Context")
  40.                 {
  41.                     Logger = new ConsoleLogger(),
  42.                 };
  43.  
  44.             using (var uow = context.CreateUnitOfWork())
  45.             {
  46.  
  47.                 var foo1 = new Foo() {Name = "Foo_1"};
  48.                 var foo2 = new Foo() {Name = "Foo_2"};
  49.  
  50.                 uow.Add(foo1);
  51.                 uow.Add(foo2);
  52.  
  53.                 uow.Add(new Bar() {Name = "Foo_1_Bar_1", Foo = foo1});
  54.                 uow.Add(new Bar() {Name = "Foo_1_Bar_2", Foo = foo1});
  55.                 uow.Add(new Bar() {Name = "Foo_1_Bar_3", Foo = foo1});
  56.                 uow.Add(new Bar() {Name = "Foo_2_Bar_4", Foo = foo2});
  57.                 uow.Add(new Bar() {Name = "Foo_2_Bar_5", Foo = foo2});
  58.                 uow.Add(new Bar() {Name = "Foo_2_Bar_6", Foo = foo2});
  59.  
  60.                 uow.SaveChanges(true);
  61.             }
  62.  
  63.             //Act
  64.             using (var uow = context.CreateUnitOfWork())
  65.             {
  66.  
  67.                 var query = getQuery(uow.Foos, uow.Bars);
  68.  
  69.                 //Assert
  70.                 var actual = query.OrderBy(x => x.Name).FirstOrDefault();
  71.  
  72.                 Assert.AreEqual("Foo_1", actual.Name);
  73.                 Assert.AreEqual(3, actual.Bars.Count());
  74.                 Assert.AreEqual("Foo_1_Bar_1", actual.Bars.ToArray()[0]);
  75.                 Assert.AreEqual("Foo_1_Bar_2", actual.Bars.ToArray()[1]);
  76.                 Assert.AreEqual("Foo_1_Bar_3", actual.Bars.ToArray()[2]);
  77.  
  78.             }
  79.         }
  80.  
  81.  
  82.         [Test]
  83.         public void TestStandardLinq()
  84.         {
  85.             //Arrange
  86.             var foos = new List<Foo>()
  87.                 {
  88.                     new Foo() {Name = "Foo_1", Id = 1},
  89.                     new Foo() {Name = "Foo_2", Id = 2},
  90.                 };
  91.             var bars = new List<Bar>()
  92.                 {
  93.                     new Bar() {Name = "Foo_1_Bar_1", FooId = 1, Id = 1},
  94.                     new Bar() {Name = "Foo_1_Bar_2", FooId = 1, Id = 2},
  95.                     new Bar() {Name = "Foo_1_Bar_3", FooId = 1, Id = 3},
  96.                     new Bar() {Name = "Foo_2_Bar_4", FooId = 2, Id = 4},
  97.                     new Bar() {Name = "Foo_2_Bar_5", FooId = 2, Id = 5},
  98.                     new Bar() {Name = "Foo_2_Bar_6", FooId = 2, Id = 6},
  99.                 };
  100.  
  101.             var query = getQuery(foos.AsQueryable(), bars.AsQueryable());
  102.             //Assert
  103.             var actual = query.OrderBy(x => x.Name).FirstOrDefault();
  104.  
  105.             Assert.AreEqual("Foo_1", actual.Name);
  106.             Assert.AreEqual(3, actual.Bars.Count());
  107.             Assert.AreEqual("Foo_1_Bar_1", actual.Bars.ToArray()[0]);
  108.             Assert.AreEqual("Foo_1_Bar_2", actual.Bars.ToArray()[1]);
  109.             Assert.AreEqual("Foo_1_Bar_3", actual.Bars.ToArray()[2]);
  110.         }
  111.  
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement