Skip to content

Commit

Permalink
Proto gen for python bindings
Browse files Browse the repository at this point in the history
Signed-off-by: Kitsu <[email protected]>
  • Loading branch information
l4l authored and kamilsa committed Dec 27, 2017
1 parent 3fb35f6 commit 406eb38
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
12 changes: 12 additions & 0 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ function(compile_proto_to_grpc_cpp PROTO)
endfunction()


function(compile_proto_to_python PROTO)
string(REGEX REPLACE "\\.proto$" "_pb2.py" PY_PB ${PROTO})
add_custom_command(
OUTPUT ${SWIG_BUILD_DIR}/${PY_PB}
COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH=${protobuf_LIBRARY_DIR}:$ENV{LD_LIBRARY_PATH} "${protoc_EXECUTABLE}"
ARGS -I${protobuf_INCLUDE_DIR} -I. --python_out=${SWIG_BUILD_DIR} ${PROTO}
DEPENDS protoc
WORKING_DIRECTORY ${IROHA_SCHEMA_DIR}
)
endfunction()


macro(set_target_description target description url commit)
set_package_properties(${target}
PROPERTIES
Expand Down
14 changes: 12 additions & 2 deletions test/module/shared_model/bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ if (SWIG_PYTHON)
add_test(NAME python_builders_test
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/builder-test.py
WORKING_DIRECTORY ${SWIG_BUILD_DIR})

foreach(item "block" "commands" "primitive")
compile_proto_to_python("${item}.proto")
list(APPEND PROTO_SWIG_DEPS "${SWIG_BUILD_DIR}/${item}_pb2.py")
endforeach(item)

add_custom_target(python_builders_test
DEPENDS "${PROTO_SWIG_DEPS}")
set_tests_properties(python_builders_test
PROPERTIES DEPENDS builders
ENVIRONMENT "PYTHONPATH=${SWIG_BUILD_DIR}")
PROPERTIES REQUIRED_FILES "${PROTO_SWIG_DEPS}"
ENVIRONMENT "PYTHONPATH=$ENV{PYTHONPATH}:${SWIG_BUILD_DIR}"
DEPENDS bindings)

endif()

if (SWIG_JAVA)
Expand Down
37 changes: 24 additions & 13 deletions test/module/shared_model/bindings/builder-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import unittest
import time

from google.protobuf.message import DecodeError
import block_pb2 as blk

class BuilderTest(unittest.TestCase):
def test_empty_tx(self):
with self.assertRaises(ValueError):
iroha.ModelBuilder().build()
iroha.ModelTransactionBuilder().build()

def generate_base(self):
return iroha.ModelBuilder().txCounter(123)\
return iroha.ModelTransactionBuilder().txCounter(123)\
.createdTime(int(time.time() * 1000))\
.creatorAccountId("admin@test")\

Expand Down Expand Up @@ -41,44 +44,52 @@ def test_add_peer_with_invalid_host(self):
with self.assertRaises(ValueError) as err:
self.generate_base().addPeer(k, self.keys.publicKey()).build()

def proto_size(self, tx):
return iroha.ModelTransactionProto().signAndAddSignature(tx, self.keys).size()
def proto(self, tx):
return iroha.ModelProtoTransaction().signAndAddSignature(tx, self.keys)

def check_proto_tx(self, blob):
try:
blk.Transaction.FromString(''.join(map(chr, blob.blob())))
except DecodeError, e:
print(e)
return False
return True

def test_keygen(self):
self.assertEqual(len(self.keys.privateKey().blob()), 64)
self.assertEqual(len(self.keys.publicKey().blob()), 32)

def test_add_peer(self):
tx = self.builder.addPeer("123.123.123.123", self.keys.publicKey()).build()
self.assertEqual(self.proto_size(tx), 180)
tx = self.builder.addPeer("123.123.123.123:123", self.keys.publicKey()).build()
self.assertTrue(self.check_proto_tx(self.proto(tx)))

def test_add_signatory(self):
tx = self.builder.addSignatory("admin@test", self.keys.publicKey()).build()
self.assertEqual(self.proto_size(tx), 175)
self.assertTrue(self.check_proto_tx(self.proto(tx)))

def test_add_asset_quantity(self):
tx = self.builder.addAssetQuantity("admin@test", "asset#domain", "12.345").build()
self.assertEqual(self.proto_size(tx), 164)
self.assertTrue(self.check_proto_tx(self.proto(tx)))

def test_remove_signatory(self):
tx = self.builder.removeSignatory("admin@test", self.keys.publicKey()).build()
self.assertEqual(self.proto_size(tx), 175)
self.assertTrue(self.check_proto_tx(self.proto(tx)))

def test_create_account(self):
tx = self.builder.createAccount("admin", "domain", self.keys.publicKey()).build()
self.assertEqual(self.proto_size(tx), 178)
self.assertTrue(self.check_proto_tx(self.proto(tx)))

def test_create_domain(self):
tx = self.builder.createDomain("domain", "role").build()
self.assertEqual(self.proto_size(tx), 143)
self.assertTrue(self.check_proto_tx(self.proto(tx)))

def test_set_account_quorum(self):
tx = self.builder.setAccountQuorum("admin@test", 123).build()
self.assertEqual(self.proto_size(tx), 143)
self.assertTrue(self.check_proto_tx(self.proto(tx)))

def test_transfer_asset(self):
tx = self.builder.transferAsset("from@test", "to@test", "asset#test", "description", "123.456").build()
self.assertEqual(self.proto_size(tx), 184)
self.assertTrue(self.check_proto_tx(self.proto(tx)))

if __name__ == '__main__':
unittest.main()

0 comments on commit 406eb38

Please sign in to comment.