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_autoslugfield.py 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # coding=utf-8
  2. """
  3. AutoSlugField taken from django-extensions at
  4. 15d3eb305957cee4768dd86e44df1bdad341a10e
  5. Uses Oscar's slugify function instead of Django's
  6. Copyright (c) 2007 Michael Trier
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. """
  23. from django.db import models
  24. from django.test import TestCase
  25. from django.test.utils import override_settings
  26. from oscar.core.loading import get_model
  27. SluggedTestModel = get_model("model_tests_app", "sluggedtestmodel")
  28. ChildSluggedTestModel = get_model("model_tests_app", "childsluggedtestmodel")
  29. CustomSluggedTestModel = get_model("model_tests_app", "CustomSluggedTestModel")
  30. class AutoSlugFieldTest(TestCase):
  31. def tearDown(self):
  32. super().tearDown()
  33. SluggedTestModel.objects.all().delete()
  34. def test_auto_create_slug(self):
  35. m = SluggedTestModel(title="foo")
  36. m.save()
  37. self.assertEqual(m.slug, "foo")
  38. def test_auto_create_next_slug(self):
  39. m = SluggedTestModel(title="foo")
  40. m.save()
  41. m = SluggedTestModel(title="foo")
  42. m.save()
  43. self.assertEqual(m.slug, "foo-2")
  44. def test_auto_create_slug_with_number(self):
  45. m = SluggedTestModel(title="foo 2012")
  46. m.save()
  47. self.assertEqual(m.slug, "foo-2012")
  48. def test_auto_update_slug_with_number(self):
  49. m = SluggedTestModel(title="foo 2012")
  50. m.save()
  51. m.save()
  52. self.assertEqual(m.slug, "foo-2012")
  53. def test_auto_create_unicode_slug(self):
  54. with override_settings(OSCAR_SLUG_ALLOW_UNICODE=True):
  55. m = SluggedTestModel(title="Château Margaux 1960")
  56. m.save()
  57. self.assertEqual(m.slug, "château-margaux-1960")
  58. def test_auto_create_next_unicode_slug(self):
  59. with override_settings(OSCAR_SLUG_ALLOW_UNICODE=True):
  60. m1 = SluggedTestModel(title="Château Margaux 1960")
  61. m1.save()
  62. m2 = SluggedTestModel(title="Château Margaux 1960")
  63. m2.save()
  64. self.assertEqual(m2.slug, "château-margaux-1960-2")
  65. def test_switch_to_unicode_slug(self):
  66. m = SluggedTestModel(title="Château Margaux 1960")
  67. m.save()
  68. self.assertEqual(m.slug, "chateau-margaux-1960")
  69. with override_settings(OSCAR_SLUG_ALLOW_UNICODE=True):
  70. m = SluggedTestModel(title="Château Margaux 1960")
  71. m.save()
  72. self.assertEqual(m.slug, "château-margaux-1960")
  73. def test_autoslugfield_allow_unicode_kwargs_precedence(self):
  74. from oscar.models.fields import AutoSlugField
  75. with override_settings(OSCAR_SLUG_ALLOW_UNICODE=True):
  76. autoslug_field = AutoSlugField(populate_from="title", allow_unicode=False)
  77. self.assertFalse(autoslug_field.allow_unicode)
  78. autoslug_field = AutoSlugField(populate_from="title")
  79. self.assertTrue(autoslug_field.allow_unicode)
  80. def test_update_slug(self):
  81. m = SluggedTestModel(title="foo")
  82. m.save()
  83. self.assertEqual(m.slug, "foo")
  84. # update m instance without using `save'
  85. SluggedTestModel.objects.filter(pk=m.pk).update(slug="foo-2012")
  86. # update m instance with new data from the db
  87. m = SluggedTestModel.objects.get(pk=m.pk)
  88. self.assertEqual(m.slug, "foo-2012")
  89. m.save()
  90. self.assertEqual(m.title, "foo")
  91. self.assertEqual(m.slug, "foo-2012")
  92. # Check slug is not overwrite
  93. m.title = "bar"
  94. m.save()
  95. self.assertEqual(m.title, "bar")
  96. self.assertEqual(m.slug, "foo-2012")
  97. def test_simple_slug_source(self):
  98. m = SluggedTestModel(title="-foo")
  99. m.save()
  100. self.assertEqual(m.slug, "foo")
  101. n = SluggedTestModel(title="-foo")
  102. n.save()
  103. self.assertEqual(n.slug, "foo-2")
  104. n.save()
  105. self.assertEqual(n.slug, "foo-2")
  106. def test_empty_slug_source(self):
  107. # regression test
  108. m = SluggedTestModel(title="")
  109. m.save()
  110. self.assertEqual(m.slug, "-2")
  111. n = SluggedTestModel(title="")
  112. n.save()
  113. self.assertEqual(n.slug, "-3")
  114. n.save()
  115. self.assertEqual(n.slug, "-3")
  116. def test_inheritance_creates_next_slug(self):
  117. m = SluggedTestModel(title="foo")
  118. m.save()
  119. n = ChildSluggedTestModel(title="foo")
  120. n.save()
  121. self.assertEqual(n.slug, "foo-2")
  122. o = SluggedTestModel(title="foo")
  123. o.save()
  124. self.assertEqual(o.slug, "foo-3")
  125. def test_separator_and_uppercase_options(self):
  126. m = CustomSluggedTestModel(title="Password reset")
  127. m.save()
  128. self.assertEqual(m.slug, "PASSWORD_RESET")
  129. m = CustomSluggedTestModel(title="Password reset")
  130. m.save()
  131. self.assertEqual(m.slug, "PASSWORD_RESET_2")
  132. def test_migration(self):
  133. """
  134. Tests making migrations with Django 1.7+'s migration framework
  135. """
  136. from django.db import migrations
  137. from django.db.migrations.writer import MigrationWriter
  138. import oscar
  139. from oscar.models.fields import AutoSlugField
  140. fields = {
  141. "autoslugfield": AutoSlugField(populate_from="otherfield"),
  142. }
  143. migration = type(
  144. str("Migration"),
  145. (migrations.Migration,),
  146. {
  147. "operations": [
  148. migrations.CreateModel(
  149. "MyModel",
  150. tuple(fields.items()),
  151. {"populate_from": "otherfield"},
  152. (models.Model,),
  153. ),
  154. ],
  155. },
  156. )
  157. writer = MigrationWriter(migration)
  158. output = writer.as_string()
  159. if isinstance(output, str):
  160. output = output.encode("utf-8")
  161. # We don't test the output formatting - that's too fragile.
  162. # Just make sure it runs for now, and that things look alright.
  163. context = {
  164. "migrations": migrations,
  165. "oscar": oscar,
  166. }
  167. result = self.safe_exec(output, context=context)
  168. self.assertIn("Migration", result)
  169. # pylint: disable=exec-used
  170. def safe_exec(self, string, value=None, context=None):
  171. loc = {}
  172. g = globals()
  173. g.update(context)
  174. try:
  175. exec(string, g, loc)
  176. except Exception as e:
  177. if value:
  178. self.fail(
  179. "Could not exec %r (from value %r): %s" % (string.strip(), value, e)
  180. )
  181. else:
  182. self.fail("Could not exec %r: %s" % (string.strip(), e))
  183. return loc