From b121dfc2d76371c5afc5469f66be78d5f18e26f9 Mon Sep 17 00:00:00 2001 From: Swen Kooij Date: Thu, 9 Feb 2017 14:57:08 +0200 Subject: [PATCH] Added __eq__ operator to LocalizedValue --- localized_fields/localized_value.py | 15 +++++++++++++++ tests/test_localized_field.py | 13 +++++++++++++ 2 files changed, 28 insertions(+) diff --git a/localized_fields/localized_value.py b/localized_fields/localized_value.py index 7a9adde..4df5c33 100644 --- a/localized_fields/localized_value.py +++ b/localized_fields/localized_value.py @@ -76,6 +76,21 @@ class LocalizedValue: return value or '' + def __eq__(self, other): + """Compares :paramref:self to :paramref:other for + equality. + + Returns: + True when :paramref:self is equal to :paramref:other. + And False when they are not. + """ + + for lang_code, _ in settings.LANGUAGES: + if self.get(lang_code) != other.get(lang_code): + return False + + return True + def __repr__(self): # pragma: no cover """Gets a textual representation of this object.""" diff --git a/tests/test_localized_field.py b/tests/test_localized_field.py index 313bca5..f557422 100644 --- a/tests/test_localized_field.py +++ b/tests/test_localized_field.py @@ -100,6 +100,19 @@ class LocalizedValueTestCase(TestCase): translation.activate(language) assert str(localized_value) == value + @staticmethod + def test_eq(): + """Tests whether the __eq__ operator + of :see:LocalizedValue works properly.""" + + a = LocalizedValue({'en': 'a', 'ar': 'b'}) + b = LocalizedValue({'en': 'a', 'ar': 'b'}) + + assert a == b + + b.en = 'b' + assert a != b + @staticmethod def test_str_fallback(): """Tests whether the :see:LocalizedValue