您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

forms_tests.py 2.4KB

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