Example #1
0
        public async Task Edit_WithNoPlantsInRepo_ShouldNotModifyDatabase()
        {
            using (var context = new RamosiContext(options))
            {
                // Arrange
                var plant = new PlantBuilder();
                var repo  = new PlantRepository(context);

                // Act
                Assert.Throws <ArgumentException>(() => repo.Edit(plant),
                                                  $"No entity of type Plant with guid {guidOne} was found");
                await repo.SaveChanges();

                // Assert
                Assert.IsFalse(context.Plants.Any());
            }
        }
Example #2
0
        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);
            }
        }
Example #3
0
        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);
            }
        }
Example #4
0
        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);
            }
        }