Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Move address comparision to model equal method

Anna Siwike84e030912b5
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index e7db13ec27..653bf301cc 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -247,8 +247,7 @@ class Checkout:
@property
def is_shipping_same_as_billing(self):
"""Return `True` if shipping and billing addresses are identical."""
- return Address.objects.are_identical(
- self.shipping_address, self.billing_address)
+ return self.shipping_address == self.billing_address
def _add_to_user_address_book(self, address, is_billing=False,
is_shipping=False):
diff --git a/saleor/checkout/views/summary.py b/saleor/checkout/views/summary.py
index 1d819e4bf9..023017cee8 100644
--- a/saleor/checkout/views/summary.py
+++ b/saleor/checkout/views/summary.py
@@ -50,7 +50,7 @@ def handle_order_placement(request, checkout):
def get_billing_forms_with_shipping(
data, addresses, billing_address, shipping_address):
"""Get billing form based on a the current billing and shipping data."""
- if Address.objects.are_identical(billing_address, shipping_address):
+ if billing_address == shipping_address:
address_form, preview = get_address_form(
data, country_code=shipping_address.country.code,
autocomplete_type='billing',
diff --git a/saleor/userprofile/models.py b/saleor/userprofile/models.py
index 9acb346e9f..18a2ee58b5 100644
--- a/saleor/userprofile/models.py
+++ b/saleor/userprofile/models.py
@@ -29,12 +29,6 @@ class AddressManager(models.Manager):
data['country'] = data['country'].code
return data
- def are_identical(self, addr1, addr2):
- """Return `True` if `addr1` and `addr2` are the same address."""
- data1 = self.as_data(addr1)
- data2 = self.as_data(addr2)
- return data1 == data2
-
def copy(self, address):
"""Return a new instance of the same address."""
data = self.as_data(address)
@@ -80,6 +74,10 @@ class Address(models.Model):
self.postal_code, self.country, self.country_area,
self.phone))
+ def __eq__(self, other):
+ """Return `True` if `addr1` and `addr2` are the same address."""
+ return Address.objects.as_data(self) == Address.objects.as_data(other)
+
class UserManager(BaseUserManager):
diff --git a/tests/conftest.py b/tests/conftest.py
index 7ac3d4927f..d2886c4f56 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -117,7 +117,8 @@ def billing_address(db): # pylint: disable=W0613
street_address_1='Tęczowa 7',
city='Wrocław',
postal_code='53-601',
- country='PL')
+ country='PL',
+ phone='+48713988102')
@pytest.fixture
diff --git a/tests/test_userprofile.py b/tests/test_userprofile.py
index 4bf82563cb..9f765f10f5 100644
--- a/tests/test_userprofile.py
+++ b/tests/test_userprofile.py
@@ -7,24 +7,13 @@ from django.core.exceptions import ValidationError
from django.http import QueryDict
from django.template import Context, Template
from django.urls import reverse
+from django_countries.fields import Country
from saleor.userprofile import forms, i18n, models
from saleor.userprofile.templatetags.i18n_address_tags import format_address
from saleor.userprofile.validators import validate_possible_number
-def billing_address(db):
- return models.Address.objects.create(
- first_name='John', last_name='Doe',
- company_name='Mirumee Software',
- street_address_1='Tęczowa 7',
- city='Wrocław',
- postal_code='53-601',
- country='PL',
- phone='+48713988102')
-
-
@pytest.mark.parametrize('country', ['CN', 'PL', 'US'])
def test_address_form_for_country(country):
data = {
@@ -69,6 +58,7 @@ def test_address_form_postal_code_validation():
errors = form.errors
assert 'postal_code' in errors
+
@pytest.mark.parametrize('form_data, form_valid, expected_preview, expected_country',[
({'preview': True}, False, True, 'PL'),
({'preview': False,
@@ -161,3 +151,22 @@ def test_format_address_all_options(billing_address):
assert address_html in rendered_html
assert 'inline-address' in rendered_html
assert str(billing_address.phone) not in rendered_html
+
+
+def test_compare_addresses(billing_address):
+ copied_address = models.Address.objects.copy(billing_address)
+ assert billing_address == copied_address
+
+
+def test_compare_addresses_with_country_object(billing_address):
+ copied_address = models.Address.objects.copy(billing_address)
+ copied_address.country = Country('PL')
+ copied_address.save()
+ assert billing_address == copied_address
+
+
+def test_compare_addresses_different_country(billing_address):
+ copied_address = models.Address.objects.copy(billing_address)
+ copied_address.country = Country('FR')
+ copied_address.save()
+ assert billing_address != copied_address