File: test_dumpscript.py

package info (click to toggle)
python-django-extensions 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,820 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (112 lines) | stat: -rw-r--r-- 3,727 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
# -*- coding: utf-8 -*-
import ast
import pytest
import shutil
import sys

from io import StringIO
from pathlib import Path

from django.core.management import call_command
from django.test import TestCase, override_settings

from .testapp.models import Name, Note, Person, Club


class DumpScriptTests(TestCase):
    def setUp(self):
        sys.stdout = StringIO()
        sys.stderr = StringIO()

    @pytest.mark.xfail
    def test_runs(self):
        # lame test...does it run?
        n = Name(name="Gabriel")
        n.save()
        call_command("dumpscript", "django_extensions")
        self.assertTrue("Gabriel" in sys.stdout.getvalue())

    @pytest.mark.xfail
    def test_replaced_stdout(self):
        # check if stdout can be replaced
        sys.stdout = StringIO()
        n = Name(name="Mike")
        n.save()
        tmp_out = StringIO()
        call_command("dumpscript", "django_extensions", stdout=tmp_out)
        self.assertTrue("Mike" in tmp_out.getvalue())  # script should go to tmp_out
        self.assertEqual(
            0, len(sys.stdout.getvalue())
        )  # there should not be any output to sys.stdout
        tmp_out.close()

    @pytest.mark.xfail
    def test_replaced_stderr(self):
        # check if stderr can be replaced, without changing stdout
        n = Name(name="Fred")
        n.save()
        tmp_err = StringIO()
        sys.stderr = StringIO()
        call_command("dumpscript", "django_extensions", stderr=tmp_err)
        self.assertTrue(
            "Fred" in sys.stdout.getvalue()
        )  # script should still go to stdout
        self.assertTrue(
            "Name" in tmp_err.getvalue()
        )  # error output should go to tmp_err
        self.assertEqual(
            0, len(sys.stderr.getvalue())
        )  # there should not be any output to sys.stderr
        tmp_err.close()

    @pytest.mark.xfail
    def test_valid_syntax(self):
        n1 = Name(name="John")
        n1.save()
        p1 = Person(name=n1, age=40)
        p1.save()
        n2 = Name(name="Jane")
        n2.save()
        p2 = Person(name=n2, age=18)
        p2.save()
        p2.children.add(p1)
        note1 = Note(note="This is the first note.")
        note1.save()
        note2 = Note(note="This is the second note.")
        note2.save()
        p2.notes.add(note1, note2)
        tmp_out = StringIO()
        call_command("dumpscript", "django_extensions", stdout=tmp_out)
        ast_syntax_tree = ast.parse(tmp_out.getvalue())
        if hasattr(ast_syntax_tree, "body"):
            self.assertTrue(len(ast_syntax_tree.body) > 1)
        else:
            self.assertTrue(len(ast_syntax_tree.asList()) > 1)
        tmp_out.close()

    @override_settings(TIME_ZONE="Asia/Seoul")
    @pytest.mark.xfail
    def test_with_datetimefield(self):
        django = Club.objects.create(name="Club Django")
        Note.objects.create(
            note="Django Tips",
            club=django,
        )

        dumpscript_path = Path(__file__).parent.parent / "django_extensions" / "scripts"
        dumpscript_path.mkdir(parents=True, exist_ok=True)

        # Delete dumpscript at the end of the test
        self.addCleanup(shutil.rmtree, dumpscript_path)

        # This script will have a dateutil codes.
        # e.g. importer.locate_object(...,
        # 'date_joined': dateutil.parser.parse("2019-05-20T03:32:27.144586+09:00")
        with (dumpscript_path / "test.py").open("wt") as test:
            call_command("dumpscript", "django_extensions", stdout=test)

        # Check dumpscript without exception
        call_command("runscript", "test")

        # Check if Note is duplicated
        self.assertEqual(Note.objects.filter(note="Django Tips").count(), 2)