Skip to content

Commit da4b170

Browse files
seroyPhotonios
authored andcommitted
add tests for ActiveRef and TranslatedRef lookups
1 parent c9ae71a commit da4b170

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

tests/test_lookups.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,71 @@ def test_localized_lookup(self):
4848

4949
# ensure that hstore lookups still work
5050
assert self.TestModel.objects.filter(text__ro="text_ro").exists()
51+
52+
53+
class LocalizedRefLookupsTestCase(TestCase):
54+
"""Tests whether ref lookups properly work with."""
55+
56+
TestModel1 = None
57+
58+
@classmethod
59+
def setUpClass(cls):
60+
"""Creates the test model in the database."""
61+
super(LocalizedRefLookupsTestCase, cls).setUpClass()
62+
cls.TestModel = get_fake_model({"text": LocalizedField()})
63+
cls.TestModel.objects.create(
64+
text=LocalizedValue(dict(en="text_en", ro="text_ro", nl="text_nl"))
65+
)
66+
67+
def test_active_ref_lookup(self):
68+
"""Tests whether active_ref lookup properly works."""
69+
70+
# assert that it properly lookups the currently active language
71+
for lang_code, _ in settings.LANGUAGES:
72+
translation.activate(lang_code)
73+
assert self.TestModel.objects.filter(
74+
text__active_ref=f"text_{lang_code}"
75+
).exists()
76+
77+
# ensure that the default language is used in case no
78+
# language is active at all
79+
translation.deactivate_all()
80+
assert self.TestModel.objects.filter(
81+
text__active_ref="text_en"
82+
).exists()
83+
84+
def test_translated_ref_lookup(self):
85+
"""Tests whether translated_ref lookup properly works."""
86+
87+
# assert that it properly lookups the currently active language
88+
for lang_code, _ in settings.LANGUAGES:
89+
translation.activate(lang_code)
90+
assert self.TestModel.objects.filter(
91+
text__translated_ref=f"text_{lang_code}"
92+
).exists()
93+
94+
# ensure that the default language is used in case no
95+
# language is active at all
96+
translation.deactivate_all()
97+
assert self.TestModel.objects.filter(
98+
text__translated_ref="text_en"
99+
).exists()
100+
101+
fallbacks = {"cs": ["ru", "ro"], "pl": ["nl", "ro"]}
102+
103+
with override_settings(LOCALIZED_FIELDS_FALLBACKS=fallbacks):
104+
with translation.override("cs"):
105+
assert self.TestModel.objects.filter(
106+
text__translated_ref="text_ro"
107+
).exists()
108+
109+
with translation.override("pl"):
110+
assert self.TestModel.objects.filter(
111+
text__translated_ref="text_nl"
112+
).exists()
113+
114+
# ensure that the default language is used in case no fallback is set
115+
with translation.override("ru"):
116+
assert self.TestModel.objects.filter(
117+
text__translated_ref="text_en"
118+
).exists()

0 commit comments

Comments
 (0)