<commit_before>﻿using System;
using System.Data.SQLite.EF6;
using System.IO;
using Dapper;
using Lemonade.Sql.Exceptions;
using Lemonade.Sql.Migrations;
using NUnit.Framework;

namespace Lemonade.Sql.Tests
{
    public class GivenSqlFeatureResolver
    {
        const string ConnectionString = "Data Source=test.db";

        [SetUp]
        public void SetUp()
        {
            Runner.Sqlite(ConnectionString).Up();
            InsertFeature(true, "MyEnabledFeature", "NUnit Lemonade.Sql.Tests");
            InsertFeature(false, "MyDisabledFeature", "NUnit Lemonade.Sql.Tests");
            InsertFeature(true, "MyEnabledApplicationSpecificFeature", "Lemonade");
        }

        [TearDown]
        public void TearDown()
        {
            Runner.Sqlite(ConnectionString).Down();
        }

        [Test]
        public void WhenIObtainAnSqlFeatureResolverViaConfiguration_AndDefaultConnectionStringExists_ThenResolverIsSet()
        {
            Assert.That(Feature.Resolver, Is.AssignableFrom<SqlFeatureResolver>());
        }

        [Test]
        public void WhenITryToObtainAnSqlFeatureResolverForANonExistantConnectionString_ThenConnectionStringNotFoundExceptionIsThrown()
        {
            Assert.Throws<ConnectionStringNotFoundException>(() =>
            {
                Feature.Resolver = new SqlFeatureResolver("ThisIsATest");
            });
        }

        [Test]
        public void WhenIGetAValidEnabledFeature_ThenReturnedValueIsTrue()
        {
            var executed = false;
            Feature.Switches.Execute("MyEnabledFeature", () => executed = true);
            Assert.That(executed, Is.True);
        }

        [Test]
        public void WhenIGetAValidDisabledFeature_ThenReturnedValueIsFalse()
        {
            var executed = false;
            Feature.Switches.Execute("MyDisabledFeature", () => executed = true);
            Assert.That(executed, Is.False);
        }

        [Test]
        public void WhenIGetAValidEnabledFeatureForADifferentApplication_ThenReturnedValueIsFalse()
        {
            var executed = false;
            Feature.Switches.Execute("MyEnabledApplicationSpecificFeature", () => executed = true);
            Assert.That(executed, Is.False);
        }

        private static void InsertFeature(bool isEnabled, string name, string application)
        {
            using (var cnn = new SQLiteProviderFactory().CreateConnection())
            {
                if (cnn != null) cnn.ConnectionString = ConnectionString;

                cnn.Execute("INSERT INTO Feature (IsEnabled, ExpirationDays, StartDate, FeatureName, ApplicationName)" +
                            "VALUES(@isEnabled, @expirationDays, @startDate, @name, @application)", new
                            {
                                isEnabled,
                                expirationDays = 1,
                                startDate = DateTime.Now,
                                name,
                                application
                            });
            }
        }
    }
}<commit_msg>Fixed unit test<commit_after>﻿using System;
using System.Data.SQLite.EF6;
using Dapper;
using Lemonade.Sql.Exceptions;
using Lemonade.Sql.Migrations;
using NUnit.Framework;

namespace Lemonade.Sql.Tests
{
    public class GivenSqlFeatureResolver
    {
        const string ConnectionString = "Data Source=test.db";

        [SetUp]
        public void SetUp()
        {
            Runner.Sqlite(ConnectionString).Up();
            InsertFeature(true, "MyEnabledFeature", AppDomain.CurrentDomain.FriendlyName);
            InsertFeature(false, "MyDisabledFeature", AppDomain.CurrentDomain.FriendlyName);
            InsertFeature(true, "MyEnabledApplicationSpecificFeature", "Lemonade");
        }

        [TearDown]
        public void TearDown()
        {
            Runner.Sqlite(ConnectionString).Down();
        }

        [Test]
        public void WhenIObtainAnSqlFeatureResolverViaConfiguration_AndDefaultConnectionStringExists_ThenResolverIsSet()
        {
            Assert.That(Feature.Resolver, Is.AssignableFrom<SqlFeatureResolver>());
        }

        [Test]
        public void WhenITryToObtainAnSqlFeatureResolverForANonExistantConnectionString_ThenConnectionStringNotFoundExceptionIsThrown()
        {
            Assert.Throws<ConnectionStringNotFoundException>(() =>
            {
                Feature.Resolver = new SqlFeatureResolver("ThisIsATest");
            });
        }

        [Test]
        public void WhenIGetAValidEnabledFeature_ThenReturnedValueIsTrue()
        {
            var executed = false;
            Feature.Switches.Execute("MyEnabledFeature", () => executed = true);
            Assert.That(executed, Is.True);
        }

        [Test]
        public void WhenIGetAValidDisabledFeature_ThenReturnedValueIsFalse()
        {
            var executed = false;
            Feature.Switches.Execute("MyDisabledFeature", () => executed = true);
            Assert.That(executed, Is.False);
        }

        [Test]
        public void WhenIGetAValidEnabledFeatureForADifferentApplication_ThenReturnedValueIsFalse()
        {
            var executed = false;
            Feature.Switches.Execute("MyEnabledApplicationSpecificFeature", () => executed = true);
            Assert.That(executed, Is.False);
        }

        private static void InsertFeature(bool isEnabled, string name, string application)
        {
            using (var cnn = new SQLiteProviderFactory().CreateConnection())
            {
                if (cnn != null) cnn.ConnectionString = ConnectionString;

                cnn.Execute("INSERT INTO Feature (IsEnabled, ExpirationDays, StartDate, FeatureName, ApplicationName)" +
                            "VALUES(@isEnabled, @expirationDays, @startDate, @name, @application)", new
                            {
                                isEnabled,
                                expirationDays = 1,
                                startDate = DateTime.Now,
                                name,
                                application
                            });
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.0845959595959596, 'fraction_numerical': 0.0006313131313131314, 'mean_word_length': 1.8077093486929552, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '7654572', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1498, 'n_words': 268}