Exemplo n.º 1
0
        public int RunTests(string testName, ITestLogger logger, ref int testsRun)
        {
            TestResult actualResult = null;
            int exitStatus = 0;
            try
            {
                //Add the test in the log file
                string msg = "\n\n************************************************************\n" +
                             string.Format("Executing {0}\n", testName) +
                             "************************************************************\n\n";

                logger.Write(msg);

                //Get the list of operations to be executed for this test. This list should comma separated, no spaces.
                _unitTestVm.Execute("Select ParamSets from TestCase where TestName=\"{0}\"", testName);
                string sets = _unitTestVm.GetString("ParamSets");

                //Extract the paramset ids
                int[] paramSet = sets.Split(',').Where(x => !string.IsNullOrEmpty(x)).Select(x => Convert.ToInt32(x)).ToArray();
                foreach (int paramSetId in paramSet)
                {
                    _unitTestVm.Execute("Select ParamValue from Params where ParamSet={0} AND ParamName=\"OPERATION\"", paramSetId);
                    string paramValue = _unitTestVm.GetString("ParamValue");

                    testsRun++;

                    //Add the operation to the log file
                    AddLogFileEntry(logger, paramValue, paramSetId, _unitTestResultVm);

                    var exec = _executors.GetTestExecutor(paramValue);
                    actualResult = exec.Execute(paramSetId);

                    exitStatus += _executors.ValidateRequest(_unitTestDb, testName, paramSetId, paramValue, actualResult, logger);
                }

                return exitStatus;
            }
            catch (MgException ex)
            {
                throw new UnitTestException(string.Format("Exception from MapGuide:\n{0}", ex.GetDetails()));
            }
        }
        public override int ValidateRequest(SqliteDotNet.SqliteDb db, string testName, int paramSetId, string operation, TestResult actualResult, ITestLogger logger)
        {
            int exitStatus = 0;
            string outcome = "pass";
            SqliteVm vm = new SqliteVm(db, false);

            object expectedResult = null;

            //If we have an exception we need to remove the stack trace because different line numbers will fail the test
            object resultData = CommonUtility.RemoveStackTraceFromResult(actualResult.ResultData);
            //Get the mime type based on the content type in the result
            string mimeType = actualResult.ContentType;
            //If we have exception message we need to remove any parts that may contain system dependent information
            //Ex. file paths
            resultData = CommonUtility.ProcessExceptionMessage(resultData);
            //Get the file extension that will be used for a dump
            string actualExtension = CommonUtility.GetExtensionFromMimeType(mimeType);

            //If we have an ALWAYSPASS parameter defined for the operation then skip the whole validation process
            //This parameter should only be used for clean up operations that are no related with the tests
            if (vm.Execute("Select ParamValue from Params where ParamName=\"ALWAYSPASS\" and ParamSet={0}", paramSetId) != Sqlite.Row)
            {
                //TestName is Test_[ServiceType]
                string type = testName.Substring(testName.IndexOf("_") + 1);
                string filePath = CommonUtility.GetPath(string.Format("../../TestData/{0}/DumpFiles/{0}ApiTest", type));
                string fileName = string.Format("{0}_{1}.{2}", filePath, paramSetId, actualExtension);

                if (this.TestExecutionMode == "dump")
                {
                    //File.WriteAllText(fileName, resultData);
                    throw new NotImplementedException("The .net test runner does not support dumping of test results. Please use the PHP test runner for this purpose");
                }
                else
                {
                    //This section is special case handling for the operations that return different data after each call
                    resultData = CommonUtility.SpecialDataHandling(operation, resultData, mimeType);

                    if (this.TestExecutionMode == "generate")
                    {
                        throw new NotImplementedException("The .net test runner does not support test update/generation. Please use the PHP test runner for this purpose");
                        /*
                        //Get the sample result that is stored in the database. If we are using file on disk for validation
                        //then do not overwrite the filename in the database
                        //To distinguish between sample data and filename all filenames should be prefixed with "@@"
                        int status = vm.Execute("Select Result from ApiTestResults where ParamSet={0}", paramSetId);
                        string sampleResult = vm.GetString("Result");

                        if (!sampleResult.StartsWith("@@"))
                        {
                            //Insert the sample data as a BLOB
                            //Update the row for that param set or create a new row if we do not have it yet

                            string responseBody = "";
                            if (status == Sqlite.Row)
                            {
                                vm.Prepare("update ApiTestResults set Result = :blob where ParamSet={0}", paramSetId);
                            }
                            else
                            {
                                Console.WriteLine("A new row has been created in ApiTestResults table to store the result for operation {0}", paramSetId);
                                Console.WriteLine("Please update the description field for that row later");
                                vm.Prepare("INSERT INTO ApiTestResults(ParamSet, Result) VALUES({0}, :blob)", paramSetId);
                            }

                            byte[] bytes = Encoding.UTF8.GetBytes(resultData);
                            vm.BindBlob(":blob", bytes);
                            vm.Execute();

                            if (mimeType != null)
                            {
                                vm.Execute("UPDATE ApiTestResults SET ContentType=\"{0}\" WHERE ParamSet={1}", mimeType, paramSetId);
                            }

                            File.WriteAllText(fileName, resultData);
                        }
                         */
                    }
                    else if (this.TestExecutionMode == "validate" || this.TestExecutionMode == "show")
                    {
                        string resultContent = "";

                        //Get the sample result and the expected content type from the database
                        int status = vm.Execute("Select Result, ContentType from ApiTestResults where ParamSet={0}", paramSetId);
                        string expectedContentType = vm.GetString("ContentType");
                        string expectedExtension = CommonUtility.GetExtensionFromMimeType(expectedContentType);
                        SqliteGcBlob blob = vm.GetBlob("Result");

                        byte[] b = blob.Read();
                        if (b != null)
                        {
                            if (expectedExtension == "xml" || expectedExtension == "txt" || expectedExtension == "html")
                                expectedResult = Encoding.UTF8.GetString(b);
                            else
                                expectedResult = b;
                        }
                        else
                        {
                            if (expectedExtension == "xml" || expectedExtension == "txt" || expectedExtension == "html")
                                expectedResult = string.Empty;
                            else
                                expectedResult = null;
                        }
                        string strExpectedResult = expectedResult as string;
                        //If we are validating from a file then get the contents of that file
                        //File names should be prefixed with "@@" to distinguish them from BLOB data
                        if (strExpectedResult != null && strExpectedResult.StartsWith("@@"))
                        {
                            string sampleDataFile = strExpectedResult.Substring(2);
                            sampleDataFile = CommonUtility.GetPath(sampleDataFile);
                            expectedResult = File.ReadAllText(sampleDataFile);
                        }

                        if (this.TestExecutionMode == "validate")
                        {
                            bool bEqual = false;
                            byte[] bExpected = expectedResult as byte[];
                            byte[] bActual = resultData as byte[];
                            string strResultData = resultData as string;

                            //FIXME: We're not processing DWF content properly to do this check properly. So just
                            //pass these for now
                            if (operation == "GETDRAWINGLAYER" || operation == "GETDRAWINGSECTION")
                            {
                                bEqual = true;
                            }
                            else
                            {
                                if (strExpectedResult != null && strResultData != null)
                                {
                                    bEqual = strResultData.Equals(strExpectedResult, StringComparison.InvariantCultureIgnoreCase);
                                }
                                else if (bExpected != null && bActual != null)
                                {
                                    bEqual = CommonUtility.ByteArraysEqual(bExpected, bActual, operation, testName);
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine(string.Format("[MgTestRunner]: {0} - {1} - Encountered disparate data types between expected and actual results. Expecting test failure :(", testName, operation));
                                }
                            }
                            //If the results are different and special validation fails then the operation failed ->mark it red
                            if (!bEqual && !CommonUtility.SpecialValidation(operation, resultData, expectedResult))
                            {
                                outcome = "fail";
                                exitStatus = 1;

                                if (expectedExtension != "xml" && expectedExtension != "html" && expectedExtension != "txt")
                                {
                                    expectedResult = "Unable to display binary data";
                                }

                                if (actualExtension != "xml" && actualExtension != "html" && actualExtension != "txt")
                                {
                                    resultData = "Unable to display binary data";
                                }
                            }
                        }
                        else
                        {
                            throw new NotImplementedException("The .net test runner does not support the given test execution mode (" + this.TestExecutionMode + "). Please use the PHP test runner for this purpose");
                            /*
                            type = testName.Substring(testName.IndexOf("_") + 1);
                            string showPath = CommonUtility.GetPath(string.Format("../../TestData/{0}/ShowFiles/{0}ApiTest", type));
                            string showName = string.Format("{0}_{1}.{2}", showPath, paramSetId, actualExtension);
                            File.WriteAllText(showName, expectedResult);
                             */
                        }
                    }
                }
            }

            if (outcome == "fail")
            {
                Console.WriteLine("****{0} {1} {2} failed.\n", testName, paramSetId, operation);
                string str = string.Format("\n****ACTUAL RESULT****\n{0}\n****EXPECTED RESULT****\n{1}\n********\n\n\n", resultData, expectedResult);
                //Console.WriteLine(str);
                //Console.WriteLine("<FAIL>");
                logger.Write(str);
            }

            vm.SqlFinalize();
            vm = null;

            return exitStatus;
        }
Exemplo n.º 3
0
        private static void AddLogFileEntry(ITestLogger logger, string operation, int paramSetId, SqliteVm vm)
        {
            logger.Write("\nParamSet: {0}\n", paramSetId);
            int status = vm.Execute("Select * from Params where ParamSet={0}", paramSetId);

            while (status == Sqlite.Row)
            {
                string paramName = vm.GetString("ParamName");
                string paramValue = vm.GetString("ParamValue");

                logger.Write("{0}: {1}\n", paramName, paramValue);
                status = vm.NextRow();
            }
        }