Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

test_forms.py 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # coding=utf-8
  2. from django.test import TestCase
  3. from django.test.utils import override_settings
  4. from oscar.apps.address.models import Country
  5. from oscar.apps.checkout.forms import ShippingAddressForm
  6. from oscar.test.factories import CountryFactory
  7. class TestShippingAddressForm(TestCase):
  8. minimal_data = {
  9. 'first_name': 'Bärry',
  10. 'last_name': 'Chuckle',
  11. 'line1': '1 King St',
  12. 'line4': 'Gothám',
  13. 'postcode': 'N1 7RR'
  14. }
  15. def setUp(self):
  16. CountryFactory(iso_3166_1_a2='GB', is_shipping_country=True)
  17. def test_removes_country_field(self):
  18. self.assertTrue('country' not in ShippingAddressForm().fields)
  19. def test_keeps_country_field(self):
  20. CountryFactory(iso_3166_1_a2='DE', is_shipping_country=True)
  21. self.assertTrue('country' in ShippingAddressForm().fields)
  22. @override_settings(OSCAR_REQUIRED_ADDRESS_FIELDS=('last_name', 'postcode'))
  23. def test_required_fields_validated(self):
  24. form = ShippingAddressForm()
  25. self.assertTrue(form.fields['last_name'].required)
  26. self.assertTrue(form.fields['postcode'].required)
  27. self.assertFalse(form.fields['first_name'].required)
  28. self.assertFalse(form.fields['line2'].required)
  29. self.assertFalse(form.fields['line3'].required)
  30. self.assertFalse(form.fields['line4'].required)
  31. @override_settings(OSCAR_REQUIRED_ADDRESS_FIELDS=('phone_number',))
  32. def test_required_phone_number_validated(self):
  33. # This needs a separate test because of the logic in PhoneNumberMixin
  34. form = ShippingAddressForm()
  35. self.assertTrue(form.fields['phone_number'].required)
  36. # Tests where the country field is hidden
  37. def test_is_valid_without_phone_number(self):
  38. self.assertTrue(ShippingAddressForm(self.minimal_data).is_valid())
  39. def test_only_accepts_british_local_phone_number(self):
  40. data = self.minimal_data.copy()
  41. data['phone_number'] = '07 914721389' # local UK number
  42. self.assertTrue(ShippingAddressForm(data).is_valid())
  43. data['phone_number'] = '0176 968 426 71' # local German number
  44. self.assertFalse(ShippingAddressForm(data).is_valid())
  45. def test_is_valid_with_international_phone_number(self):
  46. data = self.minimal_data.copy()
  47. data['phone_number'] = '+49 176 968426 71'
  48. form = ShippingAddressForm(data)
  49. self.assertTrue(form.is_valid())
  50. # Tests where the country field exists
  51. def test_needs_country_data(self):
  52. CountryFactory(iso_3166_1_a2='DE', is_shipping_country=True)
  53. self.assertFalse(ShippingAddressForm(self.minimal_data).is_valid())
  54. data = self.minimal_data.copy()
  55. data['country'] = Country.objects.get(iso_3166_1_a2='GB').pk
  56. self.assertTrue(ShippingAddressForm(data).is_valid())
  57. def test_only_accepts_local_phone_number_when_country_matches(self):
  58. CountryFactory(iso_3166_1_a2='DE', is_shipping_country=True)
  59. data = self.minimal_data.copy()
  60. data['phone_number'] = '07 914721389' # local UK number
  61. data['country'] = Country.objects.get(iso_3166_1_a2='DE').pk
  62. self.assertFalse(ShippingAddressForm(data).is_valid())
  63. data['country'] = Country.objects.get(iso_3166_1_a2='GB').pk
  64. self.assertTrue(ShippingAddressForm(data).is_valid())