public async void UpdateItemsInPreExistantList() { using var factory = new TestContextFactory(); using var context = factory.CreateContext(); using var unitOfWork = new EFUnitOfWork(context); // ensure is a new database await context.Database.EnsureDeletedAsync(); // ensure the database has been created await context.Database.EnsureCreatedAsync(); RandomizerList list = (await unitOfWork.Repository <SimpleRandomizerList>().Get(1)); list.AddItem(new TextRandomizerItem("Chouffe")); list.AddItem(new TextRandomizerItem("Goudale")); list.AddItem(new TextRandomizerItem("Leffe")); await unitOfWork.Repository <RandomizerList>().Update(list.Id, list); await unitOfWork.SaveChangesAsync(); list = (await unitOfWork.Repository <RandomizerList>().Get(list.Id)); list.Items.Count().Should().Be(0); list = (await unitOfWork.Repository <RandomizerList>().Set.Include(l => l.Items).Where(i => i.Id == list.Id).FirstOrDefaultAsync()); list.Items.Count().Should().Be(3); (await unitOfWork.Repository <RandomizerItem>().GetItems(0, 10)).Count().Should().Be(3); }
public async void DeleteItemsInPreExistantList() { using var factory = new TestContextFactory(); using var context = factory.CreateContext(); using var unitOfWork = new EFUnitOfWork(context); // ensure is a new database await context.Database.EnsureDeletedAsync(); // ensure the database has been created await context.Database.EnsureCreatedAsync(); (await unitOfWork.Repository <RandomizerItem>().GetItems(0, 10)).Count().Should().Be(0); RandomizerList list = (await unitOfWork.Repository <SimpleRandomizerList>().Get(1)); var cuvéeDesTrolls = new TextRandomizerItem("Cuvée des trolls", list); list.AddItem(cuvéeDesTrolls); list.ContainsItem(cuvéeDesTrolls).Should().BeTrue(); await unitOfWork.SaveChangesAsync(); (await unitOfWork.Repository <RandomizerItem>().GetItems(0, 10)).Count().Should().Be(1); list = (await unitOfWork.Repository <RandomizerList>().Get(list.Id)); list.Items.Count.Should().Be(1); list.RemoveItem(cuvéeDesTrolls); list.Items.Count.Should().Be(0); await unitOfWork.SaveChangesAsync(); list = (await unitOfWork.Repository <RandomizerList>().Get(list.Id)); list.Items.Count.Should().Be(0); (await unitOfWork.Repository <RandomizerItem>().GetItems(0, 10)).Count().Should().Be(0); }
public async void GetTest() { using var factory = new TestContextFactory(); using var context = factory.CreateContext(); using var unitOfWork = new EFUnitOfWork(context); context.Database.EnsureCreated(); (await unitOfWork.Repository <SimpleRandomizerList>().Get(3)).Name.Should().Be("People"); (await unitOfWork.Repository <SimpleRandomizerList>().Get(4)).Should().BeNull(); }
public async void GetItemsTest() { using var factory = new TestContextFactory(); using var context = factory.CreateContext(); using var unitOfWork = new EFUnitOfWork(context); context.Database.EnsureCreated(); (await unitOfWork.Repository <RandomizerList>().GetItems(0, 10)).Count().Should().Be(3); (await unitOfWork.Repository <RandomizerList>().GetItems(0, 2)).Count().Should().Be(2); (await unitOfWork.Repository <RandomizerList>().GetItems(1, 2)).Count().Should().Be(1); }
public async void AddItemsToNewListWithUnitOfWork() { using var factory = new TestContextFactory(); var context = factory.CreateContext(); var unitOfWork = new EFUnitOfWork(context); // ensure is a new database await context.Database.EnsureDeletedAsync(); // ensure the database has been created await context.Database.EnsureCreatedAsync(); // new list RandomizerList l = new SimpleRandomizerList { Name = "More Beers" }; // the items to add var chouffe = new TextRandomizerItem("Chouffe", l); var goudale = new TextRandomizerItem("Goudale", l); var leffe = new TextRandomizerItem("Leffe", l); // adding the items l.AddItem(chouffe); l.AddItem(goudale); l.AddItem(leffe); // adding the list l = await unitOfWork.Repository <RandomizerList>().Add(l); l.Should().NotBeNull(); // item count should still be three l.Items.Count.Should().Be(3); // saving changes await unitOfWork.SaveChangesAsync(); // total lists saved should now be 4 (await unitOfWork.Repository <RandomizerList>().GetItems(0, 10)).Count().Should().Be(4); // recovering the list in three different ways var moreBeers = (await unitOfWork.Repository <RandomizerList>().Get(l.Id)); var moreBeers2 = (await unitOfWork.Repository <RandomizerList>().Set.Include(l => l.Items).Where(i => i.Id == l.Id).FirstOrDefaultAsync()); var moreBeers3 = await unitOfWork.Repository <SimpleRandomizerList>().Set.Include(l => l.Items).Where(i => i.Id == l.Id).FirstOrDefaultAsync(); // should return the same list with three items moreBeers.Items.Count().Should().Be(3); moreBeers2.Items.Count().Should().Be(3); moreBeers3.Items.Count().Should().Be(3); }
public async void RejectChangesTest() { using var factory = new TestContextFactory(); using var context = factory.CreateContext(); using var unitOfWork = new EFUnitOfWork(context); context.Database.EnsureCreated(); await unitOfWork.Repository <RandomizerList>().Remove(1); await unitOfWork.CancelChangesAsync(); await unitOfWork.SaveChangesAsync(); (await unitOfWork.Repository <RandomizerList>().GetItems(0, 10)).Count().Should().Be(3); }
public async void Delete() { using var factory = new TestContextFactory(); using var context = factory.CreateContext(); using var unitOfWork = new EFUnitOfWork(context); context.Database.EnsureCreated(); var chewie = await unitOfWork.Repository <RandomizerList>().Get(1); await unitOfWork.Repository <RandomizerList>().Remove(chewie); await unitOfWork.SaveChangesAsync(); Assert.Empty((await unitOfWork.Repository <RandomizerList>().GetItems(0, 10)).Where(n => n.Name.Equals("Beers"))); }
public async void UpdateTest() { using var factory = new TestContextFactory(); using var context = factory.CreateContext(); using var unitOfWork = new EFUnitOfWork(context); context.Database.EnsureCreated(); var myList = await unitOfWork.Repository <RandomizerList>().Get(1); myList.Name = "My list!"; await unitOfWork.Repository <RandomizerList>().Update(myList); await unitOfWork.SaveChangesAsync(); (await unitOfWork.Repository <RandomizerList>().Get(1)).Name.Should().Be("My list!"); (await unitOfWork.Repository <RandomizerList>().GetItems(0, 10)).Where(n => n.Name.Equals("Beers")).Should().BeEmpty(); }
public async void AddTest() { using var factory = new TestContextFactory(); using var context = factory.CreateContext(); using var unitOfWork = new EFUnitOfWork(context); context.Database.EnsureCreated(); RandomizerList l = new SimpleRandomizerList { Name = "My list" }; l = await unitOfWork.Repository <RandomizerList>().Add(l); l.Should().NotBeNull(); await unitOfWork.SaveChangesAsync(); (await unitOfWork.Repository <RandomizerList>().GetItems(0, 10)).Count().Should().Be(4); }
public async void AddRangeTest() { using var factory = new TestContextFactory(); using var context = factory.CreateContext(); using var unitOfWork = new EFUnitOfWork(context); context.Database.EnsureCreated(); RandomizerList l = new SimpleRandomizerList { Name = "Ma liste incroyable", }; RandomizerList l2 = new SimpleRandomizerList { Name = "My list" }; RandomizerList l3 = new SimpleRandomizerList { Name = "Mi lista genial" }; var result = await unitOfWork.Repository <RandomizerList>().AddRange(l, l2, l3); result.Should().BeTrue(); await unitOfWork.SaveChangesAsync(); (await unitOfWork.Repository <RandomizerList>().GetItems(0, 10)).Count().Should().Be(6); }
public async void AddItemsToNewListWithoutUnitOfWork() { using (var factory = new TestContextFactory()) { await using (var context = factory.CreateContext()) { // ensure is a new database await context.Database.EnsureDeletedAsync(); // ensure the database has been created await context.Database.EnsureCreatedAsync(); } // Create new list RandomizerList l = new SimpleRandomizerList { Name = "More Beers" }; // the items to add var chouffe = new TextRandomizerItem("Chouffe", l); var goudale = new TextRandomizerItem("Goudale", l); var leffe = new TextRandomizerItem("Leffe", l); // adding the items l.AddItem(chouffe); l.AddItem(goudale); l.AddItem(leffe); // list should now have 3 items l.Items.Count().Should().Be(3); await using (var context = factory.CreateContext()) { // adding the list to the RandomizerList set l = context.Set <RandomizerList>().Add(l).Entity; l.Should().NotBeNull(); // the three items should still be there l.Items.Count.Should().Be(3); // saving changes await context.SaveChangesAsync(); } await using (var context = factory.CreateContext()) { // the number of total lists in DB should now be three (context.Set <RandomizerList>().Take(10)).Count().Should().Be(4); // recovering the list in four different ways var moreBeers = context.Set <RandomizerList>().Find(l.Id); var moreBeers2 = context.Set <SimpleRandomizerList>().Include(l => l.Items).Where(i => i.Id == l.Id).FirstOrDefault(); var moreBeers3 = context.Set <RandomizerList>().Include(l => l.Items).ToList().Where(l => l.Id == moreBeers.Id).FirstOrDefault(); var moreBeers4 = context.Lists.Include(list => list.Items).ToList().FirstOrDefault(l => l.Id == moreBeers.Id); // should return the same list with three items moreBeers.Items.Count().Should().Be(3); moreBeers2.Items.Count().Should().Be(3); moreBeers3.Items.Count().Should().Be(3); moreBeers4.Items.Count().Should().Be(3); } } }