Skip to content

Commit

Permalink
test: ProtoIO read and write to file
Browse files Browse the repository at this point in the history
  • Loading branch information
mishig25 committed Oct 15, 2018
1 parent cc79749 commit d7fe779
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
15 changes: 14 additions & 1 deletion bfcp/test_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import unittest
from config import *
from protos.bfcp_pb2 import ConnectionRoutingParams

class TestConfig(unittest.TestCase):
def test_global_var(self):
value = 5
self.assertEqual(GLOBAL_VARS['MAX_HOPS_WITHOUT_END_NODE'], value)
self.assertEqual(GLOBAL_VARS['MAX_HOPS_WITHOUT_END_NODE'], value)

def test_proto_write(self):
test_proto_obj = ConnectionRoutingParams()
test_proto_obj.uuid = "8b109c15-5d83-4ec6-9cd0-32abda2c97af"
test_proto_obj.remaining_hops = 5

ProtoIO.write_to_file('test_proto_io.txt', test_proto_obj)

def test_proto_read(self):
test_proto_obj = ProtoIO.read_from_file('test_proto_io.txt', ConnectionRoutingParams())
self.assertEqual(test_proto_obj.uuid, "8b109c15-5d83-4ec6-9cd0-32abda2c97af")
self.assertEqual(test_proto_obj.remaining_hops, 5)
22 changes: 13 additions & 9 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@ class ProtoIO:
"""
Helper class that reads/writes proto object into file
"""

def read_from_file(self, file_path: str, proto_type: BouncyMessage) -> BouncyMessage:
@staticmethod
def read_from_file(file_path: str, proto_type_instance: BouncyMessage) -> BouncyMessage:
"""
:param file_dir: directory of file to be read
:param file_path: directory of file to be read
:param proto_type_instance: NOTE: must be instance like NodeTable(), NOT NodeTable
:return: NodeTable from reading a file
usage: <protoio>.read_from_file(<file-path>, NodeTable())
usage: ProtoIO.read_from_file(<file-path>, NodeTable())
"""
text_proto = open(file_path, 'r')
proto_object = text_format.Parse(text_proto, proto_type)
text_proto_file = open(file_path, 'r')
text_proto = text_proto_file.read()
text_proto_file.close()
proto_object = text_format.Parse(text_proto, proto_type_instance)
return proto_object

def write_to_file(self, file_path: str, proto_object: BouncyMessage) -> None:
@staticmethod
def write_to_file(file_path: str, proto_object: BouncyMessage) -> None:
"""
Writes NodeTable object into file
:param file_dir: directory of file to be written
usage: <protoio>.write_to_file(<file-path>, NodeTable())
:param file_path: directory of file to be written
usage: ProtoIO.write_to_file(<file-path>, NodeTable())
"""
text_proto = text_format.MessageToString(proto_object)
file = open(file_path, 'w')
Expand Down
2 changes: 2 additions & 0 deletions test_proto_io.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
uuid: "8b109c15-5d83-4ec6-9cd0-32abda2c97af"
remaining_hops: 5

0 comments on commit d7fe779

Please sign in to comment.