test_graph.py 8.1 KB
Newer Older
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
import os
import pathlib
import pickle
import warnings
import zlib

import pytest
from click.testing import CliRunner

from treevalue import FastTreeValue, dump, graphics
from treevalue.entry.cli import treevalue_cli

t1 = FastTreeValue({'a': 1, 'b': 2, 'x': {'c': 3, 'd': 4}})
t2 = FastTreeValue({'a': 1, 'b': {2, 4}, 'x': {'c': [1, 3], 'd': 4}})
t3 = FastTreeValue({'a': 1, 'b': 2, 'x': {'c': t2.b, 'd': t2.x.c}})

g = graphics(
    (t1, 't1'), (t2, 't2'), (t3, 't3'),
    title='This is title for g.',
    cfg=dict(bgcolor='#ffffff00'),
    dup_value=(list,),
)


@pytest.mark.unittest
class TestEntryCliGraph:
    def test_simple_code_graph(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            result = runner.invoke(
                treevalue_cli,
                args=['graph', '-t', 'test.entry.cli.test_graph.t1',
                      '-o', 'test_graph.svg', '-o', 'test_graph.gv'],
            )

            assert result.exit_code == 0
            assert os.path.exists('test_graph.svg')
HansBug's avatar
HansBug 已提交
38
            assert 5000 <= os.path.getsize('test_graph.svg') <= 7000
39
            assert os.path.exists('test_graph.gv')
HansBug's avatar
HansBug 已提交
40
            assert 1500 <= os.path.getsize('test_graph.gv') <= 2500
41 42 43 44 45 46 47 48 49 50 51 52 53 54

    def test_simple_code_graph_to_stdout(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            with pytest.warns(RuntimeWarning):
                result = runner.invoke(
                    treevalue_cli,
                    args=['graph', '-t', 'test.entry.cli.test_graph.t1',
                          '-o', 'test_graph.svg', '-o', 'test_graph.gv', '-O'],
                )

            assert result.exit_code == 0
            assert not os.path.exists('test_graph.svg')
            assert not os.path.exists('test_graph.gv')
HansBug's avatar
HansBug 已提交
55
            assert 1500 <= len(result.output) <= 2500
56 57 58 59 60 61 62 63 64 65 66

    def test_simple_code_multiple_graph(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            result = runner.invoke(
                treevalue_cli,
                args=['graph', '-t', 'test.entry.cli.test_graph.t[12]', '-o', 'test_graph.svg'],
            )

            assert result.exit_code == 0
            assert os.path.exists('test_graph.svg')
HansBug's avatar
HansBug 已提交
67
            assert 10000 <= os.path.getsize('test_graph.svg') <= 13000
68 69 70 71 72 73 74 75 76

        with runner.isolated_filesystem():
            result = runner.invoke(
                treevalue_cli,
                args=['graph', '-t', 'test.entry.cli.test_graph.*', '-o', 'test_graph.svg'],
            )

            assert result.exit_code == 0
            assert os.path.exists('test_graph.svg')
HansBug's avatar
HansBug 已提交
77
            assert 15500 <= os.path.getsize('test_graph.svg') <= 17500
78 79 80 81 82 83 84 85 86 87 88 89 90 91

    def test_simple_binary_graph(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            with open('g1.bg', 'wb') as file:
                dump(t1, file, compress=zlib)

            result = runner.invoke(
                treevalue_cli,
                args=['graph', '-t', 'g1.bg', '-o', 'test_graph.svg'],
            )

            assert result.exit_code == 0
            assert os.path.exists('test_graph.svg')
HansBug's avatar
HansBug 已提交
92
            assert 5500 <= os.path.getsize('test_graph.svg') <= 6500
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

        with runner.isolated_filesystem():
            with open('g1.bg', 'wb') as file:
                dump(t1, file, compress=zlib)
            with open('g2.bg', 'wb') as file:
                dump(t2, file, compress=zlib)
            with open('g3.bg', 'wb') as file:
                dump(t3, file, compress=zlib)

            result = runner.invoke(
                treevalue_cli,
                args=['graph', '-t', '*.bg', '-o', 'test_graph.svg'],
            )

            assert result.exit_code == 0
            assert os.path.exists('test_graph.svg')
HansBug's avatar
HansBug 已提交
109
            assert 15500 <= os.path.getsize('test_graph.svg') <= 17500
110 111 112 113 114 115 116 117 118 119 120 121

        with runner.isolated_filesystem():
            with open('test.entry.cli.test_graph.t1', 'wb') as file:
                dump(t2, file, compress=zlib)

            result = runner.invoke(
                treevalue_cli,
                args=['graph', '-t', 'test.entry.cli.test_graph.t1', '-o', 'test_graph.svg'],
            )

            assert result.exit_code == 0
            assert os.path.exists('test_graph.svg')
HansBug's avatar
HansBug 已提交
122
            assert 10000 <= os.path.getsize('test_graph.svg') <= 13000
123 124 125 126 127 128 129 130 131 132 133 134

        with runner.isolated_filesystem():
            with open('test.entry.cli.test_graph.t1', 'wb') as file:
                pickle.dump([1, 2, 3], file)

            result = runner.invoke(
                treevalue_cli,
                args=['graph', '-t', 'test.entry.cli.test_graph.t1', '-o', 'test_graph.svg'],
            )

            assert result.exit_code == 0
            assert os.path.exists('test_graph.svg')
HansBug's avatar
HansBug 已提交
135
            assert 5500 <= os.path.getsize('test_graph.svg') <= 6500
136 137 138 139 140 141 142 143 144 145 146 147

    def test_duplicates(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            result = runner.invoke(
                treevalue_cli,
                args=['graph', '-t', 'test.entry.cli.test_graph.t[23]', '-D',
                      '-o', 'test_graph.svg'],
            )

            assert result.exit_code == 0
            assert os.path.exists('test_graph.svg')
HansBug's avatar
HansBug 已提交
148
            assert 8000 <= os.path.getsize('test_graph.svg') <= 12000
149

HansBug's avatar
HansBug 已提交
150
        _p = os.path.abspath(os.curdir)
151 152 153 154 155 156 157 158 159 160
        with runner.isolated_filesystem():
            result = runner.invoke(
                treevalue_cli,
                args=['graph', '-t', 'test.entry.cli.test_graph.t[23]',
                      '-d', 'list', '-d', 'set',
                      '-o', 'test_graph.svg'],
            )

            assert result.exit_code == 0
            assert os.path.exists('test_graph.svg')
HansBug's avatar
HansBug 已提交
161 162
            import shutil
            shutil.copy('test_graph.svg', os.path.join(_p, 'test_graph.svg'))
HansBug's avatar
HansBug 已提交
163
            assert 10000 <= os.path.getsize('test_graph.svg') <= 11000
164 165 166 167 168 169 170 171 172 173 174 175 176

    def test_graph(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            with pytest.warns(RuntimeWarning):
                result = runner.invoke(
                    treevalue_cli,
                    args=['graph', '-g', 'test.entry.cli.test_graph.g',
                          '-t', 'first title', '-o', 'test_graph.svg'],
                )

            assert result.exit_code == 0
            assert os.path.exists('test_graph.svg')
HansBug's avatar
HansBug 已提交
177
            assert 14000 <= os.path.getsize('test_graph.svg') <= 16500
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

            content = pathlib.Path('test_graph.svg').read_text()
            assert 'first title' not in content
            assert 'This is title for g.' in content

    def test_cfg(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            result = runner.invoke(
                treevalue_cli,
                args=['graph', '-t', 'test.entry.cli.test_graph.*',
                      '-c', 'bgcolor=#ffffff00', '-O'],
            )

            assert result.exit_code == 0
HansBug's avatar
HansBug 已提交
193
            assert 5000 <= len(result.output) <= 6000
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
            assert '#ffffff00' in result.output

        with runner.isolated_filesystem():
            result = runner.invoke(
                treevalue_cli,
                args=['graph', '-t', 'test.entry.cli.test_graph.*',
                      '-c', 'bgcolor#ffffff00', '-O'],
            )

            assert result.exit_code != 0
            assert "Configuration should be KEY=VALUE, but 'bgcolor#ffffff00' found." in result.output

    def test_file_with_invalid_permission(self):
        runner = CliRunner()
        with runner.isolated_filesystem():
            with open('g1.bg', 'wb') as file:
                dump(t1, file, compress=zlib)

            try:
                os.chmod('g1.bg', int('000', base=8))
            except PermissionError:
                warnings.warn(RuntimeWarning('Permission denied when changing the permission, skip this test.'))
            else:
                result = runner.invoke(
                    treevalue_cli,
                    args=['graph', '-t', 'g1.bg', '-o', 'test_graph.svg'],
                )

                assert result.exit_code == 0
                assert os.path.exists('test_graph.svg')
HansBug's avatar
HansBug 已提交
224
                assert 500 <= os.path.getsize('test_graph.svg') <= 1000