File: tests.py

package info (click to toggle)
python-django 1.8.18-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 41,628 kB
  • sloc: python: 189,488; xml: 695; makefile: 194; sh: 169; sql: 11
file content (45 lines) | stat: -rw-r--r-- 1,556 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from django.core.management.color import no_style
from django.core.management.sql import custom_sql_for_model
from django.db import DEFAULT_DB_ALIAS, connections
from django.test import TestCase, override_settings

from .models import Simple


class InitialSQLTests(TestCase):
    """
    The format of the included SQL file for this test suite is important.
    It must end with a trailing newline in order to test the fix for #2161.
    """

    def test_initial_sql(self):
        """
        As pointed out by #14661, test data loaded by custom SQL
        can't be relied upon; as a result, the test framework flushes the
        data contents before every test. This test validates that this has
        occurred.
        """
        self.assertEqual(Simple.objects.count(), 0)

    def test_custom_sql(self):
        """
        Simulate the custom SQL loading by migrate.
        """
        connection = connections[DEFAULT_DB_ALIAS]
        custom_sql = custom_sql_for_model(Simple, no_style(), connection)
        with connection.cursor() as cursor:
            for sql in custom_sql:
                cursor.execute(sql)
        self.assertEqual(Simple.objects.count(), 9)
        self.assertEqual(
            Simple.objects.get(name__contains='placeholders').name,
            '"100%" of % are not placeholders'
        )

    @override_settings(DEBUG=True)
    def test_custom_sql_debug(self):
        """
        Same test, ensure that CursorDebugWrapper doesn't alter sql loading
        (#3485).
        """
        self.test_custom_sql()