1
|
|
|
# tests/test_http_requests.py |
2
|
|
|
"""Module to test https request calls using the request package. |
3
|
|
|
|
4
|
|
|
Targets are mostly mocked using pytest-mock plugin:: |
5
|
|
|
|
6
|
|
|
poetry add --dev pytest-mock |
7
|
|
|
|
8
|
|
|
Meant to serve as template in case the package uses url based api calls. |
9
|
|
|
""" |
10
|
|
|
# third party packages |
11
|
|
|
import pytest |
12
|
|
|
import requests |
13
|
|
|
|
14
|
|
|
from .test_connectivity import request_url |
15
|
|
|
|
16
|
|
|
# pylint: disable=redefined-outer-name |
17
|
|
|
# disabled here, sind redefinition is how fixtures work and they are used here |
18
|
|
|
# extensively |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
@pytest.fixture |
22
|
|
|
def mock_requests_get(mocker): |
23
|
|
|
"""Fixture to mock reqeust.get calls.""" |
24
|
|
|
mock = mocker.patch("requests.get") |
25
|
|
|
mock.return_value.__enter__.return_value.json.return_value = { |
26
|
|
|
"title": "Lorem Ipsum", |
27
|
|
|
"extract": "Lorem ipsum dolor sit amet", |
28
|
|
|
} |
29
|
|
|
return mock |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
@pytest.fixture |
33
|
|
|
def request_rnd_wiki_artcl(): |
34
|
|
|
"""Try reaching the wikipedia site to get a random article.""" |
35
|
|
|
api_url = "https://en.wikipedia.org/api/rest_v1/page/random/summary" |
36
|
|
|
response = request_url(api_url) |
37
|
|
|
json_response = response.json() |
38
|
|
|
|
39
|
|
|
return json_response |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def test_mock_gets_called(mock_requests_get, request_rnd_wiki_artcl): |
43
|
|
|
""" |
44
|
|
|
Assert the requests.get was actually called. |
45
|
|
|
|
46
|
|
|
random_wiki_article gets called by fixutre wrap around mock object so |
47
|
|
|
the mock object is "requests.get" instead of the url. |
48
|
|
|
Fixture order is important! |
49
|
|
|
|
50
|
|
|
Since the data content is not of importance calling the fixture alone |
51
|
|
|
suffices. |
52
|
|
|
|
53
|
|
|
Parameters |
54
|
|
|
---------- |
55
|
|
|
mock_requests_get |
56
|
|
|
mock_requests_get fixture from above |
57
|
|
|
request_rnd_wiki_artcl |
58
|
|
|
request_random_wiki_article fixture from above |
59
|
|
|
""" |
60
|
|
|
# pylint: disable=unused-argument |
61
|
|
|
# disabled here since the fixture is needed for successful mocking |
62
|
|
|
# but unsued in the traditional argument sense of a = b; print(2*a) |
63
|
|
|
|
64
|
|
|
assert mock_requests_get.called |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
def test_mock_result_inspection(mock_requests_get, request_rnd_wiki_artcl): |
68
|
|
|
"""Test successful mock result inspection.""" |
69
|
|
|
# pylint: disable=unused-argument |
70
|
|
|
# disabled here since the fixture is needed for successful mocking |
71
|
|
|
# but unsued in the traditional argument sense of a = b; print(2*a) |
72
|
|
|
|
73
|
|
|
http_json_response = request_rnd_wiki_artcl |
74
|
|
|
assert "Lorem Ipsum" in http_json_response["title"] |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
def test_mock_param_call_inspection(mock_requests_get, request_rnd_wiki_artcl): |
78
|
|
|
""" |
79
|
|
|
Assert the requests.get was called properly. |
80
|
|
|
|
81
|
|
|
Random_wiki_article gets called by fixutre wrap around mock object so |
82
|
|
|
the mock object is "requests.get" instead of the url. |
83
|
|
|
Fixture order is implortant! |
84
|
|
|
Since the data content is not of importance calling the fixture alone |
85
|
|
|
suffices. |
86
|
|
|
|
87
|
|
|
Parameters |
88
|
|
|
---------- |
89
|
|
|
mock_requests_get |
90
|
|
|
mock_requests_get fixture from above |
91
|
|
|
request_rnd_wiki_artcl |
92
|
|
|
request_random_wiki_article fixture from above |
93
|
|
|
""" |
94
|
|
|
# pylint: disable=unused-argument |
95
|
|
|
# disabled here since the fixture is needed for successful mocking |
96
|
|
|
# but unsued in the traditional argument sense of a = b; print(2*a) |
97
|
|
|
|
98
|
|
|
args, _ = mock_requests_get.call_args |
99
|
|
|
assert "en.wikipedia.org" in args[0] |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
def test_fail_on_request_error(mock_requests_get, request_rnd_wiki_artcl): |
103
|
|
|
"""Test on failing the https request.""" |
104
|
|
|
# pylint: disable=unused-argument |
105
|
|
|
# disabled here since the fixture is needed for successful mocking |
106
|
|
|
# but unsued in the traditional argument sense of a = b; print(2*a) |
107
|
|
|
|
108
|
|
|
mock_requests_get.side_effect = requests.RequestException |
109
|
|
|
|
110
|
|
|
api_url = "https://en.wikipedia.org/api/rest_v1/page/random/summary" |
111
|
|
|
mock_response = request_url(api_url) |
112
|
|
|
|
113
|
|
|
assert "Error" in mock_response |
114
|
|
|
|