-
Notifications
You must be signed in to change notification settings - Fork 15
/
test_nwm_request_response.py
155 lines (132 loc) · 8.03 KB
/
test_nwm_request_response.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import json
import unittest
from ..communication.maas_request import NWMRequestResponse
from ..communication.maas_request.model_exec_request_response_body import ModelExecRequestResponseBody
from ..communication.message import InitRequestResponseReason
from ..communication.scheduler_request import SchedulerRequestResponse
class TestNWMRequestResponse(unittest.TestCase):
_RAW_RESPONSE_STR_EX_1 = '{"success": false, "reason": "UNRECOGNIZED_SESSION_SECRET", "message": "Request with secret 824c69aa7fabcacb43cc914558f58bf02f6ad866a7a2f952ababab2376a69792 does not correspond to a known authenticated session", "data": {}}'
_RAW_RESPONSE_STR_EX_2 = '{"success": true, "reason": "ACCEPTED", "message": "Success submitting job to scheduler (returned id 42)", "data": {"job_id": 42, "scheduler_response": {"success": true, "reason": "Job Scheduled", "message": "", "data": {"job_id": 42}}}}'
@classmethod
def get_raw_response_string_example_1(cls):
return cls._RAW_RESPONSE_STR_EX_1
@classmethod
def get_raw_response_string_example_2(cls):
return cls._RAW_RESPONSE_STR_EX_2
def setUp(self) -> None:
self.response_strings = {1: self._RAW_RESPONSE_STR_EX_1, 2: self._RAW_RESPONSE_STR_EX_2}
self.response_jsons = dict()
for k in self.response_strings:
self.response_jsons[k] = json.loads(self.response_strings[k])
def tearDown(self) -> None:
pass
def test_factory_init_from_deserialized_json_1_a(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 1 to make sure it deserializes.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[1])
self.assertIsNotNone(obj)
def test_factory_init_from_deserialized_json_1_b(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 1 to make sure it deserializes to the right
object type.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[1])
self.assertEqual(obj.__class__, NWMRequestResponse)
def test_factory_init_from_deserialized_json_1_c(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 1 to make sure the deserialized object has
the expected value for ``success``.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[1])
self.assertFalse(obj.success)
def test_factory_init_from_deserialized_json_1_d(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 1 to make sure the deserialized object has
the expected value for ``reason``.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[1])
self.assertEqual(obj.reason, InitRequestResponseReason.UNRECOGNIZED_SESSION_SECRET.name)
def test_factory_init_from_deserialized_json_2_a(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 2 to make sure it deserializes.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[2])
self.assertIsNotNone(obj)
def test_factory_init_from_deserialized_json_2_b(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 2 to make sure it deserializes to the right
object type.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[2])
self.assertEqual(obj.__class__, NWMRequestResponse)
def test_factory_init_from_deserialized_json_2_c(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 2 to make sure the deserialized object has
the expected value for ``success``.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[2])
self.assertTrue(obj.success)
def test_factory_init_from_deserialized_json_2_d(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 2 to make sure the deserialized object has
the expected value for ``reason``.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[2])
self.assertEqual(obj.reason, InitRequestResponseReason.ACCEPTED.name)
def test_factory_init_from_deserialized_json_2_e(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 2 to make sure the deserialized object has
the expected ModelExecRequestResponseBody value for ``data``. For legacy support, this can still be
treated like a dictionary.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[2])
self.assertEqual(obj.data.__class__, ModelExecRequestResponseBody)
def test_factory_init_from_deserialized_json_2_f(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 2 to make sure the deserialized object has
the expected dictionary value for ``data``, with the expected ``job_id`` key.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[2])
self.assertTrue('job_id' in obj.data)
def test_factory_init_from_deserialized_json_2_g(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 2 to make sure the deserialized object has
the expected dictionary value for ``data``, with the ``job_id`` element having the correct value.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[2])
self.assertEqual(obj.data['job_id'], 42)
def test_factory_init_from_deserialized_json_2_h(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 2 to make sure the deserialized object has
the expected dictionary value for ``data``, with the expected ``scheduler_response`` key.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[2])
self.assertTrue('scheduler_response' in obj.data)
def test_factory_init_from_deserialized_json_2_i(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 2 to make sure the deserialized object has
the expected SchedulerRequestResponse value for ``data``, with the ``scheduler_response`` being of the right type.
For legacy support, ``SchedulerRequestResponse`` can still be treated as a dictionary.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[2])
self.assertEqual(obj.data['scheduler_response'].__class__, SchedulerRequestResponse)
def test_factory_init_from_deserialized_json_2_j(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 2 to make sure the deserialized object has
the expected dictionary value for ``data``, with value mapped to ``scheduler_response`` being deserializeable.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[2])
sched_dict = obj.data['scheduler_response']
sched_resp = SchedulerRequestResponse.factory_init_from_deserialized_json(sched_dict)
self.assertIsNotNone(sched_resp)
def test_factory_init_from_deserialized_json_2_k(self):
"""
Test ``factory_init_from_deserialized_json()`` on raw string example 2 to make sure the deserialized object has
the expected dictionary value for ``data``, with value mapped to ``scheduler_response`` being deserializeable to
a :class:`SchedulerRequestResponse`.
"""
obj = NWMRequestResponse.factory_init_from_deserialized_json(self.response_jsons[2])
sched_resp = SchedulerRequestResponse.factory_init_from_deserialized_json(obj.data['scheduler_response'])
self.assertEqual(sched_resp.__class__, SchedulerRequestResponse)
# TODO: add some meaningful tests (directly)