Commit eef62d04 authored by Bogdan's avatar Bogdan

Created Unit Test for duplicated Transaction

parent fbb7f1ab
...@@ -97,4 +97,13 @@ class Repository(MongoRepositoryBase): ...@@ -97,4 +97,13 @@ class Repository(MongoRepositoryBase):
projection={'_id': 0}) projection={'_id': 0})
return list(entries) return list(entries)
def get_nodes_from_ids(self, unique_id_list: List) -> List[str]:
result = list(super().get_entries(self._transaction_collection, projection={'_id': False}, selection={"UniqueID": { "$in": unique_id_list}}))
if len(result) > 0:
return result
return None
# endregion # endregion
...@@ -84,18 +84,30 @@ class MessageHandler: ...@@ -84,18 +84,30 @@ class MessageHandler:
return return
nodes = [] nodes = []
unique_id_list = []
for layer in layers: for layer in layers:
node = {} node = {}
for prop in layer.total_properties: for prop in layer.total_properties:
node[prop] = content["properties"][prop] node[prop] = content["properties"][prop]
node["layer_name"] = layer.layer_name node["layer_name"] = layer.layer_name
node["table"] = layer.table node["table"] = layer.table
node["use_case"] = layer.use_case node["use_case"] = layer.use_case
nodes.append(node) nodes.append(node)
unique_id_list.append(node["UniqueId"])
#check for duplicates
#TODO EDIT NODES_IN_DB RETURN
nodes_in_db = self._repository.get_nodes_from_ids(unique_id_list)
if len(nodes_in_db) > 0: #found duplicates
#remove duplicates from nodes:
for node in nodes:
if node["UniqueId"] in nodes_in_db:
nodes.remove(node)
#no duplicates anymore
if len(nodes) > 0: if len(nodes) > 0:
self._repository.add_layer_nodes(nodes) self._repository.add_layer_nodes(nodes)
......
...@@ -179,10 +179,12 @@ class MessageHandler: ...@@ -179,10 +179,12 @@ class MessageHandler:
try: try:
reference = self._mongo_repo.get_transaction_with_id(transaction.id()) reference = self._mongo_repo.get_transaction_with_id(transaction.id())
if reference != None: if reference != None:
LOGGER.error("Found duplicate")
self._mongo_repo.add_duplicated_transaction(transaction) self._mongo_repo.add_duplicated_transaction(transaction)
return #TODO should return? or continue the message to semantic linking? return
except ValueError as e: except ValueError as e:
LOGGER.error(f"{e}, could not insert duplicated node.") LOGGER.error(f"{e}, could not insert duplicated node.")
return
try: try:
self._mongo_repo.add_transaction(transaction) self._mongo_repo.add_transaction(transaction)
......
...@@ -10,6 +10,7 @@ class DummyMongoRepo: ...@@ -10,6 +10,7 @@ class DummyMongoRepo:
def __init__(self): def __init__(self):
self.added_transactions = [] self.added_transactions = []
self.duplicated_transactions = []
def insert_trace(self, trace): def insert_trace(self, trace):
self.last_trace = trace self.last_trace = trace
...@@ -17,6 +18,22 @@ class DummyMongoRepo: ...@@ -17,6 +18,22 @@ class DummyMongoRepo:
def add_transaction(self, transaction): def add_transaction(self, transaction):
self.added_transactions.append(transaction) self.added_transactions.append(transaction)
def get_transaction_with_id(self, unique_id: str):
result = []
for trans in self.added_transactions:
transID = trans.id()
if transID == unique_id:
result.append(trans)
if len(result) > 0:
return result
return None
def add_duplicated_transaction(self, transaction):
self.duplicated_transactions.append(transaction)
from messaging.DummyMessageManager import DummyMessageManager as DummyMessageSender from messaging.DummyMessageManager import DummyMessageManager as DummyMessageSender
from messaging.dummy_rest_fetcher import DummyRestFetcher from messaging.dummy_rest_fetcher import DummyRestFetcher
...@@ -37,7 +54,7 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -37,7 +54,7 @@ class Test_MessageHandler(unittest.TestCase):
{ 'type': 'blockchain-transaction', { 'type': 'blockchain-transaction',
'content': 'content':
{ {
"ApplicationType": "string", "ApplicationType": "paper",
"docType": "string", "docType": "string",
"Metadata": {}, "Metadata": {},
"ResourceIds": "string", "ResourceIds": "string",
...@@ -111,5 +128,14 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -111,5 +128,14 @@ class Test_MessageHandler(unittest.TestCase):
self.assertEqual('semantic-linking', self.msg_sender.last_message['key']) self.assertEqual('semantic-linking', self.msg_sender.last_message['key'])
self.assertEqual('new-trace', json.loads(self.msg_sender.last_message['msg'])["type"]) self.assertEqual('new-trace', json.loads(self.msg_sender.last_message['msg'])["type"])
def test_handleblockchain_duplicateTrace(self):
msg = self._get_valid_message()
msg2 = self._get_valid_message()
msg = eval(msg)
msg2 = eval(msg2)
self.handler.handle_blockchain_transaction(msg['content'])
self.handler.handle_blockchain_transaction(msg2['content'])
self.assertEqual(len(self.repo.added_transactions),len(self.repo.duplicated_transactions))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment