<commit_before>﻿using System;
using System.Net;
using System.Web;
using tSQLt.Client.Net.Gateways;
using tSQLt.Client.Net.Parsers;

namespace tSQLt.Client.Net
{
    public class tSQLtTestRunner
    {
        private readonly ISqlServerGateway _gateway;

        /// <summary>
        /// Creates a tSQLtTestRunner which is used to run tests against a Sql Server database which contains the tests to run
        /// </summary>
        /// <param name="connectionString">The connection string including the initial catalog of the database to connect to</param>
        /// <param name="runTimeout">The command timeout for running the tests, defaults to 2 minutes</param>
        public tSQLtTestRunner(string connectionString, int runTimeout = 1000 * 120)
        {
            _gateway = new SqlServerGateway(connectionString, runTimeout);
        }

        /// <summary>
        /// This is typically only used to test the API. Use the "tSQLtTestRunner(string connectionString)" constructor instead
        /// </summary>
        /// <param name="gateway"></param>
        public tSQLtTestRunner(ISqlServerGateway gateway /*Typically only used for testing the API*/)
        {
            _gateway = gateway;
        }

        /// <summary>
        /// Execute the tSQLt test that is in the schema "testClass" with the name "name"
        /// 
        /// For Example to run the SQLCop test "test User Aliases" you would pass in:
        /// 
        ///     testClass = "SQLCop"
        ///     name      = "test User Aliases"
        ///
        /// </summary>
        /// <param name="testClass">The name of the schema the test lives in</param>
        /// <param name="name">The name of the test to run</param>
        /// <returns>TestSuites - The Results of the test</returns>
        public TestSuites Run(string testClass, string name)
        {
            return GetResults(Queries.GetQueryForSingleTest(testClass, name));
        }

        /// <summary>
        /// Executes all of the tSQLt tests that are within the schema "testClass"
        /// 
        /// For Example to run all of the SQLCop tests you would pass in:
        /// 
        ///     testClass = "SQLCop"
        /// 
        /// </summary>
        /// <param name="testClass">The name of the schema holding all the tests you would like to run</param>
        /// <returns></returns>
        public TestSuites RunClass(string testClass)
        {
            return GetResults(Queries.GetQueryForClass(testClass), Queries.GetQueryForJustResults());
        }

        /// <summary>
        /// Executes all of the tSQLt tests that are within the database the tSQLtTestRunner is connected to
        /// </summary>
        /// <returns></returns>
        public TestSuites RunAll()
        {
            return GetResults(Queries.GetQueryForAll(), Queries.GetQueryForJustResults());
        }

        private TestSuites GetResults(string queryRun, string queryResults)
        {
            string xml = "";

            try
            {
                _gateway.RunWithNoResult(queryRun);
                xml = _gateway.RunWithXmlResult(queryResults);
            }
            catch (Exception ex)
            {
                xml = FailureMessageXml(ex.Message);
            }

            return XmlParser.Get(xml);
        }

        private TestSuites GetResults(string query)
        {
            string xml = "";

            try
            {
                xml = _gateway.RunWithXmlResult(query);
            }
            catch (Exception ex)
            {
                xml = FailureMessageXml(ex.Message);
            }

            return XmlParser.Get(xml);
        }

        private string FailureMessageXml(string message)
        {
            return string.Format(@"<testsuites><testsuite name=""failure"" tests=""1"" errors=""1"" failures=""1""> 
                <testcase classname=""failure"" name=""message"">              
                <failure message=""{0}"" />
            </testcase></testsuite></testsuites> ", WebUtility.HtmlEncode(message.Replace("\r", "").Replace("\n", "")));

        }

    }
}<commit_msg>Handling case where failure message is null<commit_after>﻿using System;
using System.Net;
using System.Web;
using tSQLt.Client.Net.Gateways;
using tSQLt.Client.Net.Parsers;

namespace tSQLt.Client.Net
{
    public class tSQLtTestRunner
    {
        private readonly ISqlServerGateway _gateway;

        /// <summary>
        /// Creates a tSQLtTestRunner which is used to run tests against a Sql Server database which contains the tests to run
        /// </summary>
        /// <param name="connectionString">The connection string including the initial catalog of the database to connect to</param>
        /// <param name="runTimeout">The command timeout for running the tests, defaults to 2 minutes</param>
        public tSQLtTestRunner(string connectionString, int runTimeout = 1000 * 120)
        {
            _gateway = new SqlServerGateway(connectionString, runTimeout);
        }

        /// <summary>
        /// This is typically only used to test the API. Use the "tSQLtTestRunner(string connectionString)" constructor instead
        /// </summary>
        /// <param name="gateway"></param>
        public tSQLtTestRunner(ISqlServerGateway gateway /*Typically only used for testing the API*/)
        {
            _gateway = gateway;
        }

        /// <summary>
        /// Execute the tSQLt test that is in the schema "testClass" with the name "name"
        /// 
        /// For Example to run the SQLCop test "test User Aliases" you would pass in:
        /// 
        ///     testClass = "SQLCop"
        ///     name      = "test User Aliases"
        ///
        /// </summary>
        /// <param name="testClass">The name of the schema the test lives in</param>
        /// <param name="name">The name of the test to run</param>
        /// <returns>TestSuites - The Results of the test</returns>
        public TestSuites Run(string testClass, string name)
        {
            return GetResults(Queries.GetQueryForSingleTest(testClass, name));
        }

        /// <summary>
        /// Executes all of the tSQLt tests that are within the schema "testClass"
        /// 
        /// For Example to run all of the SQLCop tests you would pass in:
        /// 
        ///     testClass = "SQLCop"
        /// 
        /// </summary>
        /// <param name="testClass">The name of the schema holding all the tests you would like to run</param>
        /// <returns></returns>
        public TestSuites RunClass(string testClass)
        {
            return GetResults(Queries.GetQueryForClass(testClass), Queries.GetQueryForJustResults());
        }

        /// <summary>
        /// Executes all of the tSQLt tests that are within the database the tSQLtTestRunner is connected to
        /// </summary>
        /// <returns></returns>
        public TestSuites RunAll()
        {
            return GetResults(Queries.GetQueryForAll(), Queries.GetQueryForJustResults());
        }

        private TestSuites GetResults(string queryRun, string queryResults)
        {
            string xml = "";

            try
            {
                _gateway.RunWithNoResult(queryRun);
                xml = _gateway.RunWithXmlResult(queryResults);
            }
            catch (Exception ex)
            {
                xml = FailureMessageXml(ex.Message);
            }

            return XmlParser.Get(xml);
        }

        private TestSuites GetResults(string query)
        {
            string xml = "";

            try
            {
                xml = _gateway.RunWithXmlResult(query);
            }
            catch (Exception ex)
            {
                xml = FailureMessageXml(ex.Message);
            }

            return XmlParser.Get(xml);
        }

        private string FailureMessageXml(string message)
        {
            return string.Format(@"<testsuites><testsuite name=""failure"" tests=""1"" errors=""1"" failures=""1""> 
                <testcase classname=""failure"" name=""message"">              
                <failure message=""{0}"" />
            </testcase></testsuite></testsuites> ",  message == null ? "Unknown Failure" :  WebUtility.HtmlEncode(message.Replace("\r", "").Replace("\n", "")));

        }

    }
}
{'fraction_non_alphanumeric': 0.10964491362763916, 'fraction_numerical': 0.0028790786948176585, 'mean_word_length': 1.8089622641509433, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6873597', 'n_tokens_mistral': 2182, 'n_tokens_neox': 2113, 'n_words': 626}