diff options
| author | Simon Charette <charette.s@gmail.com> | 2019-03-16 13:48:59 -0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-03-18 09:05:39 +0100 |
| commit | 386d89ab55e620440d30590a8a104fe6d5eef830 (patch) | |
| tree | 2b93659160a172f6d469e8b7c7588174e6a2607a /tests/postgres_tests | |
| parent | 5c17c273ae2d7274f1fa78218b3b74690efddb86 (diff) | |
Fixed #30258 -- Adjusted postgres schema value quoting of ranges.
Thanks Tilman Koschnick for the report and patch.
Diffstat (limited to 'tests/postgres_tests')
| -rw-r--r-- | tests/postgres_tests/test_constraints.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/postgres_tests/test_constraints.py b/tests/postgres_tests/test_constraints.py new file mode 100644 index 0000000000..0e09a1c546 --- /dev/null +++ b/tests/postgres_tests/test_constraints.py @@ -0,0 +1,35 @@ +from django.db import connection, transaction +from django.db.models import Q +from django.db.models.constraints import CheckConstraint +from django.db.utils import IntegrityError + +from . import PostgreSQLTestCase +from .models import RangesModel + +try: + from psycopg2.extras import NumericRange +except ImportError: + pass + + +class SchemaTests(PostgreSQLTestCase): + def get_constraints(self, table): + """Get the constraints on the table using a new cursor.""" + with connection.cursor() as cursor: + return connection.introspection.get_constraints(cursor, table) + + def test_check_constraint_range_value(self): + constraint_name = 'ints_between' + self.assertNotIn(constraint_name, self.get_constraints(RangesModel._meta.db_table)) + constraint = CheckConstraint( + check=Q(ints__contained_by=NumericRange(10, 30)), + name=constraint_name, + ) + with connection.schema_editor() as editor: + editor.add_constraint(RangesModel, constraint) + with connection.cursor() as cursor: + constraints = connection.introspection.get_constraints(cursor, RangesModel._meta.db_table) + self.assertIn(constraint_name, constraints) + with self.assertRaises(IntegrityError), transaction.atomic(): + RangesModel.objects.create(ints=(20, 50)) + RangesModel.objects.create(ints=(10, 30)) |
