Completed
Push — master ( ce9cc4...380669 )
by Chris
01:06
created

test_make_dotfile_skip_empty()   A

Complexity

Conditions 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
dl 0
loc 9
rs 9.2
c 1
b 0
f 1
1
from uuid import uuid1
2
3
import pytest
4
from click.testing import CliRunner
5
6
from flask_jsondash.data_utils import filetree_digraph
7
8
9
def test_make_dotfile(tmpdir):
10
    uid = str(uuid1())
11
    tmp = tmpdir.mkdir(uid)
12
    for i in range(4):
13
        tmp.join('{}.txt'.format(i)).write('{}'.format(i))
14
    data = filetree_digraph.make_dotfile(tmp.strpath)
15
    # Ensure wrapping lines are proper digraph format.
16
    assert data.startswith('digraph {\n')
17
    assert data.endswith('\n}\n')
18
    lines = data.split('\n')
19
    # Ensure each line has the right dotfile format.
20
    for i, line in enumerate(lines[1:len(lines) - 2]):
21
        assert line == '\t"{0}" -> "{1}.txt";'.format(uid, i)
22
23
24
def test_make_dotfile_skip_empty(monkeypatch, tmpdir):
25
    uid = str(uuid1())
26
    tmp = tmpdir.mkdir(uid)
27
    # Add a bunch of empty pointers
28
    results = [' ->' for _ in range(10)] + ['foo -> bar']
29
    monkeypatch.setattr(
30
        filetree_digraph, 'path_hierarchy', lambda *a, **k: results)
31
    data = filetree_digraph.make_dotfile(tmp.strpath)
32
    assert data == 'digraph {\n\tfoo -> bar;\n}\n'
33
34
35
def test_make_dotfile_invalid_path():
36
    with pytest.raises(OSError):
37
        filetree_digraph.make_dotfile('invalid-path')
38
39
40
def test_make_dotfile_invalid_path_none():
41
    with pytest.raises(AssertionError):
42
        filetree_digraph.make_dotfile(None)
43
44
45
def test_get_dotfile_tree_invalid_path(tmpdir):
46
    runner = CliRunner()
47
    result = runner.invoke(filetree_digraph.get_dotfile_tree, ['-p', '.'])
48
    assert result.exit_code == -1
49
    assert isinstance(result.exception, ValueError)
50
    assert 'Running in the same directory when no' in str(result.exception)
51
52
53
def test_get_dotfile_tree_valid_path(tmpdir):
54
    uid = str(uuid1())
55
    tmp = tmpdir.mkdir(uid)
56
    tmppath = str(tmp.realpath())
57
    runner = CliRunner()
58
    result = runner.invoke(
59
        filetree_digraph.get_dotfile_tree, ['-p', tmppath])
60
    assert result.exit_code == 0
61
    assert 'digraph' in result.output
62
63
64
def test_get_dotfile_tree_valid_path_dotfile(tmpdir):
65
    uid = str(uuid1())
66
    tmp = tmpdir.mkdir(uid)
67
    tmpfile = tmp.join('foo.dot')
68
    tmpfilepath = str(tmpfile.realpath())
69
    tmppath = str(tmp.realpath())
70
    runner = CliRunner()
71
    result = runner.invoke(
72
        filetree_digraph.get_dotfile_tree, ['-p', tmppath, '-d', tmpfilepath])
73
    assert result.exit_code == 0
74
    with open(tmpfilepath, 'r') as res:
75
        assert 'digraph' in str(res.read())
76