<commit_before># -*- coding: utf-8 -*-
import calendar
import datetime
import time
import unittest

from dogpile.cache import make_region
from dogpile.cache.api import NoValue

from zombase.cache import cached_property, is_valid


class TestDatabase(unittest.TestCase):

    class FakeInvalidObject(object):
        id = 2

        def __init__(self, cache):
            self.cache = cache

        @cached_property('fake:{instance.id}:dumdzae', cache='cache')
        def dummy_default(self):
            return 14

    class FakeObject(object):
        id = 1
        _keystores_kt = 'default_ksk_tpl'

        def __init__(self, cache):
            self.cache = cache

        @cached_property(
            'fake:{instance.id}:dum', keystores_kt='bli', cache='cache')
        def dummy(self):
            return 12

        @cached_property(
            'fake:{instance.id}:duml',
            keystores_kt=('bli:{instance.id}', 'blo:{instance.id}'),
            cache='cache')
        def dummy_list(self):
            return 13

        @cached_property('fake:{instance.id}:dumd', cache='cache')
        def dummy_default(self):
            return 14

    def setUp(self):
        self.cache = make_region().configure('dogpile.cache.memory')
        self.object = self.FakeObject(self.cache)

    def tearDown(self):
        del self.object
        del self.cache

    def test_simple_comput(self):
        # Result is correct
        self.assertEqual(self.object.dummy, 12)

        # Result is stored in cache
        self.assertEqual(self.cache.get('fake:1:dum'), 12)

        # Key is added to store
        self.assertEqual(self.cache.get('bli'), ['fake:1:dum'])

    def test_list_store(self):
        self.object.dummy_list

        # Key is added to store
        self.assertEqual(self.cache.get('bli:1'), ['fake:1:duml'])
        self.assertEqual(self.cache.get('blo:1'), ['fake:1:duml'])

    def test_default_key(self):
        self.object.dummy_default
        self.assertEqual(self.cache.get('default_ksk_tpl'), ['fake:1:dumd'])

    def test_absent_keystore_kt(self):
        invalid_object = self.FakeInvalidObject(self.cache)
        with self.assertRaises(ValueError):
            self.assertEqual(invalid_object.dummy_default)

    def test_invalid_keystore_kt(self):
        invalid_object = self.FakeInvalidObject(self.cache)
        setattr(invalid_object, '_keystores_kt', 2)
        with self.assertRaises(ValueError):
            self.assertEqual(invalid_object.dummy_default)


class TestValidity(unittest.TestCase):

    def test_none_validity(self):
        self.assertTrue(is_valid(None, None))

    def test_NoValue_timestamp(self):
        self.assertFalse(is_valid(NoValue(), 'same_day'))

    def test_same_day(self):
        self.assertTrue(is_valid(time.time(), 'same_day'))

        today = datetime.date.today()
        self.assertTrue(is_valid(
            calendar.timegm(today.timetuple()),
            'same_day',<commit_msg>Add test<commit_after># -*- coding: utf-8 -*-
import calendar
import datetime
import time
import unittest

from dogpile.cache import make_region
from dogpile.cache.api import NoValue

from zombase.cache import cached_property, is_valid


class TestDatabase(unittest.TestCase):

    class FakeInvalidObject(object):
        id = 2

        def __init__(self, cache):
            self.cache = cache

        @cached_property('fake:{instance.id}:dumdzae', cache='cache')
        def dummy_default(self):
            return 14

    class FakeObject(object):
        id = 1
        _dummy_run = 0
        _keystores_kt = 'default_ksk_tpl'

        def __init__(self, cache):
            self.cache = cache

        @cached_property(
            'fake:{instance.id}:dum', keystores_kt='bli', cache='cache')
        def dummy(self):
            # Mesure how many times the computation has run
            self._dummy_run += 1

            return 12

        @cached_property(
            'fake:{instance.id}:duml',
            keystores_kt=('bli:{instance.id}', 'blo:{instance.id}'),
            cache='cache')
        def dummy_list(self):
            return 13

        @cached_property('fake:{instance.id}:dumd', cache='cache')
        def dummy_default(self):
            return 14

    def setUp(self):
        self.cache = make_region().configure('dogpile.cache.memory')
        self.object = self.FakeObject(self.cache)

    def tearDown(self):
        del self.object
        del self.cache

    def test_simple_comput(self):
        # Result is correct
        self.assertEqual(self.object.dummy, 12)

        # Result is stored in cache
        self.assertEqual(self.cache.get('fake:1:dum'), 12)

        # Key is added to store
        self.assertEqual(self.cache.get('bli'), ['fake:1:dum'])

    def test_list_store(self):
        self.object.dummy_list

        # Key is added to store
        self.assertEqual(self.cache.get('bli:1'), ['fake:1:duml'])
        self.assertEqual(self.cache.get('blo:1'), ['fake:1:duml'])

    def test_default_key(self):
        self.object.dummy_default
        self.assertEqual(self.cache.get('default_ksk_tpl'), ['fake:1:dumd'])

    def test_absent_keystore_kt(self):
        invalid_object = self.FakeInvalidObject(self.cache)
        with self.assertRaises(ValueError):
            self.assertEqual(invalid_object.dummy_default)

    def test_invalid_keystore_kt(self):
        invalid_object = self.FakeInvalidObject(self.cache)
        setattr(invalid_object, '_keystores_kt', 2)
        with self.assertRaises(ValueError):
            self.assertEqual(invalid_object.dummy_default)

    def test_single_run(self):
        self.assertEqual(self.object._dummy_run, 0)

        self.object.dummy
        self.assertEqual(self.object._dummy_run, 1)

        self.object.dummy
        self.assertEqual(self.object._dummy_run, 1)

class TestValidity(unittest.TestCase):

    def test_none_validity(self):
        self.assertTrue(is_valid(None, None))

    def test_NoValue_timestamp(self):
        self.assertFalse(is_valid(NoValue(), 'same_day'))

    def test_same_day(self):
        self.assertTrue(is_valid(time.time(), 'same_day'))

        today = datetime.date.today()
        self.assertTrue(is_valid(
            calendar.timegm(today.timetuple()),
            'same_day',
{'fraction_non_alphanumeric': 0.11762835797170561, 'fraction_numerical': 0.008106819265617548, 'mean_word_length': 2.653890824622532, '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': '5069178', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1881, 'n_words': 344}