| 1 |
import unittest |
| 2 |
|
| 3 |
from django.core.management.color import no_style |
| 4 |
from django.db import connection |
| 5 |
from django.db.models.expressions import Col |
| 6 |
from django.db.models.functions import Cast |
| 7 |
from django.test import SimpleTestCase |
| 8 |
|
| 9 |
from ..models import Author, Book, Person, Tag |
| 10 |
|
| 11 |
|
| 12 |
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL tests.") |
| 13 |
class PostgreSQLOperationsTests(SimpleTestCase): |
| 14 |
def test_sql_flush(self): |
| 15 |
self.assertEqual( |
| 16 |
connection.ops.sql_flush( |
| 17 |
no_style(), |
| 18 |
[Person._meta.db_table, Tag._meta.db_table], |
| 19 |
), |
| 20 |
['TRUNCATE "backends_person", "backends_tag";'], |
| 21 |
) |
| 22 |
|
| 23 |
def test_sql_flush_allow_cascade(self): |
| 24 |
self.assertEqual( |
| 25 |
connection.ops.sql_flush( |
| 26 |
no_style(), |
| 27 |
[Person._meta.db_table, Tag._meta.db_table], |
| 28 |
allow_cascade=True, |
| 29 |
), |
| 30 |
['TRUNCATE "backends_person", "backends_tag" CASCADE;'], |
| 31 |
) |
| 32 |
|
| 33 |
def test_sql_flush_sequences(self): |
| 34 |
self.assertEqual( |
| 35 |
connection.ops.sql_flush( |
| 36 |
no_style(), |
| 37 |
[Person._meta.db_table, Tag._meta.db_table], |
| 38 |
reset_sequences=True, |
| 39 |
), |
| 40 |
['TRUNCATE "backends_person", "backends_tag" RESTART IDENTITY;'], |
| 41 |
) |
| 42 |
|
| 43 |
def test_sql_flush_sequences_allow_cascade(self): |
| 44 |
self.assertEqual( |
| 45 |
connection.ops.sql_flush( |
| 46 |
no_style(), |
| 47 |
[Person._meta.db_table, Tag._meta.db_table], |
| 48 |
reset_sequences=True, |
| 49 |
allow_cascade=True, |
| 50 |
), |
| 51 |
['TRUNCATE "backends_person", "backends_tag" RESTART IDENTITY CASCADE;'], |
| 52 |
) |
| 53 |
|
| 54 |
def test_prepare_join_on_clause_same_type(self): |
| 55 |
author_table = Author._meta.db_table |
| 56 |
author_id_field = Author._meta.get_field("id") |
| 57 |
lhs_expr, rhs_expr = connection.ops.prepare_join_on_clause( |
| 58 |
author_table, |
| 59 |
author_id_field, |
| 60 |
author_table, |
| 61 |
author_id_field, |
| 62 |
) |
| 63 |
self.assertEqual(lhs_expr, Col(author_table, author_id_field)) |
| 64 |
self.assertEqual(rhs_expr, Col(author_table, author_id_field)) |
| 65 |
|
| 66 |
def test_prepare_join_on_clause_different_types(self): |
| 67 |
author_table = Author._meta.db_table |
| 68 |
author_id_field = Author._meta.get_field("id") |
| 69 |
book_table = Book._meta.db_table |
| 70 |
book_fk_field = Book._meta.get_field("author") |
| 71 |
lhs_expr, rhs_expr = connection.ops.prepare_join_on_clause( |
| 72 |
author_table, |
| 73 |
author_id_field, |
| 74 |
book_table, |
| 75 |
book_fk_field, |
| 76 |
) |
| 77 |
self.assertEqual(lhs_expr, Col(author_table, author_id_field)) |
| 78 |
self.assertEqual( |
| 79 |
rhs_expr, Cast(Col(book_table, book_fk_field), author_id_field) |
| 80 |
) |