<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using NBi.Core.Query;
using NBi.Xml.Items;
using NUnit.Framework;
using NBi.Core.Scalar.Resolver;

namespace NBi.Testing.Integration.Core.Query
{
    [TestFixture]
    public class CommandBuilderTest
    {
        
        #region SetUp & TearDown
        //Called only at instance creation
        [TestFixtureSetUp]
        public void SetupMethods()
        {

        }

        //Called only at instance destruction
        [TestFixtureTearDown]
        public void TearDownMethods()
        {
        }

        //Called before each test
        [SetUp]
        public void SetupTest()
        {
        }

        //Called after each test
        [TearDown]
        public void TearDownTest()
        {
        }
        #endregion

        
        [Test, Category("Sql")]
        public void Build_OneParameterWithTypeInt_CorrectResultSet()
        {
            var commandBuilder = new CommandBuilder();
            var cmd = commandBuilder.Build(
                ConnectionStringReader.GetSqlClient(),
                "select * from [Sales].[Customer] where CustomerID=@Param",

                new List<QueryParameter>()
                {
                    new QueryParameter("@Param", "int", new LiteralScalarResolver<object>("2"))
                }
            );

            cmd.Connection.Open();
            var dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

            Assert.That(dr.Read(), Is.True);
            Assert.That(dr.GetValue(0), Is.EqualTo(2));
            Assert.That(dr.Read(), Is.False);
        }

        [Test, Category("Sql")]
        public void Build_OneParameterWithTypeNvarchar50_CorrectResultSet()
        {
            var commandBuilder = new CommandBuilder();
            var cmd = commandBuilder.Build(
                ConnectionStringReader.GetSqlClient(),
                "select * from [Sales].[SalesTerritory] where Name=@Param",

                new List<QueryParameter>() 
                {
                    new QueryParameter("@Param", "nvarchar(50)", new LiteralScalarResolver<object>("Canada"))
                }
            );

            cmd.Connection.Open();
            var dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

            Assert.That(dr.Read(), Is.True);
            Assert.That(dr.GetValue(1), Is.EqualTo("Canada"));
            Assert.That(dr.Read(), Is.False);
        }

        [Test, Category("Sql")]
        public void Build_OneParameterWithoutTypeInt_CorrectResultSet()
        {
            var commandBuilder = new CommandBuilder();
            var cmd = commandBuilder.Build(
                ConnectionStringReader.GetSqlClient(),
                "select * from [Sales].[Customer] where CustomerID=@Param",
                
                new List<QueryParameter>() 
                {
                    new QueryParameter("@Param", string.Empty, new LiteralScalarResolver<object>("Canada"))
                }
                );

            cmd.Connection.Open();
            var dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

            Assert.That(dr.Read(), Is.True);
            Assert.That(dr.GetValue(0), Is.EqualTo(2));
            Assert.That(dr.Read(), Is.False);
        }

        [Test, Category("Sql")]
        public void Build_WithUselessParameter_CorrectResultSet()
        {
            var commandBuilder = new CommandBuilder();
            var cmd = commandBuilder.Build(
                ConnectionStringReader.GetSqlClient(),
                "select * from [Sales].[SalesTerritory] where Name=@Param",

                new List<QueryParameter>() 
                {
                    new QueryParameter("@Param", "Canada"),
                    new QueryParameter("@UnusedParam", "Useless")<commit_msg>Fix failing test<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using NBi.Core.Query;
using NBi.Xml.Items;
using NUnit.Framework;
using NBi.Core.Scalar.Resolver;

namespace NBi.Testing.Integration.Core.Query
{
    [TestFixture]
    public class CommandBuilderTest
    {
        
        #region SetUp & TearDown
        //Called only at instance creation
        [TestFixtureSetUp]
        public void SetupMethods()
        {

        }

        //Called only at instance destruction
        [TestFixtureTearDown]
        public void TearDownMethods()
        {
        }

        //Called before each test
        [SetUp]
        public void SetupTest()
        {
        }

        //Called after each test
        [TearDown]
        public void TearDownTest()
        {
        }
        #endregion

        
        [Test, Category("Sql")]
        public void Build_OneParameterWithTypeInt_CorrectResultSet()
        {
            var commandBuilder = new CommandBuilder();
            var cmd = commandBuilder.Build(
                ConnectionStringReader.GetSqlClient(),
                "select * from [Sales].[Customer] where CustomerID=@Param",

                new List<QueryParameter>()
                {
                    new QueryParameter("@Param", "int", new LiteralScalarResolver<object>("2"))
                }
            );

            cmd.Connection.Open();
            var dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

            Assert.That(dr.Read(), Is.True);
            Assert.That(dr.GetValue(0), Is.EqualTo(2));
            Assert.That(dr.Read(), Is.False);
        }

        [Test, Category("Sql")]
        public void Build_OneParameterWithTypeNvarchar50_CorrectResultSet()
        {
            var commandBuilder = new CommandBuilder();
            var cmd = commandBuilder.Build(
                ConnectionStringReader.GetSqlClient(),
                "select * from [Sales].[SalesTerritory] where Name=@Param",

                new List<QueryParameter>() 
                {
                    new QueryParameter("@Param", "nvarchar(50)", new LiteralScalarResolver<object>("Canada"))
                }
            );

            cmd.Connection.Open();
            var dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

            Assert.That(dr.Read(), Is.True);
            Assert.That(dr.GetValue(1), Is.EqualTo("Canada"));
            Assert.That(dr.Read(), Is.False);
        }

        [Test, Category("Sql")]
        public void Build_OneParameterWithoutTypeInt_CorrectResultSet()
        {
            var commandBuilder = new CommandBuilder();
            var cmd = commandBuilder.Build(
                ConnectionStringReader.GetSqlClient(),
                "select * from [Sales].[Customer] where CustomerID=@Param",
                
                new List<QueryParameter>() 
                {
                    new QueryParameter("@Param", string.Empty, new LiteralScalarResolver<object>(2))
                }
                );

            cmd.Connection.Open();
            var dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

            Assert.That(dr.Read(), Is.True);
            Assert.That(dr.GetValue(0), Is.EqualTo(2));
            Assert.That(dr.Read(), Is.False);
        }

        [Test, Category("Sql")]
        public void Build_WithUselessParameter_CorrectResultSet()
        {
            var commandBuilder = new CommandBuilder();
            var cmd = commandBuilder.Build(
                ConnectionStringReader.GetSqlClient(),
                "select * from [Sales].[SalesTerritory] where Name=@Param",

                new List<QueryParameter>() 
                {
                    new QueryParameter("@Param", "Canada"),
                    new QueryParameter("@UnusedParam", "Useless")
{'fraction_non_alphanumeric': 0.1072628586197984, 'fraction_numerical': 0.0027138795554406824, 'mean_word_length': 1.7688729874776385, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '976875', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1901, 'n_words': 379}