-
-
Notifications
You must be signed in to change notification settings - Fork 31k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #35405 -- Used @cached_property in FieldCacheMixin.
co-authored-by: Simon Charette <charette.s@gmail.com>
- Loading branch information
1 parent
ec85524
commit 973f80f
Showing
8 changed files
with
107 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from django.db.models.fields.mixins import FieldCacheMixin | ||
from django.test import SimpleTestCase | ||
from django.utils.deprecation import RemovedInDjango60Warning | ||
from django.utils.functional import cached_property | ||
|
||
from .models import Foo | ||
|
||
|
||
class ExampleOld(FieldCacheMixin): | ||
def get_cache_name(self): | ||
return "example" | ||
|
||
|
||
class Example(FieldCacheMixin): | ||
@cached_property | ||
def cache_name(self): | ||
return "example" | ||
|
||
|
||
class FieldCacheMixinTests(SimpleTestCase): | ||
def setUp(self): | ||
self.instance = Foo() | ||
self.field = Example() | ||
|
||
def test_get_cache_name_not_implemented(self): | ||
with self.assertRaises(NotImplementedError): | ||
FieldCacheMixin().get_cache_name() | ||
|
||
def test_get_cache_name_deprecated(self): | ||
msg = "Override ExampleOld.cache_name instead of get_cache_name()." | ||
with self.assertWarnsMessage(RemovedInDjango60Warning, msg): | ||
ExampleOld().cache_name | ||
|
||
def test_get_cached_value_missing(self): | ||
with self.assertRaises(KeyError): | ||
self.field.get_cached_value(self.instance) | ||
|
||
def test_get_cached_value_default(self): | ||
default = object() | ||
result = self.field.get_cached_value(self.instance, default=default) | ||
self.assertIs(result, default) | ||
|
||
def test_get_cached_value_after_set(self): | ||
value = object() | ||
|
||
self.field.set_cached_value(self.instance, value) | ||
result = self.field.get_cached_value(self.instance) | ||
|
||
self.assertIs(result, value) | ||
|
||
def test_is_cached_false(self): | ||
result = self.field.is_cached(self.instance) | ||
self.assertFalse(result) | ||
|
||
def test_is_cached_true(self): | ||
self.field.set_cached_value(self.instance, 1) | ||
result = self.field.is_cached(self.instance) | ||
self.assertTrue(result) | ||
|
||
def test_delete_cached_value(self): | ||
self.field.set_cached_value(self.instance, 1) | ||
self.field.delete_cached_value(self.instance) | ||
result = self.field.is_cached(self.instance) | ||
self.assertFalse(result) |