forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_graph.py
More file actions
47 lines (37 loc) · 1.4 KB
/
test_graph.py
File metadata and controls
47 lines (37 loc) · 1.4 KB
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
import os
import tempfile
import uuid
from unittest import TestCase
from unittest.mock import MagicMock, patch
from piccolo.apps.schema.commands.graph import graph
class TestGraph(TestCase):
def _verify_contents(self, file_contents: str):
"""
Make sure the contents of the file are correct.
"""
# Make sure no extra content was output at the start.
self.assertTrue(file_contents.startswith("digraph model_graph"))
# Make sure the tables are present
self.assertTrue("TABLE_Band [label" in file_contents)
self.assertTrue("TABLE_Manager [label" in file_contents)
# Make sure a relation is present
self.assertTrue("TABLE_Concert -> TABLE_Band" in file_contents)
@patch("piccolo.apps.schema.commands.graph.print")
def test_graph(self, print_: MagicMock):
"""
Make sure the file contents can be printed to stdout.
"""
graph()
file_contents = print_.call_args[0][0]
self._verify_contents(file_contents)
def test_graph_to_file(self):
"""
Make sure the file contents can be written to disk.
"""
directory = tempfile.gettempdir()
path = os.path.join(directory, f"{uuid.uuid4()}.dot")
graph(output=path)
with open(path, "r") as f:
file_contents = f.read()
self._verify_contents(file_contents)
os.unlink(path)