You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

core_tests.py 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from django.test import TestCase
  2. from django.core.exceptions import ValidationError
  3. from django.conf import settings
  4. from django.contrib.flatpages.models import FlatPage
  5. from oscar.core.loading import import_module, AppNotFoundError, \
  6. get_classes, get_class, ClassNotFoundError
  7. from oscar.core.validators import ExtendedURLValidator
  8. from oscar.core.validators import URLDoesNotExistValidator
  9. from oscar.test import patch_settings
  10. class ImportAppTests(TestCase):
  11. def test_a_specified_class_is_imported_correctly(self):
  12. module = import_module('analytics.models', ['ProductRecord'])
  13. self.assertEqual('oscar.apps.analytics.models', module.__name__)
  14. def test_unknown_apps_raise_exception(self):
  15. self.assertRaises(AppNotFoundError, import_module, 'banana', ['skin'])
  16. class ClassLoadingTests(TestCase):
  17. def test_loading_oscar_classes(self):
  18. Product, Category = get_classes('catalogue.models', ('Product', 'Category'))
  19. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  20. self.assertEqual('oscar.apps.catalogue.models', Category.__module__)
  21. def test_loading_oscar_class(self):
  22. Product = get_class('catalogue.models', 'Product')
  23. self.assertEqual('oscar.apps.catalogue.models', Product.__module__)
  24. def test_loading_oscar_class_from_dashboard_subapp(self):
  25. ReportForm = get_class('dashboard.reports.forms', 'ReportForm')
  26. self.assertEqual('oscar.apps.dashboard.reports.forms', ReportForm.__module__)
  27. def test_bad_appname_raises_exception(self):
  28. with self.assertRaises(AppNotFoundError):
  29. get_classes('fridge.models', ('Product', 'Category'))
  30. def test_bad_classname_raises_exception(self):
  31. with self.assertRaises(ClassNotFoundError):
  32. get_class('catalogue.models', 'Monkey')
  33. class ClassLoadingWithLocalOverrideTests(TestCase):
  34. def setUp(self):
  35. self.installed_apps = list(settings.INSTALLED_APPS)
  36. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests.site.shipping'
  37. def test_loading_class_defined_in_local_module(self):
  38. with patch_settings(INSTALLED_APPS=self.installed_apps):
  39. (Free,) = get_classes('shipping.methods', ('Free',))
  40. self.assertEqual('tests.site.shipping.methods', Free.__module__)
  41. def test_loading_class_which_is_not_defined_in_local_module(self):
  42. with patch_settings(INSTALLED_APPS=self.installed_apps):
  43. (FixedPrice,) = get_classes('shipping.methods', ('FixedPrice',))
  44. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  45. def test_loading_class_from_module_not_defined_in_local_app(self):
  46. with patch_settings(INSTALLED_APPS=self.installed_apps):
  47. (Repository,) = get_classes('shipping.repository', ('Repository',))
  48. self.assertEqual('oscar.apps.shipping.repository', Repository.__module__)
  49. def test_loading_classes_defined_in_both_local_and_oscar_modules(self):
  50. with patch_settings(INSTALLED_APPS=self.installed_apps):
  51. (Free, FixedPrice) = get_classes('shipping.methods', ('Free', 'FixedPrice'))
  52. self.assertEqual('tests.site.shipping.methods', Free.__module__)
  53. self.assertEqual('oscar.apps.shipping.methods', FixedPrice.__module__)
  54. class ValidatorTests(TestCase):
  55. def test_validate_local_url(self):
  56. v = ExtendedURLValidator(verify_exists=True)
  57. try:
  58. v('/')
  59. except ValidationError:
  60. self.fail('ExtendedURLValidator raised ValidationError'
  61. 'unexpectedly!')
  62. try:
  63. v('/?q=test') # Query strings shouldn't affect validation
  64. except ValidationError:
  65. self.fail('ExtendedURLValidator raised ValidationError'
  66. 'unexpectedly!')
  67. with self.assertRaises(ValidationError):
  68. v('/invalid/')
  69. with self.assertRaises(ValidationError):
  70. v('/invalid/?q=test') # Query strings shouldn't affect validation
  71. try:
  72. v('products/')
  73. except ValidationError:
  74. self.fail('ExtendedURLValidator raised ValidationError'
  75. 'unexpectedly!')
  76. with self.assertRaises(ValidationError):
  77. v('/products') # Missing the / is bad
  78. FlatPage(title='test page', url='/test/page/').save()
  79. try:
  80. v('/test/page/')
  81. except ValidationError:
  82. self.fail('ExtendedURLValidator raises ValidationError'
  83. 'unexpectedly!')
  84. def test_validate_url_does_not_exist(self):
  85. validator = URLDoesNotExistValidator()
  86. self.assertRaises(ValidationError, validator, '/')
  87. try:
  88. validator('/invalid/')
  89. except ValidationError:
  90. self.fail('URLDoesNotExistValidator raised ValidationError'
  91. 'unexpectedly!')
  92. FlatPage(title='test page', url='/test/page/').save()
  93. self.assertRaises(ValidationError, validator, '/test/page/')
  94. class ClassLoadingWithLocalOverrideWithMultipleSegmentsTests(TestCase):
  95. def setUp(self):
  96. self.installed_apps = list(settings.INSTALLED_APPS)
  97. self.installed_apps[self.installed_apps.index('oscar.apps.shipping')] = 'tests.site.apps.shipping'
  98. def test_loading_class_defined_in_local_module(self):
  99. with patch_settings(INSTALLED_APPS=self.installed_apps):
  100. (Free,) = get_classes('shipping.methods', ('Free',))
  101. self.assertEqual('tests.site.apps.shipping.methods', Free.__module__)