public void FindQueriesByNameOrderByName_Test() { // Arrange TestDbSet <SqlQuery> dbSet = new TestDbSet <SqlQuery>() { new SqlQuery() { SqlQueryId = 0, SqlQueryName = "Command sql", SqlQueryContent = "Simple sql command", SqlQueryCreatedDate = DateTime.Today.AddDays(-3) }, new SqlQuery() { SqlQueryId = 1, SqlQueryName = "Big query sql", SqlQueryContent = "Huge sql query", SqlQueryCreatedDate = DateTime.Today.AddDays(-2) }, new SqlQuery() { SqlQueryId = 2, SqlQueryName = "A first query", SqlQueryContent = "first query in set", SqlQueryCreatedDate = DateTime.Today } }; int QueriesWithSqlWordInName = dbSet .Where(q => q.SqlQueryName.Contains("sql")) .Count(); IEnumerable <SqlQuery> queries = dbSet .Where(q => q.SqlQueryName.Contains("sql")) .Select(i => i) .OrderBy(q => q.SqlQueryName); Mock <IContext> mock = new Mock <IContext>(); mock .Setup(q => q.SqlQueries) .Returns(dbSet); QueryRepository repository = new QueryRepository(mock.Object); // Act IEnumerable <SqlQuery> orderedQueries = repository .FindQueriesByNameOrderByName("sql"); // Assert Assert.AreEqual(QueriesWithSqlWordInName, orderedQueries.Count()); Assert.AreEqual( queries.ToList().First().SqlQueryName, orderedQueries.ToList().First().SqlQueryName ); Assert.AreEqual( queries.ToList().Last().SqlQueryId, orderedQueries.ToList().Last().SqlQueryId ); }
public void EditQueryWithTwoParams_WhenSecondParameterIsNull_ShouldRemoveParams() { // Arrange int queryElementsCountBeforeAct = queriesDbSet.Count(); int parametersElementsCountBeforeAct = parametersDbSet.Count(); int queryParametersElementsCountBeforeAct = queryParametersDbSet.Count(); int queryIdBeforeAct = queriesDbSet.First().SqlQueryId; string queryNameBeforeAct = queriesDbSet.First().SqlQueryName; string queryContentBeforeAct = queriesDbSet.First().SqlQueryContent; DateTime queryCreatedDateBeforeAct = queriesDbSet.First().SqlQueryCreatedDate; string newQueryName = "new query name"; string newQueryContent = "new query content"; int queryParametersCount = queryParametersDbSet .Where(p => p.SqlQueryId == queriesDbSet.First().SqlQueryId) .Count(); SqlQuery queryToUpdate = queriesDbSet.First(); queryToUpdate.SqlQueryName = newQueryName; queryToUpdate.SqlQueryContent = newQueryContent; string[] parameterIds = null; // Act queryRepo.Edit(queryToUpdate, parameterIds); SqlQuery updatedQuery = queriesDbSet.ElementAt(queryToUpdate.SqlQueryId); // Assert Assert.AreEqual(queryElementsCountBeforeAct, queriesDbSet.Count()); Assert.AreEqual(parametersElementsCountBeforeAct, parametersDbSet.Count()); Assert.IsTrue(queryParametersElementsCountBeforeAct > queryParametersDbSet.Count()); Assert.AreEqual(newQueryName, updatedQuery.SqlQueryName); Assert.AreEqual(newQueryContent, updatedQuery.SqlQueryContent); Assert.AreEqual(queryCreatedDateBeforeAct, updatedQuery.SqlQueryCreatedDate); Assert.AreEqual( queryParametersElementsCountBeforeAct, (queryParametersDbSet.Count() + queryParametersCount) ); Assert.IsTrue( queryParametersDbSet .Where(p => p.SqlQueryId == queryToUpdate.SqlQueryId) .Count() <= 0 ); }
public void GetQueriesById_Test() { // Arrange int queriesWithId1Count = queriesDbSet .Where(q => q.SqlQueryId == 1) .Count(); int queriesCountBeforeAct = queriesDbSet.Count(); // Act IEnumerable <SqlQuery> queries = queryRepo.GetQueriesById(1); // Assert Assert.AreEqual(queriesWithId1Count, queries.Count()); Assert.AreEqual(queriesCountBeforeAct, queriesDbSet.Count()); }
public void GetSqlQueryIdByParameterId_Test() { // Arrange TestDbSet <Parameter> parameters = new TestDbSet <Parameter>() { new Parameter() { ParameterId = 0, ParameterName = "@Name", ParameterRuName = "RuName", ParameterType = "text", ParameterCreatedDate = DateTime.Today }, new Parameter() { ParameterId = 1, ParameterName = "@Age", ParameterRuName = "RuAge", ParameterType = "number", ParameterCreatedDate = DateTime.Today.AddDays(-1) } }; TestDbSet <SqlQuery> queries = new TestDbSet <SqlQuery>() { new SqlQuery() { SqlQueryId = 0, SqlQueryName = "SelectAllFromTable", SqlQueryContent = "Select * from Table", SqlQueryCreatedDate = DateTime.Now }, new SqlQuery() { SqlQueryId = 1, SqlQueryName = "DropTable", SqlQueryContent = "Drop table Table", SqlQueryCreatedDate = DateTime.Today } }; TestDbSet <SqlQueryParameter> queryParams = new TestDbSet <SqlQueryParameter>() { new SqlQueryParameter() { SqlQueryParameterId = 0, ParameterId = 0, SqlQueryId = 1 }, new SqlQueryParameter() { SqlQueryParameterId = 1, ParameterId = 1, SqlQueryId = 0 } }; Mock <IContext> mock = new Mock <IContext>(); mock .Setup(p => p.Parameters) .Returns(parameters); mock .Setup(q => q.SqlQueries) .Returns(queries); mock .Setup(s => s.SqlQueryParameters) .Returns(queryParams); SqlQueryParameterRepository queryParamRepo = new SqlQueryParameterRepository(mock.Object); int parameterId = parameters.First().ParameterId; List <int> queryIds = queryParams. Where(p => p.ParameterId == parameterId). Select(q => q.SqlQueryId). ToList(); // Act IEnumerable <int> funcResult = queryParamRepo.GetSqlQueryIdByParameterId(parameters.First().ParameterId); // Assert Assert.AreEqual(queryIds.Count(), funcResult.Count()); Assert.AreEqual(queryIds.First(), funcResult.First()); }