-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathdefault_instances.py
More file actions
172 lines (128 loc) · 5.22 KB
/
default_instances.py
File metadata and controls
172 lines (128 loc) · 5.22 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""
Fixtures that provide default instances for AIoD and ORM classes.
This way you have easy access to, for instance, an AIoDDataset filled with default values.
"""
import copy
import json
from functools import partial
from typing import Callable
import pytest
from sqlalchemy.engine import Engine
from database.model.agent.contact import Contact
from database.model.agent.organisation import Organisation
from database.model.agent.person import Person
from database.model.dataset.dataset import Dataset
from database.model.knowledge_asset.publication import Publication
from database.model.models_and_experiments.experiment import Experiment
from database.model.models_and_experiments.ml_model import MLModel
from database.model.platform.platform import Platform
from database.model.resource_read_and_create import resource_create
from database.model.serializers import deserialize_resource_relationships
from database.session import DbSession
from database.model.project.project import Project
from tests.testutils.paths import path_test_resources
@pytest.fixture(scope="session")
def load_body_concept() -> dict:
with open(path_test_resources() / "schemes" / "aiod" / "aiod_concept.json", "r") as f:
return json.load(f)
@pytest.fixture()
def body_concept(load_body_concept: dict) -> dict:
return copy.deepcopy(load_body_concept)
@pytest.fixture(scope="session")
def load_body_resource() -> dict:
with open(path_test_resources() / "schemes" / "aiod" / "ai_resource.json", "r") as f:
return json.load(f)
@pytest.fixture()
def body_resource(body_concept: dict, load_body_resource: dict) -> dict:
body = copy.deepcopy(body_concept)
body.update(load_body_resource)
return copy.deepcopy(body)
@pytest.fixture(scope="session")
def load_body_asset() -> dict:
with open(path_test_resources() / "schemes" / "aiod" / "ai_asset.json", "r") as f:
return json.load(f)
@pytest.fixture()
def body_asset(body_resource: dict, load_body_asset: dict) -> dict:
body = copy.deepcopy(body_resource)
body.update(load_body_asset)
return copy.deepcopy(body)
@pytest.fixture(scope="session")
def load_body_agent() -> dict:
with open(path_test_resources() / "schemes" / "aiod" / "agent.json", "r") as f:
return json.load(f)
@pytest.fixture()
def body_agent(body_resource: dict, load_body_agent: dict) -> dict:
body = copy.deepcopy(body_resource)
body.update(load_body_agent)
return copy.deepcopy(body)
def make_publication(
body_asset: dict, with_random_platform_identifier: bool = False
) -> Publication:
body = copy.deepcopy(body_asset)
body["permanent_identifier"] = "http://dx.doi.org/10.1093/ajae/aaq063"
body["isbn"] = "9783161484100"
body["issn"] = "20493630"
body["type"] = "journal"
return _create_class_with_body(Publication, body)
@pytest.fixture
def publication(body_asset: dict) -> Publication:
return make_publication(body_asset)
@pytest.fixture
def publication_factory(body_asset: dict) -> Callable[[], Publication]:
return partial(make_publication, body_asset, with_random_platform_identifier=True)
@pytest.fixture
def contact(body_concept, engine: Engine) -> Contact:
body = copy.deepcopy(body_concept)
body["email"] = ["a@b.com"]
body["name"] = "Aaron Bar"
body["telephone"] = ["0032 XXXX XXXX"]
body["location"] = [
{
"address": {"country": "Spain", "street": "Street Name 10", "postal_code": "1234AB"},
"geo": {"latitude": 37.42242, "longitude": -122.08585, "elevation_millimeters": 2000},
}
]
return _create_class_with_body(Contact, body)
@pytest.fixture
def dataset(body_asset: dict) -> Dataset:
body = copy.deepcopy(body_asset)
body["issn"] = "20493630"
body["measurement_technique"] = "mass spectrometry"
body["temporal_coverage"] = "2011/2012"
return _create_class_with_body(Dataset, body)
@pytest.fixture
def organisation(body_agent: dict) -> Organisation:
body = copy.deepcopy(body_agent)
body["date_founded"] = "2022-01-01"
body["legal_name"] = "Legal Name"
body["ai_relevance"] = "Description of relevance in AI"
return _create_class_with_body(Organisation, body)
@pytest.fixture
def person(body_agent: dict) -> Person:
body = copy.deepcopy(body_agent)
body["expertise"] = ["machine learning"]
body["language"] = ["eng", "nld"]
return _create_class_with_body(Person, body)
@pytest.fixture
def experiment(body_asset: dict) -> Experiment:
return _create_class_with_body(Experiment, body_asset)
@pytest.fixture
def ml_model(body_asset: dict) -> MLModel:
return _create_class_with_body(MLModel, body_asset)
@pytest.fixture
def project(body_asset: dict) -> Project:
return _create_class_with_body(Project, body_asset)
@pytest.fixture
def platform() -> Platform:
body = {"name": "aiod"}
return _create_class_with_body(Platform, body)
def _create_class_with_body(clz, body: dict):
pydantic_class = resource_create(clz)
res_create = pydantic_class(**body)
res = clz.from_orm(res_create)
with DbSession() as session:
deserialize_resource_relationships(session, clz, res, res_create, user=None)
session.commit()
if hasattr(res, "ai_resource"):
res.ai_resource.type = clz.__tablename__
return res