forked from joschabach/micropsi2
/
conftest.py
157 lines (116 loc) · 4.49 KB
/
conftest.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
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
import os
import shutil
import pytest
import logging
try:
shutil.rmtree('/tmp/micropsi_tests/')
except OSError:
pass
os.makedirs('/tmp/micropsi_tests/worlds')
os.makedirs('/tmp/micropsi_tests/nodenets')
# override config
from configuration import config
config['paths']['resource_path'] = '/tmp/micropsi_tests'
config['paths']['server_settings_path'] = os.path.join(config['paths']['resource_path'], 'server_config.json')
config['paths']['usermanager_path'] = os.path.join(config['paths']['resource_path'], 'user-db.json')
if 'theano' in config:
config['theano']['initial_number_of_nodes'] = '50'
from micropsi_core import runtime as micropsi
# create testuser
from micropsi_server.micropsi_app import usermanager
usermanager.create_user('Pytest User', 'test', 'Administrator', uid='Pytest User')
user_token = usermanager.start_session('Pytest User', 'test', True)
# reset logging levels
logging.getLogger('system').setLevel(logging.WARNING)
logging.getLogger('world').setLevel(logging.WARNING)
world_uid = 'WorldOfPain'
nn_uid = 'Testnet'
nodetype_file = os.path.join(config['paths']['resource_path'], 'nodetypes.json')
nodefunc_file = os.path.join(config['paths']['resource_path'], 'nodefunctions.py')
recipes_file = os.path.join(config['paths']['resource_path'], 'recipes.py')
try:
import theano
engine_defaults = "dict_engine,theano_engine"
except:
engine_defaults = "dict_engine"
def set_logging_levels():
logging.getLogger('system').setLevel(logging.WARNING)
logging.getLogger('world').setLevel(logging.WARNING)
micropsi.cfg['logging']['level_agent'] = 'WARNING'
def pytest_addoption(parser):
parser.addoption("--engine", action="store", default=engine_defaults,
help="The engine that should be used for this testrun.")
def pytest_generate_tests(metafunc):
if 'engine' in metafunc.fixturenames:
engines = []
for e in metafunc.config.option.engine.split(','):
if e in ['theano_engine', 'dict_engine']:
engines.append(e)
if not engines:
pytest.exit("Unknown engine.")
metafunc.parametrize("engine", engines, scope="session")
def pytest_configure(config):
# register an additional marker
config.addinivalue_line("markers",
"engine(name): mark test to run only on the specified engine")
def pytest_runtest_setup(item):
engine_marker = item.get_marker("engine")
if engine_marker is not None:
engine_marker = engine_marker.args[0]
if engine_marker != item.callspec.params['engine']:
pytest.skip("test requires engine %s" % engine_marker)
micropsi.logger.clear_logs()
def pytest_runtest_teardown(item, nextitem):
if nextitem is None:
print("DELETING ALL STUFF")
shutil.rmtree(config['paths']['resource_path'])
else:
uids = list(micropsi.nodenets.keys())
for uid in uids:
micropsi.delete_nodenet(uid)
if os.path.isfile(nodetype_file):
os.remove(nodetype_file)
if os.path.isfile(nodefunc_file):
os.remove(nodefunc_file)
if os.path.isfile(recipes_file):
os.remove(recipes_file)
micropsi.reload_native_modules()
set_logging_levels()
@pytest.fixture(scope="session")
def resourcepath():
return config['paths']['resource_path']
@pytest.fixture()
def nodetype_def():
return nodetype_file
@pytest.fixture
def nodefunc_def():
return nodefunc_file
@pytest.fixture
def recipes_def():
return recipes_file
@pytest.fixture(scope="function")
def test_world(request):
global world_uid
worlds = micropsi.get_available_worlds("Pytest User")
if world_uid not in worlds:
success, world_uid = micropsi.new_world("World of Pain", "Island", "Pytest User", uid=world_uid)
def fin():
try:
micropsi.delete_world(world_uid)
except:
pass # world was deleted in test
request.addfinalizer(fin)
return world_uid
@pytest.fixture(scope="function")
def test_nodenet(request, test_world, engine):
global nn_uid
nodenets = micropsi.get_available_nodenets("Pytest User") or {}
if nn_uid not in nodenets:
success, nn_uid = micropsi.new_nodenet("Testnet", engine=engine, owner="Pytest User", uid='Testnet')
micropsi.save_nodenet(nn_uid)
return nn_uid
@pytest.fixture(scope="function")
def node(request, test_nodenet):
res, uid = micropsi.add_node(test_nodenet, 'Pipe', [10, 10], name='N1')
micropsi.add_link(test_nodenet, uid, 'gen', uid, 'gen')
return uid