Пример #1
0
    def test_cached_values_are_properly_updated(self):
        setting_key = "key"
        setting_old_value = "old value"
        setting_new_value = "new value"

        # First, let's create a ConfigurationSetting instance and save it in the database.
        setting = ConfigurationSetting(key=setting_key, _value=setting_old_value)
        self._db.add(setting)
        self._db.commit()

        # Let's save ConfigurationSetting's ID to find it later.
        setting_id = setting.id

        # Now let's fetch the configuration setting from the database and add it to the cache.
        db_setting1 = (
            self._db.query(ConfigurationSetting)
            .filter(ConfigurationSetting.key == setting_key)
            .one()
        )
        ConfigurationSetting.cache_warm(self._db, lambda: [db_setting1])

        # After, let's fetch it again and change its value.
        db_setting2 = (
            self._db.query(ConfigurationSetting)
            .filter(ConfigurationSetting.key == setting_key)
            .one()
        )
        db_setting2.value = setting_new_value

        # Now let's make sure that the cached value has also been updated.
        assert (
            ConfigurationSetting.by_id(self._db, setting_id)._value == setting_new_value
        )
Пример #2
0
    def test_cached_value_deleted(self):
        # Get setting
        setting = ConfigurationSetting.sitewide(self._db, "test")
        setting.value = "testing"

        # Delete setting
        self._db.delete(setting)

        # we should no longer be able to get setting from cache
        cached = ConfigurationSetting.by_id(self._db, setting.id)
        cache = ConfigurationSetting._cache_from_session(self._db)
        assert cached is None
        assert len(cache.id) == 0
        assert len(cache.key) == 0