Пример #1
0
        public ValidationResponse Validate(Connection testConnection, Test test)
        {
            if (test == null) return null;
            _scriptResolver = new ScriptResolverFactory(test).GetScriptResolver();
            if (_scriptResolver == null) return null;
            var scriptValue = _scriptResolver.GetSqlScript();
            var runLog = new StringBuilder();
            var resultMessage = new StringBuilder();
            var valResponse = new ValidationResponse
            {
                RunDateTime = DateTime.Now,
                TestId = test.Id,
                TestName = test.Name,
                TestValue = test.TestValue
            };
            RunTimer.Reset();
            RunTimer.Start();

            try
            {
                using (var dbConnection = (DbConnection) DbConnectionFactory.GetConnection(testConnection))
                {
                    using (
                        var reader = _sqlScriptRunner.RunScript(dbConnection,
                            scriptValue))
                    {
                        while (reader.Read())
                        {
                            foreach (var expResult in test.ExpectedResults.Clone())
                            {
                                if (reader[expResult.ResultIndex].IsNullOrDbNull()) continue;
                                var actual = reader[expResult.ResultIndex].ToString();
                                runLog.AppendFormat("Comparing results: Expected = {0} \n Response = {1} \n",
                                    expResult.ExpectedValue, actual);

                                var comparerFactory = new ComparerFactory();
                                var comparer = comparerFactory.GetComparer(expResult.AssertType.Name);
                                valResponse.IsValid = comparer.Compare(expResult.ExpectedValue, actual);

                                if (valResponse.IsValid)
                                {
                                    resultMessage.Append("Is Valid!");
                                    continue;
                                }
                                resultMessage.AppendFormat("expected: {0} \n but was: {1}",
                                    expResult.ExpectedValue, actual);
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                runLog.AppendFormat("Exception caught: {0}", exception.Message);
                valResponse.IsValid = false;
                resultMessage.AppendFormat("Error occurred while trying to run validation {0}. \n \n {1} : {2}",
                    scriptValue,
                    exception.Message,
                    exception.StackTrace);
            }
            finally
            {
                RunTimer.Stop();
                valResponse.Duration = (decimal) RunTimer.Elapsed.TotalMilliseconds;
                valResponse.RunLog = runLog.ToString();
                valResponse.ResultMessage = resultMessage.ToString();
            }
            return valResponse;
        }
Пример #2
0
 public ValidationResponse Validate()
 {
     var scriptVal = new SqlScriptValidator();
     Response = scriptVal.Validate(Connection, Test);
     return Response;
 }