github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/sdk/python/tests/test_context.py (about) 1 # Copyright 2017 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 16 import unittest 17 from unittest.mock import Mock 18 19 from collections import OrderedDict 20 21 from sawtooth_sdk.processor.context import Context 22 from sawtooth_sdk.messaging.future import Future 23 from sawtooth_sdk.messaging.future import FutureResult 24 25 from sawtooth_sdk.protobuf.validator_pb2 import Message 26 from sawtooth_sdk.protobuf.state_context_pb2 import TpStateEntry 27 from sawtooth_sdk.protobuf.state_context_pb2 import TpStateGetRequest 28 from sawtooth_sdk.protobuf.state_context_pb2 import TpStateGetResponse 29 from sawtooth_sdk.protobuf.state_context_pb2 import TpStateSetRequest 30 from sawtooth_sdk.protobuf.state_context_pb2 import TpStateSetResponse 31 from sawtooth_sdk.protobuf.state_context_pb2 import TpStateDeleteRequest 32 from sawtooth_sdk.protobuf.state_context_pb2 import TpStateDeleteResponse 33 from sawtooth_sdk.protobuf.state_context_pb2 import TpReceiptAddDataRequest 34 from sawtooth_sdk.protobuf.state_context_pb2 import TpReceiptAddDataResponse 35 from sawtooth_sdk.protobuf.state_context_pb2 import TpEventAddRequest 36 from sawtooth_sdk.protobuf.state_context_pb2 import TpEventAddResponse 37 from sawtooth_sdk.protobuf.events_pb2 import Event 38 39 40 class ContextTest(unittest.TestCase): 41 def setUp(self): 42 self.context_id = "test" 43 self.mock_stream = Mock() 44 self.context = Context(self.mock_stream, self.context_id) 45 self.addresses = ["a", "b", "c"] 46 self.data = [addr.encode() for addr in self.addresses] 47 48 def _make_future(self, message_type, content): 49 f = Future(self.context_id) 50 f.set_result(FutureResult( 51 message_type=message_type, 52 content=content)) 53 return f 54 55 def _make_entries(self, protobuf=True): 56 if protobuf: 57 return [ 58 TpStateEntry(address=a, data=d) 59 for a, d in zip(self.addresses, self.data) 60 ] 61 62 entries = OrderedDict() 63 for a, d in zip(self.addresses, self.data): 64 entries[a] = d 65 return entries 66 67 def test_state_get(self): 68 """Tests that State gets addresses correctly.""" 69 self.mock_stream.send.return_value = self._make_future( 70 message_type=Message.TP_STATE_GET_RESPONSE, 71 content=TpStateGetResponse( 72 status=TpStateGetResponse.OK, 73 entries=self._make_entries()).SerializeToString()) 74 75 self.context.get_state(self.addresses) 76 77 self.mock_stream.send.assert_called_with( 78 Message.TP_STATE_GET_REQUEST, 79 TpStateGetRequest( 80 context_id=self.context_id, 81 addresses=self.addresses).SerializeToString()) 82 83 def test_state_set(self): 84 """Tests that State sets addresses correctly.""" 85 self.mock_stream.send.return_value = self._make_future( 86 message_type=Message.TP_STATE_SET_RESPONSE, 87 content=TpStateSetResponse( 88 status=TpStateSetResponse.OK, 89 addresses=self.addresses).SerializeToString()) 90 91 self.context.set_state(self._make_entries(protobuf=False)) 92 93 self.mock_stream.send.assert_called_with( 94 Message.TP_STATE_SET_REQUEST, 95 TpStateSetRequest( 96 context_id=self.context_id, 97 entries=self._make_entries()).SerializeToString()) 98 99 def test_state_delete(self): 100 """Tests that State deletes addresses correctly.""" 101 self.mock_stream.send.return_value = self._make_future( 102 message_type=Message.TP_STATE_DELETE_RESPONSE, 103 content=TpStateDeleteResponse( 104 status=TpStateDeleteResponse.OK, 105 addresses=self.addresses).SerializeToString()) 106 107 self.context.delete_state(self.addresses) 108 109 self.mock_stream.send.assert_called_with( 110 Message.TP_STATE_DELETE_REQUEST, 111 TpStateDeleteRequest( 112 context_id=self.context_id, 113 addresses=self.addresses).SerializeToString()) 114 115 def test_add_receipt_data(self): 116 """Tests that State adds receipt data correctly.""" 117 self.mock_stream.send.return_value = self._make_future( 118 message_type=Message.TP_RECEIPT_ADD_DATA_RESPONSE, 119 content=TpReceiptAddDataResponse( 120 status=TpReceiptAddDataResponse.OK).SerializeToString()) 121 122 self.context.add_receipt_data(b"test") 123 124 self.mock_stream.send.assert_called_with( 125 Message.TP_RECEIPT_ADD_DATA_REQUEST, 126 TpReceiptAddDataRequest( 127 context_id=self.context_id, 128 data=b"test").SerializeToString()) 129 130 def test_add_event(self): 131 """Tests that State adds events correctly.""" 132 self.mock_stream.send.return_value = self._make_future( 133 message_type=Message.TP_EVENT_ADD_RESPONSE, 134 content=TpEventAddResponse( 135 status=TpEventAddResponse.OK).SerializeToString()) 136 137 self.context.add_event("test", [("test", "test")], b"test") 138 139 self.mock_stream.send.assert_called_with( 140 Message.TP_EVENT_ADD_REQUEST, 141 TpEventAddRequest( 142 context_id=self.context_id, 143 event=Event( 144 event_type="test", 145 attributes=[Event.Attribute(key="test", value="test")], 146 data=b"test")).SerializeToString())