diff --git a/feder/institutions/factories.py b/feder/institutions/factories.py index b5732df72..a6a464546 100644 --- a/feder/institutions/factories.py +++ b/feder/institutions/factories.py @@ -12,6 +12,16 @@ 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..d39ad6e69 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,22 @@ 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):