public void Execute_ConsidersMultipleSearchTerms() { DbSet<Link> testDbSet = new TestDbSet<Link>(); var firstLinkToFind = new Link {Url = "http://bar.foo"}; testDbSet.Add(firstLinkToFind); var secondLinkToFind = new Link {Name = "bar"}; testDbSet.Add(secondLinkToFind); IQueryable<Link> results = StringQuery.Execute(testDbSet, new[] {"bar"}); Assert.AreEqual(2, results.Count()); }
public void Execute_ConsidersUrl() { DbSet<Link> testDbSet = new TestDbSet<Link>(); var linkToFind = new Link {Url = "http://bar.foo"}; testDbSet.Add(linkToFind); var otherLink = new Link {Url = "foo"}; testDbSet.Add(otherLink); IQueryable<Link> results = StringQuery.Execute(testDbSet, new[] {"bar"}); Assert.AreEqual(1, results.Count()); Assert.AreEqual(linkToFind, results.First()); }
public async void Delete_ShouldThrowContentNotFoundException_WhenUserWasNotFound() { var loggerMock = new Mock <ILogger <UserService> >(); var contextMock = new Mock <IDatabaseContext>(); var user = GetUserSample("name", "password"); var guid = Guid.NewGuid(); var dbSet = new TestDbSet <User>(); dbSet.Add(user); contextMock.Setup(x => x.Users) .Returns(dbSet); var userService = new UserService(loggerMock.Object, contextMock.Object); await Assert.ThrowsAsync <ContentNotFoundException>(() => userService.DeleteAsync(guid)); }
public async void GetById_ShouldReturnFirstUserWithMatchingId() { var loggerMock = new Mock <ILogger <UserService> >(); var contextMock = new Mock <IDatabaseContext>(); var user = GetUserSample("name", "password"); var dbSet = new TestDbSet <User>(); dbSet.Add(user); contextMock.Setup(x => x.Users) .Returns(dbSet); var userService = new UserService(loggerMock.Object, contextMock.Object); var result = await userService.GetByIdAsync(user.Id); Assert.NotNull(result); }
public async void Update_ShouldThrowContentNotFoundException_WhenUserWithSpecifiedIdDoesntExist() { var loggerMock = new Mock <ILogger <UserService> >(); var contextMock = new Mock <IDatabaseContext>(); var user1 = GetUserSample("name", "password"); var user2 = GetUserSample("name2", "password2"); var dbSet = new TestDbSet <User>(); dbSet.Add(user1); contextMock.Setup(x => x.Users) .Returns(dbSet); var userService = new UserService(loggerMock.Object, contextMock.Object); await Assert.ThrowsAsync <ContentNotFoundException>(() => userService.UpdateAsync(user2)); }
public async void Create_ShouldThrowUserAlreadyExistsException_WhenUserWithSpecifiedUsernameAlreadyExists() { var contextMock = new Mock <IDatabaseContext>(); var loggerMock = new Mock <ILogger <UserService> >(); var username = "******"; var user = GetUserSample(username, "randomPassword"); var userModel = GetUserModelSample(username, "password"); var dbSet = new TestDbSet <User>(); dbSet.Add(user); contextMock.Setup(x => x.Users) .Returns(dbSet); var userService = new UserService(loggerMock.Object, contextMock.Object); await Assert.ThrowsAsync <UserAlreadyExistsException>(() => userService.CreateAsync(userModel)); }
public void TestRetrieveTasks_Success(BenchmarkContext benchContext) { var context = new MockProjectManagerEntities(); var tasks = new TestDbSet <DAC.Task>(); var users = new TestDbSet <DAC.User>(); var parentTasks = new TestDbSet <DAC.ParentTask>(); parentTasks.Add(new DAC.ParentTask() { Parent_ID = 123456, Parent_Task_Name = "PNB" }); context.ParentTasks = parentTasks; users.Add(new DAC.User() { Employee_ID = "414942", First_Name = "Raj", Last_Name = "Aryan", User_ID = 123, Task_ID = 1 }); context.Users = users; int projectid = 1234; tasks.Add(new DAC.Task() { Task_ID = 1, Task_Name = "MYTASK", Parent_ID = 123456, Project_ID = 1234, Start_Date = DateTime.Now, End_Date = DateTime.Now.AddDays(2), Priority = 10, Status = 0 }); context.Tasks = tasks; var controller = new TaskController(new BC.TaskBC(context)); var result = controller.RetrieveTaskByProjectId(projectid) as JSendResponse; addCounter.Increment(); }
public async void Authenticate_ShouldReturnNull_WhenUserWithSpecifiedUsernameDoesntExist() { var contextMock = new Mock <IDatabaseContext>(); var loggerMock = new Mock <ILogger <UserService> >(); var password = "******"; var user = GetUserSample("username1", password); var dbSet = new TestDbSet <User>(); dbSet.Add(user); contextMock.Setup(x => x.Users) .Returns(dbSet); var userService = new UserService(loggerMock.Object, contextMock.Object); var result = await userService.AuthenticateAsync("username", password); Assert.Null(result); }
public async void Authenticate_ShouldReturnNull_WhenPasswordIsNotValid() { var contextMock = new Mock <IDatabaseContext>(); var loggerMock = new Mock <ILogger <UserService> >(); var username = "******"; var user = GetUserSample(username, "gadgadgadg"); var dbSet = new TestDbSet <User>(); dbSet.Add(user); contextMock.Setup(x => x.Users) .Returns(dbSet); var userService = new UserService(loggerMock.Object, contextMock.Object); var result = await userService.AuthenticateAsync(username, "azxczxcxzc"); Assert.Null(result); }
public async void Update_ShouldUpdateUserInDatabase_WhenOneOfUserPropertiesIsAssigned() { var loggerMock = new Mock <ILogger <UserService> >(); var contextMock = new Mock <IDatabaseContext>(); var user = GetUserSample("name", null); var dbSet = new TestDbSet <User>(); dbSet.Add(user); contextMock.Setup(x => x.Users) .Returns(dbSet); var userService = new UserService(loggerMock.Object, contextMock.Object); var newPassword = "******"; var updateUser = GetUserSample(null, newPassword); updateUser.Id = user.Id; await userService.UpdateAsync(updateUser, newPassword); Assert.True(VerifyPasswordHash(newPassword, user.PasswordHash, user.PasswordSalt)); var newUsername = "******"; updateUser = GetUserSample(newUsername, null); updateUser.Id = user.Id; await userService.UpdateAsync(updateUser); Assert.True(user.Username == newUsername); var newEmail = "*****@*****.**"; updateUser = GetUserSample("name", null, newEmail); updateUser.Id = user.Id; await userService.UpdateAsync(updateUser); Assert.True(user.Email == newEmail); }
public async void Authenticate_ShouldReturnUser_WhenUsernameAndPasswordAreValid() { var contextMock = new Mock <IDatabaseContext>(); var loggerMock = new Mock <ILogger <UserService> >(); var username = "******"; var password = "******"; var user = GetUserSample(username, password); var dbSet = new TestDbSet <User>(); dbSet.Add(user); contextMock.Setup(x => x.Users) .Returns(dbSet); var userService = new UserService(loggerMock.Object, contextMock.Object); var result = await userService.AuthenticateAsync(username, password); Assert.True(result.Id == user.Id); }
public async void Delete_ShouldRemoveUserFromDatabase_WhenUserHaSpecifiedId() { var loggerMock = new Mock <ILogger <UserService> >(); var contextMock = new Mock <IDatabaseContext>(); var user = GetUserSample("name", "password"); var dbSet = new TestDbSet <User>(); dbSet.Add(user); contextMock.Setup(x => x.Users) .Returns(dbSet); contextMock.Setup(x => x.SaveChanges()) .Verifiable(); var userService = new UserService(loggerMock.Object, contextMock.Object); await userService.DeleteAsync(user.Id); Assert.True(!dbSet.Any(x => x.Id == user.Id)); contextMock.Verify(x => x.SaveChanges()); }
public void TestInsertTasks_Success(BenchmarkContext benchContext) { var context = new MockProjectManagerEntities(); var users = new TestDbSet <DAC.User>(); users.Add(new DAC.User() { Employee_ID = "414942", First_Name = "Raj", Last_Name = "Aryan", User_ID = 123, Task_ID = 123 }); context.Users = users; var task = new ProjectManager.Models.Task() { Task_Name = "ASDQW", Parent_ID = 123674, Project_ID = 34856, Start_Date = DateTime.Now, End_Date = DateTime.Now.AddDays(2), Priority = 10, Status = 0, User = new User() { FirstName = "Sweta", LastName = "ChowdMisrahury", EmployeeId = "123456", UserId = 123 } }; var controller = new TaskController(new BC.TaskBC(context)); var result = controller.InsertTaskDetails(task) as JSendResponse; addCounter.Increment(); }
public void TestInsertProjects_Success(BenchmarkContext benchContext) { var context = new MockProjectManagerEntities(); var users = new TestDbSet <DAC.User>(); users.Add(new DAC.User() { Employee_ID = "414948", First_Name = "Sweta", Last_Name = "Misra", User_ID = 123, Task_ID = 123 }); context.Users = users; var testProject = new ProjectManager.Models.Project() { ProjectId = 12345, ProjectName = "MyProject", ProjectStartDate = DateTime.Now, ProjectEndDate = DateTime.Now.AddDays(5), Priority = 3, NoOfCompletedTasks = 3, NoOfTasks = 5, User = new User() { FirstName = "Sweta", LastName = "Misra", EmployeeId = "123456", UserId = 123 } }; var controller = new ProjectController(new BC.ProjectBC(context)); var result = controller.InsertProjectDetails(testProject) as JSendResponse; addCounter.Increment(); }
public async Task TestToPagedQueryResultsExtension() { var list = new TestDbSet <PagedQueryResultsTestClass>(); for (var i = 0; i < 10; i++) { list.Add(new PagedQueryResultsTestClass { Id = i }); } var start = 0; var limit = 1; Action <PagedQueryResults <PagedQueryResultsTestClass> > tester = (results) => { Assert.AreEqual(list.Count(), results.Total); Assert.AreEqual(limit, results.Results.Count); }; var queryable = list.AsQueryable(); var pagedResults = queryable.ToPagedQueryResults(start, limit); var pagedResultsAsync = await queryable.ToPagedQueryResultsAsync(start, limit); }
public async void GetRefreshTokenAsync_ShouldReturnRefreshTokenStringFromDatabase() { var loggerMock = new Mock <ILogger <AccessTokenService> >(); var optionsMock = new Mock <IOptions <AppSettings> >(); var appSettingsMock = new Mock <AppSettings>(); var contextMock = new Mock <IDatabaseContext>(); optionsMock.Setup(x => x.Value) .Returns(appSettingsMock.Object); var refreshToken = GetRefreshTokenSample(); var dbSet = new TestDbSet <RefreshToken>(); dbSet.Add(refreshToken); contextMock.Setup(x => x.RefreshTokens) .Returns(dbSet); var service = new AccessTokenService(loggerMock.Object, contextMock.Object, optionsMock.Object); var result = await service.GetRefreshTokenAsync(refreshToken.UserId.ToString()); Assert.Equal(result, refreshToken.Token); }
public void Execute_IgnoresCase() { DbSet<Link> testDbSet = new TestDbSet<Link>(); var linkToFind = new Link {Name = "FOO"}; testDbSet.Add(linkToFind); var otherLink = new Link {Name = "BAR"}; testDbSet.Add(otherLink); IQueryable<Link> results = StringQuery.Execute(testDbSet, new[] {"foo"}); Assert.AreEqual(1, results.Count()); Assert.AreEqual(linkToFind, results.First()); }
private static DbSet<Link> GetTaggableDbSet(params string[] tagStrings) { DbSet<Link> testDbSet = new TestDbSet<Link>(); foreach (string tagString in tagStrings) { testDbSet.Add(new Link {TagString = tagString}); } return testDbSet; }
public DbLaundryTest() { Shirts = new TestDbSet <Shirt>(); Services = new TestDbSet <Service>(); Rates = new TestDbSet <Rates>(); Transactions = new TestDbSet <Transaction>(); TransactionDetails = new TestDbSet <TransactionDetail>(); #region generate dummy data Shirts Shirts.Add(new Shirt() { Id = 1, Name = "T-Shirt" }); Shirts.Add(new Shirt() { Id = 2, Name = "Jeans" }); Shirts.Add(new Shirt() { Id = 3, Name = "Jacket" }); #endregion #region generate dummy data Services Services.Add(new Service() { Id = 1, Name = "Wash" }); Services.Add(new Service() { Id = 2, Name = "Dry" }); Services.Add(new Service() { Id = 3, Name = "Rinse" }); #endregion #region genereate dummy data Rates Rates.Add(new Rates() { Id = 1, ShirtId = 1, ServiceId = 1, Price = 2000, Unit = "Kg" }); Rates.Add(new Rates() { Id = 2, ShirtId = 1, ServiceId = 2, Price = 2500, Unit = "Kg" }); Rates.Add(new Rates() { Id = 3, ShirtId = 1, ServiceId = 3, Price = 3000, Unit = "Kg" }); #endregion #region generate dummy data Transaction Transactions.Add(new Transaction() { Id = 1, Name = "Ucok", Address = "Jalan Bakpau", Phone = "012345678", TransactionDetails = new Collection <TransactionDetail>() { new TransactionDetail() { RatesId = 1, TransactionId = 1, Quantity = 2 }, new TransactionDetail() { RatesId = 2, TransactionId = 1, Quantity = 1 } } }); Transactions.Add(new Transaction() { Id = 2, Name = "Burhan", Address = "Jalan Tak Ada Harapan", Phone = "0696969", TransactionDetails = new Collection <TransactionDetail>() { new TransactionDetail() { RatesId = 1, TransactionId = 2, Quantity = 3 }, new TransactionDetail() { RatesId = 3, TransactionId = 2, Quantity = 2 } } }); Transactions.Add(new Transaction() { Id = 3, Name = "Goku", Address = "Planet Namek", Phone = "88888888", TransactionDetails = new Collection <TransactionDetail>() { new TransactionDetail() { RatesId = 2, TransactionId = 3, Quantity = 4 }, new TransactionDetail() { RatesId = 3, TransactionId = 3, Quantity = 3 } } }); #endregion }
public void CreateQueryWithTwoParams_WhenSecondParameterIsNotNull() { // Arrange int queryElementsCountBeforeAct = queriesDbSet.Count(); int queryParametersElementsCountBeforeAct = queryParametersDbSet.Count(); int parametersElementsCountBeforeAct = parametersDbSet.Count(); string createQueryName = "New query"; string createQueryContent = "string type"; int firstQueryParameterId = parametersElementsCountBeforeAct + 1; string firstQueryParameterName = "CreatedQueryFirstParameterName"; string firstQueryParameterRuName = "первый параметр запроса"; string firstQueryParameterType = "text"; int secondQueryParameterId = firstQueryParameterId + 1; string secondQueryParameterName = "CreateQuerySecondParameterName"; string secondQueryParameterRuName = "второй параметр запроса"; string secondQueryParameterType = "phone"; SqlQuery queryToCreate = new SqlQuery() { SqlQueryId = queryElementsCountBeforeAct + 1, SqlQueryName = createQueryName, SqlQueryContent = createQueryContent, SqlQueryCreatedDate = DateTime.Now }; Parameter firstParameter = new Parameter() { ParameterId = firstQueryParameterId, ParameterName = firstQueryParameterName, ParameterRuName = firstQueryParameterRuName, ParameterType = firstQueryParameterType }; parametersDbSet.Add(firstParameter); Parameter secondParameter = new Parameter() { ParameterId = secondQueryParameterId, ParameterName = secondQueryParameterName, ParameterRuName = secondQueryParameterRuName, ParameterType = secondQueryParameterType }; parametersDbSet.Add(secondParameter); string[] parameterIds = new string[] { firstParameter.ParameterId.ToString(), secondParameter.ParameterId.ToString() }; // Act queryRepo.Create(queryToCreate, parameterIds); SqlQuery createdQuery = queriesDbSet.Last(); int lastQueryParameterId = queryParametersDbSet.Last().SqlQueryParameterId; SqlQueryParameter[] twoLastQueryParameters = new SqlQueryParameter[] { queryParametersDbSet.Last(), queryParametersDbSet.ToList()[queryParametersDbSet.Count() - 2] }; // Assert Assert.IsTrue(queriesDbSet.Count() > queryElementsCountBeforeAct); Assert.IsTrue(parametersDbSet.Count() > parametersElementsCountBeforeAct); Assert.IsTrue(queryParametersDbSet.Count() > queryParametersElementsCountBeforeAct); foreach (SqlQueryParameter queryParameter in twoLastQueryParameters) { Assert.IsTrue(queryParameter.SqlQueryId == createdQuery.SqlQueryId); Assert.IsTrue( queryParameter.ParameterId == firstQueryParameterId || queryParameter.ParameterId == secondQueryParameterId ); } }
public void Execute_ReturnsEmptyIfSearchTermsAreNull() { DbSet<Link> testDbSet = new TestDbSet<Link>(); testDbSet.Add(new Link()); IQueryable<Link> results = StringQuery.Execute(testDbSet, new string[] {null}); Assert.IsNotNull(results); Assert.AreEqual(0, results.Count()); }