<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Searchlight.Configuration;
using Searchlight.Configuration.Default;
using Searchlight.Parsing;
using Searchlight.Query;
using Searchlight.Exceptions;
using NUnit.Framework;
using Searchlight.DataSource;

namespace Searchlight.Tests.Parsing
{
    public class SafeQueryParserTests
    {
        private SearchlightDataSource _source;

        public SafeQueryParserTests()
        {
            _source = new SearchlightDataSource();
            _source.ColumnDefinitions = new CustomColumnDefinition()
                .WithColumn("a", typeof(String), null)
                .WithColumn("b", typeof(Int32), null)
                .WithColumn("colLong", typeof(Int64), null)
                .WithColumn("colNullableGuid", typeof(Nullable<Guid>), null)
                .WithColumn("colULong", typeof(UInt64), null)
                .WithColumn("colNullableULong", typeof(Nullable<UInt64>), null)
                .WithColumn("colGuid", typeof(Guid), null);
            _source.Columnifier = new NoColumnify();
            _source.DatabaseType = DatabaseType.Mysql;
        }

        [Test(Description = "Parser.IncorrectFieldValueType")]
        public void IncorrectFieldValueType()
        {
            string originalFilter = "a = 'test' and b = 'Hello!'";
            var ex = Assert.Throws<FieldValueException>(() => SafeQueryParser.ParseFilter(originalFilter, _source));
            Assert.AreEqual("b", ex.FieldName);
            Assert.AreEqual("System.Int32", ex.FieldType);
            Assert.AreEqual("Hello!", ex.FieldValue);
            Assert.AreEqual(originalFilter, ex.OriginalFilter);
        }

        [Test(Description = "Parser.AllParenthesis")]
        public void AllParenthesis()
        {
            // Basic problem: if you never close a parenthesis that's a syntax error
            var ex1 = Assert.Throws<TrailingConjunctionException>(() => SafeQueryParser.ParseFilter("(((((((((((", _source));

            // If you unbalance your parenthesis, that's a syntax error
            var ex2 = Assert.Throws<OpenClauseException>(() => SafeQueryParser.ParseFilter("((((((((((()))", _source));
            var ex3 = Assert.Throws<OpenClauseException>(() => SafeQueryParser.ParseFilter("((())))))))))))", _source));

            // If you forget to supply any actual criteria, that's a syntax error
            var ex4 = Assert.Throws<EmptyClauseException>(() => SafeQueryParser.ParseFilter("()", _source));
        }

        [Test(Description = "Parser.OrderByParseTest")]
        [TestCase("a, b DESC")]
        [TestCase("a AsC, b DESc")]
        [TestCase("a asc   , b    DESC    ")]
        public void OrderByParseTest(string orderby)
        {
            var result = SafeQueryParser.ParseOrderBy(orderby, _source);
            Assert.AreEqual("a", result[0].Column.FieldName);
            Assert.AreEqual(SortDirection.Ascending, result[0].Direction);
            Assert.AreEqual("b", result[1].Column.FieldName);
            Assert.AreEqual(SortDirection.Descending, result[1].Direction);
        }

        [Test(Description = "Parser.OrderByExceptions")]
        public void OrderByExceptionsTest()
        {
            // Field doesn't exist
            Assert.Throws<FieldNameException>(() => SafeQueryParser.ParseOrderBy("c, d ASC", _source));

            // No comma between fields
            Assert.Throws<FieldNameException>(() => SafeQueryParser.ParseOrderBy("a b DESC", _source));

            // Trailing comma
            Assert.Throws<FieldNameException>(() => SafeQueryParser.ParseOrderBy("a, b,", _source));
        }

        [Test(Description = "Parser.FilterParseTest")]
        public void FilterParseTest()
        {
            WhereClause actual = _parser.ParseWhereClause("a = 'booya' AND b != 1");
            Assert.AreEqual("a = @p1 AND b <> @p2", actual.ValidatedFilter);
            Assert.AreEqual(2, actual.SqlParameters.ParameterNames.Count());

            // TODO: way more tests
        }

        [Test(Description = "Parser.FullyQualifySelectWhereAndOrderBy.MySQL")]
        public void FullyQualifySelectWhereAndOrderBy()
        {
            var safeColumns = new CustomColumnDefinition()<commit_msg>Small amounts of work on the integration tests<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Searchlight.Configuration;
using Searchlight.Configuration.Default;
using Searchlight.Parsing;
using Searchlight.Query;
using Searchlight.Exceptions;
using NUnit.Framework;
using Searchlight.DataSource;

namespace Searchlight.Tests.Parsing
{
    public class SafeQueryParserTests
    {
        private SearchlightDataSource _source;

        public SafeQueryParserTests()
        {
            _source = new SearchlightDataSource();
            _source.ColumnDefinitions = new CustomColumnDefinition()
                .WithColumn("a", typeof(String), null)
                .WithColumn("b", typeof(Int32), null)
                .WithColumn("colLong", typeof(Int64), null)
                .WithColumn("colNullableGuid", typeof(Nullable<Guid>), null)
                .WithColumn("colULong", typeof(UInt64), null)
                .WithColumn("colNullableULong", typeof(Nullable<UInt64>), null)
                .WithColumn("colGuid", typeof(Guid), null);
            _source.Columnifier = new NoColumnify();
            _source.DatabaseType = DatabaseType.Mysql;
        }

        [Test(Description = "Parser.IncorrectFieldValueType")]
        public void IncorrectFieldValueType()
        {
            string originalFilter = "a = 'test' and b = 'Hello!'";
            var ex = Assert.Throws<FieldValueException>(() => SafeQueryParser.ParseFilter(originalFilter, _source));
            Assert.AreEqual("b", ex.FieldName);
            Assert.AreEqual("System.Int32", ex.FieldType);
            Assert.AreEqual("Hello!", ex.FieldValue);
            Assert.AreEqual(originalFilter, ex.OriginalFilter);
        }

        [Test(Description = "Parser.AllParenthesis")]
        public void AllParenthesis()
        {
            // Basic problem: if you never close a parenthesis that's a syntax error
            var ex1 = Assert.Throws<TrailingConjunctionException>(() => SafeQueryParser.ParseFilter("(((((((((((", _source));

            // If you unbalance your parenthesis, that's a syntax error
            var ex2 = Assert.Throws<OpenClauseException>(() => SafeQueryParser.ParseFilter("((((((((((()))", _source));
            var ex3 = Assert.Throws<OpenClauseException>(() => SafeQueryParser.ParseFilter("((())))))))))))", _source));

            // If you forget to supply any actual criteria, that's a syntax error
            var ex4 = Assert.Throws<EmptyClauseException>(() => SafeQueryParser.ParseFilter("()", _source));
        }

        [Test(Description = "Parser.OrderByParseTest")]
        [TestCase("a, b DESC")]
        [TestCase("a AsC, b DESc")]
        [TestCase("a asc   , b    DESC    ")]
        public void OrderByParseTest(string orderby)
        {
            var result = SafeQueryParser.ParseOrderBy(orderby, _source);
            Assert.AreEqual("a", result[0].Column.FieldName);
            Assert.AreEqual(SortDirection.Ascending, result[0].Direction);
            Assert.AreEqual("b", result[1].Column.FieldName);
            Assert.AreEqual(SortDirection.Descending, result[1].Direction);
        }

        [Test(Description = "Parser.OrderByExceptions")]
        public void OrderByExceptionsTest()
        {
            // Field doesn't exist
            Assert.Throws<FieldNameException>(() => SafeQueryParser.ParseOrderBy("c, d ASC", _source));

            // No comma between fields
            Assert.Throws<FieldNameException>(() => SafeQueryParser.ParseOrderBy("a b DESC", _source));

            // Trailing comma
            Assert.Throws<FieldNameException>(() => SafeQueryParser.ParseOrderBy("a, b,", _source));
        }

        [Test(Description = "Parser.FilterParseTest")]
        public void FilterParseTest()
        {
            var actual = SafeQueryParser.ParseFilter("a = 'booya' AND b != 1");
            Assert.AreEqual("a", actual[0].
            Assert.AreEqual("a = @p1 AND b <> @p2", actual.ValidatedFilter);
            Assert.AreEqual(2, actual.SqlParameters.ParameterNames.Count());

            // TODO: way more tests
        }

        [Test(Description = "Parser.FullyQualifySelectWhereAndOrderBy.MySQL")]
        public void FullyQualifySelectWhereAndOrderBy()
        {
            var safeColumns = new CustomColumnDefinition()
{'fraction_non_alphanumeric': 0.12593109869646182, 'fraction_numerical': 0.005237430167597765, 'mean_word_length': 2.672222222222222, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2365501', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2235, 'n_words': 478}