Exemplo n.º 1
0
 public static TestResult FromByteReader(MgByteReader byteReader, string operation = "")
 {
     try
     {
         TestResult res = new TestResult();
         if (byteReader != null)
         {
             res.ContentType = byteReader.GetMimeType();
             if (res.ContentType == MgMimeType.Html ||
                 res.ContentType == MgMimeType.Json ||
                 res.ContentType == MgMimeType.Kml ||
                 res.ContentType == MgMimeType.Text ||
                 res.ContentType == MgMimeType.Xml)
             {
                 res.ResultData = byteReader.ToString();
             }
             else
             {
                 MgByteSink sink = new MgByteSink(byteReader);
                 string path = operation + Guid.NewGuid().ToString() + "Result.bin";
                 if (string.IsNullOrEmpty(operation))
                     path = Path.GetTempFileName();
                 sink.ToFile(path);
                 res.ResultData = File.ReadAllBytes(path);
                 if (string.IsNullOrEmpty(operation))
                     File.Delete(path);
                 else
                     System.Diagnostics.Debug.WriteLine(string.Format("[MgTestRunner]: Check out {0} if binary comparison results are strange", path));
                 /*
                 byte[] bytes = new byte[byteReader.GetLength()];
                 byteReader.Read(bytes, bytes.Length);
                 res.ResultData = bytes;
                 */
             }
         }
         return res;
     }
     catch (MgException ex)
     {
         return FromMgException(ex);
     }
 }
Exemplo n.º 2
0
 public abstract int ValidateRequest(SqliteDb db, string testName, int paramSetId, string operation, TestResult actualResult, ITestLogger logger);
        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;
        }