예제 #1
0
 def test_fill_twice(self):
     cache = MongoCache(**self.OPTIONS)
     self._insert_foo_bar_doc(cache)
     self._insert_foo_bar_doc(cache, 'another_content')
     uri, content = cache.get('foo', 'bar')
     self.assertEqual(uri, 'bar')
     self.assertEqual(content, 'another_content')
예제 #2
0
 def test_fill_after_invalid_status(self):
     cache = MongoCache(**self.OPTIONS)
     cache._lock('foo', 'bar', -42)
     with self.assertRaises(Exception) as exc:
         cache.fill('foo', 'bar', 'content')
     self.assertEqual(
         exc.exception.message,
         'Could not set document content: foo/bar'
     )
예제 #3
0
 def test_fill_existing_resource(self):
     cache = MongoCache(**self.OPTIONS)
     self.collection.insert({
         'operation': 'foo',
         'hash': hashlib.sha256('bar'.decode('utf8')).hexdigest(),
         'status': MongoCache.CACHE_STATUS,
         'uri': 'bar',
     })
     with self.assertRaises(ResourceAlreadyExists) as exc:
         cache.fill('foo', 'bar', 'some_content', [])
     self.assertEqual(exc.exception.operation, 'foo')
     self.assertEqual(exc.exception.uri, 'bar')
예제 #4
0
    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')
예제 #5
0
 def test_fill_with_multiple_match(self):
     """Test fill method when there are 2 matching documents"""
     # indices need to be deactivated...
     cache = MongoCache(**self.OPTIONS)
     cache._collection.drop_indexes()
     uri = 'bar'
     for _ in range(2):
         cache._collection.insert({
             'hash': hashlib.sha256(uri.decode('utf8')).hexdigest(),
             'operation': 'foo',
             'status': MongoCache.CACHE_STATUS
         })
     with self.assertRaises(Exception) as exc:
         cache.fill('foo', 'bar', 'content')
     self.assertEqual(
         exc.exception.message,
         'Unexpected matched results while filling document: ' +
         'foo/bar, matched: 2'
     )
예제 #6
0
 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)
예제 #7
0
 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)
예제 #8
0
 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')
예제 #9
0
 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')
예제 #10
0
 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')