Beispiel #1
0
class CacheIntegrationTest(TestCase):
    def setUp(self):
        self.cache = mock.Mock()
        self.cache.get.return_value = {}
        self.mydict = ModelDict(ModelDictModel, key='key', value='value', auto_create=True, cache=self.cache)

    def test_switch_creation(self):
        self.mydict['hello'] = 'foo'
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 2)
        self.cache.set.assert_any_call(self.mydict.remote_cache_key, {'hello': 'foo'})
        self.cache.set.assert_any_call(self.mydict.remote_cache_last_updated_key, self.mydict._last_checked_for_remote_changes)

    def test_switch_change(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        self.mydict['hello'] = 'bar'
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 2)
        self.cache.set.assert_any_call(self.mydict.remote_cache_key, {'hello': 'bar'})
        self.cache.set.assert_any_call(self.mydict.remote_cache_last_updated_key, self.mydict._last_checked_for_remote_changes)

    def test_switch_delete(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        del self.mydict['hello']
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 2)
        self.cache.set.assert_any_call(self.mydict.remote_cache_key, {})
        self.cache.set.assert_any_call(self.mydict.remote_cache_last_updated_key, self.mydict._last_checked_for_remote_changes)

    def test_switch_access(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        self.assertEquals(foo, 'foo')
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 0)

    def test_switch_access_without_local_cache(self):
        self.mydict['hello'] = 'foo'
        self.mydict._local_cache = None
        self.mydict._last_checked_for_remote_changes = None
        self.cache.reset_mock()
        foo = self.mydict['hello']
        self.assertEquals(foo, 'foo')
        # "1" here signifies that we didn't ask the remote cache for its last
        # updated value
        self.assertEquals(self.cache.get.call_count, 1)
        self.assertEquals(self.cache.set.call_count, 0)
        self.cache.get.assert_any_call(self.mydict.remote_cache_key)
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 0)

    def test_switch_access_with_expired_local_cache(self):
        self.mydict['hello'] = 'foo'
        self.mydict._last_checked_for_remote_changes = None
        self.cache.reset_mock()
        foo = self.mydict['hello']
        self.assertEquals(foo, 'foo')
        self.assertEquals(self.cache.get.call_count, 2)
        self.assertEquals(self.cache.set.call_count, 0)
        self.cache.get.assert_any_call(self.mydict.remote_cache_last_updated_key)
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 0)

    def test_does_not_pull_down_all_data(self):
        self.mydict['hello'] = 'foo'
        self.cache.get.return_value = self.mydict._local_last_updated - 100
        self.cache.reset_mock()

        self.mydict._cleanup()

        self.assertEquals(self.mydict['hello'], 'foo')
        self.cache.get.assert_called_once_with(
            self.mydict.remote_cache_last_updated_key
        )
Beispiel #2
0
class CacheIntegrationTest(TestCase):
    def setUp(self):
        self.cache = mock.Mock()
        self.cache.get.return_value = {}
        self.mydict = ModelDict(ModelDictModel,
                                key='key',
                                value='value',
                                auto_create=True,
                                cache=self.cache)

    def _get_other_version_key(self, mydict=None):
        if mydict is None:
            mydict = self.mydict
        expected_other_version_no = '2' if sys.version_info[0] == 3 else '3'
        return mydict.remote_cache_key[:-1] + expected_other_version_no

    def test_switch_creation(self):
        self.mydict['hello'] = 'foo'
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 1
        self.cache.set_many.assert_any_call({
            self.mydict.remote_cache_key: {
                u'hello': u'foo'
            },
            self.mydict.remote_cache_last_updated_key:
            self.mydict._last_checked_for_remote_changes,
        })

    def test_other_version_cache_deleted_on_switch_creation(self):
        expected_other_version_cache_key = self._get_other_version_key()

        self.mydict['hello'] = 'foo'

        self.cache.delete.assert_called_once_with(
            expected_other_version_cache_key)

    def test_other_version_cache_deleted_on_switch_change(self):
        expected_other_version_cache_key = self._get_other_version_key()

        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        self.mydict['hello'] = 'bar'

        self.cache.delete.assert_called_once_with(
            expected_other_version_cache_key)

    @mock.patch('modeldict.base.CachedDict.get_cache_data')
    def test_cache_is_refreshed_if_key_is_missing(self, mock_get_cache_data):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()

        self.cache.get.return_value = None
        self.mydict._last_checked_for_remote_changes = 0.0
        self.mydict['hello']

        self.cache.set.assert_called_once_with(
            self.mydict.remote_cache_key, mock_get_cache_data.return_value)

    def test_switch_creation_with_custom_remote_timeout(self):
        cache = mock.Mock()
        mydict = ModelDict(ModelDictModel,
                           key='key',
                           value='value',
                           auto_create=True,
                           cache=cache,
                           remote_timeout=None)
        mydict['hello'] = 'foo'
        assert cache.get.call_count == 0
        assert cache.set_many.call_count == 1
        cache.set_many.assert_any_call(
            {
                mydict.remote_cache_key: {
                    u'hello': u'foo'
                },
                mydict.remote_cache_last_updated_key:
                mydict._last_checked_for_remote_changes,
            },
            timeout=None)

    def test_switch_change(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        self.mydict['hello'] = 'bar'
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 1
        self.cache.set_many.assert_any_call({
            self.mydict.remote_cache_key: {
                u'hello': u'bar'
            },
            self.mydict.remote_cache_last_updated_key:
            self.mydict._last_checked_for_remote_changes
        })

    def test_switch_delete(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        del self.mydict['hello']
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 1
        self.cache.set_many.assert_any_call({
            self.mydict.remote_cache_key: {},
            self.mydict.remote_cache_last_updated_key:
            self.mydict._last_checked_for_remote_changes
        })

    def test_switch_access(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        assert foo == 'foo'
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 0

    def test_switch_access_without_local_cache(self):
        self.mydict['hello'] = 'foo'
        self.mydict._local_cache = {}
        self.mydict._local_last_updated = None
        self.mydict._last_checked_for_remote_changes = 0.0
        self.cache.reset_mock()
        foo = self.mydict['hello']
        assert foo == 'foo'
        # "1" here signifies that we didn't ask the remote cache for its last
        # updated value
        assert self.cache.get.call_count == 1
        assert self.cache.set_many.call_count == 0
        self.cache.get.assert_any_call(self.mydict.remote_cache_key)
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 0

    def test_switch_access_with_expired_local_cache(self):
        self.mydict['hello'] = 'foo'
        self.mydict._last_checked_for_remote_changes = 0.0
        self.cache.reset_mock()
        foo = self.mydict['hello']
        assert foo == 'foo'
        assert self.cache.get.call_count == 2
        assert self.cache.set_many.call_count == 0
        self.cache.get.assert_any_call(
            self.mydict.remote_cache_last_updated_key)
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 0

    def test_does_not_pull_down_all_data(self):
        self.mydict['hello'] = 'foo'
        self.cache.get.return_value = self.mydict._local_last_updated - 100
        self.cache.reset_mock()

        self.mydict._cleanup()

        assert self.mydict['hello'] == 'foo'
        self.cache.get.assert_called_once_with(
            self.mydict.remote_cache_last_updated_key)
Beispiel #3
0
class CacheIntegrationTest(TestCase):
    def setUp(self):
        self.cache = mock.Mock()
        self.cache.get.return_value = {}
        self.mydict = ModelDict(ModelDictModel, key='key', value='value', auto_create=True, cache=self.cache)

    def test_switch_creation(self):
        self.mydict['hello'] = 'foo'
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 1
        self.cache.set_many.assert_any_call({
            self.mydict.remote_cache_key: {u'hello': u'foo'},
            self.mydict.remote_cache_last_updated_key: self.mydict._last_checked_for_remote_changes,
        })

    def test_switch_change(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        self.mydict['hello'] = 'bar'
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 1
        self.cache.set_many.assert_any_call({
            self.mydict.remote_cache_key: {u'hello': u'bar'},
            self.mydict.remote_cache_last_updated_key: self.mydict._last_checked_for_remote_changes
        })

    def test_switch_delete(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        del self.mydict['hello']
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 1
        self.cache.set_many.assert_any_call({
            self.mydict.remote_cache_key: {},
            self.mydict.remote_cache_last_updated_key: self.mydict._last_checked_for_remote_changes
        })

    def test_switch_access(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        assert foo == 'foo'
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 0

    def test_switch_access_without_local_cache(self):
        self.mydict['hello'] = 'foo'
        self.mydict._local_cache = {}
        self.mydict._local_last_updated = None
        self.mydict._last_checked_for_remote_changes = 0.0
        self.cache.reset_mock()
        foo = self.mydict['hello']
        assert foo == 'foo'
        # "1" here signifies that we didn't ask the remote cache for its last
        # updated value
        assert self.cache.get.call_count == 1
        assert self.cache.set_many.call_count == 0
        self.cache.get.assert_any_call(self.mydict.remote_cache_key)
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 0

    def test_switch_access_with_expired_local_cache(self):
        self.mydict['hello'] = 'foo'
        self.mydict._last_checked_for_remote_changes = 0.0
        self.cache.reset_mock()
        foo = self.mydict['hello']
        assert foo == 'foo'
        assert self.cache.get.call_count == 2
        assert self.cache.set_many.call_count == 0
        self.cache.get.assert_any_call(self.mydict.remote_cache_last_updated_key)
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        assert self.cache.get.call_count == 0
        assert self.cache.set_many.call_count == 0

    def test_does_not_pull_down_all_data(self):
        self.mydict['hello'] = 'foo'
        self.cache.get.return_value = self.mydict._local_last_updated - 100
        self.cache.reset_mock()

        self.mydict._cleanup()

        assert self.mydict['hello'] == 'foo'
        self.cache.get.assert_called_once_with(
            self.mydict.remote_cache_last_updated_key
        )
Beispiel #4
0
class CacheIntegrationTest(TestCase):
    def setUp(self):
        self.cache = mock.Mock()
        self.cache.get.return_value = {}
        self.mydict = ModelDict(ModelDictModel,
                                key='key',
                                value='value',
                                auto_create=True,
                                cache=self.cache)

    def test_switch_creation(self):
        self.mydict['hello'] = 'foo'
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 2)
        self.cache.set.assert_any_call(self.mydict.remote_cache_key,
                                       {u'hello': u'foo'})
        self.cache.set.assert_any_call(
            self.mydict.remote_cache_last_updated_key,
            self.mydict._last_checked_for_remote_changes)

    def test_switch_change(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        self.mydict['hello'] = 'bar'
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 2)
        self.cache.set.assert_any_call(self.mydict.remote_cache_key,
                                       {u'hello': u'bar'})
        self.cache.set.assert_any_call(
            self.mydict.remote_cache_last_updated_key,
            self.mydict._last_checked_for_remote_changes)

    def test_switch_delete(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        del self.mydict['hello']
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 2)
        self.cache.set.assert_any_call(self.mydict.remote_cache_key, {})
        self.cache.set.assert_any_call(
            self.mydict.remote_cache_last_updated_key,
            self.mydict._last_checked_for_remote_changes)

    def test_switch_access(self):
        self.mydict['hello'] = 'foo'
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        self.assertEquals(foo, 'foo')
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 0)

    def test_switch_access_without_local_cache(self):
        self.mydict['hello'] = 'foo'
        self.mydict._local_cache = None
        self.mydict._last_checked_for_remote_changes = None
        self.cache.reset_mock()
        foo = self.mydict['hello']
        self.assertEquals(foo, 'foo')
        # "1" here signifies that we didn't ask the remote cache for its last
        # updated value
        self.assertEquals(self.cache.get.call_count, 1)
        self.assertEquals(self.cache.set.call_count, 0)
        self.cache.get.assert_any_call(self.mydict.remote_cache_key)
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 0)

    def test_switch_access_with_expired_local_cache(self):
        self.mydict['hello'] = 'foo'
        self.mydict._last_checked_for_remote_changes = None
        self.cache.reset_mock()
        foo = self.mydict['hello']
        self.assertEquals(foo, 'foo')
        self.assertEquals(self.cache.get.call_count, 2)
        self.assertEquals(self.cache.set.call_count, 0)
        self.cache.get.assert_any_call(
            self.mydict.remote_cache_last_updated_key)
        self.cache.reset_mock()
        foo = self.mydict['hello']
        foo = self.mydict['hello']
        self.assertEquals(self.cache.get.call_count, 0)
        self.assertEquals(self.cache.set.call_count, 0)

    def test_does_not_pull_down_all_data(self):
        self.mydict['hello'] = 'foo'
        self.cache.get.return_value = self.mydict._local_last_updated - 100
        self.cache.reset_mock()

        self.mydict._cleanup()

        self.assertEquals(self.mydict['hello'], 'foo')
        self.cache.get.assert_called_once_with(
            self.mydict.remote_cache_last_updated_key)