<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using ADatabase;
using ADatabase.Interfaces;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ACopyTestHelper;

namespace ADatabaseTest
{
    [TestClass]
    public class TestDatabase
    {
        private IDbContext _msContext;
        private IDbContext _oraContext;
        private ICommands _commands;
        private IDbSchema _dbSchema;
        private string _testTable;
        private readonly ConnectionStrings _connectionStrings = new ConnectionStrings(@"..\..\ConnectionStrings.json");

        [TestInitialize]
        public void Setup()
        {
            _msContext = DbContextFactory.CreateSqlServerContext(_connectionStrings.GetSqlServer());
            _oraContext = DbContextFactory.CreateOracleContext(_connectionStrings.GetOracle());
            _testTable = "htestdatabase";
        }

        [TestCleanup]
        public void Cleanup()
        {
            _dbSchema.DropTable(_testTable);
        }

        private void Initialize(IDbContext dbContext)
        {
            _dbSchema = dbContext.PowerPlant.CreateDbSchema();
            _commands = dbContext.PowerPlant.CreateCommands();

            _dbSchema.DropTable(_testTable);
        }

        private void TestExecuteNonQuery(IDbContext dbContext)
        {
            Initialize(dbContext);
            TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, _testTable);
            _commands.ExecuteNonQuery($"update {_testTable} set val = 'AH'").Should().Be(1);
        }

        [TestMethod, TestCategory("SqlServer")]
        public void TestMS_ExecuteNonQuery()
        {
            TestExecuteNonQuery(_msContext);
        }

        [TestMethod, TestCategory("Oracle")]
        public void TestOra_ExecuteNonQuery()
        {
            TestExecuteNonQuery(_oraContext);
        }

        private void TestIsTable_When_TableExists(IDbContext dbContext)
        {
            Initialize(dbContext);
            TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, _testTable);
            _dbSchema.IsTable(_testTable).Should().BeTrue($"because {_testTable} should exist");
        }

        [TestMethod, TestCategory("SqlServer")]
        public void TestMS_IsTable_When_TableExists()
        {
            TestIsTable_When_TableExists(_msContext);
        }

        [TestMethod, TestCategory("Oracle")]
        public void TestOra_IsTable_When_TableDoesNotExist()
        {
            TestIsTable_When_TableExists(_oraContext);
        }

        private void TestCreateTableWithVarcharColumn(IDbContext dbContext)
        {
            Initialize(dbContext);
            TableDefinition tableDefinition = TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, _testTable);

            _dbSchema.IsTable(tableDefinition.Name).Should().BeTrue();
        }

        [TestMethod, TestCategory("SqlServer")]
        public void TestMSCreateTableWithVarchar()
        {
            TestCreateTableWithVarcharColumn(_msContext);
        }

        [TestMethod, TestCategory("Oracle")]
        public void TestOra_CreateTableWithVarchar()
        {
            TestCreateTableWithVarcharColumn(_oraContext);
        }

        private void TestGetTableNames_When_SingleTable(IDbContext dbContext)
        {
            Initialize(dbContext);
            TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, _testTable);
            _dbSchema.GetTableNames(_testTable)[0].Name.Should().BeEquivalentTo(_testTable);
        }

        [TestMethod, TestCategory("SqlServer")]
        public void TestMS_GetTableNames_When_SingleTable()
        {
            TestGetTableNames_When_SingleTable(_msContext);
        }

        [TestMethod, TestCategory("Oracle")]
        public void TestOra_GetTableNames_When_SingleTable()
        {
            TestGetTableNames_When_SingleTable(_oraContext);
        }

        private static void CompareToAcrparordAcrrepord(List<ITableShortInfo> actual)
        {
            (from t in actual
             where String.Compare(t.Name, "acrparord", StringComparison.OrdinalIgnoreCase) == 0
             select t).Count().Should().Be(1, "because acrparord exists");
            (from t in actual
             where String.Compare(t.Name, "acrrepord", StringComparison.OrdinalIgnoreCase) == 0
             select t).Count().Should().Be(1, "because acrrepord exists");
        }

        private void TestGetTableNames_When_WildcardMany(IDbContext dbContext)
        {
            Initialize(dbContext);
            List<ITableShortInfo> actual = _dbSchema.GetTableNames("acr%ord");
            CompareToAcrparordAcrrepord(actual);
        }

        [TestMethod, TestCategory("SqlServer")]
        public void TestMS_GetTableNames_When_WildcardMany()
        {
            TestGetTableNames_When_WildcardMany(_msContext);
        }

        [TestMethod, TestCategory("Oracle")]
        public void TestOra_GetTableNames_When_WildcardMany()
        {
            TestGetTableNames_When_WildcardMany(_oraContext);
        }

        private void TestGetTableNames_When_WildcardOne(IDbContext dbContext)
        {
            Initialize(dbContext);
            List<ITableShortInfo> actual = _dbSchema.GetTableNames("acr___ord");
            CompareToAcrparordAcrrepord(actual);
        }

        [TestMethod, TestCategory("SqlServer")]
        public void Test_MS_GetTableNames_When_WildcardOne()
        {
            TestGetTableNames_When_WildcardOne(_msContext);
        }

        [TestMethod, TestCategory("Oracle")]
        public void Test_Ora_GetTableNames_When_WildcardOne()
        {
            TestGetTableNames_When_WildcardOne(_oraContext);
        }<commit_msg>Tests don't depend on acrrepord and acrparord tables<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using ADatabase;
using ADatabase.Interfaces;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ACopyTestHelper;

namespace ADatabaseTest
{
    [TestClass]
    public class TestDatabase
    {
        private IDbContext _msContext;
        private IDbContext _oraContext;
        private ICommands _commands;
        private IDbSchema _dbSchema;
        private string _testTable;
        private readonly ConnectionStrings _connectionStrings = new ConnectionStrings(@"..\..\ConnectionStrings.json");

        [TestInitialize]
        public void Setup()
        {
            _msContext = DbContextFactory.CreateSqlServerContext(_connectionStrings.GetSqlServer());
            _oraContext = DbContextFactory.CreateOracleContext(_connectionStrings.GetOracle());
            _testTable = "htestdatabase";
        }

        [TestCleanup]
        public void Cleanup()
        {
            _dbSchema.DropTable(_testTable);
        }

        private void Initialize(IDbContext dbContext)
        {
            _dbSchema = dbContext.PowerPlant.CreateDbSchema();
            _commands = dbContext.PowerPlant.CreateCommands();

            _dbSchema.DropTable(_testTable);
        }

        private void TestExecuteNonQuery(IDbContext dbContext)
        {
            Initialize(dbContext);
            TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, _testTable);
            _commands.ExecuteNonQuery($"update {_testTable} set val = 'AH'").Should().Be(1);
        }

        [TestMethod, TestCategory("SqlServer")]
        public void TestMS_ExecuteNonQuery()
        {
            TestExecuteNonQuery(_msContext);
        }

        [TestMethod, TestCategory("Oracle")]
        public void TestOra_ExecuteNonQuery()
        {
            TestExecuteNonQuery(_oraContext);
        }

        private void TestIsTable_When_TableExists(IDbContext dbContext)
        {
            Initialize(dbContext);
            TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, _testTable);
            _dbSchema.IsTable(_testTable).Should().BeTrue($"because {_testTable} should exist");
        }

        [TestMethod, TestCategory("SqlServer")]
        public void TestMS_IsTable_When_TableExists()
        {
            TestIsTable_When_TableExists(_msContext);
        }

        [TestMethod, TestCategory("Oracle")]
        public void TestOra_IsTable_When_TableDoesNotExist()
        {
            TestIsTable_When_TableExists(_oraContext);
        }

        private void TestCreateTableWithVarcharColumn(IDbContext dbContext)
        {
            Initialize(dbContext);
            TableDefinition tableDefinition = TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, _testTable);

            _dbSchema.IsTable(tableDefinition.Name).Should().BeTrue();
        }

        [TestMethod, TestCategory("SqlServer")]
        public void TestMSCreateTableWithVarchar()
        {
            TestCreateTableWithVarcharColumn(_msContext);
        }

        [TestMethod, TestCategory("Oracle")]
        public void TestOra_CreateTableWithVarchar()
        {
            TestCreateTableWithVarcharColumn(_oraContext);
        }

        private void TestGetTableNames_When_SingleTable(IDbContext dbContext)
        {
            Initialize(dbContext);
            TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, _testTable);
            _dbSchema.GetTableNames(_testTable)[0].Name.Should().BeEquivalentTo(_testTable);
        }

        [TestMethod, TestCategory("SqlServer")]
        public void TestMS_GetTableNames_When_SingleTable()
        {
            TestGetTableNames_When_SingleTable(_msContext);
        }

        [TestMethod, TestCategory("Oracle")]
        public void TestOra_GetTableNames_When_SingleTable()
        {
            TestGetTableNames_When_SingleTable(_oraContext);
        }

        private static void CompareToAcrparordAcrrepord(List<ITableShortInfo> actual, string shouldFind1, string shouldFind2)
        {
            (from t in actual
             where String.Compare(t.Name, shouldFind1, StringComparison.OrdinalIgnoreCase) == 0
             select t).Count().Should().Be(1, $"because {shouldFind1} exists");
            (from t in actual
             where String.Compare(t.Name, shouldFind2, StringComparison.OrdinalIgnoreCase) == 0
             select t).Count().Should().Be(1, $"because {shouldFind2} exists");
        }

        private void TestGetTableNames_When_Wildcard(IDbContext dbContext, string searchString, string shouldFind1, string shouldFind2)
        {
            Initialize(dbContext);
            const string table1 = "htest0table";
            const string table2 = "htestsometable";
            try
            {
                TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, table1);
                TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, table2);
                TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, shouldFind1);
                TestTableCreator.CreateTableSomeColumnsAndOneRow(dbContext, shouldFind2);
                List<ITableShortInfo> actual = _dbSchema.GetTableNames(searchString);
                CompareToAcrparordAcrrepord(actual, shouldFind1, shouldFind2);

            }
            finally
            {
                _dbSchema.DropTable(table1);
                _dbSchema.DropTable(table2);
                _dbSchema.DropTable(shouldFind1);
                _dbSchema.DropTable(shouldFind2);
            }
        }

        [TestMethod, TestCategory("SqlServer")]
        public void TestMS_GetTableNames_When_WildcardMany()
        {
            TestGetTableNames_When_Wildcard(_msContext, "htest%table", "htest1table", "htestarvetable");
        }

        [TestMethod, TestCategory("Oracle")]
        public void TestOra_GetTableNames_When_WildcardMany()
        {
            TestGetTableNames_When_Wildcard(_oraContext, "htest%table", "htest1table", "htestarvetable");
        }

        [TestMethod, TestCategory("SqlServer")]
        public void Test_MS_GetTableNames_When_WildcardOne()
        {
            TestGetTableNames_When_Wildcard(_msContext, "htest__table", "htest11table", "htest98table");
        }

        [TestMethod, TestCategory("Oracle")]
        public void Test_Ora_GetTableNames_When_WildcardOne()
        {
            TestGetTableNames_When_Wildcard(_oraContext, "htest__table", "htest11table", "htest98table");
        }
{'fraction_non_alphanumeric': 0.08206958073148975, 'fraction_numerical': 0.003487146216851837, 'mean_word_length': 2.621732745961821, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '7194052', 'n_tokens_mistral': 3338, 'n_tokens_neox': 3255, 'n_words': 506}