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 4.6KB

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