<commit_before>import unittest

from pymongo import MongoClient

from backache.cache import MongoCache
from backache.errors import (
    ResourceAlreadyExists,
    ResourceLocked,
    ResourceNotLocked,
    UnknownResource,
)


class MongoTest(unittest.TestCase):
    OPTIONS = {
        'connection_params': {
            'host': 'localhost',
        },
        'db': 'backache',
        'collection': 'backache',
    }

    @classmethod
    def setUpClass(cls):
        client = MongoClient(**cls.OPTIONS['connection_params'])
        db = client[cls.OPTIONS['db']]
        cls.collection = db[cls.OPTIONS['collection']]

    def setUp(self):
        self.collection.drop()

    def test_init(self):
        MongoCache(**self.OPTIONS)
        indices = list(self.collection.index_information())
        self.assertEqual(len(indices), 3)
        self.assertIn('_backache_uri_op', indices)
        self.assertIn('_backache_redirects', indices)

    def test_lock(self):
        cache = MongoCache(**self.OPTIONS)
        cache.lock('foo', 'bar')
        try:
            with self.assertRaises(ResourceLocked) as exc:
                cache.lock('foo', 'bar')
            self.assertEqual(exc.exception.operation, 'foo')
            self.assertEqual(exc.exception.uri, 'bar')
        finally:
            cache.release('foo', 'bar')

    def test_bad_unlock(self):
        cache = MongoCache(**self.OPTIONS)
        with self.assertRaises(ResourceNotLocked) as exc:
            cache.release('foo', 'bar')
        self.assertEqual(exc.exception.operation, 'foo')
        self.assertEqual(exc.exception.uri, 'bar')

    def test_delete_unknown_lock(self):
        cache = MongoCache(**self.OPTIONS)
        with self.assertRaises(ResourceNotLocked) as exc:
            cache.delete_lock('foo', 'bar')
        self.assertEqual(exc.exception.operation, 'foo')
        self.assertEqual(exc.exception.uri, 'bar')

    def test_fill_unknown_resource(self):
        cache = MongoCache(**self.OPTIONS)
        with self.assertRaises(UnknownResource) as exc:
            cache.fill('foo', 'bar', 'some_content', [])
        self.assertEqual(exc.exception.operation, 'foo')
        self.assertEqual(exc.exception.uri, 'bar')

    def test_fill_existing_resource(self):
        cache = MongoCache(**self.OPTIONS)
        self.collection.insert({
            'operation': 'foo',
            'uri': 'bar',
            'status': MongoCache.CACHE_STATUS,
        })
        with self.assertRaises(ResourceAlreadyExists) as exc:
            cache.fill('foo', 'bar', 'some_content', [])
        self.assertEqual(exc.exception.operation, 'foo')
        self.assertEqual(exc.exception.uri, 'bar')

    def test_fill_redirects(self):
        cache = MongoCache(**self.OPTIONS)
        try:
            cache.lock('foo', 'bar')
            cache.fill('foo', 'bar', 'content', ['kikoo', 'lol'])
        finally:
            cache.release('foo', 'bar')
        self.assertEqual(cache.get('foo', 'lol')[1], 'content')

        # Now let's add a redirect
        try:
            cache.lock('foo', 'bar')
            cache.fill('foo', 'bar', None, ['bonjour'])
        finally:
            cache.release('foo', 'bar')
        self.assertEqual(cache.get('foo', 'bonjour')[1], 'content')
        self.assertEqual(cache.get('foo', 'lol')[1], 'content')

    def test_indices(self):
        cache = MongoCache(**self.OPTIONS)
        for op in ['op1', 'op2']:
            for key in ['key1', 'key2']:
                try:
                    cache.lock(op, key)
                    cache.fill(op, key, 'content')
                finally:
                    cache.release(op, key)

    def _get_document(self, cache, operation, uri):
        document = cache._collection.find_one({
            'operation': 'foo',
            'uri': 'bar'
        })
        self.assertIsNotNone(document)
        return document

    def test_counters(self):
        cache = MongoCache(**self.OPTIONS)
        try:
            cache.lock('foo', 'bar')
            cache.fill('foo', 'bar', 'content', ['kikoo', 'lol'])
        finally:
            cache.release('foo', 'bar')
        document = self._get_document(cache, 'foo', 'bar')
        self.assertFalse('direct_hits' in document)
        self.assertFalse('redirects_hits' in document)
        self.assertEqual(
            cache.get('foo', 'bar'),
            ('bar', 'content')
        )
        document = self._get_document(cache, 'foo', 'bar')
        self.assertTrue(len(document['direct_hits']), 1)
        self.assertFalse('redirects_hits' in document)
        self.assertEqual(
            cache.get('foo', 'kikoo'),
            ('bar', 'content')
        )
        document = self._get_document(cache, 'foo', 'bar')
        self.assertTrue(len(document['direct_hits']), 1)
        self.assertTrue(len(document['redirect_hits']), 1)


if __name__ == '__main__':
    unittest.main()<commit_msg>Removed code duplication in UT<commit_after>import unittest

from pymongo import MongoClient

from backache.cache import MongoCache
from backache.errors import (
    ResourceAlreadyExists,
    ResourceLocked,
    ResourceNotLocked,
    UnknownResource,
)


class MongoTest(unittest.TestCase):
    OPTIONS = {
        'connection_params': {
            'host': 'localhost',
        },
        'db': 'backache',
        'collection': 'backache',
    }

    @classmethod
    def setUpClass(cls):
        client = MongoClient(**cls.OPTIONS['connection_params'])
        db = client[cls.OPTIONS['db']]
        cls.collection = db[cls.OPTIONS['collection']]

    def setUp(self):
        self.collection.drop()

    def test_init(self):
        MongoCache(**self.OPTIONS)
        indices = list(self.collection.index_information())
        self.assertEqual(len(indices), 3)
        self.assertIn('_backache_uri_op', indices)
        self.assertIn('_backache_redirects', indices)

    def test_lock(self):
        cache = MongoCache(**self.OPTIONS)
        cache.lock('foo', 'bar')
        try:
            with self.assertRaises(ResourceLocked) as exc:
                cache.lock('foo', 'bar')
            self.assertEqual(exc.exception.operation, 'foo')
            self.assertEqual(exc.exception.uri, 'bar')
        finally:
            cache.release('foo', 'bar')

    def test_bad_unlock(self):
        cache = MongoCache(**self.OPTIONS)
        with self.assertRaises(ResourceNotLocked) as exc:
            cache.release('foo', 'bar')
        self.assertEqual(exc.exception.operation, 'foo')
        self.assertEqual(exc.exception.uri, 'bar')

    def test_delete_unknown_lock(self):
        cache = MongoCache(**self.OPTIONS)
        with self.assertRaises(ResourceNotLocked) as exc:
            cache.delete_lock('foo', 'bar')
        self.assertEqual(exc.exception.operation, 'foo')
        self.assertEqual(exc.exception.uri, 'bar')

    def test_fill_unknown_resource(self):
        cache = MongoCache(**self.OPTIONS)
        with self.assertRaises(UnknownResource) as exc:
            cache.fill('foo', 'bar', 'some_content', [])
        self.assertEqual(exc.exception.operation, 'foo')
        self.assertEqual(exc.exception.uri, 'bar')

    def test_fill_existing_resource(self):
        cache = MongoCache(**self.OPTIONS)
        self.collection.insert({
            'operation': 'foo',
            'uri': 'bar',
            'status': MongoCache.CACHE_STATUS,
        })
        with self.assertRaises(ResourceAlreadyExists) as exc:
            cache.fill('foo', 'bar', 'some_content', [])
        self.assertEqual(exc.exception.operation, 'foo')
        self.assertEqual(exc.exception.uri, 'bar')

    def test_fill_redirects(self):
        cache = MongoCache(**self.OPTIONS)
        self._insert_foo_bar_doc(cache)
        self.assertEqual(cache.get('foo', 'lol')[1], 'content')

        # Now let's add a redirect
        try:
            cache.lock('foo', 'bar')
            cache.fill('foo', 'bar', None, ['bonjour'])
        finally:
            cache.release('foo', 'bar')
        self.assertEqual(cache.get('foo', 'bonjour')[1], 'content')
        self.assertEqual(cache.get('foo', 'lol')[1], 'content')

    def test_indices(self):
        cache = MongoCache(**self.OPTIONS)
        for op in ['op1', 'op2']:
            for key in ['key1', 'key2']:
                try:
                    cache.lock(op, key)
                    cache.fill(op, key, 'content')
                finally:
                    cache.release(op, key)

    def _get_document(self, cache, operation, uri):
        document = cache._collection.find_one({
            'operation': 'foo',
            'uri': 'bar'
        })
        self.assertIsNotNone(document)
        return document

    def _insert_foo_bar_doc(self, cache):
        try:
            cache.lock('foo', 'bar')
            cache.fill('foo', 'bar', 'content', ['kikoo', 'lol'])
        finally:
            cache.release('foo', 'bar')

    def test_counters(self):
        cache = MongoCache(**self.OPTIONS)
        self._insert_foo_bar_doc(cache)
        document = self._get_document(cache, 'foo', 'bar')
        self.assertFalse('direct_hits' in document)
        self.assertFalse('redirects_hits' in document)
        self.assertEqual(
            cache.get('foo', 'bar'),
            ('bar', 'content')
        )
        document = self._get_document(cache, 'foo', 'bar')
        self.assertTrue(len(document['direct_hits']), 1)
        self.assertFalse('redirects_hits' in document)
        self.assertEqual(
            cache.get('foo', 'kikoo'),
            ('bar', 'content')
        )
        document = self._get_document(cache, 'foo', 'bar')
        self.assertTrue(len(document['direct_hits']), 1)
        self.assertTrue(len(document['redirect_hits']), 1)


if __name__ == '__main__':
    unittest.main()
{'fraction_non_alphanumeric': 0.1433241393400756, 'fraction_numerical': 0.002247420574113801, 'mean_word_length': 2.3654176693021656, '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': '5132055', 'n_tokens_mistral': 2944, 'n_tokens_neox': 2861, 'n_words': 531}