-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathshed_app_test_utils.py
73 lines (57 loc) · 1.59 KB
/
shed_app_test_utils.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
import contextlib
import shutil
import threading
from tempfile import mkdtemp
from typing import NamedTuple
from werkzeug.serving import run_simple
from planemo import network_util
from .shed_app import (
app,
InMemoryShedDataModel,
)
DEFAULT_OP_TIMEOUT = 2
def mock_model(directory):
return (
InMemoryShedDataModel(directory)
.add_category("c1", "Text Manipulation")
.add_category("c2", "Sequence Analysis")
.add_category("c3", "Tool Dependency Packages")
.add_repository(
"r1",
name="test_repo_1",
owner="iuc",
)
)
def setup_mock_shed():
port = network_util.get_free_port()
directory = mkdtemp()
model = mock_model(directory)
def run():
app.debug = True
app.config["model"] = model
run_simple("localhost", port, app, use_reloader=False, use_debugger=True)
t = threading.Thread(target=run, daemon=True)
t.start()
network_util.wait_net_service("localhost", port, DEFAULT_OP_TIMEOUT)
return MockShed(f"http://localhost:{port}", directory, t, model)
@contextlib.contextmanager
def mock_shed():
mock_shed_obj = None
try:
mock_shed_obj = setup_mock_shed()
yield mock_shed_obj
finally:
if mock_shed_obj is not None:
mock_shed_obj.shutdown()
class MockShed(NamedTuple):
url: str
directory: str
thread: threading.Thread
model: InMemoryShedDataModel
def shutdown(self):
self.thread.join(timeout=1)
shutil.rmtree(self.directory)
__all__ = (
"setup_mock_shed",
"mock_shed",
)