-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_library.py
130 lines (111 loc) · 3.98 KB
/
test_library.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from rdflib import Graph
from buildingmotif.dataclasses import Library
def test_get_all_libraries(client, building_motif):
# Setup
Library.create("my_library")
Library.create("your_library")
# Act
results = client.get("/libraries")
# Assert
assert results.status_code == 200
db_libraries = building_motif.table_connection.get_all_db_libraries()
assert results.json == [
{
"id": library.id,
"name": library.name,
"template_ids": [t.id for t in library.templates],
"shape_collection_id": library.shape_collection_id,
}
for library in db_libraries
]
def test_get_all_shapes(client, building_motif):
# Setup
my_library = Library.create("my_library")
shape_graph_ttl = """
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix brick: <https://brickschema.org/schema/Brick#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix : <urn:ex/> .
:my_shape a sh:NodeShape ;
sh:targetClass brick:Entity ;
rdfs:label "my shape label" ;
skos:description "my shape description" .
:my_other_shape a sh:NodeShape ;
sh:targetClass brick:Entity ;
rdfs:label "my shape other label" ;
skos:description "my shape other description" .
"""
g = Graph()
g.parse(data=shape_graph_ttl)
my_library.get_shape_collection().add_graph(g)
shape_graph_ttl = """
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix brick: <https://brickschema.org/schema/Brick#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix : <urn:ex/> .
:your_shape a sh:NodeShape ;
sh:targetClass brick:Entity ;
rdfs:label "your shape label" ;
skos:description "your shape description" .
"""
g = Graph()
g.parse(data=shape_graph_ttl)
your_library = Library.create("your_library")
your_library.get_shape_collection().add_graph(g)
# Act
results = client.get("/libraries/shapes")
# Assert
assert results.status_code == 200
expected = sorted(
[
{
"library_name": "my_library",
"library_id": my_library.id,
"uri": "urn:ex/my_shape",
"label": "my shape label",
"description": "my shape description",
},
{
"library_name": "my_library",
"library_id": my_library.id,
"uri": "urn:ex/my_other_shape",
"label": "my shape other label",
"description": "my shape other description",
},
{
"library_name": "your_library",
"library_id": your_library.id,
"uri": "urn:ex/your_shape",
"label": "your shape label",
"description": "your shape description",
},
],
key=lambda x: x["uri"],
)
actual = sorted(results.json, key=lambda x: x["uri"])
assert expected == actual
def test_get_library(client, building_motif):
# Setup
lib = Library.create("my_library")
lib.create_template("my_template")
# Act
results = client.get(f"/libraries/{lib.id}")
# Assert
assert results.status_code == 200
db_library = building_motif.table_connection.get_db_library_by_id(lib.id)
assert results.json == {
"id": db_library.id,
"name": db_library.name,
"template_ids": [t.id for t in db_library.templates],
"shape_collection_id": db_library.shape_collection_id,
}
def test_get_library_not_found(client):
# Act
results = client.get("/libraries/-1")
# Assert
assert results.status_code == 404
assert results.json == {"message": "No library with id -1"}