public void Get_WithPlantMatchingGuidInRepo_ShouldReturnMatchingPlant() { using (var context = new RamosiContext(options)) { // Arrange var plants = new List <Plant>() { new PlantBuilder() .WithGuid(guidOne) .WithPlantCharacteristic(new PlantCharacteristicBuilder().WithGuid(guidOne)), new PlantBuilder() .WithGuid(guidTwo) .WithPlantCharacteristic(new PlantCharacteristicBuilder().WithGuid(guidTwo)), new PlantBuilder() .WithGuid(guidThree) .WithPlantCharacteristic(new PlantCharacteristicBuilder().WithGuid(guidThree)), }; context.Plants.AddRange(plants); context.SaveChanges(); var repo = new PlantRepository(context); // Act var plant = repo.Get(guidTwo); // Assert Assert.AreEqual(guidTwo, plant.Guid); Assert.AreEqual(guidTwo, plant.PlantCharacteristic.Guid); } }
public async Task Delete_WithNoMatchingPlantInRepo_ShouldNotDeleteExistingRecord() { using (var context = new RamosiContext(options)) { // Arrange var plant = new PlantBuilder() .WithGuid(guidOne) .WithName("Plant") .WithPlantCharacteristic(new PlantCharacteristicBuilder().WithNotes("This is a characteristic")) .WithPlantCollection(new List <PlantCollection> { new PlantCollectionBuilder().WithNickname("Part of the collection") }); context.Plants.Add(plant); context.SaveChanges(); var repo = new PlantRepository(context); // Act Assert.Throws <ArgumentException>(() => repo.Delete(guidTwo), $"No entity of type Plant with guid {guidTwo} was found"); await repo.SaveChanges(); // Assert Assert.AreEqual(1, context.Plants.Count()); Assert.AreEqual(guidOne, context.Plants.First().Guid); } }
public async Task Edit_WithPlantWithMatchingGuidInRepo_ShouldNotEditPlantCharacteristicOrPlantCollection() { using (var context = new RamosiContext(options)) { // Arrange var plants = new List <Plant>() { new PlantBuilder() .WithName("one") .WithPlantCharacteristic(new PlantCharacteristicBuilder() .WithNotes("note one")) .WithPlantCollection(new List <PlantCollection>() { new PlantCollectionBuilder().WithNickname("nickname one") }), }; context.Plants.AddRange(plants); context.SaveChanges(); var plant = new PlantBuilder() .WithName("changed one!") .WithPlantCharacteristic(new PlantCharacteristicBuilder() .WithNotes("changed note one!")) .WithPlantCollection(new List <PlantCollection>() { new PlantCollectionBuilder().WithNickname("changed nickname one!") }); var repo = new PlantRepository(context); // Act repo.Edit(plant); await repo.SaveChanges(); // Arrange Assert.AreEqual(1, context.Plants.Count()); var plantsInDb = context.Plants.ToList(); Assert.AreEqual(guidOne, plantsInDb[0].PlantCharacteristic.Guid); Assert.AreEqual("note one", plantsInDb[0].PlantCharacteristic.Notes); Assert.AreEqual(guidOne, plantsInDb[0].PlantCollection[0].Guid); Assert.AreEqual("nickname one", plantsInDb[0].PlantCollection[0].Nickname); } }
public void GetAll_WithPlantsInRepo_ShouldReturnAllPlants() { using (var context = new RamosiContext(options)) { // Arrange var plantsInRepo = new List <Plant>() { new PlantBuilder() .WithGuid(guidOne) .WithPlantCharacteristic(new PlantCharacteristicBuilder().WithGuid(guidOne)), new PlantBuilder() .WithGuid(guidTwo) .WithPlantCharacteristic(new PlantCharacteristicBuilder().WithGuid(guidTwo)), new PlantBuilder() .WithGuid(guidThree) .WithPlantCharacteristic(new PlantCharacteristicBuilder().WithGuid(guidThree)), }; context.Plants.AddRange(plantsInRepo); context.SaveChanges(); var repo = new PlantRepository(context); // Act var plants = repo.Get(); // Assert Assert.AreEqual(3, plants.Count()); var results = plants.ToList(); Assert.AreEqual(guidOne, results[0].Guid); Assert.AreEqual(guidOne, results[0].PlantCharacteristic.Guid); Assert.AreEqual(guidTwo, results[1].Guid); Assert.AreEqual(guidTwo, results[1].PlantCharacteristic.Guid); Assert.AreEqual(guidThree, results[2].Guid); Assert.AreEqual(guidThree, results[2].PlantCharacteristic.Guid); } }
public async Task Delete_WithPlantsInRepo_ShouldOnlyDeletePlantWithMatchingGuid() { using (var context = new RamosiContext(options)) { // Arrange var plants = new List <Plant>() { new PlantBuilder() .WithGuid(guidOne).WithName("one"), new PlantBuilder() .WithGuid(guidTwo).WithName("two") .WithPlantCharacteristic(new PlantCharacteristicBuilder().WithGuid(guidTwo)), new PlantBuilder() .WithGuid(guidThree).WithName("three") .WithPlantCharacteristic(new PlantCharacteristicBuilder().WithGuid(guidThree)) }; context.Plants.AddRange(plants); context.SaveChanges(); var repo = new PlantRepository(context); // Act repo.Delete(guidTwo); await repo.SaveChanges(); // Assert Assert.AreEqual(2, context.Plants.Count()); var plantsInRepo = context.Plants.ToList(); Assert.AreEqual(guidOne, plantsInRepo[0].Guid); Assert.AreEqual("one", plantsInRepo[0].Name); Assert.AreEqual(guidThree, plantsInRepo[1].Guid); Assert.AreEqual("three", plantsInRepo[1].Name); } }
public async Task Edit_WithNoPlantWithMatchingGuidInRepo_ShouldNotEditExistingPlants() { using (var context = new RamosiContext(options)) { // Arrange var plants = new List <Plant>() { new PlantBuilder() .WithName("one") .WithPlantCharacteristic(new PlantCharacteristicBuilder() .WithNotes("note one")) .WithPlantCollection(new List <PlantCollection>() { new PlantCollectionBuilder().WithNickname("nickname one") }), new PlantBuilder() .WithGuid(guidTwo) .WithName("two") .WithPlantCharacteristic(new PlantCharacteristicBuilder() .WithGuid(guidTwo) .WithNotes("note two")) .WithPlantCollection(new List <PlantCollection>() { new PlantCollectionBuilder() .WithGuid(guidTwo) .WithNickname("nickname two") }), new PlantBuilder() .WithGuid(guidThree) .WithName("three") .WithPlantCharacteristic(new PlantCharacteristicBuilder() .WithGuid(guidThree) .WithNotes("note three")) .WithPlantCollection(new List <PlantCollection>() { new PlantCollectionBuilder() .WithGuid(guidThree) .WithNickname("nickname three") }), }; var plant = new PlantBuilder().WithGuid(guidFour); context.Plants.AddRange(plants); context.SaveChanges(); var repo = new PlantRepository(context); // Act Assert.Throws <ArgumentException>(() => repo.Edit(plant), $"No entity of type Plant with guid {guidFour} was found"); await repo.SaveChanges(); // Assert Assert.AreEqual(3, context.Plants.Count()); var plantsInDb = context.Plants.ToList(); Assert.AreEqual(guidOne, plantsInDb[0].Guid); Assert.AreEqual("one", plantsInDb[0].Name); Assert.AreEqual(guidOne, plantsInDb[0].PlantCharacteristic.Guid); Assert.AreEqual("note one", plantsInDb[0].PlantCharacteristic.Notes); Assert.AreEqual(guidOne, plantsInDb[0].PlantCollection[0].Guid); Assert.AreEqual("nickname one", plantsInDb[0].PlantCollection[0].Nickname); Assert.AreEqual(guidTwo, plantsInDb[1].Guid); Assert.AreEqual("two", plantsInDb[1].Name); Assert.AreEqual(guidTwo, plantsInDb[1].PlantCharacteristic.Guid); Assert.AreEqual("note two", plantsInDb[1].PlantCharacteristic.Notes); Assert.AreEqual(guidTwo, plantsInDb[1].PlantCollection[0].Guid); Assert.AreEqual("nickname two", plantsInDb[1].PlantCollection[0].Nickname); Assert.AreEqual(guidThree, plantsInDb[2].Guid); Assert.AreEqual("three", plantsInDb[2].Name); Assert.AreEqual(guidThree, plantsInDb[2].PlantCharacteristic.Guid); Assert.AreEqual("note three", plantsInDb[2].PlantCharacteristic.Notes); Assert.AreEqual(guidThree, plantsInDb[2].PlantCollection[0].Guid); Assert.AreEqual("nickname three", plantsInDb[2].PlantCollection[0].Nickname); } }
public async Task Edit_WithNullPlant_ShouldThrowNullArgExcpetion() { using (var context = new RamosiContext(options)) { // Arrange var plants = new List <Plant>() { new PlantBuilder() .WithName("one") .WithPlantCharacteristic(new PlantCharacteristicBuilder() .WithNotes("note one")) .WithPlantCollection(new List <PlantCollection>() { new PlantCollectionBuilder().WithNickname("nickname one") }), new PlantBuilder() .WithGuid(guidTwo) .WithName("two") .WithPlantCharacteristic(new PlantCharacteristicBuilder() .WithGuid(guidTwo) .WithNotes("note two")) .WithPlantCollection(new List <PlantCollection>() { new PlantCollectionBuilder() .WithGuid(guidTwo) .WithNickname("nickname two") }), new PlantBuilder() .WithGuid(guidThree) .WithName("three") .WithPlantCharacteristic(new PlantCharacteristicBuilder() .WithGuid(guidThree) .WithNotes("note three")) .WithPlantCollection(new List <PlantCollection>() { new PlantCollectionBuilder() .WithGuid(guidThree) .WithNickname("nickname three") }), }; context.Plants.AddRange(plants); context.SaveChanges(); var repo = new PlantRepository(context); // Act Assert.Throws <ArgumentNullException>(() => repo.Edit(null), "Cannot edit null entity"); await repo.SaveChanges(); // Assert Assert.AreEqual(3, context.Plants.Count()); var plantsInDb = context.Plants.ToList(); Assert.AreEqual(guidOne, plantsInDb[0].Guid); Assert.AreEqual("one", plantsInDb[0].Name); Assert.AreEqual(guidOne, plantsInDb[0].PlantCharacteristic.Guid); Assert.AreEqual("note one", plantsInDb[0].PlantCharacteristic.Notes); Assert.AreEqual(guidOne, plantsInDb[0].PlantCollection[0].Guid); Assert.AreEqual("nickname one", plantsInDb[0].PlantCollection[0].Nickname); Assert.AreEqual(guidTwo, plantsInDb[1].Guid); Assert.AreEqual("two", plantsInDb[1].Name); Assert.AreEqual(guidTwo, plantsInDb[1].PlantCharacteristic.Guid); Assert.AreEqual("note two", plantsInDb[1].PlantCharacteristic.Notes); Assert.AreEqual(guidTwo, plantsInDb[1].PlantCollection[0].Guid); Assert.AreEqual("nickname two", plantsInDb[1].PlantCollection[0].Nickname); Assert.AreEqual(guidThree, plantsInDb[2].Guid); Assert.AreEqual("three", plantsInDb[2].Name); Assert.AreEqual(guidThree, plantsInDb[2].PlantCharacteristic.Guid); Assert.AreEqual("note three", plantsInDb[2].PlantCharacteristic.Notes); Assert.AreEqual(guidThree, plantsInDb[2].PlantCollection[0].Guid); Assert.AreEqual("nickname three", plantsInDb[2].PlantCollection[0].Nickname); } }