Exemplo n.º 1
0
        public async Task Create_WithPlant_ShouldSavePlantInDatabase()
        {
            using (var context = new RamosiContext(options))
            {
                // Arrange
                var plant = new PlantBuilder()
                            .WithName("Plant")
                            .WithPlantCharacteristic(new PlantCharacteristicBuilder().WithNotes("This is a characteristic"))
                            .WithPlantCollection(new List <PlantCollection>
                {
                    new PlantCollectionBuilder().WithNickname("Part of the collection")
                });

                var repo = new PlantRepository(context);

                // Act
                repo.Create(plant);
                await repo.SaveChanges();

                // Assert
                Assert.AreEqual(1, context.Plants.Count());
                var created = context.Plants.First();

                Assert.AreEqual("Plant", created.Name);
                Assert.AreEqual("This is a characteristic", created.PlantCharacteristic.Notes);
                Assert.AreEqual(1, created.PlantCollection.Count);
                Assert.AreEqual("Part of the collection", created.PlantCollection.First().Nickname);
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
        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);
            }
        }
Exemplo n.º 4
0
        public void Setup()
        {
            connection = new SqliteConnection("DataSource=one;Mode=memory");
            connection.Open();

            options = new DbContextOptionsBuilder <RamosiContext>()
                      .UseSqlite(connection)
                      .Options;

            // Create the schema
            using (var context = new RamosiContext(options))
                context.Database.EnsureCreated();
        }
Exemplo n.º 5
0
        public void Get_WithNoPlantsInRepo_ShouldReturnNull()
        {
            using (var context = new RamosiContext(options))
            {
                // Arrange
                var repo = new PlantRepository(context);

                // Act
                var plant = repo.Get(guidOne);

                // Assert
                Assert.IsNull(plant);
            }
        }
Exemplo n.º 6
0
        public void GetAll_WithNoPlantsInRepo_ShouldReturnEmptyCollection()
        {
            using (var context = new RamosiContext(options))
            {
                // Arrange
                var repo = new PlantRepository(context);

                // Act
                var plants = repo.Get();

                // Assert
                Assert.IsFalse(plants.Any());
            }
        }
Exemplo n.º 7
0
        public void Create_WithNullPlant_ShouldNotSavePlant()
        {
            using (var context = new RamosiContext(options))
            {
                // Arrange
                var repo = new PlantRepository(context);

                // Act
                Assert.Throws <ArgumentNullException>(() => repo.Create(null),
                                                      "Entity of type Plant cannot be null");

                // Assert
                Assert.IsFalse(context.Plants.Any());
            }
        }
Exemplo n.º 8
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());
            }
        }
Exemplo n.º 9
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);
            }
        }
Exemplo n.º 10
0
        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);
            }
        }
Exemplo n.º 11
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);
            }
        }
Exemplo n.º 12
0
 public PlantRepository(RamosiContext context) : base(context)
 {
 }
Exemplo n.º 13
0
 public PlantCollectionRepository(RamosiContext context) : base(context)
 {
 }
Exemplo n.º 14
0
 protected BaseRepository(RamosiContext context)
 {
     this.context = context;
     entities     = this.context.Set <T>();
 }
Exemplo n.º 15
0
 public PlantCharacteristicRepository(RamosiContext context) : base(context)
 {
 }
Exemplo n.º 16
0
 public WateringScheduleRepository(RamosiContext context) : base(context)
 {
 }
Exemplo n.º 17
0
 public UserRepository(RamosiContext context) : base(context)
 {
 }
Exemplo n.º 18
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);
            }
        }
Exemplo n.º 19
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);
            }
        }