-
Notifications
You must be signed in to change notification settings - Fork 104
/
test_basic.py
56 lines (47 loc) · 2.05 KB
/
test_basic.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
from mock import MagicMock
from mock import patch
class TestBasic:
"""Some basic tests, checking import, making sure APIs remain consistent, etc"""
def test_import_serverextension(self):
"""Check that serverextension hooks are available"""
from jupyter_resource_usage import (
_jupyter_server_extension_points,
_jupyter_nbextension_paths,
load_jupyter_server_extension,
)
assert _jupyter_server_extension_points() == [
{"module": "jupyter_resource_usage"}
]
assert _jupyter_nbextension_paths() == [
{
"section": "notebook",
"dest": "jupyter_resource_usage",
"src": "static",
"require": "jupyter_resource_usage/main",
}
]
# mock a notebook app
nbapp_mock = MagicMock()
nbapp_mock.web_app.settings = {"base_url": ""}
# mock these out for unit test
with patch("tornado.ioloop.PeriodicCallback") as periodic_callback_mock, patch(
"jupyter_resource_usage.server_extension.ResourceUseDisplay"
) as resource_use_display_mock, patch(
"jupyter_resource_usage.server_extension.PrometheusHandler"
) as prometheus_handler_mock, patch(
"jupyter_resource_usage.server_extension.PSUtilMetricsLoader"
) as psutil_metrics_loader:
# load up with mock
load_jupyter_server_extension(nbapp_mock)
# assert that we installed the application in settings
print(nbapp_mock.web_app.settings)
assert (
"jupyter_resource_usage_display_config" in nbapp_mock.web_app.settings
)
# assert that we instantiated a periodic callback with the fake
# prometheus
assert periodic_callback_mock.return_value.start.call_count == 1
assert prometheus_handler_mock.call_count == 1
prometheus_handler_mock.assert_called_with(
psutil_metrics_loader(nbapp_mock)
)