Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

test_customisation.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. )
  39. contents = path.join("order").join("__init__.py").read()
  40. assert expected_string in contents
  41. def test_handles_dashboard_app(tmpdir):
  42. # Dashboard apps are fiddly as they aren't identified by a single app
  43. # label.
  44. path = tmpdir.mkdir("fork")
  45. customisation.fork_app("catalogue_dashboard", str(path), "dashboard.catalogue")
  46. # Check __init__.py created (and supporting folders)
  47. path.join("dashboard").join("catalogue").join("__init__.py").ensure()
  48. def test_creates_models_and_admin_file(tmpdir):
  49. path = tmpdir.mkdir("fork")
  50. customisation.fork_app("order", str(path), "order")
  51. for module, expected_string in [
  52. ("models", "from oscar.apps.order.models import *"),
  53. ("admin", "from oscar.apps.order.admin import *"),
  54. ("apps", "OrderConfig"),
  55. ]:
  56. filepath = path.join("order").join("%s.py" % module)
  57. filepath.ensure()
  58. contents = filepath.read()
  59. assert expected_string in contents
  60. def test_copies_in_migrations_when_needed(tmpdir):
  61. path = tmpdir.mkdir("fork")
  62. for app, has_models in [("order", True), ("search", False)]:
  63. customisation.fork_app(app, str(path), app)
  64. native_migration_path = path.join(app).join("migrations")
  65. assert has_models == native_migration_path.check()
  66. def test_dashboard_app_config(tmpdir, monkeypatch):
  67. path = tmpdir.mkdir("fork")
  68. customisation.fork_app("dashboard", str(path), "dashboard")
  69. path.join("__init__.py").write("")
  70. monkeypatch.syspath_prepend(str(tmpdir))
  71. config_module = __import__(
  72. "%s.dashboard.apps" % path.basename, fromlist=["DashboardConfig"]
  73. )
  74. assert hasattr(config_module, "DashboardConfig")
  75. class TestForkApp(TestCase):
  76. def setUp(self):
  77. self.original_paths = sys.path[:]
  78. sys.path.append("./tests/_site/")
  79. def tearDown(self):
  80. sys.path = self.original_paths
  81. def test_fork_third_party(self):
  82. tmpdir = tempfile.mkdtemp()
  83. installed_apps = list(settings.INSTALLED_APPS)
  84. installed_apps.append("thirdparty_package.apps.myapp.apps.MyAppConfig")
  85. with override_settings(INSTALLED_APPS=installed_apps):
  86. customisation.fork_app("myapp", tmpdir, "custom_myapp")
  87. forked_app_dir = join(tmpdir, "custom_myapp")
  88. assert exists(forked_app_dir)
  89. assert exists(join(forked_app_dir, "apps.py"))
  90. sys.path.append(tmpdir)
  91. config_module = __import__(
  92. "custom_myapp.apps", fromlist=["CustomMyAppConfig"]
  93. )
  94. assert hasattr(config_module, "MyAppConfig")
  95. assert config_module.MyAppConfig.name.endswith(".custom_myapp")
  96. def test_absolute_target_path(self):
  97. tmpdir = tempfile.mkdtemp()
  98. customisation.fork_app("order", tmpdir, "order")
  99. sys.path.append(tmpdir)
  100. config_module = __import__("order.apps", fromlist=["OrderConfig"])
  101. assert hasattr(config_module, "OrderConfig")
  102. config_app_name = config_module.OrderConfig.name
  103. assert not config_app_name.startswith(".")
  104. def test_local_folder(self):
  105. tmpdir = tempfile.mkdtemp()
  106. os.chdir(tmpdir)
  107. customisation.fork_app("basket", ".", "basket")
  108. sys.path.append(tmpdir)
  109. config_module = __import__("basket.apps", fromlist=["BasketConfig"])
  110. assert hasattr(config_module, "BasketConfig")
  111. assert config_module.BasketConfig.name == "basket"