def test_get_or_create_model_key(self, mock_sql, mock_model_cache):
     """
     get_or_create_model_key returns existing key when one exists
     """
     mock_model_cache.retrieve_model_cache_info.return_value = ModelCacheInfo(
         table_name=u'tests_manufacturer', table_key='unique_id')
     key, created = self.mixin.get_or_create_model_key()
     self.assertFalse(created)
     self.assertEquals(key, 'unique_id')
 def test_key_components(self, mock_sql, mock_model_cache):
     """
     Ensure key created by mixin is a hash of model_key, sql query and database name
     """
     mock_sql.return_value = 'sql'
     mock_model_cache.retrieve_model_cache_info.return_value = ModelCacheInfo(
         table_name=u'tests_manufacturer', table_key='unique_id')
     expected_key_value = hashlib.md5(u'unique_idsqldb').hexdigest()
     self.assertEquals(expected_key_value, self.mixin.generate_key())
 def test_invalidate_model_cache(self, mock_uuid, mock_model_cache):
     """
     Mixin broadcasts new model cache info when a model is invalidated
     """
     mock_uuid4 = Mock(hex='unique_id')
     mock_uuid.uuid4.return_value = mock_uuid4
     self.mixin.invalidate_model_cache()
     mock_model_cache.share_model_cache_info.assert_called_once_with(
         ModelCacheInfo(table_name=u'tests_manufacturer',
                        table_key='unique_id'))
    def test_share_model_cache_info_with_model_update(self):
        """
        Cached model should be updated when calling 'share_model_cache_info'
        """
        # Add a new model info with the same table name
        cache_model_info = ModelCacheInfo(self.cache_model_info.table_name,
                                          'key2')
        self.shared_memory.share_model_cache_info(cache_model_info)

        cached_model = self.shared_memory._cache_backend.get('table1', None)
        self.assertEqual(cached_model, cache_model_info)
 def test_new_key_generation(self, mock_uuid, mock_sql, mock_model_cache):
     """
     Mixin broadcasts key for the model when it is newly created
     """
     mock_model_cache.retrieve_model_cache_info.return_value = None
     mock_uuid4 = Mock(hex='unique_id')
     mock_uuid.uuid4.return_value = mock_uuid4
     self.mixin.generate_key()
     model_cache_info = ModelCacheInfo(table_name=u'tests_manufacturer',
                                       table_key='unique_id')
     mock_model_cache.share_model_cache_info.assert_called_once_with(
         model_cache_info)
 def setUp(self):
     self.shared_memory = model_cache_backend
     self.cache_model_info = ModelCacheInfo('table1', 'key1')
     self.shared_memory.share_model_cache_info(self.cache_model_info)