<commit_before>﻿using NUnit.Framework;
using RatanaLibrary.Common.Cache;
using System;

namespace Tests.RatanaLibrary.Common.Cache
{
    [TestFixture]
    public class RedisCacheTest
    {
        [Test]
        [TestCase("RedisCacheTest:GetOrAdd:test-key1", "test-value", "test-fake-value")]
        [TestCase("RedisCacheTest:GetOrAdd:test-key2", "", "test-fake-value")]
        public void GetOrAdd(string cacheKey, string cacheValue, string fakeValue)
        {
            #region Arrange
            // Set up some variables
            var cache = new RedisCache(new RedisCache.RedisSettings() { });

            // Make sure the key we're about to test is empty
            ((ICache)cache).Remove(cacheKey);
            #endregion


            #region Act
            // 1 Try to save cacheValue under cacheKey.
            //   Since this key is new, the cacheValue should be saved to the cache
            //   and returned to returnedCachValue1;
            var returnedCachValue1 = ((ICache)cache).GetOrAdd(cacheKey, () =>
            {
                return cacheValue;
            });

            // 2 Try to save fakeValue under cacheKey.
            var returnedCachValue2 = ((ICache)cache).GetOrAdd(cacheKey, () =>
            {
                return fakeValue;
            });
            #endregion


            #region Assert
            // returnedCachValue1 should equal cacheValue because the cache is empty.
            Assert.AreEqual(cacheValue, returnedCachValue1);

            // returnedCachValue2 should equal cacheValue because the cache exists, 
            // so fakeValue was never returned.
            Assert.AreEqual(cacheValue, returnedCachValue2);
            Assert.AreNotEqual(fakeValue, returnedCachValue2);
            #endregion
        }

        [Test]
        public void GetOrAddAnonymousType()
        {
            #region Arrange
            // Set up some variables
            var cacheKey = "RedisCacheTest:GetOrAddAnonymousType:test-key";
            var cacheValue = new { Name = "test-name", Value = "test-value" };
            var fakeValue = new { Name = "test-fake-name", Value = "test-fake-value" };
            var cache = new RedisCache(new RedisCache.RedisSettings() { });

            // Make sure the key we're about to test is empty
            ((ICache)cache).Remove(cacheKey);
            #endregion


            #region Act
            // 1 Try to save cacheValue under cacheKey.
            //   Since this key is new, the cacheValue should be saved to the cache
            //   and returned to returnedCachValue1;
            var returnedCachValue1 = ((ICache)cache).GetOrAdd(cacheKey, () =>
            {
                return cacheValue;
            });

            // 2 Try to save fakeValue under cacheKey.
            var returnedCachValue2 = ((ICache)cache).GetOrAdd(cacheKey, () =>
            {
                return fakeValue;
            });
            #endregion


            #region Assert
            // returnedCachValue1 should equal cacheValue because the cache is empty.
            Assert.AreEqual(cacheValue, returnedCachValue1);

            // returnedCachValue2 should equal cacheValue because the cache exists, 
            // so fakeValue was never returned.
            Assert.AreEqual(cacheValue, returnedCachValue2);
            Assert.AreNotEqual(fakeValue, returnedCachValue2);
            #endregion
        }

        [Test]
        [TestCase("RedisCacheTest:Remove:test-key", "test-value-1", "test-value-2")]
        [TestCase("RedisCacheTest:Remove:test-key", "", "test-value-2")]
        [TestCase("", "test-value-1", "test-value-2")]
        public void Remove(string cacheKey, string cacheValue1, string cacheValue2)
        {
            #region Arrange
            // Set up some variables
            var cache = new RedisCache(new RedisCache.RedisSettings(){});

            // Make sure the key we're about to test is empty
            ((ICache)cache).Remove(cacheKey);
            #endregion
<commit_msg>Refactor RedisCacheTest<commit_after>﻿using NUnit.Framework;
using RatanaLibrary.Common.Cache;
using System;

namespace Tests.RatanaLibrary.Common.Cache
{
    [TestFixture]
    public class RedisCacheTest
    {
        private readonly RedisCache.RedisSettings redisSettings = new RedisCache.RedisSettings()
        {
            Server = "localhost"
        };

        [Test]
        [TestCase("RedisCacheTest:GetOrAdd:test-key1", "test-value", "test-fake-value")]
        [TestCase("RedisCacheTest:GetOrAdd:test-key2", "", "test-fake-value")]
        public void GetOrAdd(string cacheKey, string cacheValue, string fakeValue)
        {
            #region Arrange
            // Set up some variables
            var cache = new RedisCache(this.redisSettings);

            // Make sure the key we're about to test is empty
            ((ICache)cache).Remove(cacheKey);
            #endregion


            #region Act
            // 1 Try to save cacheValue under cacheKey.
            //   Since this key is new, the cacheValue should be saved to the cache
            //   and returned to returnedCachValue1;
            var returnedCachValue1 = ((ICache)cache).GetOrAdd(cacheKey, () =>
            {
                return cacheValue;
            });

            // 2 Try to save fakeValue under cacheKey.
            var returnedCachValue2 = ((ICache)cache).GetOrAdd(cacheKey, () =>
            {
                return fakeValue;
            });
            #endregion


            #region Assert
            // returnedCachValue1 should equal cacheValue because the cache is empty.
            Assert.AreEqual(cacheValue, returnedCachValue1);

            // returnedCachValue2 should equal cacheValue because the cache exists, 
            // so fakeValue was never returned.
            Assert.AreEqual(cacheValue, returnedCachValue2);
            Assert.AreNotEqual(fakeValue, returnedCachValue2);
            #endregion
        }

        [Test]
        public void GetOrAddAnonymousType()
        {
            #region Arrange
            // Set up some variables
            var cacheKey = "RedisCacheTest:GetOrAddAnonymousType:test-key";
            var cacheValue = new { Name = "test-name", Value = "test-value" };
            var fakeValue = new { Name = "test-fake-name", Value = "test-fake-value" };
            var cache = new RedisCache(this.redisSettings);

            // Make sure the key we're about to test is empty
            ((ICache)cache).Remove(cacheKey);
            #endregion


            #region Act
            // 1 Try to save cacheValue under cacheKey.
            //   Since this key is new, the cacheValue should be saved to the cache
            //   and returned to returnedCachValue1;
            var returnedCachValue1 = ((ICache)cache).GetOrAdd(cacheKey, () =>
            {
                return cacheValue;
            });

            // 2 Try to save fakeValue under cacheKey.
            var returnedCachValue2 = ((ICache)cache).GetOrAdd(cacheKey, () =>
            {
                return fakeValue;
            });
            #endregion


            #region Assert
            // returnedCachValue1 should equal cacheValue because the cache is empty.
            Assert.AreEqual(cacheValue, returnedCachValue1);

            // returnedCachValue2 should equal cacheValue because the cache exists, 
            // so fakeValue was never returned.
            Assert.AreEqual(cacheValue, returnedCachValue2);
            Assert.AreNotEqual(fakeValue, returnedCachValue2);
            #endregion
        }

        [Test]
        [TestCase("RedisCacheTest:Remove:test-key", "test-value-1", "test-value-2")]
        [TestCase("RedisCacheTest:Remove:test-key", "", "test-value-2")]
        [TestCase("", "test-value-1", "test-value-2")]
        public void Remove(string cacheKey, string cacheValue1, string cacheValue2)
        {
            #region Arrange
            // Set up some variables
            var cache = new RedisCache(this.redisSettings);

            // Make sure the key we're about to test is empty
            ((ICache)cache).Remove(cacheKey);
            #endregion

{'fraction_non_alphanumeric': 0.09283268101761252, 'fraction_numerical': 0.007093933463796477, 'mean_word_length': 1.853105373342638, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6339315', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2090, 'n_words': 572}