1 # ==================================================================================
2 # Copyright (c) 2019 Nokia
3 # Copyright (c) 2018-2019 AT&T Intellectual Property.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ==================================================================================
19 from rmr.rmr_mocks import rmr_mocks
26 def _partial_dict_comparison(subset_dict, target_dict):
28 Compares that target_dict[k] == subset_dict[k] for all k <- subset_dict
30 for k, v in subset_dict.items():
31 assert k in target_dict
32 assert target_dict[k] == subset_dict[k]
35 def test_send_mock(monkeypatch):
39 monkeypatch.setattr("rmr.rmr.rmr_send_msg", rmr_mocks.send_mock_generator(12))
40 rmr_mocks.patch_rmr(monkeypatch)
41 sbuf = rmr.rmr_alloc_msg(MRC, SIZE)
42 rmr.set_payload_and_length("testttt".encode("utf8"), sbuf)
46 "message source": "localtest:80",
49 "message status": "RMR_OK",
50 "payload": b"testttt",
52 "payload max size": 4096,
55 _partial_dict_comparison(expected, rmr.message_summary(sbuf))
58 sbuf.contents.mtype = 666
60 # send it (the fake send sets the state, and touches nothing else)
61 sbuf = rmr.rmr_send_msg(MRC, sbuf)
65 "message source": "localtest:80",
68 "message status": "RMR_ERR_TIMEOUT",
71 "payload max size": 4096,
74 _partial_dict_comparison(expected, rmr.message_summary(sbuf))
77 def test_rcv_mock(monkeypatch):
79 tests the rmr recieve mocking generator
81 rmr_mocks.patch_rmr(monkeypatch)
82 sbuf = rmr.rmr_alloc_msg(MRC, SIZE)
85 monkeypatch.setattr("rmr.rmr.rmr_rcv_msg", rmr_mocks.rcv_mock_generator({"foo": "bar"}, 666, 0, True))
86 sbuf = rmr.rmr_rcv_msg(MRC, sbuf)
87 assert rmr.get_payload(sbuf) == b'{"foo": "bar"}'
88 assert sbuf.contents.mtype == 666
89 assert sbuf.contents.state == 0
90 assert sbuf.contents.len == 14
92 # test torcv, although the timeout portion is not currently mocked or tested
93 monkeypatch.setattr("rmr.rmr.rmr_torcv_msg", rmr_mocks.rcv_mock_generator({"foo": "bar"}, 666, 0, True, 50))
94 sbuf = rmr.rmr_torcv_msg(MRC, sbuf, 5)
95 assert rmr.get_payload(sbuf) == b'{"foo": "bar"}'
96 assert sbuf.contents.mtype == 666
97 assert sbuf.contents.state == 0
98 assert sbuf.contents.len == 14
101 def test_alloc(monkeypatch):
103 test alloc with all fields set
105 rmr_mocks.patch_rmr(monkeypatch)
106 sbuf = rmr.rmr_alloc_msg(
107 MRC, SIZE, payload=b"foo", gen_transaction_id=True, mtype=5, meid=b"mee", sub_id=234, fixed_transaction_id=b"t" * 32
109 summary = rmr.message_summary(sbuf)
110 assert summary["payload"] == b"foo"
111 assert summary["transaction id"] == b"t" * 32
112 assert summary["message type"] == 5
113 assert summary["meid"] == b"mee"
114 assert summary["subscription id"] == 234