diff options
| author | Hasan Ramezani <hasan.r67@gmail.com> | 2019-11-07 15:35:33 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-11-08 10:59:24 +0100 |
| commit | 153c7956f84ef4bd203e750f8669f70b6f7fa3f7 (patch) | |
| tree | a153af9b3d98e9da47bebc843e07b48ae41a2b63 /tests/postgres_tests | |
| parent | 8058d9d7adb189fec75a4b57565f225996c7b22c (diff) | |
Fixed #24858 -- Added support for get_FOO_display() to ArrayField and RangeFields.
_get_FIELD_display() crashed when Field.choices was unhashable.
Diffstat (limited to 'tests/postgres_tests')
| -rw-r--r-- | tests/postgres_tests/test_array.py | 47 | ||||
| -rw-r--r-- | tests/postgres_tests/test_ranges.py | 25 |
2 files changed, 72 insertions, 0 deletions
diff --git a/tests/postgres_tests/test_array.py b/tests/postgres_tests/test_array.py index 7b7793f6c1..481d93f830 100644 --- a/tests/postgres_tests/test_array.py +++ b/tests/postgres_tests/test_array.py @@ -37,6 +37,53 @@ except ImportError: pass +@isolate_apps('postgres_tests') +class BasicTests(PostgreSQLSimpleTestCase): + def test_get_field_display(self): + class MyModel(PostgreSQLModel): + field = ArrayField( + models.CharField(max_length=16), + choices=[ + ['Media', [(['vinyl', 'cd'], 'Audio')]], + (('mp3', 'mp4'), 'Digital'), + ], + ) + + tests = ( + (['vinyl', 'cd'], 'Audio'), + (('mp3', 'mp4'), 'Digital'), + (('a', 'b'), "('a', 'b')"), + (['c', 'd'], "['c', 'd']"), + ) + for value, display in tests: + with self.subTest(value=value, display=display): + instance = MyModel(field=value) + self.assertEqual(instance.get_field_display(), display) + + def test_get_field_display_nested_array(self): + class MyModel(PostgreSQLModel): + field = ArrayField( + ArrayField(models.CharField(max_length=16)), + choices=[ + [ + 'Media', + [([['vinyl', 'cd'], ('x',)], 'Audio')], + ], + ((['mp3'], ('mp4',)), 'Digital'), + ], + ) + tests = ( + ([['vinyl', 'cd'], ('x',)], 'Audio'), + ((['mp3'], ('mp4',)), 'Digital'), + ((('a', 'b'), ('c',)), "(('a', 'b'), ('c',))"), + ([['a', 'b'], ['c']], "[['a', 'b'], ['c']]"), + ) + for value, display in tests: + with self.subTest(value=value, display=display): + instance = MyModel(field=value) + self.assertEqual(instance.get_field_display(), display) + + class TestSaveLoad(PostgreSQLTestCase): def test_integer(self): diff --git a/tests/postgres_tests/test_ranges.py b/tests/postgres_tests/test_ranges.py index b68f22112f..326015e46d 100644 --- a/tests/postgres_tests/test_ranges.py +++ b/tests/postgres_tests/test_ranges.py @@ -7,6 +7,7 @@ from django.core import exceptions, serializers from django.db.models import DateField, DateTimeField, F, Func, Value from django.http import QueryDict from django.test import override_settings +from django.test.utils import isolate_apps from django.utils import timezone from . import PostgreSQLSimpleTestCase, PostgreSQLTestCase @@ -22,6 +23,30 @@ except ImportError: pass +@isolate_apps('postgres_tests') +class BasicTests(PostgreSQLSimpleTestCase): + def test_get_field_display(self): + class Model(PostgreSQLModel): + field = pg_fields.IntegerRangeField( + choices=[ + ['1-50', [((1, 25), '1-25'), ([26, 50], '26-50')]], + ((51, 100), '51-100'), + ], + ) + + tests = ( + ((1, 25), '1-25'), + ([26, 50], '26-50'), + ((51, 100), '51-100'), + ((1, 2), '(1, 2)'), + ([1, 2], '[1, 2]'), + ) + for value, display in tests: + with self.subTest(value=value, display=display): + instance = Model(field=value) + self.assertEqual(instance.get_field_display(), display) + + class TestSaveLoad(PostgreSQLTestCase): def test_all_fields(self): |
