Add ability to set xaction deterministically, bug fixes
[ric-plt/lib/rmr.git] / src / bindings / rmr-python / tests / test_rmr_mocks.py
1 # ==================================================================================
2 #       Copyright (c) 2019 Nokia
3 #       Copyright (c) 2018-2019 AT&T Intellectual Property.
4 #
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
8 #
9 #          http://www.apache.org/licenses/LICENSE-2.0
10 #
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 # ==================================================================================
17 import pytest
18 from rmr import rmr
19 from rmr.rmr_mocks import rmr_mocks
20
21
22 MRC = None
23 SIZE = 256
24
25
26 def _partial_dict_comparison(subset_dict, target_dict):
27     """
28     Compares that target_dict[k] == subset_dict[k] for all k <- subset_dict
29     """
30     for k, v in subset_dict.items():
31         assert k in target_dict
32         assert target_dict[k] == subset_dict[k]
33
34
35 def test_send_mock(monkeypatch):
36     """
37     tests the send mock
38     """
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)
43
44     expected = {
45         "meid": None,
46         "message source": "localtest:80",
47         "message state": 0,
48         "message type": 0,
49         "message status": "RMR_OK",
50         "payload": b"testttt",
51         "payload length": 7,
52         "payload max size": 4096,
53         "subscription id": 0,
54     }
55     _partial_dict_comparison(expected, rmr.message_summary(sbuf))
56
57     # set the mtype
58     sbuf.contents.mtype = 666
59
60     # send it (the fake send sets the state, and touches nothing else)
61     sbuf = rmr.rmr_send_msg(MRC, sbuf)
62
63     expected = {
64         "meid": None,
65         "message source": "localtest:80",
66         "message state": 12,
67         "message type": 666,
68         "message status": "RMR_ERR_TIMEOUT",
69         "payload": None,
70         "payload length": 7,
71         "payload max size": 4096,
72         "subscription id": 0,
73     }
74     _partial_dict_comparison(expected, rmr.message_summary(sbuf))
75
76
77 def test_rcv_mock(monkeypatch):
78     """
79     tests the rmr recieve mocking generator
80     """
81     rmr_mocks.patch_rmr(monkeypatch)
82     sbuf = rmr.rmr_alloc_msg(MRC, SIZE)
83
84     # test rcv
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
91
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
99
100
101 def test_alloc(monkeypatch):
102     """
103     test alloc with all fields set
104     """
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
108     )
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