Commit cd5c2e2d authored by Bogdan's avatar Bogdan

Fixed edge case for more than 1 trace with same id in the DB

parent d670155e
...@@ -44,8 +44,8 @@ class Repository(MongoRepositoryBase): ...@@ -44,8 +44,8 @@ class Repository(MongoRepositoryBase):
def get_transaction_with_id(self, unique_id: str) -> Transaction: def get_transaction_with_id(self, unique_id: str) -> Transaction:
result = list(super().get_entries(self._transaction_collection, projection={'_id': False}, selection={"UniqueID": unique_id})) result = list(super().get_entries(self._transaction_collection, projection={'_id': False}, selection={"UniqueID": unique_id}))
if len(result) == 1: if len(result) >= 1:
return Transaction.from_serializable_dict(result[0]) return Transaction.from_serializable_dict(result)
return None return None
......
...@@ -180,12 +180,13 @@ class MessageHandler: ...@@ -180,12 +180,13 @@ class MessageHandler:
#check for duplicates #check for duplicates
try: try:
reference = self._mongo_repo.get_transaction_with_id(transaction.id()) references = self._mongo_repo.get_transaction_with_id(transaction.id())
if reference != None: if references != None:
if (reference[0].table == transaction.table) and (reference[0].use_case == transaction.use_case): for item in references:
LOGGER.error("Found duplicate") if (item.table == transaction.table) and (item.use_case == transaction.use_case):
self._mongo_repo.add_duplicated_transaction(transaction) LOGGER.error("Found duplicate")
return self._mongo_repo.add_duplicated_transaction(transaction)
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 return
......
...@@ -178,6 +178,7 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -178,6 +178,7 @@ class Test_MessageHandler(unittest.TestCase):
self.handler.handle_blockchain_transaction(msg['content']) self.handler.handle_blockchain_transaction(msg['content'])
self.handler.handle_blockchain_transaction(msg2['content']) self.handler.handle_blockchain_transaction(msg2['content'])
self.assertEqual(len(self.repo.added_transactions),len(self.repo.duplicated_transactions)) self.assertEqual(len(self.repo.added_transactions),len(self.repo.duplicated_transactions))
self.assertEqual(len(self.repo.added_transactions),1)
def test_handleBlockchainTransaction_duplicateTraceDifferentTable_bothTransactionsAddedAsUnique(self): def test_handleBlockchainTransaction_duplicateTraceDifferentTable_bothTransactionsAddedAsUnique(self):
msg = self._get_valid_message() msg = self._get_valid_message()
...@@ -197,5 +198,23 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -197,5 +198,23 @@ class Test_MessageHandler(unittest.TestCase):
self.handler.handle_blockchain_transaction(msg2['content']) self.handler.handle_blockchain_transaction(msg2['content'])
self.assertEqual(len(self.repo.added_transactions),2) self.assertEqual(len(self.repo.added_transactions),2)
def test_handleBlockchainTransaction_multipleTransactions_3AddedUnique2Duplicate(self):
msg = self._get_valid_message()
msg2 = self._get_valid_message2()
msg3 = self._get_valid_message3()
msg4 = self._get_valid_message3()
msg5 = self._get_valid_message3()
msg = eval(msg)
msg2 = eval(msg2)
msg3 = eval(msg3)
msg4 = eval(msg4)
msg5 = eval(msg5)
self.handler.handle_blockchain_transaction(msg['content'])
self.handler.handle_blockchain_transaction(msg2['content'])
self.handler.handle_blockchain_transaction(msg3['content'])
self.handler.handle_blockchain_transaction(msg4['content'])
self.handler.handle_blockchain_transaction(msg5['content'])
self.assertEqual(len(self.repo.added_transactions),3)
self.assertEqual(len(self.repo.duplicated_transactions),2)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
\ No newline at end of file
...@@ -18,7 +18,7 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) ...@@ -18,7 +18,7 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def send_transaction_to_rest_gateway(transaction: dict): def send_transaction_to_rest_gateway(transaction: dict):
# token from Rest Gateway to authorize # token from Rest Gateway to authorize
JWT_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InJlZ3VsYXJAaXRlYy5hYXUuYXQiLCJjcmVhdGVkX2F0IjoiMjAyMS0wMi0wOCAxMzo0NzoxOC40NzUxMjEiLCJ2YWxpZF91bnRpbCI6IjIwMjEtMDItMDkgMTM6NDc6MTguNDc1MTIxIn0.DWY9c0X2XQJDz0Ef35-k1IVY6GWf00ogaVOCeX8Irlo' JWT_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InJlZ3VsYXJAaXRlYy5hYXUuYXQiLCJjcmVhdGVkX2F0IjoiMjAyMS0wMy0xNiAxMzoxNDoyMS42MDc1NjciLCJ2YWxpZF91bnRpbCI6IjIwMjEtMDMtMTcgMTM6MTQ6MjEuNjA3NTY3In0.ZGObriEDWYo1BgiYN3pQSosS7UuNrq10GSCSjmRHSAw'
res = requests.post( res = requests.post(
url = 'https://articonf1.itec.aau.at:30401/api/trace', url = 'https://articonf1.itec.aau.at:30401/api/trace',
...@@ -47,15 +47,49 @@ if __name__ == '__main__': ...@@ -47,15 +47,49 @@ if __name__ == '__main__':
transaction['ApplicationType'] = 'reddit' transaction['ApplicationType'] = 'reddit'
transaction['docType'] = 'reddit' transaction['docType'] = 'reddit'
for key, value in obj_dict.items(): for key, value in obj_dict.items():
transaction[key] = value transaction[key] = value
#####################TEEEEEEEEEEST###########
transaction = {
#"type": "blockchain-transaction",
"ApplicationType": "debug",
"docType": "pizza",
"id": 1,
"name": "MEXICAANA",
"dough": {
"type": "wheat",
"spinach": False
},
"toppings": [
{
"name": "Tomato Sauce",
"price": 1.00
},
{
"name": "Cheese",
"price": 0.50
},
{
"name": "Chilli Oil",
"price": 0.50
},
{
"name": "Peppers",
"price": 1.50
}
]
}
###################FIN TEEEEST ###############
send_transaction_to_rest_gateway(transaction) send_transaction_to_rest_gateway(transaction)
summ+=1 summ+=1
if (summ % 1000 == 0 ): if (summ % 1000 == 0 ):
print ("Uploaded " + str(summ) + " transactions.") print ("Uploaded " + str(summ) + " transactions.")
if summ >= 1:
break
print ("TOTAL Uploaded " + str(summ) + " transactions.") print ("TOTAL Uploaded " + str(summ) + " transactions.")
\ 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