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 (128 lines) | stat: -rw-r--r-- 4,323 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from __future__ import unicode_literals

import warnings

from django.core import management
from django.db import transaction
from django.test import TestCase, TransactionTestCase
from django.utils.six import StringIO

from .models import Article, Book


class SampleTestCase(TestCase):
    fixtures = ['fixture1.json', 'fixture2.json']

    def testClassFixtures(self):
        "Test cases can load fixture objects into models defined in packages"
        self.assertEqual(Article.objects.count(), 3)
        self.assertQuerysetEqual(
            Article.objects.all(), [
                "Django conquers world!",
                "Copyright is fine the way it is",
                "Poker has no place on ESPN",
            ],
            lambda a: a.headline
        )


class TestNoInitialDataLoading(TransactionTestCase):

    available_apps = ['fixtures_model_package']

    def test_migrate(self):
        with transaction.atomic():
            Book.objects.all().delete()

            management.call_command(
                'migrate',
                verbosity=0,
                load_initial_data=False
            )
            self.assertQuerysetEqual(Book.objects.all(), [])

    def test_flush(self):
        # Test presence of fixture (flush called by TransactionTestCase)
        self.assertQuerysetEqual(
            Book.objects.all(), [
                'Achieving self-awareness of Python programs'
            ],
            lambda a: a.name
        )

        with transaction.atomic():
            management.call_command(
                'flush',
                verbosity=0,
                interactive=False,
                load_initial_data=False
            )
            self.assertQuerysetEqual(Book.objects.all(), [])


class FixtureTestCase(TestCase):
    def test_initial_data(self):
        "Fixtures can load initial data into models defined in packages"
        # migrate introduces 1 initial data object from initial_data.json
        # this behavior is deprecated and will be removed in Django 1.9
        self.assertQuerysetEqual(
            Book.objects.all(), [
                'Achieving self-awareness of Python programs'
            ],
            lambda a: a.name
        )

    def test_loaddata(self):
        "Fixtures can load data into models defined in packages"
        # Load fixture 1. Single JSON file, with two objects
        management.call_command("loaddata", "fixture1.json", verbosity=0)
        self.assertQuerysetEqual(
            Article.objects.all(), [
                "Time to reform copyright",
                "Poker has no place on ESPN",
            ],
            lambda a: a.headline,
        )

        # Load fixture 2. JSON file imported by default. Overwrites some
        # existing objects
        management.call_command("loaddata", "fixture2.json", verbosity=0)
        self.assertQuerysetEqual(
            Article.objects.all(), [
                "Django conquers world!",
                "Copyright is fine the way it is",
                "Poker has no place on ESPN",
            ],
            lambda a: a.headline,
        )

        # Load a fixture that doesn't exist
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            management.call_command("loaddata", "unknown.json", verbosity=0)
        self.assertEqual(len(w), 1)
        self.assertTrue(w[0].message, "No fixture named 'unknown' found.")

        self.assertQuerysetEqual(
            Article.objects.all(), [
                "Django conquers world!",
                "Copyright is fine the way it is",
                "Poker has no place on ESPN",
            ],
            lambda a: a.headline,
        )


class InitialSQLTests(TestCase):

    def test_custom_sql(self):
        """
        #14300 -- Verify that custom_sql_for_model searches `app/sql` and not
        `app/models/sql` (the old location will work until Django 1.9)
        """
        out = StringIO()
        management.call_command("sqlcustom", "fixtures_model_package", stdout=out)
        output = out.getvalue()
        self.assertIn("INSERT INTO fixtures_model_package_book (name) VALUES ('My Book')", output)
        # value from deprecated search path models/sql (remove in Django 1.9)
        self.assertIn("Deprecated Book", output)