Beispiel #1
0
        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 void Delete_WhenCalled_DeletesPlantFromDatabase()
        {
            var data = new List <Plant>
            {
                new Plant("plant1", 1, DateTime.UtcNow),
                new Plant("plant2", 2, DateTime.UtcNow),
            }.AsQueryable();

            var mockSet = new Mock <DbSet <Plant> >();

            mockSet.As <IQueryable <Plant> >().Setup(m => m.Provider).Returns(data.Provider);
            mockSet.As <IQueryable <Plant> >().Setup(m => m.Expression).Returns(data.Expression);
            mockSet.As <IQueryable <Plant> >().Setup(m => m.ElementType).Returns(data.ElementType);
            mockSet.As <IQueryable <Plant> >().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());


            var mockContext = new Mock <PlantAppDbContext>();

            mockContext.Setup(m => m.Plants).Returns(mockSet.Object);

            var service = new PlantRepository(mockContext.Object);

            service.Delete(mockContext.Object.Plants.First());
            mockSet.Verify(m => m.Remove(It.IsAny <Plant>()), Times.Once);
            mockContext.Verify(m => m.SaveChanges(), Times.Once());
        }
Beispiel #3
0
        public async Task Delete_WithNoPlantsInRepo_ShouldNotModifyDatabase()
        {
            using (var context = new RamosiContext(options))
            {
                // Arrange
                var repo = new PlantRepository(context);

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

                // Assert
                Assert.IsFalse(context.Plants.Any());
            }
        }
Beispiel #4
0
        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);
            }
        }
Beispiel #5
0
        public void Delete(int plantId)
        {
            Plant plant = plantRepository.GetById(plantId);

            plantRepository.Delete(plant);
        }
Beispiel #6
0
 public bool Delete(int id)
 {
     return(_plantRepository.Delete(id));
 }