1. import unittest
    
  2. 
    
  3. from django.core.management.color import no_style
    
  4. from django.db import connection
    
  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.         )