From f3855a8d2d9eee7f82e412a69bf42ad2df867462 Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Wed, 23 Oct 2019 10:43:47 +0200 Subject: [PATCH] Added tests for Field.get_choices()'s limit_choices_to argument. --- tests/model_fields/tests.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index abc5273d90..206ac8c84c 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -266,3 +266,26 @@ class GetChoicesOrderingTests(TestCase): self.field.remote_field.get_choices(include_blank=False), [self.bar2, self.bar1] ) + + +class GetChoicesLimitChoicesToTests(TestCase): + @classmethod + def setUpTestData(cls): + cls.foo1 = Foo.objects.create(a='a', d='12.34') + cls.foo2 = Foo.objects.create(a='b', d='12.34') + cls.bar1 = Bar.objects.create(a=cls.foo1, b='b') + cls.bar2 = Bar.objects.create(a=cls.foo2, b='a') + cls.field = Bar._meta.get_field('a') + + def assertChoicesEqual(self, choices, objs): + self.assertEqual(choices, [(obj.pk, str(obj)) for obj in objs]) + + def test_get_choices(self): + self.assertChoicesEqual( + self.field.get_choices(include_blank=False, limit_choices_to={'a': 'a'}), + [self.foo1], + ) + self.assertChoicesEqual( + self.field.get_choices(include_blank=False, limit_choices_to={}), + [self.foo1, self.foo2], + )