<commit_before>﻿using DbShell.Driver.Common.CommonTypeSystem;
using DbShell.EngineProviders.Test;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Xunit;

namespace DbShell.Test.DbEngineTests
{
    public class AnalyserTests : DatabaseTestBase
    {
        [Theory]
        [ClassData(typeof(DatabaseEngineGenerator))]
        public void FullAnalyse(string engine)
        {
            Initialize(engine);

            using (var conn = OpenConnection())
            {
                var factory = DatabaseFactory;
                var analyser = factory.CreateAnalyser();
                analyser.Connection = conn;
                analyser.FullAnalysis();
                var result = analyser.Structure;
                Assert.Equal(5, result.Tables.Count);

                var album = result.FindTableLike("album");
                Assert.NotNull(album);
                Assert.Equal(3, album.ColumnCount);
                Assert.True(album.Columns[0].CommonType is DbTypeInt);
                Assert.True(album.Columns[1].CommonType is DbTypeString);
                Assert.True(album.Columns[2].CommonType is DbTypeInt);
                Assert.True(album.Columns[0].AutoIncrement);
                Assert.Equal(1, album.ForeignKeys.Count);
                var fk = album.ForeignKeys[0];
                Assert.Equal("artist", fk.RefTableName?.ToLower());
                Assert.Equal(1, fk.Columns.Count);

                var genre = result.FindTableLike("genre");
                Assert.False(genre.Columns[0].AutoIncrement);

                // check autoincrement flag of empty table
                var importedData = result.FindTableLike("importedData");
                Assert.True(importedData.Columns[0].AutoIncrement);
            }
        }

        [Theory]
        [ClassData(typeof(DatabaseEngineGenerator))]
        public void IncrementalAnalyse(string engine)
        {
            Initialize(engine);
            var dialect = DatabaseFactory.CreateDialect();

            using (var conn = OpenConnection())
            {
                var factory = DatabaseFactory;
                var analyser = factory.CreateAnalyser();
                analyser.Connection = conn;
                analyser.FullAnalysis();
                var dbInfo = analyser.Structure;

                // new analyser
                analyser = factory.CreateAnalyser();
                analyser.Connection = conn;
                analyser.Structure = dbInfo;
                analyser.GetModifications();
                var changeSet = analyser.ChangeSet;
                Assert.Equal(0, changeSet.Items.Count);

                Thread.Sleep(TimeSpan.FromSeconds(1.5));
                RunScript($"alter table {dialect.QuoteIdentifier("Genre")} add testflag int null;");
                analyser = factory.CreateAnalyser();
                analyser.Connection = conn;
                analyser.Structure = dbInfo;
                analyser.GetModifications();
                changeSet = analyser.ChangeSet;
                Assert.Equal(1, changeSet.Items.Count);

                analyser = factory.CreateAnalyser();
                analyser.Connection = conn;
                analyser.Structure = dbInfo;
                analyser.ChangeSet = changeSet;
                analyser.IncrementalAnalysis();
                var newDbInfo = analyser.Structure;

                var genre = newDbInfo.FindTableLike("genre");
                Assert.Equal(3, genre.ColumnCount);
                Assert.Equal("testflag", genre.Columns[2].Name);
            }
        }
    }
}<commit_msg>drop table - incremental change test<commit_after>﻿using DbShell.Driver.Common.CommonTypeSystem;
using DbShell.EngineProviders.Test;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Xunit;

namespace DbShell.Test.DbEngineTests
{
    public class AnalyserTests : DatabaseTestBase
    {
        [Theory]
        [ClassData(typeof(DatabaseEngineGenerator))]
        public void FullAnalyse(string engine)
        {
            Initialize(engine);

            using (var conn = OpenConnection())
            {
                var factory = DatabaseFactory;
                var analyser = factory.CreateAnalyser();
                analyser.Connection = conn;
                analyser.FullAnalysis();
                var result = analyser.Structure;
                Assert.Equal(5, result.Tables.Count);

                var album = result.FindTableLike("album");
                Assert.NotNull(album);
                Assert.Equal(3, album.ColumnCount);
                Assert.True(album.Columns[0].CommonType is DbTypeInt);
                Assert.True(album.Columns[1].CommonType is DbTypeString);
                Assert.True(album.Columns[2].CommonType is DbTypeInt);
                Assert.True(album.Columns[0].AutoIncrement);
                Assert.Equal(1, album.ForeignKeys.Count);
                var fk = album.ForeignKeys[0];
                Assert.Equal("artist", fk.RefTableName?.ToLower());
                Assert.Equal(1, fk.Columns.Count);

                var genre = result.FindTableLike("genre");
                Assert.False(genre.Columns[0].AutoIncrement);

                // check autoincrement flag of empty table
                var importedData = result.FindTableLike("importedData");
                Assert.True(importedData.Columns[0].AutoIncrement);
            }
        }

        [Theory]
        [ClassData(typeof(DatabaseEngineGenerator))]
        public void IncrementalAnalyse(string engine)
        {
            Initialize(engine);
            var dialect = DatabaseFactory.CreateDialect();

            using (var conn = OpenConnection())
            {
                var factory = DatabaseFactory;
                var analyser = factory.CreateAnalyser();
                analyser.Connection = conn;
                analyser.FullAnalysis();
                var dbInfo = analyser.Structure;

                // new analyser
                analyser = factory.CreateAnalyser();
                analyser.Connection = conn;
                analyser.Structure = dbInfo;
                analyser.GetModifications();
                var changeSet = analyser.ChangeSet;
                Assert.Equal(0, changeSet.Items.Count);

                Thread.Sleep(TimeSpan.FromSeconds(1.5));
                RunScript($"alter table {dialect.QuoteIdentifier("Genre")} add testflag int null;");
                analyser = factory.CreateAnalyser();
                analyser.Connection = conn;
                analyser.Structure = dbInfo;
                analyser.GetModifications();
                changeSet = analyser.ChangeSet;
                Assert.Equal(1, changeSet.Items.Count);

                analyser = factory.CreateAnalyser();
                analyser.Connection = conn;
                analyser.Structure = dbInfo;
                analyser.ChangeSet = changeSet;
                analyser.IncrementalAnalysis();
                var newDbInfo = analyser.Structure;

                var genre = newDbInfo.FindTableLike("genre");
                Assert.Equal(3, genre.ColumnCount);
                Assert.Equal("testflag", genre.Columns[2].Name);

                Thread.Sleep(TimeSpan.FromSeconds(1.5));
                RunScript($"drop table {dialect.QuoteIdentifier("Genre")};");
                analyser = factory.CreateAnalyser();
                analyser.Connection = conn;
                analyser.Structure = newDbInfo;
                analyser.GetModifications();
                analyser.IncrementalAnalysis();
                var newDbInfo2 = analyser.Structure;
                Assert.Equal(4, newDbInfo2.Tables.Count);
                var genre2 = newDbInfo.FindTableLike("genre");
                Assert.Null(genre2);
           }
        }
    }
}
{'fraction_non_alphanumeric': 0.09647896275581543, 'fraction_numerical': 0.005211643574424813, 'mean_word_length': 1.7376478775226165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2000698', 'n_tokens_mistral': 2039, 'n_tokens_neox': 1932, 'n_words': 349}