Example #1
0
        public override void ExecuteTask(ISpecialExecutionTaskTestAction testAction)
        {
            IParameter IPconnString = testAction.GetParameter("ConnectionString", false, new[] { ActionMode.Input });
            IParameter IPquery = testAction.GetParameter("Query", false, new[] { ActionMode.Input });
            IParameter IPresult = testAction.GetParameter("Result", true, new[] { ActionMode.Verify, ActionMode.Buffer });

            string connString = IPconnString.GetAsInputValue().Value;
            string query = IPquery.GetAsInputValue().Value;

            if (File.Exists(query))
            {
                query = File.ReadAllText(query);
            }

            Datalink dl = new Datalink(connString);

            if (!dl.IsValidSqlConnectionString())
            {
                throw new InvalidOperationException("Connectionstring is not valid or the database cannot be reached");
            }

            string result = dl.Execute(query);
            if (IPresult != null )
            {
                IInputValue expectedResult = IPresult.Value as IInputValue;

                if (IPresult.ActionMode == ActionMode.Buffer)
                {
                    Buffers.Instance.SetBuffer(expectedResult.Value, result);
                    testAction.SetResult(SpecialExecutionTaskResultState.Ok, string.Format("Buffer {0} set to value {1}.",expectedResult.Value, result));
                    return;
                }

                if (expectedResult.Value == result)
                {
                    testAction.SetResult(SpecialExecutionTaskResultState.Ok, "Actual rows match expected number of rows.");
                }
                else
                {
                    testAction.SetResult(SpecialExecutionTaskResultState.Failed, string.Format("Expected result: {0}. Actual result {1}. Incorrect", expectedResult.Value, result.ToString()));
                }
            }
            else
            {
                testAction.SetResult(SpecialExecutionTaskResultState.Ok, "Query executed");
            }
        }
Example #2
0
        public override void ExecuteTask(ISpecialExecutionTaskTestAction testAction)
        {
            const string BEGINWHERE = " where 1=1 ";
            const string BEGINSELECT = " select ";
            const string BEGINORDER = " order by ";
            const string ORDERDIRECTION = " desc ";

            var connStringParam = testAction.SearchQuery.XParameters.ConfigurationParameters.FirstOrDefault( xParam => xParam.Name == "ConnectionString");
            string connString = connStringParam.UnparsedValue;

            var tableNameParam = testAction.SearchQuery.XParameters.ConfigurationParameters.FirstOrDefault(xParam => xParam.Name == "TableName");
            string tableName = tableNameParam.UnparsedValue;

            var orderAttribParam = testAction.SearchQuery.XParameters.ConfigurationParameters.FirstOrDefault(xParam => xParam.Name == "OrderAttribute");
            string orderAttrib = orderAttribParam.UnparsedValue;

            string queryWhere = BEGINWHERE;
            string queryBegin = BEGINSELECT;
            string query = string.Empty;
            string queryOrder = string.Format("{0} {1} {2}", BEGINORDER, orderAttrib, ORDERDIRECTION);
            int count = 0;

            var constraints = testAction.Parameters.FindAll(p => p.ActionMode == ActionMode.Constraint);
            if (constraints.Count > 0)
            {
                foreach (var parameter in constraints)
                {
                    IInputValue value = parameter.Value as IInputValue;
                    queryWhere += string.Format(" and {0} = '{1}' ", parameter.Name, value.Value);
                }
            }

            var buffersVerifies = testAction.Parameters.FindAll(p => p.ActionMode == ActionMode.Verify || p.ActionMode == ActionMode.Buffer);

            if (buffersVerifies.Count == 0 )
            {
                // No use in continuing if there is nothing to verify or buffer
                return;
            }

            queryBegin += String.Join(",", buffersVerifies.Select(p => p.Name).ToArray());

            // Generate db query
            query = string.Format("{0} FROM {1} {2} {3}", queryBegin, tableName, queryWhere, queryOrder);

            // Connect to the database
            Datalink dl = new Datalink();
            dl.ConnectionString = connString;
            if (!dl.IsValidSqlConnectionString())
            {
                testAction.SetResult(SpecialExecutionTaskResultState.Failed, string.Format("Cannot connect to the SQL database with the following connection string: {0}", connString));
                return;
            }

            DataRow dr = dl.GetRowFromDb(query);
            // If there is not a result from the database, set the testresult to failed
            if (dr == null)
            {
                testAction.SetResult(SpecialExecutionTaskResultState.Failed, "No records found in with selected searchcriteria" );
                return;
            }

            foreach (IParameter parameter in buffersVerifies.Where(p => p.ActionMode == ActionMode.Verify))
            {
                IInputValue value = parameter.Value as IInputValue;
                if (value.Value == dr[parameter.Name].ToString())
                {
                    testAction.SetResultForParameter(parameter, SpecialExecutionTaskResultState.Ok, string.Format("{0}: Expected value {1} matches actual value {2}.", parameter.Name, value.Value, dr[parameter.Name].ToString()));
                }
                else
                {
                    count++;
                    testAction.SetResultForParameter(parameter, SpecialExecutionTaskResultState.Failed, string.Format("Error {0}: Expected value {1} does not match actual value {2}.", parameter.Name, value.Value, dr[parameter.Name].ToString()));
                }
            }

            foreach (IParameter parameter in buffersVerifies.Where(p => p.ActionMode == ActionMode.Buffer))
            {
                IInputValue buffer = parameter.Value as IInputValue;
                Buffers.Instance.SetBuffer(buffer.Value, dr[parameter.Name].ToString());
                testAction.SetResultForParameter(parameter, SpecialExecutionTaskResultState.Ok, string.Format("Buffer {0} set to value {1}.", buffer.Value, dr[parameter.Name].ToString()));
            }

            if (count == 0)
            {
                testAction.SetResult(SpecialExecutionTaskResultState.Ok, "All values match.");
            }
            else
            {
                int numberOfVerifies = buffersVerifies.Count(p => p.ActionMode == ActionMode.Verify);
                testAction.SetResult(SpecialExecutionTaskResultState.Failed, string.Format("{0} out of {1} values match.", numberOfVerifies - count, numberOfVerifies));
            }
        }