This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
/
test_cli.py
95 lines (71 loc) · 3.42 KB
/
test_cli.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
"""Test app drivers in postqa.cli module."""
from __future__ import (division, absolute_import, print_function,
unicode_literals)
from builtins import * # NOQA
from future.standard_library import install_aliases
install_aliases() # NOQA
import jsonschema
import responses
from postqa.schemas import load_schema
import postqa.cli
import postqa.lsstsw
def test_build_json_docs(mocker, qa_json_path, lsstsw_dir):
# Mock the git repos on the file system
mocker.patch('postqa.lsstsw.git.Repo')
postqa.lsstsw.git.Repo.return_value.active_branch.name = 'master'
metric_json, job_json = postqa.cli.build_json_docs(qa_json_path,
lsstsw_dir)
metric_schema = load_schema(schema='metric')
jsonschema.validate(metric_json, metric_schema)
job_schema = load_schema(schema='job')
jsonschema.validate(job_json, job_schema)
@responses.activate
def test_load_registered_metrics():
api_url = 'http://squash.lsst.codes/dashboard/api/'
metric_endpoint_url = 'http://squash.lsst.codes/dashboard/api/metrics/'
# Mock requests
with open("tests/data/api_response.json") as f:
api_response = f.read()
responses.add(responses.GET, api_url,
body=api_response, status=200,
content_type='application/json')
with open("tests/data/metric_endpoint_response.json") as f:
metric_endpoint_response = f.read()
responses.add(responses.GET, metric_endpoint_url,
body=metric_endpoint_response, status=200,
content_type='application/json')
postqa.cli.load_registered_metrics(api_url)
assert len(responses.calls) == 2
assert responses.calls[0].request.url == api_url
assert responses.calls[0].response.text == api_response
assert responses.calls[1].request.url == metric_endpoint_url
assert responses.calls[1].response.text == metric_endpoint_response
@responses.activate
def test_upload_json_doc(mocker, qa_json_path, lsstsw_dir):
api_url = 'http://squash.lsst.codes/dashboard/api/'
metric_endpoint_url = 'http://squash.lsst.codes/dashboard/api/metrics/'
job_endpoint_url = 'http://squash.lsst.codes/dashboard/api/jobs/'
# Mock the git repos on the file system
mocker.patch('postqa.lsstsw.git.Repo')
postqa.lsstsw.git.Repo.return_value.active_branch.name = 'master'
# Mock requests
with open("tests/data/api_response.json") as f:
api_response = f.read()
responses.add(responses.GET, api_url,
body=api_response, status=200,
content_type='application/json')
responses.add(responses.POST, metric_endpoint_url,
body='{}', status=201,
content_type='application/json')
responses.add(responses.POST, job_endpoint_url,
body='{}', status=201,
content_type='application/json')
metric_json, job_json = postqa.cli.build_json_docs(qa_json_path,
lsstsw_dir)
postqa.cli.upload_json_doc(metric_json, api_url, 'metrics')
postqa.cli.upload_json_doc(job_json, api_url, 'jobs')
assert len(responses.calls) == 4
assert responses.calls[0].request.url == api_url
assert responses.calls[0].response.text == api_response
assert responses.calls[1].request.url == metric_endpoint_url
assert responses.calls[3].request.url == job_endpoint_url