Commit eef62d04 authored by Bogdan's avatar Bogdan

Created Unit Test for duplicated Transaction

parent fbb7f1ab
......@@ -97,4 +97,13 @@ class Repository(MongoRepositoryBase):
projection={'_id': 0})
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
......@@ -84,18 +84,30 @@ class MessageHandler:
return
nodes = []
unique_id_list = []
for layer in layers:
node = {}
for prop in layer.total_properties:
node[prop] = content["properties"][prop]
node["layer_name"] = layer.layer_name
node["table"] = layer.table
node["use_case"] = layer.use_case
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:
self._repository.add_layer_nodes(nodes)
......
......@@ -179,10 +179,12 @@ class MessageHandler:
try:
reference = self._mongo_repo.get_transaction_with_id(transaction.id())
if reference != None:
LOGGER.error("Found duplicate")
self._mongo_repo.add_duplicated_transaction(transaction)
return #TODO should return? or continue the message to semantic linking?
return
except ValueError as e:
LOGGER.error(f"{e}, could not insert duplicated node.")
return
try:
self._mongo_repo.add_transaction(transaction)
......
......@@ -10,6 +10,7 @@ class DummyMongoRepo:
def __init__(self):
self.added_transactions = []
self.duplicated_transactions = []
def insert_trace(self, trace):
self.last_trace = trace
......@@ -17,6 +18,22 @@ class DummyMongoRepo:
def add_transaction(self, 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.dummy_rest_fetcher import DummyRestFetcher
......@@ -37,7 +54,7 @@ class Test_MessageHandler(unittest.TestCase):
{ 'type': 'blockchain-transaction',
'content':
{
"ApplicationType": "string",
"ApplicationType": "paper",
"docType": "string",
"Metadata": {},
"ResourceIds": "string",
......@@ -111,5 +128,14 @@ class Test_MessageHandler(unittest.TestCase):
self.assertEqual('semantic-linking', self.msg_sender.last_message['key'])
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__':
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