<commit_before>﻿using eFormSqlController;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;

namespace eFormSDK.Integration.Tests
{
    [TestFixture]
    public abstract class DbTestFixture
    {
        protected MicrotingDbMs DbContext;
        protected string ConnectionString => @"data source=(LocalDb)\SharedInstance;Initial catalog=eformsdk-tests";

        private static string userName = "__USER_NAME__";
        private static string password = <PASSWORD>";
        private static string databaseName = "__DBNAME__";
        private static string databaseServerId = "__DB_SERVER_ID__";
        private static string directoryId = "__DIRECTORY_ID__";
        private static string applicationId = "__APPLICATION_ID__";

        [SetUp]
        public void Setup()
        {            
            DbContext = new MicrotingDbMs(ConnectionString);
            DbContext.Database.CommandTimeout = 300;


            if (!userName.Contains("USER_NAME"))
            {
                var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(applicationId, password, directoryId, AzureEnvironment.AzureGlobalCloud);
                var azure = Azure
                    .Configure()
                    .Authenticate(credentials)
                    .WithDefaultSubscription();

                var sqlServer = azure.SqlServers.GetById(databaseServerId);
                sqlServer.Databases.Define(databaseName: databaseName).Create();
            }
            DbContext.Database.CreateIfNotExists();

            DbContext.Database.Initialize(true);

            DoSetup();
        }

        [TearDown]
        public void TearDown()
        {
            var metadata = ((IObjectContextAdapter)DbContext).ObjectContext.MetadataWorkspace.GetItems(DataSpace.SSpace);

            List<string> tables = new List<string>();
            foreach (var item in metadata)
            {
                if (item.ToString().Contains("CodeFirstDatabaseSchema"))
                {
                    tables.Add(item.ToString().Replace("CodeFirstDatabaseSchema.", ""));
                }
            }


            foreach (string tableName in tables)
            {
                try
                {
                    DbContext.Database.ExecuteSqlCommand("DELETE FROM [" + tableName + "]");
                }
                catch (Exception e)
                {
                }
                
            }

            DbContext.Dispose();
<commit_msg>Fixing the deleting of table content, so we do it in setup also.<commit_after>﻿using eFormSqlController;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;

namespace eFormSDK.Integration.Tests
{
    [TestFixture]
    public abstract class DbTestFixture
    {
        protected MicrotingDbMs DbContext;
        protected string ConnectionString => @"data source=(LocalDb)\SharedInstance;Initial catalog=eformsdk-tests";

        private static string userName = "__USER_NAME__";
        private static string password = <PASSWORD>";
        private static string databaseName = "__DBNAME__";
        private static string databaseServerId = "__DB_SERVER_ID__";
        private static string directoryId = "__DIRECTORY_ID__";
        private static string applicationId = "__APPLICATION_ID__";

        [SetUp]
        public void Setup()
        {            
            DbContext = new MicrotingDbMs(ConnectionString);
            DbContext.Database.CommandTimeout = 300;


            ClearDb();


            if (!userName.Contains("USER_NAME"))
            {
                var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(applicationId, password, directoryId, AzureEnvironment.AzureGlobalCloud);
                var azure = Azure
                    .Configure()
                    .Authenticate(credentials)
                    .WithDefaultSubscription();

                var sqlServer = azure.SqlServers.GetById(databaseServerId);
                sqlServer.Databases.Define(databaseName: databaseName).Create();
            }
            DbContext.Database.CreateIfNotExists();

            DbContext.Database.Initialize(true);

            DoSetup();
        }

        [TearDown]
        public void TearDown()
        {

            ClearDb();

            DbContext.Dispose();

        }

        public void ClearDb()
        {
            var metadata = ((IObjectContextAdapter)DbContext).ObjectContext.MetadataWorkspace.GetItems(DataSpace.SSpace);

            List<string> tables = new List<string>();
            foreach (var item in metadata)
            {
                if (item.ToString().Contains("CodeFirstDatabaseSchema"))
                {
                    tables.Add(item.ToString().Replace("CodeFirstDatabaseSchema.", ""));
                }
            }

            foreach (string tableName in tables)
            {
                try
                {
                    DbContext.Database.ExecuteSqlCommand("DELETE FROM [" + tableName + "]");
                }
                catch 
                { }

            }
{'fraction_non_alphanumeric': 0.08663864010235789, 'fraction_numerical': 0.0010966916468652897, 'mean_word_length': 1.9755301794453508, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '1073599', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1276, 'n_words': 265}