|
1
|
|
|
from uuid import uuid1 |
|
2
|
|
|
import json |
|
3
|
|
|
|
|
4
|
|
|
import pytest |
|
5
|
|
|
from click.testing import CliRunner |
|
6
|
|
|
|
|
7
|
|
|
from flask_jsondash.data_utils import filetree |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
def test_path_hierarchy(tmpdir): |
|
11
|
|
|
uid = uuid1() |
|
12
|
|
|
tmp = tmpdir.mkdir('{}'.format(uid)) |
|
13
|
|
|
data = filetree.path_hierarchy(tmp.strpath) |
|
14
|
|
|
assert json.dumps(data) |
|
15
|
|
|
for key in ['type', 'name', 'path']: |
|
16
|
|
|
assert key in data |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def test_path_hierarchy_invalid_path(tmpdir): |
|
20
|
|
|
with pytest.raises(OSError): |
|
21
|
|
|
filetree.path_hierarchy('invalid-path') |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def test_path_hierarchy_invalid_path_none(tmpdir): |
|
25
|
|
|
with pytest.raises(AssertionError): |
|
26
|
|
|
filetree.path_hierarchy(None) |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def test_path_hierarchy_invalid_path_empty_path(tmpdir): |
|
30
|
|
|
with pytest.raises(OSError): |
|
31
|
|
|
filetree.path_hierarchy('') |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
def test_get_tree_invalid_path(tmpdir): |
|
35
|
|
|
runner = CliRunner() |
|
36
|
|
|
result = runner.invoke(filetree.get_tree, ['-p', '/{}'.format(uuid1())]) |
|
37
|
|
|
assert result.exit_code == -1 |
|
38
|
|
|
assert isinstance(result.exception, OSError) |
|
39
|
|
|
assert 'No such file or directory' in str(result.exception) |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def test_get_tree_valid_path(tmpdir): |
|
43
|
|
|
uid = str(uuid1()) |
|
44
|
|
|
tmp = tmpdir.mkdir(uid) |
|
45
|
|
|
runner = CliRunner() |
|
46
|
|
|
result = runner.invoke(filetree.get_tree, ['-p', tmp.strpath]) |
|
47
|
|
|
assert result.exit_code == 0 |
|
48
|
|
|
assert 'path' in result.output |
|
49
|
|
|
assert 'name' in result.output |
|
50
|
|
|
assert 'type' in result.output |
|
51
|
|
|
assert 'children' in result.output |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def test_get_tree_valid_path_jsonfile(tmpdir): |
|
55
|
|
|
uid = str(uuid1()) |
|
56
|
|
|
tmp = tmpdir.mkdir(uid) |
|
57
|
|
|
jsonfile = tmp.join('foo.json') |
|
58
|
|
|
jsonpath = str(jsonfile.realpath()).encode('utf-8') |
|
59
|
|
|
jsonfile.write('') |
|
60
|
|
|
assert str(jsonfile.read()) == '' |
|
61
|
|
|
runner = CliRunner() |
|
62
|
|
|
result = runner.invoke( |
|
63
|
|
|
filetree.get_tree, ['-p', tmp.strpath, '-j', jsonpath]) |
|
64
|
|
|
assert result.exit_code == 0 |
|
65
|
|
|
data = str(jsonfile.read()) |
|
66
|
|
|
assert 'path' in data |
|
67
|
|
|
assert 'name' in data |
|
68
|
|
|
assert 'type' in data |
|
69
|
|
|
assert 'children' in data |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
def test_get_tree_valid_path_prettyprint(tmpdir): |
|
73
|
|
|
uid = str(uuid1()) |
|
74
|
|
|
tmp = tmpdir.mkdir(uid) |
|
75
|
|
|
runner = CliRunner() |
|
76
|
|
|
result = runner.invoke( |
|
77
|
|
|
filetree.get_tree, ['-p', tmp.strpath, '--ppr']) |
|
78
|
|
|
assert result.exit_code == 0 |
|
79
|
|
|
assert 'path' in result.output |
|
80
|
|
|
assert 'name' in result.output |
|
81
|
|
|
assert 'type' in result.output |
|
82
|
|
|
assert 'children' in result.output |
|
83
|
|
|
|