<commit_before># -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab
"""
    tests.test_cache
    ~~~~~~~~~~~~~~~~

    Provides unit tests.
"""
from __future__ import (
    absolute_import, division, print_function, unicode_literals)

import time
import random

import nose.tools as nt

from mezmorize import Cache, function_namespace
from mezmorize.utils import HAS_MEMCACHE, HAS_REDIS, get_cache_config

from mezmorize.backends import (
    SimpleCache, FileSystemCache, RedisCache, MemcachedCache,
    SASLMemcachedCache, SpreadSASLMemcachedCache, AVAIL_MEMCACHES)

BIGINT = 2 ** 21
BIGGERINT = 2 ** 28


def setup_func(*args, **kwargs):
    namespace = kwargs.pop('namespace', None)
    client_name = kwargs.pop('client_name', None)

    if client_name:
        CACHE_OPTIONS = kwargs.get('CACHE_OPTIONS', {})
        CACHE_OPTIONS['preferred_memcache'] = client_name
        kwargs['CACHE_OPTIONS'] = CACHE_OPTIONS

    config = get_cache_config(*args, **kwargs)
    cache = Cache(namespace=namespace, **config)
    return cache


def check_cache_type(cache, cache_type):
    nt.assert_equal(cache.config['CACHE_TYPE'], cache_type)


def check_cache_instance(cache, cache_instance):
    nt.assert_is_instance(cache.cache, cache_instance)


def check_client_name(cache, expected):
    nt.assert_equal(cache.cache.client_name, expected)


def check_too_big(cache, times, error=None):
    if cache.cache.TooBig:
        with nt.assert_raises(error or cache.cache.TooBig):
            cache.set('big', 'a' * times)

        nt.assert_is_none(cache.get('big'))
    else:
        cache.set('big', 'a' * times)
        nt.assert_equal(cache.get('big'), 'a' * times)


def check_set_delete(cache, key, value, multiplier=None):
    if multiplier:
        value *= multiplier

    cache.set(key, value)
    nt.assert_equal(cache.get(key), value)

    cache.delete(key)
    nt.assert_is_none(cache.get(key))


class TestCache(object):
    def setup(self):
        self.cache = setup_func('simple')

    def teardown(self):
        self.cache.clear()

    def test_dict_config(self):
        check_cache_type(self.cache, 'simple')
        check_cache_instance(self.cache, SimpleCache)

    def test_000_set(self):
        self.cache.set('hi', 'hello')
        nt.assert_equal(self.cache.get('hi'), 'hello')

    def test_add(self):
        self.cache.add('hi', 'hello')
        nt.assert_equal(self.cache.get('hi'), 'hello')

        self.cache.add('hi', 'foobar')
        nt.assert_equal(self.cache.get('hi'), 'hello')

    def test_delete(self):
        check_set_delete(self.cache, 'hi', 'hello')

    def test_memoize(self):
        @self.cache.memoize(5)
        def func(a, b):
            return a + b + random.randrange(0, 100000)

        result = func(5, 2)
        time.sleep(1)
        nt.assert_equal(func(5, 2), result)

        result2 = func(5, 3)
        nt.assert_not_equal(result2, result)

        time.sleep(6)<commit_msg>Add unicode tests<commit_after># -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab
"""
    tests.test_cache
    ~~~~~~~~~~~~~~~~

    Provides unit tests.
"""
from __future__ import (
    absolute_import, division, print_function, unicode_literals)

import time
import random

import nose.tools as nt

from mezmorize import Cache, function_namespace
from mezmorize.utils import HAS_MEMCACHE, HAS_REDIS, get_cache_config

from mezmorize.backends import (
    SimpleCache, FileSystemCache, RedisCache, MemcachedCache,
    SASLMemcachedCache, SpreadSASLMemcachedCache, AVAIL_MEMCACHES)

BIGINT = 2 ** 21
BIGGERINT = 2 ** 28


def setup_func(*args, **kwargs):
    namespace = kwargs.pop('namespace', None)
    client_name = kwargs.pop('client_name', None)

    if client_name:
        CACHE_OPTIONS = kwargs.get('CACHE_OPTIONS', {})
        CACHE_OPTIONS['preferred_memcache'] = client_name
        kwargs['CACHE_OPTIONS'] = CACHE_OPTIONS

    config = get_cache_config(*args, **kwargs)
    cache = Cache(namespace=namespace, **config)
    return cache


def check_cache_type(cache, cache_type):
    nt.assert_equal(cache.config['CACHE_TYPE'], cache_type)


def check_cache_instance(cache, cache_instance):
    nt.assert_is_instance(cache.cache, cache_instance)


def check_client_name(cache, expected):
    nt.assert_equal(cache.cache.client_name, expected)


def check_too_big(cache, times, error=None):
    if cache.cache.TooBig:
        with nt.assert_raises(error or cache.cache.TooBig):
            cache.set('big', 'a' * times)

        nt.assert_is_none(cache.get('big'))
    else:
        cache.set('big', 'a' * times)
        nt.assert_equal(cache.get('big'), 'a' * times)


def check_set_delete(cache, key, value, multiplier=None):
    if multiplier:
        value *= multiplier

    cache.set(key, value)
    nt.assert_equal(cache.get(key), value)

    cache.delete(key)
    nt.assert_is_none(cache.get(key))


class TestCache(object):
    def setup(self):
        self.cache = setup_func('simple')

    def teardown(self):
        self.cache.clear()

    def test_dict_config(self):
        check_cache_type(self.cache, 'simple')
        check_cache_instance(self.cache, SimpleCache)

    def test_000_set(self):
        self.cache.set('hi', 'hello')
        nt.assert_equal(self.cache.get('hi'), 'hello')

    def test_add(self):
        self.cache.add('hi', 'hello')
        nt.assert_equal(self.cache.get('hi'), 'hello')

        self.cache.add('hi', 'foobar')
        nt.assert_equal(self.cache.get('hi'), 'hello')

    def test_add_unicode(self):
        self.cache.add('ȟį', 'ƕɛĺłö')
        nt.assert_equal(self.cache.get('ȟį'), 'ƕɛĺłö')

        self.cache.add('ȟį', 'fööƀåř')
        nt.assert_equal(self.cache.get('ȟį'), 'ƕɛĺłö')

    def test_delete(self):
        check_set_delete(self.cache, 'hi', 'hello')

    def test_delete_unicode(self):
        check_set_delete(self.cache, 'ȟį', 'ƕɛĺłö')

    def test_memoize(self):
        @self.cache.memoize(5)
        def func(a, b):
            return a + b + random.randrange(0, 100000)

        result = func(5, 2)
        time.sleep(1)
        nt.assert_equal(func(5, 2), result)

        result2 = func(5, 3)
        nt.assert_not_equal(result2, result)

        time.sleep(6)
{'fraction_non_alphanumeric': 0.13670927034460442, 'fraction_numerical': 0.00970716712506067, 'mean_word_length': 3.3473980309423346, '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': '2806456', 'n_tokens_mistral': 2218, 'n_tokens_neox': 2094, 'n_words': 390}