public void TestAddAnimalNoType()
        {
            Animal animal = new Animal()
            {
                TypeName         = "",
                HungerPerSecond  = 0.1m,
                SadnessPerSecond = 0.1m
            };

            using (ApiContext context = new ApiContext(dbOptions)) {
                AnimalService service = new AnimalService(context);

                // Test with an empty string
                Assert.Catch <System.ArgumentException>(() => service.Add(animal));

                // Test with only whitespace
                animal.TypeName = "    ";
                Assert.Catch <System.ArgumentException>(() => service.Add(animal));

                // Test with type as null
                animal.TypeName = null;
                Assert.Catch <System.ArgumentException>(() => service.Add(animal));
            }

            // Make sure no animals are in the DB
            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(0, context.Animals.Count());
            }
        }
        public void TestAddAnimalDuplicate()
        {
            Animal animal = new Animal()
            {
                TypeName         = "Some new animal",
                HungerPerSecond  = 0.1m,
                SadnessPerSecond = 0.1m
            };

            using (ApiContext context = new ApiContext(dbOptions)) {
                context.Animals.Add(animal);
                context.SaveChanges();
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                AnimalService service = new AnimalService(context);

                Assert.Catch <DuplicateEntryException>(() => service.Add(animal));

                // Test with whitespace
                animal.TypeName = "   Some new animal ";
                Assert.Catch <DuplicateEntryException>(() => service.Add(animal));

                // Test with different casing
                animal.TypeName = "some New Animal";
                Assert.Catch <DuplicateEntryException>(() => service.Add(animal));
            }

            // Make sure no additional animals are in the DB
            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.Animals.Count());
            }
        }
Ejemplo n.º 3
0
        public void AnimalAgeCantBeNegative()
        {
            // Arrange
            Mock <IAnimalRepository>  animalRepository  = new Mock <IAnimalRepository>();
            Mock <ILodgingRepository> lodgingRepository = new Mock <ILodgingRepository>();

            IAnimalService animalService = new AnimalService(animalRepository.Object, lodgingRepository.Object);

            Animal dog = new Animal
            {
                ID              = 1,
                Name            = "Doggo",
                Age             = -1,
                Description     = "Good boi",
                AnimalType      = AnimalType.Dog,
                Race            = "Beautiful Doggos",
                Picture         = "Goodboi.png",
                DateOfDeath     = null,
                Castrated       = true,
                ChildFriendly   = ChildFriendly.Yes,
                ReasonGivenAway = "Too good a boi",
            };

            animalRepository.Setup(e => e.FindByID(dog.ID))
            .Returns(dog);

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => animalService.Add(dog));

            // Assert
            Assert.Equal("AS_Services", ex.Source);  // Make sure the error is actually thrown in the service, not somewhere else
            Assert.Equal("Add", ex.TargetSite.Name); // Make sure the error is thrown by the Add method; not something else
            Assert.Equal("Age can't be less then 0", ex.Message);
        }
Ejemplo n.º 4
0
        public void Add_Fail_NullContract()
        {
            var repo        = GetMockedAnimalRepository();
            var failMessage = "Null contract.";
            var service     = new AnimalService(null);
            var result      = service.Add(null);

            Assert.IsFalse(result.Success);
            Assert.AreEqual(result.Message, failMessage);
        }
        public void TestAnimalInvalidSadness()
        {
            Animal animal = new Animal()
            {
                TypeName         = "Some new animal",
                HungerPerSecond  = 0.1m,
                SadnessPerSecond = 0
            };

            using (ApiContext context = new ApiContext(dbOptions)) {
                AnimalService service = new AnimalService(context);

                Assert.Catch <System.ArgumentException>(() => service.Add(animal));

                // Test with a value lower than zero
                animal.SadnessPerSecond = -0.1m;
                Assert.Catch <System.ArgumentException>(() => service.Add(animal));
            }

            // Make sure no animals are in the DB
            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(0, context.Animals.Count());
            }
        }
Ejemplo n.º 6
0
        public void Add_Success()
        {
            var repo = GetMockedAnimalRepository();

            repo.Setup(x => x.Add(It.IsAny <AnimalContract>()));

            var service = new AnimalService(repo.Object);
            var result  = service.Add(new AnimalContract()
            {
                ID = Guid.NewGuid()
            });

            Assert.IsNotEmpty(result.Message);
            Assert.IsTrue(result.Success);
        }
Ejemplo n.º 7
0
        public void Add_Fail_RepositoryException()
        {
            var repo = GetMockedAnimalRepository();

            var failMessage = "fail";

            repo.Setup(x => x.Add(It.IsAny <AnimalContract>())).Throws(new Exception(failMessage));

            var service = new AnimalService(repo.Object);
            var result  = service.Add(new AnimalContract()
            {
                ID = Guid.NewGuid()
            });

            Assert.IsFalse(result.Success);
            Assert.AreEqual(result.Message, failMessage);
        }
        public void TestAddAnimal()
        {
            Animal animal = new Animal()
            {
                TypeName         = "Some new animal",
                HungerPerSecond  = 0.1m,
                SadnessPerSecond = 0.1m
            };

            using (ApiContext context = new ApiContext(dbOptions)) {
                AnimalService service = new AnimalService(context);

                service.Add(animal);
            }

            // Make sure the animal was added to the DB
            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.Animals.Count());
                Assert.AreNotEqual(0, context.Animals.First().Id);
                Assert.IsTrue(context.Animals.Any(a => a.TypeName == animal.TypeName));
            }

            // Make sure that white space is trimmed
            string newAnimalName = "  New animal type  ";

            using (ApiContext context = new ApiContext(dbOptions)) {
                AnimalService service = new AnimalService(context);

                Animal animal2 = new Animal {
                    TypeName         = newAnimalName,
                    HungerPerSecond  = 0.1m,
                    SadnessPerSecond = 0.1m
                };

                service.Add(animal2);
            }

            // Make sure the user was added to the DB and auto-increment works
            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(2, context.Animals.Count());
                Assert.AreNotEqual(0, context.Animals.Skip(1).First().Id);
                Assert.IsTrue(context.Animals.Any(a => a.TypeName == newAnimalName.Trim()));
            }
        }
        public void TestAddAnimalExistingId()
        {
            Animal animal = new Animal()
            {
                TypeName         = "Some new animal",
                HungerPerSecond  = 0.1m,
                SadnessPerSecond = 0.1m
            };

            Animal animal2 = new Animal()
            {
                TypeName         = "Another new animal",
                HungerPerSecond  = 0.1m,
                SadnessPerSecond = 0.1m
            };

            using (ApiContext context = new ApiContext(dbOptions)) {
                context.Animals.Add(animal);
                context.SaveChanges();
            }

            // Set the second animal to have a duplicate identifier
            animal2.Id = animal.Id;

            using (ApiContext context = new ApiContext(dbOptions)) {
                AnimalService service = new AnimalService(context);
                service.Add(animal2);
            }

            // Make sure 2 animals are in the DB and are correct
            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(2, context.Animals.Count());
                Assert.AreEqual(animal.Id, context.Animals.First().Id);
                Assert.AreEqual(animal.TypeName, context.Animals.First().TypeName);
                Assert.AreEqual(animal2.Id, context.Animals.Last().Id);
                Assert.AreEqual(animal2.TypeName, context.Animals.Last().TypeName);
            }
        }
Ejemplo n.º 10
0
        public void AnimalEstimatedAgeCalculatesProperly()
        {
            // Arrange
            Mock <IAnimalRepository> animalRepository = new Mock <IAnimalRepository>();

            Mock <ILodgingRepository> lodgingRepository = new Mock <ILodgingRepository>();

            IAnimalService animalService = new AnimalService(animalRepository.Object, lodgingRepository.Object);

            Animal dog = new Animal
            {
                ID              = 1,
                Name            = "Doggo",
                EstimatedAge    = 2,
                Description     = "Good boi",
                AnimalType      = AnimalType.Dog,
                Race            = "Beautiful Doggos",
                Picture         = "Goodboi.png",
                DateOfDeath     = null,
                Castrated       = true,
                ChildFriendly   = ChildFriendly.Yes,
                ReasonGivenAway = "Too good a boi",
            };

            animalRepository.Setup(e => e.FindByID(dog.ID))
            .Returns(dog);

            // Act
            animalService.Add(dog);

            // Assert
            // Get our dog with the calculated age
            Animal calculatedDog = animalRepository.Object.FindByID(dog.ID);

            animalRepository.Verify(x => x.Add(dog), Times.Once());
            animalRepository.Verify(x => x.Add(dog));
            Assert.Equal(2, calculatedDog.Age);
        }