Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 21, 2022
1 parent 90054b2 commit 3cf2340
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
14 changes: 7 additions & 7 deletions categories/migrations/0005_unique_category_slug.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@


def make_slugs_unique(apps, schema_editor):
Category = apps.get_model('categories', 'Category')
duplicates = Category.tree.values('slug').annotate(slug_count=models.Count('slug')).filter(slug_count__gt=1)
Category = apps.get_model("categories", "Category")
duplicates = Category.tree.values("slug").annotate(slug_count=models.Count("slug")).filter(slug_count__gt=1)
for duplicate in duplicates:
slug = duplicate['slug']
slug = duplicate["slug"]
categories = Category.tree.filter(slug=slug)
count = categories.count()
i = 0
Expand All @@ -21,14 +21,14 @@ def make_slugs_unique(apps, schema_editor):
class Migration(migrations.Migration):

dependencies = [
('categories', '0004_auto_20200517_1832'),
("categories", "0004_auto_20200517_1832"),
]

operations = [
migrations.RunPython(make_slugs_unique, reverse_code=migrations.RunPython.noop),
migrations.AlterField(
model_name='category',
name='slug',
field=models.SlugField(unique=True, verbose_name='slug'),
model_name="category",
name="slug",
field=models.SlugField(unique=True, verbose_name="slug"),
),
]
53 changes: 26 additions & 27 deletions categories/tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
import sys


if (sys.version_info >= (3, 0)):
if sys.version_info >= (3, 0):
from django_test_migrations.contrib.unittest_case import MigratorTestCase

class TestMigrations(MigratorTestCase):

migrate_from = ('categories', '0004_auto_20200517_1832')
migrate_to = ('categories', '0005_unique_category_slug')
migrate_from = ("categories", "0004_auto_20200517_1832")
migrate_to = ("categories", "0005_unique_category_slug")

def prepare(self):
Category = self.old_state.apps.get_model('categories', 'Category')
Category.tree.create(slug='foo', lft=0, rght=0, tree_id=0, level=0)
Category.tree.create(slug='foo', lft=0, rght=0, tree_id=0, level=0)
Category.tree.create(slug='foo', lft=0, rght=0, tree_id=0, level=0)
Category = self.old_state.apps.get_model("categories", "Category")
Category.tree.create(slug="foo", lft=0, rght=0, tree_id=0, level=0)
Category.tree.create(slug="foo", lft=0, rght=0, tree_id=0, level=0)
Category.tree.create(slug="foo", lft=0, rght=0, tree_id=0, level=0)
for i in range(1, 12):
Category.tree.create(slug='bar', lft=0, rght=0, tree_id=0, level=0)
Category.tree.create(slug='baz', lft=0, rght=0, tree_id=0, level=0)
Category.tree.create(slug="bar", lft=0, rght=0, tree_id=0, level=0)
Category.tree.create(slug="baz", lft=0, rght=0, tree_id=0, level=0)
assert Category.tree.count() == 15

def test_unique_slug_migration(self):
Category = self.new_state.apps.get_model('categories', 'Category')
Category = self.new_state.apps.get_model("categories", "Category")

self.assertListEqual(
list(Category.tree.values_list('slug', flat=True)),
list(Category.tree.values_list("slug", flat=True)),
[
'foo',
'foo_1',
'foo_2',
'bar',
'bar_01',
'bar_02',
'bar_03',
'bar_04',
'bar_05',
'bar_06',
'bar_07',
'bar_08',
'bar_09',
'bar_10',
'baz',
"foo",
"foo_1",
"foo_2",
"bar",
"bar_01",
"bar_02",
"bar_03",
"bar_04",
"bar_05",
"bar_06",
"bar_07",
"bar_08",
"bar_09",
"bar_10",
"baz",
],
)
2 changes: 1 addition & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-r test.txt

bump2version>=1.0.1
git-fame>=1.12.2
generate-changelog
git-fame>=1.12.2
pre-commit

0 comments on commit 3cf2340

Please sign in to comment.