public void TestUpdateInvalid() { 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(); } using (ApiContext context = new ApiContext(dbOptions)) { UserAnimalService service = new UserAnimalService(context); userAnimal = context.UserAnimals.Include(ua => ua.Animal).First(); // Test with an invalid user ID userAnimal.UserId = user.Id + 1; Assert.Catch <NotFoundException>(() => service.Update(userAnimal)); // Test with an invalid animal ID userAnimal.UserId = user.Id; userAnimal.AnimalId = animal.Id + 1; Assert.Catch <NotFoundException>(() => service.Update(userAnimal)); } }
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); } }