| 1 |
import unittest |
| 2 |
|
| 3 |
from django.core.management.color import no_style |
| 4 |
from django.db import connection, models |
| 5 |
from django.test import TestCase |
| 6 |
|
| 7 |
from ..models import Person, Tag |
| 8 |
|
| 9 |
|
| 10 |
@unittest.skipUnless(connection.vendor == "sqlite", "SQLite tests.") |
| 11 |
class SQLiteOperationsTests(TestCase): |
| 12 |
def test_sql_flush(self): |
| 13 |
self.assertEqual( |
| 14 |
connection.ops.sql_flush( |
| 15 |
no_style(), |
| 16 |
[Person._meta.db_table, Tag._meta.db_table], |
| 17 |
), |
| 18 |
[ |
| 19 |
'DELETE FROM "backends_person";', |
| 20 |
'DELETE FROM "backends_tag";', |
| 21 |
], |
| 22 |
) |
| 23 |
|
| 24 |
def test_sql_flush_allow_cascade(self): |
| 25 |
statements = connection.ops.sql_flush( |
| 26 |
no_style(), |
| 27 |
[Person._meta.db_table, Tag._meta.db_table], |
| 28 |
allow_cascade=True, |
| 29 |
) |
| 30 |
self.assertEqual( |
| 31 |
# The tables are processed in an unordered set. |
| 32 |
sorted(statements), |
| 33 |
[ |
| 34 |
'DELETE FROM "backends_person";', |
| 35 |
'DELETE FROM "backends_tag";', |
| 36 |
'DELETE FROM "backends_verylongmodelnamezzzzzzzzzzzzzzzzzzzzzz' |
| 37 |
"zzzzzzzzzzzzzzzzzzzz_m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzz" |
| 38 |
'zzzzzzzzzzzzzzzzzzzzzzz";', |
| 39 |
], |
| 40 |
) |
| 41 |
|
| 42 |
def test_sql_flush_sequences(self): |
| 43 |
self.assertEqual( |
| 44 |
connection.ops.sql_flush( |
| 45 |
no_style(), |
| 46 |
[Person._meta.db_table, Tag._meta.db_table], |
| 47 |
reset_sequences=True, |
| 48 |
), |
| 49 |
[ |
| 50 |
'DELETE FROM "backends_person";', |
| 51 |
'DELETE FROM "backends_tag";', |
| 52 |
'UPDATE "sqlite_sequence" SET "seq" = 0 WHERE "name" IN ' |
| 53 |
"('backends_person', 'backends_tag');", |
| 54 |
], |
| 55 |
) |
| 56 |
|
| 57 |
def test_sql_flush_sequences_allow_cascade(self): |
| 58 |
statements = connection.ops.sql_flush( |
| 59 |
no_style(), |
| 60 |
[Person._meta.db_table, Tag._meta.db_table], |
| 61 |
reset_sequences=True, |
| 62 |
allow_cascade=True, |
| 63 |
) |
| 64 |
self.assertEqual( |
| 65 |
# The tables are processed in an unordered set. |
| 66 |
sorted(statements[:-1]), |
| 67 |
[ |
| 68 |
'DELETE FROM "backends_person";', |
| 69 |
'DELETE FROM "backends_tag";', |
| 70 |
'DELETE FROM "backends_verylongmodelnamezzzzzzzzzzzzzzzzzzzzzz' |
| 71 |
"zzzzzzzzzzzzzzzzzzzz_m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzz" |
| 72 |
'zzzzzzzzzzzzzzzzzzzzzzz";', |
| 73 |
], |
| 74 |
) |
| 75 |
self.assertIs( |
| 76 |
statements[-1].startswith( |
| 77 |
'UPDATE "sqlite_sequence" SET "seq" = 0 WHERE "name" IN (' |
| 78 |
), |
| 79 |
True, |
| 80 |
) |
| 81 |
self.assertIn("'backends_person'", statements[-1]) |
| 82 |
self.assertIn("'backends_tag'", statements[-1]) |
| 83 |
self.assertIn( |
| 84 |
"'backends_verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" |
| 85 |
"zzzz_m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" |
| 86 |
"zzz'", |
| 87 |
statements[-1], |
| 88 |
) |
| 89 |
|
| 90 |
def test_bulk_batch_size(self): |
| 91 |
self.assertEqual(connection.ops.bulk_batch_size([], [Person()]), 1) |
| 92 |
first_name_field = Person._meta.get_field("first_name") |
| 93 |
last_name_field = Person._meta.get_field("last_name") |
| 94 |
self.assertEqual( |
| 95 |
connection.ops.bulk_batch_size([first_name_field], [Person()]), 500 |
| 96 |
) |
| 97 |
self.assertEqual( |
| 98 |
connection.ops.bulk_batch_size( |
| 99 |
[first_name_field, last_name_field], [Person()] |
| 100 |
), |
| 101 |
connection.features.max_query_params // 2, |
| 102 |
) |
| 103 |
composite_pk = models.CompositePrimaryKey("first_name", "last_name") |
| 104 |
composite_pk.fields = [first_name_field, last_name_field] |
| 105 |
self.assertEqual( |
| 106 |
connection.ops.bulk_batch_size( |
| 107 |
[composite_pk, first_name_field], [Person()] |
| 108 |
), |
| 109 |
connection.features.max_query_params // 3, |
| 110 |
) |