public async void GivenObjectIsNotSaved_ItShouldNotGetSavedByOtherSavesAsync() { var options = new DbContextOptionsBuilder <TestContext>() .UseInMemoryDatabase(Helper.GetCallerName()) .Options; var seedData = options.EnsureSeeded(); var seedObject = seedData.First(); using (var context = new TestContext(options)) { var repository = new AsyncRepository <TestObject>(context); var list = await repository.ListAsync(o => o.Id.Equals(seedObject.Id)); var testObject = list.First(); testObject.Name = "ChangedName"; var otherList = await repository.ListAsync(o => !o.Id.Equals(seedObject.Id)); // Potentially saves all tracked objects await repository.EditAsync(otherList.First()); } using (var context = new TestContext(options)) { var repository = new AsyncRepository <TestObject>(context); var list = await repository.ListAsync(o => o.Id.Equals(seedObject.Id)); var testObject = list.First(); testObject.Name.Should().NotBe("ChangedName"); } }
public void GivenDatabaseContainsObjects_ItShouldReturnAllObjects() { var options = new DbContextOptionsBuilder <TestContext>() .UseInMemoryDatabase(Helper.GetCallerName()) .Options; var seedData = options.EnsureSeeded(); IList <TestObject> returnedList; using (var context = new TestContext(options)) { var repository = new Repository <TestObject>(context); returnedList = repository.List(); } returnedList.Should().BeEquivalentTo(seedData); }
public async void GivenDatabaseContainsObjects_ItShouldReturnAllBySpecificationAsync() { var options = new DbContextOptionsBuilder <TestContext>() .UseInMemoryDatabase(Helper.GetCallerName()) .Options; var seedData = options.EnsureSeeded(); IList <TestObject> returnedList; using (var context = new TestContext(options)) { var repository = new AsyncRepository <TestObject>(context); returnedList = await repository.ListAsync(new EmptySpecification <TestObject>(o => true)); } returnedList.Should().BeEquivalentTo(seedData); }
public async void GivenDatabaseHasObjects_ItShouldReturnOneByIdAsync() { var options = new DbContextOptionsBuilder <TestContext>() .UseInMemoryDatabase(Helper.GetCallerName()) .Options; var seedData = options.EnsureSeeded(); var expectedObject = seedData[0]; TestObject foundObject; using (var context = new TestContext(options)) { var repository = new AsyncRepository <TestObject>(context); foundObject = await repository.GetByIdAsync(expectedObject.Id); } foundObject.Should().BeEquivalentTo(expectedObject); }
public async void GivenDatabaseContainsObjects_ItShouldReturnOnlyObjectsWithAThreeInTheNameAsync() { var options = new DbContextOptionsBuilder <TestContext>() .UseInMemoryDatabase(Helper.GetCallerName()) .Options; options.EnsureSeeded(); IList <TestObject> returnedList; using (var context = new TestContext(options)) { var repository = new AsyncRepository <TestObject>(context); returnedList = await repository.ListAsync(o => o.Name.Contains("3")); } returnedList.Should() .OnlyContain(o => o.Name.Contains("3")); }
public async void GivenListByMultipleCriteria_ItShouldReturnObjectsMeetingCriteriaAsync() { var options = new DbContextOptionsBuilder <TestContext>() .UseInMemoryDatabase(Helper.GetCallerName()) .Options; var seedData = options.EnsureSeeded(); var searchingObject = seedData.First(); IList <TestObject> returnedList; using (var context = new TestContext(options)) { var repository = new AsyncRepository <TestObject>(context); returnedList = await repository.ListAsync(o => o.Id.Equals(searchingObject.Id) && o.Name.Equals(searchingObject.Name)); } returnedList.Should() .ContainSingle(o => o.Id.Equals(searchingObject.Id) && o.Name.Equals(searchingObject.Name)); }
public async void GivenDeleteZero_ItShouldRemoveFirstObjectAsync() { var options = new DbContextOptionsBuilder <TestContext>() .UseInMemoryDatabase(Helper.GetCallerName()) .Options; var seedData = options.EnsureSeeded(); using (var context = new TestContext(options)) { var repository = new AsyncRepository <TestObject>(context); await repository.DeleteAsync(seedData[0]); } using (var context = new TestContext(options)) { var foundObject = context.TestObjects.Find(seedData[0].Id); foundObject.Should().BeNull(); } }
public async void GivenObjectContainedInDatabaseIsEdited_ItShouldBePersistedAsync() { var options = new DbContextOptionsBuilder <TestContext>() .UseInMemoryDatabase(Helper.GetCallerName()) .Options; var seedData = options.EnsureSeeded(); var changedObject = seedData[0]; using (var context = new TestContext(options)) { var repository = new AsyncRepository <TestObject>(context); changedObject.Name = "EditedObject"; await repository.EditAsync(changedObject); } using (var context = new TestContext(options)) { var foundObject = context.Find <TestObject>(changedObject.Id); foundObject.Should().BeEquivalentTo(changedObject); } }