forked from kinow/protobuf-uml-diagram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
105 lines (87 loc) · 3.53 KB
/
tests.py
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
# Copyright 2019 Bruno P. Kinoshita
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from pathlib import Path
from tempfile import TemporaryDirectory
import pytest
from protobuf_uml_diagram import PathPath, Diagram
def test_path_path():
"""Test the converter used for the command line args."""
path_path = PathPath()
path = path_path.convert(value="blue", param="color", ctx=None)
assert isinstance(path, Path)
class TestDiagramBuilder:
def test_from_file_raises(self):
with pytest.raises(ValueError) as e:
Diagram().from_file('')
assert 'Missing proto file' in str(e.value)
def test_to_file_raises(self):
with pytest.raises(ValueError) as e:
Diagram().to_file(None)
assert 'Missing output location' in str(e.value)
def test_with_format_raises(self):
with pytest.raises(ValueError) as e:
Diagram().with_format(None)
assert 'Missing file' in str(e.value)
def test_build_raises(self):
with pytest.raises(ValueError) as e:
Diagram().build()
assert 'No Protobuf' in str(e.value)
with pytest.raises(ValueError) as e:
Diagram() \
.from_file('test_data.data_messages.proto') \
.build()
assert 'No output' in str(e.value)
with pytest.raises(ValueError) as e:
d = Diagram() \
.from_file('test_data.data_messages.proto') \
.to_file(Path('abc'))
d._file_format = None
d.build()
assert 'No file format' in str(e.value)
def test_happy_path(self):
with TemporaryDirectory() as tmpdir:
tf = os.path.join(tmpdir, 'diagram.png')
Diagram() \
.from_file('test_data.data_messages.proto') \
.to_file(Path(tf)) \
.with_format('png') \
.build()
assert os.path.getsize(tf) > 0
def test_homonymous(self):
"""A test for when you have two 'subclasses' with same names."""
with TemporaryDirectory() as tmpdir:
tf = os.path.join(tmpdir, 'diagram.png')
Diagram() \
.from_file('test_data.issue_10.proto') \
.to_file(Path(tf)) \
.with_format('png') \
.build()
assert os.path.getsize(tf) > 0
def test_logs_module_not_found(self):
with pytest.raises(ModuleNotFoundError) as e:
Diagram() \
.from_file('piracicaba') \
.build()
assert 'piracicaba' in str(e)
def test_contains_dot_proto_in_middle_of_the_name(self):
"""A test where the input data contains .proto, but doesn't end with it."""
with TemporaryDirectory() as tmpdir:
tf = os.path.join(tmpdir, 'diagram.png')
Diagram() \
.from_file('test_data.issue_27.proto.configs_data_pb2') \
.to_file(Path(tf)) \
.with_format('png') \
.build()
assert os.path.getsize(tf) > 0