public async System.Threading.Tasks.Task GetUserIDTestAsync() { var options = new DbContextOptionsBuilder <UsersDBContext>() .UseInMemoryDatabase(databaseName: "User") .Options; // Insert seed data into the database using one instance of the context using (var context = new UsersDBContext(options)) { context.Add(new User() { Address = "142345678910111213141516171819a", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test" }); context.Add(new User() { Address = "142345678910111213141516171819a", DateOfBith = DateTime.Now, FirstName = "Correct", LastName = "Test1234" }); context.Add(new User() { Address = "142345678910111213141516171819a", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test" }); context.SaveChanges(); var Service = new GetUserService(context); var cancelToken = new CancellationToken(); var response = await Service.Handle(new GetUserRequest() { Id = 1 }, cancelToken).ConfigureAwait(false); Assert.IsNotNull(response); Assert.IsTrue(response.Id == 1); } }
public void BasicFindingCorrectByIdUserTest() { var options = new DbContextOptionsBuilder <UsersDBContext>() .UseInMemoryDatabase(databaseName: "User") .Options; // Insert seed data into the database using one instance of the context using (var context = new UsersDBContext(options)) { context.Add(new User() { Address = "12345", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test" }); context.Add(new User() { Address = "12345", DateOfBith = DateTime.Now, FirstName = "Correct", LastName = "Test1234" }); context.Add(new User() { Address = "12345", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test" }); context.SaveChanges(); } // Use a clean instance of the context to run the test using (var context = new UsersDBContext(options)) { var user = context.Users.FirstOrDefault(s => s.Id == 3); Assert.IsNotNull(user); Assert.IsFalse(string.IsNullOrWhiteSpace(user.FirstName)); Assert.IsFalse(string.IsNullOrWhiteSpace(user.LastName)); Assert.IsFalse(string.IsNullOrWhiteSpace(user.Address)); Assert.IsTrue(user.DateOfBith > DateTime.Now.AddDays(-1)); Assert.IsTrue(user.LastName.Equals("Test1234", StringComparison.InvariantCultureIgnoreCase)); } }
public void BasicAddingUserTest() { var options = new DbContextOptionsBuilder <UsersDBContext>() .UseInMemoryDatabase(databaseName: "User") .Options; // Insert seed data into the database using one instance of the context using (var context = new UsersDBContext(options)) { context.Add(new User() { Address = "12345", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test" }); context.SaveChanges(); } // Use a clean instance of the context to run the test using (var context = new UsersDBContext(options)) { List <User> users = context.Users.Select(s => s).ToList(); Assert.AreEqual(1, users.Count); } }
public void TestDbValidationOfDataModel() { var options = new DbContextOptionsBuilder <UsersDBContext>() .UseInMemoryDatabase(databaseName: "User") .Options; try { // Insert seed data into the database using one instance of the context using (var context = new UsersDBContext(options)) { context.Add(new User() { Address = "12345", DateOfBith = DateTime.Now, FirstName = "", LastName = "Test" }); context.SaveChanges(); } } catch (ValidationException ex) { Assert.IsTrue(ex.Message.Contains("The FirstName field is required")); } }