test_storages.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import sys
  2. from types import ModuleType
  3. from django.conf import (
  4. DEFAULT_FILE_STORAGE_DEPRECATED_MSG,
  5. DEFAULT_STORAGE_ALIAS,
  6. STATICFILES_STORAGE_ALIAS,
  7. STATICFILES_STORAGE_DEPRECATED_MSG,
  8. Settings,
  9. settings,
  10. )
  11. from django.contrib.staticfiles.storage import (
  12. ManifestStaticFilesStorage,
  13. staticfiles_storage,
  14. )
  15. from django.core.exceptions import ImproperlyConfigured
  16. from django.core.files.storage import Storage, StorageHandler, default_storage, storages
  17. from django.test import TestCase, ignore_warnings
  18. from django.utils.deprecation import RemovedInDjango51Warning
  19. class StaticfilesStorageDeprecationTests(TestCase):
  20. msg = STATICFILES_STORAGE_DEPRECATED_MSG
  21. def test_override_settings_warning(self):
  22. with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
  23. with self.settings(
  24. STATICFILES_STORAGE=(
  25. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
  26. )
  27. ):
  28. pass
  29. def test_settings_init(self):
  30. old_staticfiles_storage = settings.STORAGES.get(STATICFILES_STORAGE_ALIAS)
  31. settings_module = ModuleType("fake_settings_module")
  32. settings_module.USE_TZ = True
  33. settings_module.STATICFILES_STORAGE = (
  34. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
  35. )
  36. sys.modules["fake_settings_module"] = settings_module
  37. try:
  38. with self.assertWarnsMessage(RemovedInDjango51Warning, self.msg):
  39. fake_settings = Settings("fake_settings_module")
  40. self.assertEqual(
  41. fake_settings.STORAGES[STATICFILES_STORAGE_ALIAS],
  42. {
  43. "BACKEND": (
  44. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
  45. ),
  46. },
  47. )
  48. # settings.STORAGES is not mutated.
  49. self.assertEqual(
  50. settings.STORAGES.get(STATICFILES_STORAGE_ALIAS),
  51. old_staticfiles_storage,
  52. )
  53. finally:
  54. del sys.modules["fake_settings_module"]
  55. def test_settings_storages_init(self):
  56. settings_module = ModuleType("fake_settings_module")
  57. settings_module.USE_TZ = True
  58. settings_module.STORAGES = {
  59. STATICFILES_STORAGE_ALIAS: {
  60. "BACKEND": (
  61. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
  62. )
  63. }
  64. }
  65. sys.modules["fake_settings_module"] = settings_module
  66. try:
  67. fake_settings = Settings("fake_settings_module")
  68. self.assertEqual(
  69. fake_settings.STATICFILES_STORAGE,
  70. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
  71. )
  72. finally:
  73. del sys.modules["fake_settings_module"]
  74. def test_access_warning(self):
  75. with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
  76. settings.STATICFILES_STORAGE
  77. # Works a second time.
  78. with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
  79. settings.STATICFILES_STORAGE
  80. @ignore_warnings(category=RemovedInDjango51Warning)
  81. def test_access(self):
  82. with self.settings(
  83. STATICFILES_STORAGE=(
  84. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
  85. )
  86. ):
  87. self.assertEqual(
  88. settings.STATICFILES_STORAGE,
  89. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
  90. )
  91. # Works a second time.
  92. self.assertEqual(
  93. settings.STATICFILES_STORAGE,
  94. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
  95. )
  96. def test_use_both_error(self):
  97. msg = "STATICFILES_STORAGE/STORAGES are mutually exclusive."
  98. settings_module = ModuleType("fake_settings_module")
  99. settings_module.USE_TZ = True
  100. settings_module.STATICFILES_STORAGE = (
  101. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
  102. )
  103. settings_module.STORAGES = {}
  104. sys.modules["fake_settings_module"] = settings_module
  105. try:
  106. with self.assertRaisesMessage(ImproperlyConfigured, msg):
  107. Settings("fake_settings_module")
  108. finally:
  109. del sys.modules["fake_settings_module"]
  110. @ignore_warnings(category=RemovedInDjango51Warning)
  111. def test_storage(self):
  112. empty_storages = StorageHandler()
  113. with self.settings(
  114. STATICFILES_STORAGE=(
  115. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
  116. )
  117. ):
  118. self.assertIsInstance(
  119. storages[STATICFILES_STORAGE_ALIAS],
  120. ManifestStaticFilesStorage,
  121. )
  122. self.assertIsInstance(
  123. empty_storages[STATICFILES_STORAGE_ALIAS],
  124. ManifestStaticFilesStorage,
  125. )
  126. self.assertIsInstance(staticfiles_storage, ManifestStaticFilesStorage)
  127. @ignore_warnings(category=RemovedInDjango51Warning)
  128. def test_staticfiles_storage(self):
  129. with self.settings(
  130. STORAGES={
  131. STATICFILES_STORAGE_ALIAS: {
  132. "BACKEND": (
  133. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
  134. )
  135. }
  136. }
  137. ):
  138. self.assertEqual(
  139. settings.STATICFILES_STORAGE,
  140. "django.contrib.staticfiles.storage.ManifestStaticFilesStorage",
  141. )
  142. self.assertIsInstance(
  143. storages[STATICFILES_STORAGE_ALIAS],
  144. ManifestStaticFilesStorage,
  145. )
  146. class DefaultStorageDeprecationTests(TestCase):
  147. msg = DEFAULT_FILE_STORAGE_DEPRECATED_MSG
  148. def test_override_settings_warning(self):
  149. with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
  150. with self.settings(
  151. DEFAULT_FILE_STORAGE=("django.core.files.storage.Storage")
  152. ):
  153. pass
  154. def test_settings_init(self):
  155. old_default_storage = settings.STORAGES.get(DEFAULT_STORAGE_ALIAS)
  156. settings_module = ModuleType("fake_settings_module")
  157. settings_module.USE_TZ = True
  158. settings_module.DEFAULT_FILE_STORAGE = "django.core.files.storage.Storage"
  159. sys.modules["fake_settings_module"] = settings_module
  160. try:
  161. with self.assertWarnsMessage(RemovedInDjango51Warning, self.msg):
  162. fake_settings = Settings("fake_settings_module")
  163. self.assertEqual(
  164. fake_settings.STORAGES[DEFAULT_STORAGE_ALIAS],
  165. {"BACKEND": "django.core.files.storage.Storage"},
  166. )
  167. # settings.STORAGES is not mutated.
  168. self.assertEqual(
  169. settings.STORAGES.get(DEFAULT_STORAGE_ALIAS),
  170. old_default_storage,
  171. )
  172. finally:
  173. del sys.modules["fake_settings_module"]
  174. def test_settings_storages_init(self):
  175. settings_module = ModuleType("fake_settings_module")
  176. settings_module.USE_TZ = True
  177. settings_module.STORAGES = {
  178. DEFAULT_STORAGE_ALIAS: {
  179. "BACKEND": "django.core.files.storage.Storage",
  180. }
  181. }
  182. sys.modules["fake_settings_module"] = settings_module
  183. try:
  184. fake_settings = Settings("fake_settings_module")
  185. self.assertEqual(
  186. fake_settings.DEFAULT_FILE_STORAGE,
  187. "django.core.files.storage.Storage",
  188. )
  189. finally:
  190. del sys.modules["fake_settings_module"]
  191. def test_access_warning(self):
  192. with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
  193. settings.DEFAULT_FILE_STORAGE
  194. # Works a second time.
  195. with self.assertRaisesMessage(RemovedInDjango51Warning, self.msg):
  196. settings.DEFAULT_FILE_STORAGE
  197. @ignore_warnings(category=RemovedInDjango51Warning)
  198. def test_access(self):
  199. with self.settings(DEFAULT_FILE_STORAGE="django.core.files.storage.Storage"):
  200. self.assertEqual(
  201. settings.DEFAULT_FILE_STORAGE,
  202. "django.core.files.storage.Storage",
  203. )
  204. # Works a second time.
  205. self.assertEqual(
  206. settings.DEFAULT_FILE_STORAGE,
  207. "django.core.files.storage.Storage",
  208. )
  209. def test_use_both_error(self):
  210. msg = "DEFAULT_FILE_STORAGE/STORAGES are mutually exclusive."
  211. settings_module = ModuleType("fake_settings_module")
  212. settings_module.USE_TZ = True
  213. settings_module.DEFAULT_FILE_STORAGE = "django.core.files.storage.Storage"
  214. settings_module.STORAGES = {}
  215. sys.modules["fake_settings_module"] = settings_module
  216. try:
  217. with self.assertRaisesMessage(ImproperlyConfigured, msg):
  218. Settings("fake_settings_module")
  219. finally:
  220. del sys.modules["fake_settings_module"]
  221. @ignore_warnings(category=RemovedInDjango51Warning)
  222. def test_storage(self):
  223. empty_storages = StorageHandler()
  224. with self.settings(DEFAULT_FILE_STORAGE="django.core.files.storage.Storage"):
  225. self.assertIsInstance(storages[DEFAULT_STORAGE_ALIAS], Storage)
  226. self.assertIsInstance(empty_storages[DEFAULT_STORAGE_ALIAS], Storage)
  227. self.assertIsInstance(default_storage, Storage)
  228. @ignore_warnings(category=RemovedInDjango51Warning)
  229. def test_default_file_storage(self):
  230. with self.settings(
  231. STORAGES={
  232. DEFAULT_STORAGE_ALIAS: {
  233. "BACKEND": "django.core.files.storage.Storage",
  234. }
  235. }
  236. ):
  237. self.assertEqual(
  238. settings.DEFAULT_FILE_STORAGE,
  239. "django.core.files.storage.Storage",
  240. )
  241. self.assertIsInstance(storages[DEFAULT_STORAGE_ALIAS], Storage)