Ejemplo n.º 1
0
    def test_touch_updates_last_accessed(self, datetime):
        datetime.utcnow.return_value = self.now

        e = CacheEntry('foo', created_at=self.minute_before,
            last_accessed_at=self.minute_before)

        e.touch()
        self.assertEqual(e.last_accessed_at, self.now)
Ejemplo n.º 2
0
    def test_touch_updates_last_accessed(self, datetime):
        datetime.utcnow.return_value = self.now

        e = CacheEntry('foo', created_at=self.minute_before,
            last_accessed_at=self.minute_before)

        e.touch()
        self.assertEqual(e.last_accessed_at, self.now)
Ejemplo n.º 3
0
    def test_cache_put(self, CacheEntry, CacheStats):
        store = MagicMock()

        c = Cache(store=MagicMock(return_value=store))
        c.put('foo', 'Value Of Foo', new=True)

        CacheEntry.assert_called_once_with('Value Of Foo')

        store.__setitem__.assert_called_once_with('foo',
            CacheEntry.return_value)
        CacheStats.return_value.put.assert_called_once_with(new=True)
Ejemplo n.º 4
0
    def test_to_dict(self):
        e = CacheEntry('foo', created_at=self.hour_before,
            last_accessed_at=self.minute_before)

        data = e.to_dict()

        self.assertEqual(data['value'], 'foo')
        self.assertEqual(data['created_at'],
            '2013-01-01 09:30:00.000000')
        self.assertEqual(data['last_accessed_at'],
            '2013-01-01 10:29:00.000000')
Ejemplo n.º 5
0
    def test_cache_put(self, CacheEntry, CacheStats):
        store = MagicMock()

        c = Cache(store=MagicMock(return_value=store))
        c.put('foo', 'Value Of Foo', new=True)

        CacheEntry.assert_called_once_with('Value Of Foo')

        store.__setitem__.assert_called_once_with('foo',
            CacheEntry.return_value)
        CacheStats.return_value.put.assert_called_once_with(new=True)
Ejemplo n.º 6
0
    def test_to_dict(self):
        e = CacheEntry('foo', created_at=self.hour_before,
            last_accessed_at=self.minute_before)

        data = e.to_dict()

        self.assertEqual(data['value'], 'foo')
        self.assertEqual(data['created_at'],
            '2013-01-01 09:30:00.000000')
        self.assertEqual(data['last_accessed_at'],
            '2013-01-01 10:29:00.000000')
Ejemplo n.º 7
0
    def test_is_expired_checks_ttl_tti(self, datetime):
        datetime.utcnow.return_value = self.now

        e = CacheEntry('foo', created_at=self.hour_before,
            last_accessed_at=self.minute_before)

        # check ttl
        self.assertTrue(e.is_expired(3600, 24 * 3600))
        self.assertFalse(e.is_expired(3601, 24 * 3600))
        # check tti
        self.assertTrue(e.is_expired(24 * 3600, 60))
        self.assertFalse(e.is_expired(24 * 3600, 61))
Ejemplo n.º 8
0
    def test_parse_invalid_dates(self, datetime):
        datetime.strptime.side_effect = ValueError

        e = CacheEntry.parse({'value': 'foo'})

        self.assertEqual(e.value, 'foo')
        self.assertEqual(e.created_at, datetime.utcnow.return_value)
        self.assertEqual(e.last_accessed_at, datetime.utcnow.return_value)
Ejemplo n.º 9
0
    def test_parse_invalid_dates(self, datetime):
        datetime.strptime.side_effect = ValueError

        e = CacheEntry.parse({'value': 'foo'})

        self.assertEqual(e.value, 'foo')
        self.assertEqual(e.created_at, datetime.utcnow.return_value)
        self.assertEqual(e.last_accessed_at, datetime.utcnow.return_value)
Ejemplo n.º 10
0
    def test_parse(self):
        e = CacheEntry.parse({
            'value': 'foo',
            'created_at': '2013-01-01 09:30:00.0',
            'last_accessed_at': '2013-01-01 10:29:00.0'
        })

        self.assertEqual(e.value, 'foo')
        self.assertEqual(e.created_at, self.hour_before)
        self.assertEqual(e.last_accessed_at, self.minute_before)
Ejemplo n.º 11
0
    def test_everything(self):
        with patch.dict('sys.modules', {'pymemcache': object(), 'pymemcache.client': MagicMock(Client=self.Memcache)}):
            s = MemcachedStore()

        self.assertEqual(len(s), 0)
        self.assertIsNone(s['foo'])

        s['foo'] = CacheEntry('Value Of Foo')
        self.assertEqual(len(s), 1)
        self.assertEqual(s['foo'].value, 'Value Of Foo')

        del s['foo']
        self.assertEqual(len(s), 0)

        del s['nonexistent']  # shouldn't raise anything

        s['bar'] = CacheEntry(1)
        s.clear()
        self.assertEqual(len(s), 0)
Ejemplo n.º 12
0
    def test_parse(self):
        e = CacheEntry.parse({
            'value': 'foo',
            'created_at': '2013-01-01 09:30:00.0',
            'last_accessed_at': '2013-01-01 10:29:00.0'
        })

        self.assertEqual(e.value, 'foo')
        self.assertEqual(e.created_at, self.hour_before)
        self.assertEqual(e.last_accessed_at, self.minute_before)
Ejemplo n.º 13
0
    def test_cache_entry_to_dict_parse(self):
        _, acc = self.create_account(self.app.accounts)
        acc.update({'surname': 'Surname'})

        acc_entry_before = acc._store._get_cache(acc.href).store[acc.href]
        acc_entry_after = CacheEntry.parse(acc_entry_before.to_dict())
        self.assertEqual(acc_entry_before.value, acc_entry_after.value)
        self.assertEqual(acc_entry_before.created_at,
                         acc_entry_after.created_at)
        self.assertEqual(acc_entry_before.last_accessed_at,
                         acc_entry_after.last_accessed_at)
Ejemplo n.º 14
0
    def test_cache_entry_to_dict_parse(self):
        _, acc = self.create_account(self.app.accounts)
        acc.update({'surname': 'Surname'})

        acc_entry_before = acc._store._get_cache(acc.href).store[acc.href]
        acc_entry_after = CacheEntry.parse(acc_entry_before.to_dict())
        self.assertEqual(acc_entry_before.value, acc_entry_after.value)
        self.assertEqual(
            acc_entry_before.created_at, acc_entry_after.created_at)
        self.assertEqual(
            acc_entry_before.last_accessed_at,
            acc_entry_after.last_accessed_at)
Ejemplo n.º 15
0
    def test_everything(self):
        # make sure our mocked redis is available
        with patch.dict('sys.modules', {'redis': MagicMock(Redis=self.Redis)}):
            s = RedisStore()

        self.assertEqual(len(s), 0)

        self.assertIsNone(s['foo'])

        s['foo'] = CacheEntry('Value Of Foo')
        self.assertEqual(len(s), 1)
        self.assertEqual(s['foo'].value, 'Value Of Foo')

        del s['foo']
        self.assertEqual(len(s), 0)

        del s['nonexistent']  # shouldn't raise anything

        s['bar'] = CacheEntry(1)
        s.clear()
        self.assertEqual(len(s), 0)
Ejemplo n.º 16
0
    def test_is_expired_checks_ttl_tti(self, datetime):
        datetime.utcnow.return_value = self.now

        e = CacheEntry('foo', created_at=self.hour_before,
            last_accessed_at=self.minute_before)

        # check ttl
        self.assertTrue(e.is_expired(3600, 24 * 3600))
        self.assertFalse(e.is_expired(3601, 24 * 3600))
        # check tti
        self.assertTrue(e.is_expired(24 * 3600, 60))
        self.assertFalse(e.is_expired(24 * 3600, 61))
Ejemplo n.º 17
0
    def test_entry_init_with_default_values(self, datetime):
        e = CacheEntry('foo')

        self.assertEqual(e.created_at, datetime.utcnow.return_value)
        self.assertEqual(e.last_accessed_at, datetime.utcnow.return_value)
Ejemplo n.º 18
0
 def test_entry_init_with_custom_values(self, datetime):
     e = CacheEntry('foo', created_at=self.hour_before,
         last_accessed_at=self.minute_before)
     self.assertEqual(e.created_at, self.hour_before)
     self.assertEqual(e.last_accessed_at, self.minute_before)