From 7614edab7f6c7b67fa66b30451bc9739b84b081b Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Thu, 28 Jan 2021 18:10:32 +0100 Subject: [PATCH 1/2] institutions: Fix tag sorting in autocomplete --- feder/institutions/factories.py | 9 +++++++++ feder/institutions/tests.py | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/feder/institutions/factories.py b/feder/institutions/factories.py index b5732df72..105d5f86c 100644 --- a/feder/institutions/factories.py +++ b/feder/institutions/factories.py @@ -12,6 +12,15 @@ class InstitutionFactory(factory.django.DjangoModelFactory): class Meta: model = Institution + @factory.post_generation + def tags(self, create, extracted, **kwargs): + if not create: + # Simple build, do nothing. + return + + if extracted: + for tag in extracted: + self.tags.add(tag) class TagFactory(factory.django.DjangoModelFactory): name = factory.Sequence("tag-{}".format) diff --git a/feder/institutions/tests.py b/feder/institutions/tests.py index bbdda1a83..0fe00081d 100644 --- a/feder/institutions/tests.py +++ b/feder/institutions/tests.py @@ -1,3 +1,4 @@ +import json from django.urls import reverse from django.test import RequestFactory, TestCase from django.utils.encoding import force_text @@ -177,6 +178,18 @@ def test_get_result_label_with_institution(self): response = TagAutocomplete.as_view()(request) self.assertContains(response, "123 (1)") + def test_get_sorted_by_name(self): + source_names = ["a2", "a3", "a1"] + tags = [TagFactory(name=name) for name in source_names] + [InstitutionFactory.create_batch(tags=[tag], size=count) for count, tag in enumerate(tags)] + request = self.factory.get("/customer/details") + response = TagAutocomplete.as_view()(request) + body = json.loads(response.content) + + expected_names = sorted(source_names) + result_names = [x["text"].split(' ')[0] for x in body['results']] + + self.assertListEqual(expected_names, result_names) class SitemapTestCase(ObjectMixin, TestCase): def test_institutions(self): From 11441a3c57c9e6526a5eec837c1e5a8d7d219085 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 28 Jan 2021 17:11:19 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- feder/institutions/factories.py | 1 + feder/institutions/tests.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/feder/institutions/factories.py b/feder/institutions/factories.py index 105d5f86c..a6a464546 100644 --- a/feder/institutions/factories.py +++ b/feder/institutions/factories.py @@ -22,6 +22,7 @@ def tags(self, create, extracted, **kwargs): for tag in extracted: self.tags.add(tag) + class TagFactory(factory.django.DjangoModelFactory): name = factory.Sequence("tag-{}".format) diff --git a/feder/institutions/tests.py b/feder/institutions/tests.py index 0fe00081d..d39ad6e69 100644 --- a/feder/institutions/tests.py +++ b/feder/institutions/tests.py @@ -181,16 +181,20 @@ def test_get_result_label_with_institution(self): def test_get_sorted_by_name(self): source_names = ["a2", "a3", "a1"] tags = [TagFactory(name=name) for name in source_names] - [InstitutionFactory.create_batch(tags=[tag], size=count) for count, tag in enumerate(tags)] + [ + InstitutionFactory.create_batch(tags=[tag], size=count) + for count, tag in enumerate(tags) + ] request = self.factory.get("/customer/details") response = TagAutocomplete.as_view()(request) body = json.loads(response.content) expected_names = sorted(source_names) - result_names = [x["text"].split(' ')[0] for x in body['results']] + result_names = [x["text"].split(" ")[0] for x in body["results"]] self.assertListEqual(expected_names, result_names) + class SitemapTestCase(ObjectMixin, TestCase): def test_institutions(self): url = reverse("sitemaps", kwargs={"section": "institutions"})