選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

autoslugfield_tests.py 5.9KB

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