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.

test_customisation.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import os
  2. import sys
  3. import tempfile
  4. from os.path import exists, join
  5. import pytest
  6. from django.conf import settings
  7. from django.test import TestCase, override_settings
  8. from oscar.core import customisation
  9. from tests import delete_from_import_cache
  10. VALID_FOLDER_PATH = 'tests/_site/apps'
  11. class TestUtilities(TestCase):
  12. def test_subfolder_extraction(self):
  13. folders = list(customisation.subfolders('/var/www/eggs'))
  14. self.assertEqual(folders, ['/var', '/var/www', '/var/www/eggs'])
  15. def test_raises_exception_for_nonexistant_app_label():
  16. with pytest.raises(ValueError):
  17. customisation.fork_app('sillytown', 'somefolder', 'sillytown')
  18. def test_raises_exception_if_app_has_already_been_forked():
  19. # We piggyback on another test which means a custom app is already in
  20. # the apps directory we use for the test suite. We just check that's still
  21. # the case here.
  22. assert exists(join(VALID_FOLDER_PATH, 'partner'))
  23. with pytest.raises(ValueError):
  24. customisation.fork_app('partner', VALID_FOLDER_PATH, 'partner')
  25. def test_creates_new_folder(tmpdir):
  26. path = tmpdir.mkdir('fork')
  27. customisation.fork_app('order', str(path), 'order')
  28. path.join('order').ensure_dir()
  29. def test_creates_init_file(tmpdir, monkeypatch):
  30. path = tmpdir.mkdir('fork')
  31. customisation.fork_app('order', str(path), 'order')
  32. path.join('order').join('__init__.py').ensure()
  33. monkeypatch.syspath_prepend(str(tmpdir))
  34. config_module = __import__('fork.order.apps', fromlist=['OrderConfig'])
  35. delete_from_import_cache('fork.order.apps')
  36. expected_string = "default_app_config = '{}.apps.OrderConfig".format(
  37. config_module.OrderConfig.name)
  38. contents = path.join('order').join('__init__.py').read()
  39. assert expected_string in contents
  40. def test_handles_dashboard_app(tmpdir):
  41. # Dashboard apps are fiddly as they aren't identified by a single app
  42. # label.
  43. path = tmpdir.mkdir('fork')
  44. customisation.fork_app('catalogue_dashboard', str(path), 'dashboard.catalogue')
  45. # Check __init__.py created (and supporting folders)
  46. path.join('dashboard').join('catalogue').join('__init__.py').ensure()
  47. def test_creates_models_and_admin_file(tmpdir):
  48. path = tmpdir.mkdir('fork')
  49. customisation.fork_app('order', str(path), 'order')
  50. for module, expected_string in [
  51. ('models', 'from oscar.apps.order.models import *'),
  52. ('admin', 'from oscar.apps.order.admin import *'),
  53. ('apps', 'OrderConfig')
  54. ]:
  55. filepath = path.join('order').join('%s.py' % module)
  56. filepath.ensure()
  57. contents = filepath.read()
  58. assert expected_string in contents
  59. def test_copies_in_migrations_when_needed(tmpdir):
  60. path = tmpdir.mkdir('fork')
  61. for app, has_models in [('order', True), ('search', False)]:
  62. customisation.fork_app(app, str(path), app)
  63. native_migration_path = path.join(app).join('migrations')
  64. assert has_models == native_migration_path.check()
  65. def test_dashboard_app_config(tmpdir, monkeypatch):
  66. path = tmpdir.mkdir('fork')
  67. customisation.fork_app('dashboard', str(path), 'dashboard')
  68. path.join('__init__.py').write('')
  69. monkeypatch.syspath_prepend(str(tmpdir))
  70. config_module = __import__(
  71. '%s.dashboard.apps' % path.basename, fromlist=['DashboardConfig']
  72. )
  73. assert hasattr(config_module, 'DashboardConfig')
  74. class TestForkApp(TestCase):
  75. def setUp(self):
  76. self.original_paths = sys.path[:]
  77. sys.path.append('./tests/_site/')
  78. def tearDown(self):
  79. sys.path = self.original_paths
  80. def test_fork_third_party(self):
  81. tmpdir = tempfile.mkdtemp()
  82. installed_apps = list(settings.INSTALLED_APPS)
  83. installed_apps.append('thirdparty_package.apps.myapp.apps.MyAppConfig')
  84. with override_settings(INSTALLED_APPS=installed_apps):
  85. customisation.fork_app('myapp', tmpdir, 'custom_myapp')
  86. forked_app_dir = join(tmpdir, 'custom_myapp')
  87. assert exists(forked_app_dir)
  88. assert exists(join(forked_app_dir, 'apps.py'))
  89. sys.path.append(tmpdir)
  90. config_module = __import__('custom_myapp.apps', fromlist=['CustomMyAppConfig'])
  91. assert hasattr(config_module, 'MyAppConfig')
  92. assert config_module.MyAppConfig.name.endswith('.custom_myapp')
  93. def test_absolute_target_path(self):
  94. tmpdir = tempfile.mkdtemp()
  95. customisation.fork_app('order', tmpdir, 'order')
  96. sys.path.append(tmpdir)
  97. config_module = __import__('order.apps', fromlist=['OrderConfig'])
  98. assert hasattr(config_module, 'OrderConfig')
  99. config_app_name = config_module.OrderConfig.name
  100. assert not config_app_name.startswith('.')
  101. def test_local_folder(self):
  102. tmpdir = tempfile.mkdtemp()
  103. os.chdir(tmpdir)
  104. customisation.fork_app('basket', '.', 'basket')
  105. sys.path.append(tmpdir)
  106. config_module = __import__('basket.apps', fromlist=['BasketConfig'])
  107. assert hasattr(config_module, 'BasketConfig')
  108. assert config_module.BasketConfig.name == 'basket'