Skip to content

Commit b121dfc

Browse files
committed
Added __eq__ operator to LocalizedValue
1 parent d529da8 commit b121dfc

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

localized_fields/localized_value.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ def __str__(self) -> str:
7676

7777
return value or ''
7878

79+
def __eq__(self, other):
80+
"""Compares :paramref:self to :paramref:other for
81+
equality.
82+
83+
Returns:
84+
True when :paramref:self is equal to :paramref:other.
85+
And False when they are not.
86+
"""
87+
88+
for lang_code, _ in settings.LANGUAGES:
89+
if self.get(lang_code) != other.get(lang_code):
90+
return False
91+
92+
return True
93+
7994
def __repr__(self): # pragma: no cover
8095
"""Gets a textual representation of this object."""
8196

tests/test_localized_field.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ def test_str():
100100
translation.activate(language)
101101
assert str(localized_value) == value
102102

103+
@staticmethod
104+
def test_eq():
105+
"""Tests whether the __eq__ operator
106+
of :see:LocalizedValue works properly."""
107+
108+
a = LocalizedValue({'en': 'a', 'ar': 'b'})
109+
b = LocalizedValue({'en': 'a', 'ar': 'b'})
110+
111+
assert a == b
112+
113+
b.en = 'b'
114+
assert a != b
115+
103116
@staticmethod
104117
def test_str_fallback():
105118
"""Tests whether the :see:LocalizedValue

0 commit comments

Comments
 (0)