-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
132 lines (107 loc) · 4.44 KB
/
tests.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
import json
import mock
import os
import requests
import subprocess
import sys
import time
import unittest
import docker
import on_startup
from test_utils import TestContainerRunner
class ContainerIntegrationTests(unittest.TestCase):
def setUp(self):
self.base_url = "http://localhost:{}/".format(
test_container_runner.container_port
)
self.tilesets_url = '{}api/v1/tilesets/'.format(self.base_url)
for i in range(60): # probably overkill, but Travis is slow sometimes
try:
requests.get(self.tilesets_url)
break
except Exception:
print('Still waiting for server...')
time.sleep(1)
else:
self.fail('Server never came up')
def assert_run(self, command, res=[r'']):
output = subprocess.check_output(
command.format(**os.environ),
shell=True
).strip()
for re in res:
self.assertRegexpMatches(output.decode('utf-8'), re)
def test_home_page(self):
response = requests.get(self.base_url)
self.assertEqual(response.status_code, 200)
self.assertIn(b"HiGlass - App", response.content)
# Test if the data we specify in INPUT_JSON_URL gets ingested properly by
# higlass server upon container startup
def test_data_ingested(self):
response = json.loads(requests.get(self.tilesets_url).content)
self.assertEqual(response["count"], 5)
class StartupScriptTests(unittest.TestCase):
def setUp(self):
self.tilesets = []
mock.patch.object(on_startup, "DATA_DIRECTORY", "/tmp/").start()
with open("test-data/input.json") as f:
input_dict = json.loads(f.read())
self.cooler_file_url = input_dict["file_relationships"][0]
self.bigwig_file_url = input_dict["file_relationships"][3]
self.cooler_tileset = self._create_tileset(self.cooler_file_url)
self.bigwig_tileset = self._create_tileset(self.bigwig_file_url)
def tearDown(self):
mock.patch.stopall()
def _create_tileset(self, tileset_url):
tileset = on_startup.Tileset({"file_url": tileset_url})
self.tilesets.append(tileset)
return tileset
def test_tileset_filetype_is_set(self):
self.assertEqual(self.cooler_tileset.file_type, "cooler")
self.assertEqual(self.bigwig_tileset.file_type, "bigwig")
def test_tileset_datatype_is_set(self):
self.assertEqual(self.cooler_tileset.data_type, "matrix")
self.assertEqual(self.bigwig_tileset.data_type, "vector")
def test_tileset_is_bigwig(self):
self.assertFalse(self.cooler_tileset.is_bigwig())
self.assertTrue(self.bigwig_tileset.is_bigwig())
def _tileset_repr_assertions(self, tileset):
self.assertEqual(
"Tileset: {} {} {}".format(
tileset.file_path,
tileset.file_type,
tileset.data_type
),
str(tileset)
)
def test_tileset_repr(self):
for tileset in self.tilesets:
self._tileset_repr_assertions(tileset)
def test_tileset_file_downloaded(self):
for tileset in self.tilesets:
self.assertTrue(os.path.exists("/tmp/" + tileset.file_name))
def test_tileset_ingest(self):
with mock.patch("on_startup.call_command") as call_command_mock:
self.cooler_tileset.ingest()
self.bigwig_tileset.ingest()
self.assertEqual(call_command_mock.call_count, 2)
@mock.patch("on_startup.call_command")
@mock.patch("on_startup._fetch_default_viewconf")
@mock.patch("on_startup._start_nginx")
def test_module_invocation(self, start_nginx_mock, fetch_default_viewconf_mock, call_command_mock):
os.environ["INPUT_JSON_URL"] = "http://{}:{}/test-data/input.json".format(
test_container_runner.test_fixture_server.ip,
test_container_runner.test_fixture_server.port
)
on_startup.main()
self.assertTrue(fetch_default_viewconf_mock.called)
self.assertTrue(start_nginx_mock.called)
self.assertEqual(call_command_mock.call_count, 5)
@mock.patch('on_startup.error_page')
def test_error_handling(self, error_page_mock):
on_startup.main()
self.assertTrue(error_page_mock.called)
if __name__ == '__main__':
test_container_runner = TestContainerRunner()
with test_container_runner:
unittest.main()