Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check None before returning config #296

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Unreleased

* Switch from ``edx-sphinx-theme`` to ``sphinx-book-theme`` since the former is
deprecated
* Fixed ConfigurationModel.current: it will make sure that it does not return None for current configuration.

[2.3.0] - 2022-01-19
~~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion config_models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def current(cls, *args):
"""
cache_key = cls.cache_key_name(*args)
cached_response = TieredCache.get_cached_response(cache_key)
if cached_response.is_found:
if cached_response.is_found and cached_response.value is not None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[inform] The None check was there before at this place. But it was removed when TieredCache was included.

https://github.com/openedx/django-config-models/pull/55/files#diff-89972c71903f4383220fc6ece46180b9c5c52f3467b07de6cc662ae9a546f49aL199-L201

return cached_response.value

key_dict = dict(zip(cls.KEY_FIELDS, args))
Expand Down
18 changes: 17 additions & 1 deletion tests/test_config_models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""
Tests of ConfigurationModel
"""

from unittest import mock

import ddt
from django.contrib.auth import get_user_model
from edx_django_utils.cache.utils import CachedResponse
from example.models import (ExampleConfig, ExampleKeyedConfig,
ManyToManyExampleConfig)
from freezegun import freeze_time
Expand Down Expand Up @@ -45,6 +46,21 @@ def test_no_config_empty_cache(self):
self.assertEqual(cached_current.int_field, 10)
self.assertEqual(cached_current.string_field, '')

@mock.patch('config_models.models.TieredCache.get_cached_response')
def test_config_with_cached_response_value_none(self, mock_cached_response):
mock_cached_response.return_value = CachedResponse(is_found=True, key='test-key', value=None)
# First time reads from the database
with self.assertNumQueries(1):
current = ExampleConfig.current()
self.assertEqual(current.int_field, 10)
self.assertEqual(current.string_field, '')

# Follow-on request reads from database instead of cache as cache value will be None.
with self.assertNumQueries(1):
cached_current = ExampleConfig.current()
self.assertEqual(cached_current.int_field, 10)
self.assertEqual(cached_current.string_field, '')

def test_config_ordering(self):
with freeze_time('2012-01-01'):
first = ExampleConfig(changed_by=self.user)
Expand Down