From 6e3587ed14913df35d03fbed9b2c39cf1b8f826b Mon Sep 17 00:00:00 2001 From: Donatas Adamonis Date: Tue, 30 May 2017 19:17:08 +0200 Subject: Uni test done --- .../ApartmentManagerUnitTestProject/UnitTest.cs | 75 +++++++++++++++++++--- .../Controllers/ResidentsController.cs | 7 +- 2 files changed, 69 insertions(+), 13 deletions(-) diff --git a/ApartmentManager/ApartmentManagerUnitTestProject/UnitTest.cs b/ApartmentManager/ApartmentManagerUnitTestProject/UnitTest.cs index bd66b06..f474cd3 100644 --- a/ApartmentManager/ApartmentManagerUnitTestProject/UnitTest.cs +++ b/ApartmentManager/ApartmentManagerUnitTestProject/UnitTest.cs @@ -9,33 +9,88 @@ namespace ApartmentManagerUnitTestProject public class UnitTest1 { [TestMethod] - public void TestMethod1() + public void TestCreateResidentFail() { + //// try to add Resident without appartment ID it has to fail //// ApartmentViewModel model = new ApartmentViewModel(); - model.UserSingleton.CurrentUser.ApartmentId = 1; + model.UserSingleton.CurrentUser.ApartmentId = 0; model.NewResident.FirstName = "asd"; model.NewResident.LastName = "lopas"; model.ApartmentHandler.GetApartmentResidents(); - var currentDefects = model.CatalogSingleton.Residents.Count; + int residentsBefore = model.CatalogSingleton.Residents.Count; //test// model.ApartmentHandler.CreateResident(); - Assert.AreNotEqual(currentDefects, model.CatalogSingleton.Residents); - + Assert.AreNotEqual(residentsBefore, model.CatalogSingleton.Residents.Count); } [TestMethod] - public void TestMethod2() + public void TestCreateResidentPass() { - + //// try to add Resident with appartment ID it has to pass //// ApartmentViewModel model = new ApartmentViewModel(); - model.UserSingleton.CurrentUser.ApartmentId = 0; + model.UserSingleton.CurrentUser.ApartmentId = 1; model.NewResident.FirstName = "asd"; model.NewResident.LastName = "lopas"; model.ApartmentHandler.GetApartmentResidents(); - var currentDefects = model.CatalogSingleton.Residents.Count; + int residentsBefore = model.CatalogSingleton.Residents.Count; //test// model.ApartmentHandler.CreateResident(); - Assert.AreNotEqual(currentDefects, model.CatalogSingleton.Residents); + Assert.AreNotEqual(residentsBefore, model.CatalogSingleton.Residents.Count); + } + [TestMethod] + public void TestUpdateResidentFail() + { + //// try to update Resident with wrong ID it has to fail //// + ApartmentViewModel model = new ApartmentViewModel(); + model.UserSingleton.CurrentUser.ApartmentId = 1; + model.ApartmentHandler.GetApartmentResidents(); + string residentsBefore = model.CatalogSingleton.Residents[0].FirstName; + model.NewResident.ResidentId = 999; + model.NewResident.FirstName = "jonny"; + //test// + model.ApartmentHandler.UpdateResident(); + Assert.AreNotEqual(residentsBefore, model.CatalogSingleton.Residents[0].FirstName); + } + [TestMethod] + public void TestUpdateResidentPass() + { + //// try to update Resident with wrong ID it has to fail //// + ApartmentViewModel model = new ApartmentViewModel(); + model.UserSingleton.CurrentUser.ApartmentId = 1; + model.ApartmentHandler.GetApartmentResidents(); + string residentsBefore = model.CatalogSingleton.Residents[0].FirstName; + model.NewResident.ResidentId = model.CatalogSingleton.Residents[0].ResidentId; + model.NewResident.FirstName = "jonny"; + //test// + model.ApartmentHandler.UpdateResident(); + Assert.AreNotEqual(residentsBefore, model.CatalogSingleton.Residents[0].FirstName); + } + [TestMethod] + public void TestDeleteResidentFail() + { + //// try to delete Resident with wrong ID it has to fail //// + ApartmentViewModel model = new ApartmentViewModel(); + model.UserSingleton.CurrentUser.ApartmentId = 1; + model.ApartmentHandler.GetApartmentResidents(); + int residentsBefore = model.CatalogSingleton.Residents.Count; + model.NewResident.ResidentId = 999; + + //test// + model.ApartmentHandler.DeleteResident(); + Assert.AreNotEqual(residentsBefore, model.CatalogSingleton.Residents.Count); + } + [TestMethod] + public void TestDeleteResidentPass() + { + //// try to delete Resident with good ID it has to pass //// + ApartmentViewModel model = new ApartmentViewModel(); + model.UserSingleton.CurrentUser.ApartmentId = 1; + model.ApartmentHandler.GetApartmentResidents(); + int residentsBefore = model.CatalogSingleton.Residents.Count; + model.NewResident.ResidentId = model.CatalogSingleton.Residents[0].ResidentId; + //test// + model.ApartmentHandler.DeleteResident(); + Assert.AreNotEqual(residentsBefore, model.CatalogSingleton.Residents.Count); } } } diff --git a/ApartmentManager/HousingWebApi/Controllers/ResidentsController.cs b/ApartmentManager/HousingWebApi/Controllers/ResidentsController.cs index 80d5741..359c760 100644 --- a/ApartmentManager/HousingWebApi/Controllers/ResidentsController.cs +++ b/ApartmentManager/HousingWebApi/Controllers/ResidentsController.cs @@ -15,10 +15,11 @@ namespace HousingWebApi.Controllers [Route("api/ApartmentResidents/{id}")] public IQueryable GetResidents(int id) { - var roomlist = from resident in db.Residents + var residentslist = from resident in db.Residents where (resident.ApartmentId == id) - select resident; - return roomlist; + orderby resident.ResidentId descending + select resident; + return residentslist; } // GET: api/Residents -- cgit v1.2.3