Ejemplo n.º 1
0
        public void TestFeed()
        {
            UserAnimal userAnimal = new UserAnimal()
            {
                Animal = testAnimal
            };

            // Wait until hunger is at maximum
            Thread.Sleep(2000);

            // Make sure that feeding reduces back to zero
            userAnimal.Feed(1);
            Assert.AreEqual(0, userAnimal.Hunger);

            // Make sure that hunger goes to -1
            userAnimal.Feed(1);
            Assert.AreEqual(-1, userAnimal.Hunger);

            // Make sure that hunger goes no lower than -1
            userAnimal.Feed(1);
            Assert.AreEqual(-1, userAnimal.Hunger);

            // Wait until hunger is at maximum
            Thread.Sleep(2000);

            // Make sure that feeding reduces back to -1 and no further
            userAnimal.Feed(3);
            Assert.AreEqual(-1, userAnimal.Hunger);
        }
Ejemplo n.º 2
0
        public void TestFeedInvalid()
        {
            UserAnimal userAnimal = new UserAnimal()
            {
                Animal = testAnimal
            };

            Assert.Catch <System.ArgumentException>(() => userAnimal.Feed(-0.1m));
            Assert.Catch <System.ArgumentException>(() => userAnimal.Feed(0));
        }
        public ActionResult <UserAnimal> Feed(uint userId, uint id)
        {
            UserAnimal userAnimal = service
                                    .FindAll()
                                    .FirstOrDefault(ua => ua.UserId == userId && ua.AnimalId == id)
            ;

            if (userAnimal == null)
            {
                return(new NotFoundResult());
            }

            // Feed the animal and update the database
            userAnimal.Feed(FEED_AMOUNT);
            service.Update(userAnimal);

            return(Get(userId, id));
        }
        public void TestUpdate()
        {
            User user = new User()
            {
                DisplayName = "Some display name"
            };

            Animal animal = new Animal()
            {
                TypeName         = "Test animal",
                HungerPerSecond  = 0.5m,
                SadnessPerSecond = 0.4m
            };

            UserAnimal userAnimal;

            using (ApiContext context = new ApiContext(dbOptions)) {
                context.Users.Add(user);
                context.Animals.Add(animal);

                userAnimal = new UserAnimal()
                {
                    UserId   = user.Id,
                    AnimalId = animal.Id,
                };
                context.UserAnimals.Add(userAnimal);
                context.SaveChanges();
            }

            // Check the initial state of the animal
            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();

                Assert.AreEqual(0, userAnimal.Hunger);
                Assert.AreEqual(0, userAnimal.Happiness);
            }

            // Add an update (at this stage it won't really update much)
            Thread.Sleep(1000);
            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                service.Update(userAnimal);
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();
                Assert.AreEqual(0.5, userAnimal.Hunger);
                Assert.AreEqual(-0.4, userAnimal.Happiness);
            }

            // Feed the animal and update again - this will cause the internal dates to update
            Thread.Sleep(1000);
            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                userAnimal.Feed(1);
                service.Update(userAnimal);
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();
                Assert.AreEqual(0, userAnimal.Hunger);
                Assert.AreEqual(-0.8, userAnimal.Happiness);
            }

            // Stroke the animal and update again - this will cause the internal dates to update
            Thread.Sleep(1000);
            using (ApiContext context = new ApiContext(dbOptions)) {
                UserAnimalService service = new UserAnimalService(context);

                userAnimal.Stroke(1);
                service.Update(userAnimal);
            }

            using (ApiContext context = new ApiContext(dbOptions)) {
                Assert.AreEqual(1, context.UserAnimals.Count());

                userAnimal = context.UserAnimals.Include(ua => ua.Animal).First();
                Assert.AreEqual(0.5, userAnimal.Hunger);
                Assert.AreEqual(0, userAnimal.Happiness);
            }
        }