public void GetOneGovernmentRecordAndSkipOneGovernemntRecord() { try { var context = new AutoTestDataContext(); // Arrange. // Add a user. var user = new user() { name = "user1" }; // Add a location with that userId. var location = new location() { name = "location1", user = user }; // Add a facility with that locationId. var facility = new facility() { name = "facility1", facilityType = "Commercial", location = location }; var facilityRepository = new Repository <facility>(context); facilityRepository.Add(facility, "xingl"); var newfacility = new facility(); newfacility.name = "facility2"; newfacility.facilityType = "Government"; facilityRepository.Add(newfacility, "theox"); var facility3 = new facility(); facility3.name = "facility3"; facility3.facilityType = "Government"; facilityRepository.Add(facility3, "theox"); // Act. var retrievedFacility = facilityRepository.Get(filterString: "facilityType=\"Government\"", skip: 1); // Assert. Assert.IsTrue(retrievedFacility != null); Assert.AreEqual(1, retrievedFacility.Where(i => i.facilityType == "Government").Count()); } finally { // Clean up database. var context = new AutoTestDataContextNonTrackerEnabled(); context.users.RemoveRange(context.users.ToList()); context.locations.RemoveRange(context.locations.ToList()); context.facilities.RemoveRange(context.facilities.ToList()); context.SaveChanges(); var context2 = new AutoTestDataContext(); context2.LogDetails.RemoveRange(context2.LogDetails.ToList()); context2.AuditLog.RemoveRange(context2.AuditLog.ToList()); context2.SaveChanges(); } }
public void ValidateGovernmentFacility() { try { var context = new AutoTestDataContextNonTrackerEnabled(); // Arrange. // Add a user. var user = new user() { name = "user1" }; // Add a location with that userId. var location = new location() { name = "location1", user = user }; // Add a facility with that locationId. var facility = new facility() { name = "facility1", facilityType = "Commercial", location = location }; var facilityRepository = new Repository <facility>(context); var validation = new CustomValidation <facility>(); var facilityService = new Service <facility>(facilityRepository); facilityService.Validation = validation; // Act. facilityService.Add(facility, "xingl"); // Assert. Assert.IsFalse(facilityService.IsValid(facility)); Assert.IsTrue(facilityService.Errors.Any()); Assert.IsTrue(facilityService.Errors.FirstOrDefault().Description == "Validation event"); } finally { // Clean up database. var context = new AutoTestDataContextNonTrackerEnabled(); context.users.RemoveRange(context.users.ToList()); context.locations.RemoveRange(context.locations.ToList()); context.facilities.RemoveRange(context.facilities.ToList()); context.SaveChanges(); var context2 = new AutoTestDataContext(); context2.LogDetails.RemoveRange(context2.LogDetails.ToList()); context2.AuditLog.RemoveRange(context2.AuditLog.ToList()); context2.SaveChanges(); } }
public void GetSkipZeroTakeTwoGovernmentFacilityRecordsWithOutProxy2() { try { var context = new AutoTestDataContextNonTrackerEnabled(); // Arrange. // Add a user. var user = new user() { name = "user1" }; // Add a location with that userId. var location = new location() { name = "location1", user = user }; var locationRepository = new Repository <location>(new AutoTestDataContext()); location = locationRepository.Add(location); // Add a facility with that locationId. var facility = new facility() { name = "facility1", facilityType = "Commercial", locationId = location.locationId }; var facilityRepository = new Repository <facility>(new AutoTestDataContextNonTrackerEnabled()); // Act. facility = facilityRepository.Add(facility, "xingl"); // Assert. Assert.IsTrue(facility.facilityId != 0); Assert.IsTrue(facility.locationId != 0); } finally { // Clean up database. var context = new AutoTestDataContextNonTrackerEnabled(); context.users.RemoveRange(context.users.ToList()); context.locations.RemoveRange(context.locations.ToList()); context.facilities.RemoveRange(context.facilities.ToList()); context.SaveChanges(); var context2 = new AutoTestDataContext(); context2.LogDetails.RemoveRange(context2.LogDetails.ToList()); context2.AuditLog.RemoveRange(context2.AuditLog.ToList()); context2.SaveChanges(); } }
public void GetSkipZeroTakeTwoGovernmentFacilityRecordsWithOutProxy() { try { var context = new AutoTestDataContextNonTrackerEnabled(); // Arrange. // Add a user. var user = new user() { name = "user1" }; // Add a location with that userId. var location = new location() { name = "location1", user = user }; // Add a facility with that locationId. var facility = new facility() { name = "facility1", facilityType = "Commercial", location = location }; var facilityRepository = new Repository <facility>(new AutoTestDataContextNonTrackerEnabled()); facilityRepository.Add(facility, "xingl"); var newfacility = new facility(); newfacility.name = "facility2"; newfacility.facilityType = "Government"; facilityRepository.Add(newfacility, "theox"); var facility3 = new facility(); facility3.name = "facility3"; facility3.facilityType = "Government"; facilityRepository.Add(facility3, "theox"); facilityRepository = new Repository <facility>(new AutoTestDataContextNonTrackerEnabled()); facilityRepository.ProxyCreationEnabled = false; facilityRepository.LazyLoadingEnabled = false; // Act. var retrievedFacility = facilityRepository.Get(filterString: "facilityType=\"Commercial\"", skip: 0, take: 2); // Assert. Assert.IsTrue(retrievedFacility != null); Assert.AreEqual(null, retrievedFacility.First().location); } finally { // Clean up database. var context = new AutoTestDataContextNonTrackerEnabled(); context.users.RemoveRange(context.users.ToList()); context.locations.RemoveRange(context.locations.ToList()); context.facilities.RemoveRange(context.facilities.ToList()); context.SaveChanges(); var context2 = new AutoTestDataContext(); context2.LogDetails.RemoveRange(context2.LogDetails.ToList()); context2.AuditLog.RemoveRange(context2.AuditLog.ToList()); context2.SaveChanges(); } }
public void DeleteAsync() { try { // Arrange. var context = new AutoTestDataContext(); // Add a user. var user = new user() { name = "user1" }; // Add a location with that userId. var location = new location() { name = "location1", user = user }; // Add a facility with that locationId. var facility = new facility() { name = "facility1", facilityType = "Commercial", location = location }; var facilityRepository = new Repository <facility>(context); var facilityService = new Service <facility>(facilityRepository); facilityService.Add(facility, "xingl"); var newfacility = new facility(); newfacility.name = "facility2"; newfacility.facilityType = "Government"; facilityService.Add(newfacility, "theox"); var facility3 = new facility(); facility3.name = "facility3"; facility3.facilityType = "Government"; facilityService.Add(facility3, "theox"); // Act. var result = new Service <facility>(new Repository <facility>(new AutoTestDataContext())).DeleteAsync(facility3.facilityId, "theox", softDelete: true).Result; var shouldNotHaveIt = new Service <facility>(new Repository <facility>(new AutoTestDataContext())).Queryable() .Where(i => i.facilityId == facility3.facilityId); var shouldHaveIt = new Service <facility>(new Repository <facility>(new AutoTestDataContext())).Queryable(includeSoftDeleted: true) .Where(i => i.facilityId == facility3.facilityId); // Assert. Assert.IsFalse(shouldNotHaveIt.Any()); Assert.IsTrue(shouldHaveIt.Any()); } finally { // Clean up database. var context = new AutoTestDataContextNonTrackerEnabled(); context.users.RemoveRange(context.users.ToList()); context.locations.RemoveRange(context.locations.ToList()); context.facilities.RemoveRange(context.facilities.ToList()); context.SaveChanges(); var context2 = new AutoTestDataContext(); context2.LogDetails.RemoveRange(context2.LogDetails.ToList()); context2.AuditLog.RemoveRange(context2.AuditLog.ToList()); context2.SaveChanges(); } }
public void GetObjectsBack() { try { var context = new AutoTestDataContextNonTrackerEnabled(); // Arrange. // Add a user. var user = new user() { name = "user1" }; // Add a location with that userId. var location = new location() { name = "location1", user = user }; // Add a facility with that locationId. var facility = new facility() { name = "facility1", facilityType = "Commercial", location = location }; var facilityRepository = new Repository <facility>(context); var facilityService = new Service <facility>(facilityRepository); facilityService.Add(facility, "xingl"); var facilityODataController = new ODataApiController <facility>(facilityService); facility.name = "facility1x"; // Act. var result = facilityODataController.Get(); // Assert. Assert.IsTrue(result != null); var posRes = ((System.Web.Http.Results.OkNegotiatedContentResult <System.Linq.IQueryable <AutoClutch.Test.Data.facility> >)result).Content; Assert.AreEqual(1, posRes.Count()); } finally { // Clean up database. var context = new AutoTestDataContextNonTrackerEnabled(); context.users.RemoveRange(context.users.ToList()); context.locations.RemoveRange(context.locations.ToList()); context.facilities.RemoveRange(context.facilities.ToList()); context.SaveChanges(); var context2 = new AutoTestDataContext(); context2.LogDetails.RemoveRange(context2.LogDetails.ToList()); context2.AuditLog.RemoveRange(context2.AuditLog.ToList()); context2.SaveChanges(); } }
public void GetEntityHistory() { try { var context = new AutoTestDataContext(); // Arrange. // Add a user. var user = new user() { name = "user1" }; // Add a location with that userId. var location = new location() { name = "location1", user = user }; // Add a facility with that locationId. var facility = new facility() { name = "facility1", facilityType = "Commercial", location = location }; var facilityRepository = new Repository <facility>(context); facilityRepository.Add(facility, "xingl"); var newfacility = new facility(); newfacility.name = "facility2"; newfacility.facilityType = "Government"; facilityRepository.Add(newfacility, "theox"); var facility3 = new facility(); facility3.name = "facility3"; facility3.facilityType = "Government"; facilityRepository.Add(facility3, "theox"); // Act. var found = facilityRepository.EntityAuditLog(facility3.facilityId); // Assert. Assert.IsTrue(found != null && found.Any()); } finally { // Clean up database. var context = new AutoTestDataContextNonTrackerEnabled(); context.users.RemoveRange(context.users.ToList()); context.locations.RemoveRange(context.locations.ToList()); context.facilities.RemoveRange(context.facilities.ToList()); context.SaveChanges(); var context2 = new AutoTestDataContext(); context2.LogDetails.RemoveRange(context2.LogDetails.ToList()); context2.AuditLog.RemoveRange(context2.AuditLog.ToList()); context2.SaveChanges(); } }
public void UpdateChildModels() { try { var context = new AutoTestDataContext(); var context2 = new AutoTestDataContext(); // Arrange. // Add a user. var userRepository = new Repository <user>(context); var user = new user() { name = "user1" }; userRepository.Add(user); // Save user id. var userId = user.userId; // Add a location with that userId. var locationRepository = new Repository <location>(context); var location = new location() { name = "location1", contactUserId = user.userId }; locationRepository.Add(location); // Add a facility with that locationId. var facilityRepository = new Repository <facility>(context); var facility = new facility() { name = "facility1", locationId = location.locationId }; facilityRepository.Add(facility); // Get the facility back. var facilityRepository2 = new Repository <facility>(context2); facility = facilityRepository2.Get(filter: i => i.facilityId == facility.facilityId).SingleOrDefault(); // Change the name of the user and put the object into the facility model. var user2 = new user(); user2.userId = user.userId; user2.name = "user2"; facility.location.user = user2; // Act. facilityRepository2.Update(facility); // Assert. facility = context2.facilities.SingleOrDefault(i => i.facilityId == facility.facilityId); Assert.AreEqual(facility.location.contactUserId, userId); Assert.AreEqual(facility.location.user.name, user2.name); } finally { // Clean up database. var context = new AutoTestDataContextNonTrackerEnabled(); context.users.RemoveRange(context.users.ToList()); context.locations.RemoveRange(context.locations.ToList()); context.facilities.RemoveRange(context.facilities.ToList()); context.SaveChanges(); var context2 = new AutoTestDataContext(); context2.LogDetails.RemoveRange(context2.LogDetails.ToList()); context2.AuditLog.RemoveRange(context2.AuditLog.ToList()); context2.SaveChanges(); } }
public void NotAddDuplicates() { try { var context = new AutoTestDataContext(); var context2 = new AutoTestDataContext(); // Arrange. // Add a user. var userRepository = new Repository <user>(context); var user = new user() { name = "user1" }; userRepository.Add(user); // Save user id. var userId = user.userId; // Add a location with that userId. var locationRepository = new Repository <location>(context); var location = new location() { name = "location1", contactUserId = user.userId }; locationRepository.Add(location); var location2 = new location() { name = "location2", contactUserId = user.userId }; locationRepository.Add(location2); // Add a facility with that locationId. var facilityRepository = new Repository <facility>(context); var facility = new facility() { name = "facility1", locationId = location.locationId }; facilityRepository.Add(facility); // Get the facility back. var facilityRepository2 = new Repository <facility>(context2); facility = facilityRepository2.Get(filter: i => i.facilityId == facility.facilityId).SingleOrDefault(); // Change the name of the user and put the object into the facility model. facility.locationId = location.locationId; // If facility.location is set to location2 then an exception will be thrown. This is the correct behavior. facility.location = location; bool exceptionCaught = false; // Act. try { facilityRepository2.Update(facility); } catch (Exception ex) { // Assert. Assert.IsTrue(true, "There was an exception caught when trying to add a duplicate to the database. This is correct, duplicates are not being caught."); exceptionCaught = true; } if (!exceptionCaught) { Assert.IsFalse(false, "There was no exception caught when trying to add a duplicate to the database. This is incorrect, duplicates are not being caught."); } //// Assert. //facility = context2.facilities.SingleOrDefault(i => i.facilityId == facility.facilityId); //Assert.AreEqual(facility.location.contactUserId, userId); } finally { // Clean up database. var context = new AutoTestDataContextNonTrackerEnabled(); context.users.RemoveRange(context.users.ToList()); context.locations.RemoveRange(context.locations.ToList()); context.facilities.RemoveRange(context.facilities.ToList()); context.SaveChanges(); var context2 = new AutoTestDataContext(); context2.LogDetails.RemoveRange(context2.LogDetails.ToList()); context2.AuditLog.RemoveRange(context2.AuditLog.ToList()); context2.SaveChanges(); } }