2020-04-15 17:20:46 +08:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from django.core.management.color import no_style
|
|
|
|
from django.db import connection
|
2023-04-18 16:19:06 +08:00
|
|
|
from django.db.models.expressions import Col
|
|
|
|
from django.db.models.functions import Cast
|
2020-04-15 17:20:46 +08:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
2023-04-18 16:19:06 +08:00
|
|
|
from ..models import Author, Book, Person, Tag
|
2020-04-15 17:20:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL tests.")
|
|
|
|
class PostgreSQLOperationsTests(SimpleTestCase):
|
|
|
|
def test_sql_flush(self):
|
|
|
|
self.assertEqual(
|
|
|
|
connection.ops.sql_flush(
|
|
|
|
no_style(),
|
|
|
|
[Person._meta.db_table, Tag._meta.db_table],
|
|
|
|
),
|
|
|
|
['TRUNCATE "backends_person", "backends_tag";'],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_sql_flush_allow_cascade(self):
|
|
|
|
self.assertEqual(
|
|
|
|
connection.ops.sql_flush(
|
|
|
|
no_style(),
|
|
|
|
[Person._meta.db_table, Tag._meta.db_table],
|
|
|
|
allow_cascade=True,
|
|
|
|
),
|
|
|
|
['TRUNCATE "backends_person", "backends_tag" CASCADE;'],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_sql_flush_sequences(self):
|
|
|
|
self.assertEqual(
|
|
|
|
connection.ops.sql_flush(
|
|
|
|
no_style(),
|
|
|
|
[Person._meta.db_table, Tag._meta.db_table],
|
2020-04-15 17:20:46 +08:00
|
|
|
reset_sequences=True,
|
2020-04-15 17:20:46 +08:00
|
|
|
),
|
2020-04-15 17:20:46 +08:00
|
|
|
['TRUNCATE "backends_person", "backends_tag" RESTART IDENTITY;'],
|
2020-04-15 17:20:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_sql_flush_sequences_allow_cascade(self):
|
|
|
|
self.assertEqual(
|
|
|
|
connection.ops.sql_flush(
|
|
|
|
no_style(),
|
|
|
|
[Person._meta.db_table, Tag._meta.db_table],
|
2020-04-15 17:20:46 +08:00
|
|
|
reset_sequences=True,
|
2020-04-15 17:20:46 +08:00
|
|
|
allow_cascade=True,
|
|
|
|
),
|
2020-04-15 17:20:46 +08:00
|
|
|
['TRUNCATE "backends_person", "backends_tag" RESTART IDENTITY CASCADE;'],
|
2020-04-15 17:20:46 +08:00
|
|
|
)
|
2023-04-18 16:19:06 +08:00
|
|
|
|
|
|
|
def test_prepare_join_on_clause_same_type(self):
|
|
|
|
author_table = Author._meta.db_table
|
|
|
|
author_id_field = Author._meta.get_field("id")
|
|
|
|
lhs_expr, rhs_expr = connection.ops.prepare_join_on_clause(
|
|
|
|
author_table,
|
|
|
|
author_id_field,
|
|
|
|
author_table,
|
|
|
|
author_id_field,
|
|
|
|
)
|
|
|
|
self.assertEqual(lhs_expr, Col(author_table, author_id_field))
|
|
|
|
self.assertEqual(rhs_expr, Col(author_table, author_id_field))
|
|
|
|
|
|
|
|
def test_prepare_join_on_clause_different_types(self):
|
|
|
|
author_table = Author._meta.db_table
|
|
|
|
author_id_field = Author._meta.get_field("id")
|
|
|
|
book_table = Book._meta.db_table
|
|
|
|
book_fk_field = Book._meta.get_field("author")
|
|
|
|
lhs_expr, rhs_expr = connection.ops.prepare_join_on_clause(
|
|
|
|
author_table,
|
|
|
|
author_id_field,
|
|
|
|
book_table,
|
|
|
|
book_fk_field,
|
|
|
|
)
|
|
|
|
self.assertEqual(lhs_expr, Col(author_table, author_id_field))
|
|
|
|
self.assertEqual(
|
|
|
|
rhs_expr, Cast(Col(book_table, book_fk_field), author_id_field)
|
|
|
|
)
|