Repository: Naruno/Naruno (language: python)

Issue:
tests: Adding unit test for Status function



Discussion / hints:

Fix patch:
diff --git a/decentra_network/lib/status.py b/decentra_network/lib/status.py
index 7bb6fb1e..f230fb21 100644
--- a/decentra_network/lib/status.py
+++ b/decentra_network/lib/status.py
@@ -6,47 +6,60 @@
 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
 import time
 
+from decentra_network.blockchain.block.block_main import Block
 from decentra_network.blockchain.block.get_block import GetBlock
 from decentra_network.node.unl import Unl
-from decentra_network.transactions.my_transactions.get_my_transaction import (
-    GetMyTransaction,
-)
+from decentra_network.transactions.my_transactions.get_my_transaction import \
+    GetMyTransaction
 
 
-def Status():
+def Status(
+    custom_TEMP_BLOCK_PATH: str = None,
+    custom_UNL_NODES_PATH: str = None,
+    custom_first_block: Block = None,
+    custom_new_block: Block = None,
+    custom_connections: list = None,
+    custom_transactions: list = None,
+) -> dict:
     """
     Returns the status of the network.
     """
 
-    first_block = GetBlock()
-    start_time = time.time()
-    while True:
-
-        time.sleep(15)
-        new_time = time.time()
-        new_block = GetBlock()
-
-        status_json = {
-            "status": "",
-            "first_block": str(first_block.__dict__),
-            "new_block": str(new_block.__dict__),
-            "last_transaction_of_block": str(new_block.validating_list[-1])
-            if len(new_block.validating_list) > 0
-            else "",
-            "transactions_of_us": str(
-                [f"{str(i[0].__dict__)} | {str(i[1])}" for i in GetMyTransaction()]
-            ),
-            "connected_nodes": [
-                str(the_connections)
-                for the_connections in Unl.get_as_node_type(Unl.get_unl_nodes())
-            ],
-        }
-
-        status_json["status"] = (
-            "Not working"
-            if (first_block.sequance_number + first_block.empty_block_number)
-            == (new_block.sequance_number + new_block.empty_block_number)
-            else "Working"
-        )
-
-        return status_json
+    first_block = (GetBlock(custom_TEMP_BLOCK_PATH=custom_TEMP_BLOCK_PATH)
+                   if custom_first_block is None else custom_first_block)
+
+    time.sleep(15)
+    new_block = (GetBlock(custom_TEMP_BLOCK_PATH=custom_TEMP_BLOCK_PATH)
+                 if custom_new_block is None else custom_new_block)
+
+    connections = (Unl.get_as_node_type(
+        Unl.get_unl_nodes(custom_UNL_NODES_PATH=custom_UNL_NODES_PATH))
+                   if custom_connections is None else custom_connections)
+    connected_nodes = [
+        str(f"{the_connections.host}:{the_connections.port}")
+        for the_connections in connections
+    ]
+
+    transactions = (GetMyTransaction()
+                    if custom_transactions is None else custom_transactions)
+    transactions_of_us = str(
+        [f"{str(i[0].__dict__)} | {str(i[1])}" for i in transactions])
+
+    last_transaction_of_block = (str(new_block.validating_list[-1].dump_json())
+                                 if len(new_block.validating_list) > 0 else "")
+
+    status_json = {
+        "status": "",
+        "first_block": str(first_block.__dict__),
+        "new_block": str(new_block.__dict__),
+        "last_transaction_of_block": last_transaction_of_block,
+        "transactions_of_us": transactions_of_us,
+        "connected_nodes": connected_nodes,
+    }
+
+    status_json["status"] = ("Not working" if (first_block.sequance_number +
+                                               first_block.empty_block_number)
+                             == (new_block.sequance_number +
+                                 new_block.empty_block_number) else "Working")
+
+    return status_json


Test patch:
diff --git a/tests/unit_tests/test_lib.py b/tests/unit_tests/test_lib.py
index 88c6f220..e92e300d 100644
--- a/tests/unit_tests/test_lib.py
+++ b/tests/unit_tests/test_lib.py
@@ -6,11 +6,21 @@
 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
 import os
 import sys
+import time
+
+from decentra_network.blockchain.block.block_main import Block
+from decentra_network.lib.status import Status
+from decentra_network.transactions.transaction import Transaction
 
 sys.path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
 import unittest
 from unittest import mock
 
+from decentra_network.config import (
+    CONNECTED_NODES_PATH, LOADING_ACCOUNTS_PATH, LOADING_BLOCK_PATH,
+    LOADING_BLOCKSHASH_PART_PATH, LOADING_BLOCKSHASH_PATH,
+    PENDING_TRANSACTIONS_PATH, TEMP_ACCOUNTS_PATH, TEMP_BLOCK_PATH,
+    TEMP_BLOCKSHASH_PART_PATH, TEMP_BLOCKSHASH_PATH, UNL_NODES_PATH)
 from decentra_network.lib.clean_up import CleanUp_tests
 from decentra_network.lib.mix.mixlib import (banner_maker, ended_text_centered,
                                              menu_maker, menu_seperator,
@@ -18,6 +28,8 @@
                                              printcentertext, question_maker,
                                              quit_menu_maker,
                                              starting_text_centered)
+from decentra_network.node.server.server import server
+from decentra_network.node.unl import Unl
 
 
 class Test_Lib(unittest.TestCase):
@@ -26,6 +38,177 @@ class Test_Lib(unittest.TestCase):
     def setUpClass(cls):
         CleanUp_tests()
 
+        cls.custom_TEMP_BLOCK_PATH0 = TEMP_BLOCK_PATH.replace(
+            ".json", "_0.json").replace("temp_", "test_temp_")
+        cls.custom_TEMP_BLOCK_PATH1 = TEMP_BLOCK_PATH.replace(
+            ".json", "_1.json").replace("temp_", "test_temp_")
+        cls.custom_TEMP_BLOCK_PATH2 = TEMP_BLOCK_PATH.replace(
+            ".json", "_2.json").replace("temp_", "test_temp_")
+        cls.custom_LOADING_BLOCK_PATH0 = LOADING_BLOCK_PATH.replace(
+            ".json", "_0.json").replace("loading_", "test_loading_temp_")
+        cls.custom_LOADING_BLOCK_PATH1 = LOADING_BLOCK_PATH.replace(
+            ".json", "_1.json").replace("loading_", "test_loading_temp_")
+        cls.custom_LOADING_BLOCK_PATH2 = LOADING_BLOCK_PATH.replace(
+            ".json", "_2.json").replace("loading_", "test_loading_temp_")
+
+        cls.custom_TEMP_ACCOUNTS_PATH0 = TEMP_ACCOUNTS_PATH.replace(
+            ".db", "_0.db").replace("temp_", "test_temp_")
+        cls.custom_TEMP_ACCOUNTS_PATH1 = TEMP_ACCOUNTS_PATH.replace(
+            ".db", "_1.db").replace("temp_", "test_temp_")
+        cls.custom_TEMP_ACCOUNTS_PATH2 = TEMP_ACCOUNTS_PATH.replace(
+            ".db", "_2.db").replace("temp_", "test_temp_")
+        cls.custom_LOADING_ACCOUNTS_PATH0 = LOADING_ACCOUNTS_PATH.replace(
+            ".db", "_0.db").replace("loading_", "test_loading_temp_")
+        cls.custom_LOADING_ACCOUNTS_PATH1 = LOADING_ACCOUNTS_PATH.replace(
+            ".db", "_1.db").replace("loading_", "test_loading_temp_")
+        cls.custom_LOADING_ACCOUNTS_PATH2 = LOADING_ACCOUNTS_PATH.replace(
+            ".db", "_2.db").replace("loading_", "test_loading_temp_")
+
+        cls.custom_TEMP_BLOCKSHASH_PATH0 = TEMP_BLOCKSHASH_PATH.replace(
+            ".json", "_0.json").replace("temp_", "test_temp_")
+        cls.custom_TEMP_BLOCKSHASH_PATH1 = TEMP_BLOCKSHASH_PATH.replace(
+            ".json", "_1.json").replace("temp_", "test_temp_")
+        cls.custom_TEMP_BLOCKSHASH_PATH2 = TEMP_BLOCKSHASH_PATH.replace(
+            ".json", "_2.json").replace("temp_", "test_temp_")
+        cls.custom_LOADING_BLOCKSHASH_PATH0 = LOADING_BLOCKSHASH_PATH.replace(
+            ".json", "_0.json").replace("loading_", "test_loading_temp_")
+        cls.custom_LOADING_BLOCKSHASH_PATH1 = LOADING_BLOCKSHASH_PATH.replace(
+            ".json", "_1.json").replace("loading_", "test_loading_temp_")
+        cls.custom_LOADING_BLOCKSHASH_PATH2 = LOADING_BLOCKSHASH_PATH.replace(
+            ".json", "_2.json").replace("loading_", "test_loading_temp_")
+
+        cls.custom_TEMP_BLOCKSHASH_PART_PATH0 = TEMP_BLOCKSHASH_PART_PATH.replace(
+            ".json", "_0.json").replace("temp_", "test_temp_")
+        cls.custom_TEMP_BLOCKSHASH_PART_PATH1 = TEMP_BLOCKSHASH_PART_PATH.replace(
+            ".json", "_1.json").replace("temp_", "test_temp_")
+        cls.custom_TEMP_BLOCKSHASH_PART_PATH2 = TEMP_BLOCKSHASH_PART_PATH.replace(
+            ".json", "_2.json").replace("temp_", "test_temp_")
+        cls.custom_LOADING_BLOCKSHASH_PART_PATH0 = LOADING_BLOCKSHASH_PART_PATH.replace(
+            ".json", "_0.json").replace("loading_", "test_loading_temp_")
+        cls.custom_LOADING_BLOCKSHASH_PART_PATH1 = LOADING_BLOCKSHASH_PART_PATH.replace(
+            ".json", "_1.json").replace("loading_", "test_loading_temp_")
+        cls.custom_LOADING_BLOCKSHASH_PART_PATH2 = LOADING_BLOCKSHASH_PART_PATH.replace(
+            ".json", "_2.json").replace("loading_", "test_loading_temp_")
+
+        cls.custom_CONNECTED_NODES_PATH0 = CONNECTED_NODES_PATH.replace(
+            "connected_nodes", "connected_nodes_test_0")
+        cls.custom_CONNECTED_NODES_PATH1 = CONNECTED_NODES_PATH.replace(
+            "connected_nodes", "connected_nodes_test_1")
+        cls.custom_CONNECTED_NODES_PATH2 = CONNECTED_NODES_PATH.replace(
+            "connected_nodes", "connected_nodes_test_2")
+
+        cls.custom_PENDING_TRANSACTIONS_PATH0 = PENDING_TRANSACTIONS_PATH.replace(
+            "pending_transactions", "pending_transactions_test_0")
+        cls.custom_PENDING_TRANSACTIONS_PATH1 = PENDING_TRANSACTIONS_PATH.replace(
+            "pending_transactions", "pending_transactions_test_1")
+        cls.custom_PENDING_TRANSACTIONS_PATH2 = PENDING_TRANSACTIONS_PATH.replace(
+            "pending_transactions", "pending_transactions_test_2")
+
+        cls.node_0 = server(
+            "127.0.0.1",
+            10000,
+            save_messages=True,
+            custom_TEMP_BLOCK_PATH=cls.custom_TEMP_BLOCK_PATH0,
+            custom_LOADING_BLOCK_PATH=cls.custom_LOADING_BLOCK_PATH0,
+            custom_TEMP_ACCOUNTS_PATH=cls.custom_TEMP_ACCOUNTS_PATH0,
+            custom_LOADING_ACCOUNTS_PATH=cls.custom_LOADING_ACCOUNTS_PATH0,
+            custom_TEMP_BLOCKSHASH_PATH=cls.custom_TEMP_BLOCKSHASH_PATH0,
+            custom_LOADING_BLOCKSHASH_PATH=cls.custom_LOADING_BLOCKSHASH_PATH0,
+            custom_TEMP_BLOCKSHASH_PART_PATH=cls.
+            custom_TEMP_BLOCKSHASH_PART_PATH0,
+            custom_LOADING_BLOCKSHASH_PART_PATH=cls.
+            custom_LOADING_BLOCKSHASH_PART_PATH0,
+            custom_CONNECTED_NODES_PATH=cls.custom_CONNECTED_NODES_PATH0,
+            custom_PENDING_TRANSACTIONS_PATH=cls.
+            custom_PENDING_TRANSACTIONS_PATH0,
+            custom_variables=True,
+        )
+
+        cls.node_1 = server(
+            "127.0.0.1",
+            10001,
+            save_messages=True,
+            custom_TEMP_BLOCK_PATH=cls.custom_TEMP_BLOCK_PATH1,
+            custom_LOADING_BLOCK_PATH=cls.custom_LOADING_BLOCK_PATH1,
+            custom_TEMP_ACCOUNTS_PATH=cls.custom_TEMP_ACCOUNTS_PATH1,
+            custom_LOADING_ACCOUNTS_PATH=cls.custom_LOADING_ACCOUNTS_PATH1,
+            custom_TEMP_BLOCKSHASH_PATH=cls.custom_TEMP_BLOCKSHASH_PATH1,
+            custom_LOADING_BLOCKSHASH_PATH=cls.custom_LOADING_BLOCKSHASH_PATH1,
+            custom_TEMP_BLOCKSHASH_PART_PATH=cls.
+            custom_TEMP_BLOCKSHASH_PART_PATH1,
+            custom_LOADING_BLOCKSHASH_PART_PATH=cls.
+            custom_LOADING_BLOCKSHASH_PART_PATH1,
+            custom_CONNECTED_NODES_PATH=cls.custom_CONNECTED_NODES_PATH1,
+            custom_PENDING_TRANSACTIONS_PATH=cls.
+            custom_PENDING_TRANSACTIONS_PATH1,
+            custom_variables=True,
+        )
+        cls.node_2 = server(
+            "127.0.0.1",
+            10002,
+            save_messages=True,
+            custom_TEMP_BLOCK_PATH=cls.custom_TEMP_BLOCK_PATH2,
+            custom_LOADING_BLOCK_PATH=cls.custom_LOADING_BLOCK_PATH2,
+            custom_TEMP_ACCOUNTS_PATH=cls.custom_TEMP_ACCOUNTS_PATH2,
+            custom_LOADING_ACCOUNTS_PATH=cls.custom_LOADING_ACCOUNTS_PATH2,
+            custom_TEMP_BLOCKSHASH_PATH=cls.custom_TEMP_BLOCKSHASH_PATH2,
+            custom_LOADING_BLOCKSHASH_PATH=cls.custom_LOADING_BLOCKSHASH_PATH2,
+            custom_TEMP_BLOCKSHASH_PART_PATH=cls.
+            custom_TEMP_BLOCKSHASH_PART_PATH2,
+            custom_LOADING_BLOCKSHASH_PART_PATH=cls.
+            custom_LOADING_BLOCKSHASH_PART_PATH2,
+            custom_CONNECTED_NODES_PATH=cls.custom_CONNECTED_NODES_PATH2,
+            custom_PENDING_TRANSACTIONS_PATH=cls.
+            custom_PENDING_TRANSACTIONS_PATH2,
+            custom_variables=True,
+        )
+        Unl.save_new_unl_node(cls.node_0.id)
+        Unl.save_new_unl_node(cls.node_1.id)
+        Unl.save_new_unl_node(cls.node_2.id)
+        time.sleep(2)
+        cls.node_0.connect("127.0.0.1", 10001)
+        cls.node_0.connect("127.0.0.1", 10002)
+        time.sleep(2)
+        cls.node_2.connect("127.0.0.1", 10001)
+
+        print(cls.node_0.clients)
+        print(cls.node_1.clients)
+        print(cls.node_2.clients)
+        print("started")
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.node_0.stop()
+        cls.node_1.stop()
+        cls.node_2.stop()
+
+        time.sleep(2)
+
+        cls.node_1.join()
+        cls.node_2.join()
+        cls.node_0.join()
+
+        for a_client in cls.node_0.clients:
+            the_dict = {}
+            the_dict["id"] = a_client.id
+            the_dict["host"] = a_client.host
+            the_dict["port"] = a_client.port
+            cls.node_0.connected_node_delete(the_dict)
+
+        for a_client in cls.node_1.clients:
+            the_dict = {}
+            the_dict["id"] = a_client.id
+            the_dict["host"] = a_client.host
+            the_dict["port"] = a_client.port
+            cls.node_1.connected_node_delete(the_dict)
+
+        for a_client in cls.node_2.clients:
+            the_dict = {}
+            the_dict["id"] = a_client.id
+            the_dict["host"] = a_client.host
+            the_dict["port"] = a_client.port
+            cls.node_2.connected_node_delete(the_dict)
+
     def test_starting_text_centered(self):
         self.assertEqual(starting_text_centered(), "\nSTARTING\n")
 
@@ -94,5 +277,121 @@ def test_question_maker_other(self):
         with mock.patch("builtins.input", return_value=1):
             self.assertEqual(question_maker(mode="maina"), 1)
 
+    def test_status_not_working(self):
+        custom_first_block = Block("Onur")
+        custom_new_block = Block("Onur")
+        custom_connections = self.node_0.clients
+        the_transaction_json = {
+            "sequance_number": 1,
+            "signature":
+            "MEUCIHABt7ypkpvFlpqL4SuogwVuzMu2gGynVkrSw6ohZ/GyAiEAg2O3iOei1Ft/vQRpboX7Sm1OOey8a3a67wPJaH/FmVE=",
+            "fromUser":
+            "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE0AYA7B+neqfUA17wKh3OxC67K8UlIskMm9T2qAR+pl+kKX1SleqqvLPM5bGykZ8tqq4RGtAcGtrtvEBrB9DTPg==",
+            "toUser": "onur",
+            "data": "blockchain-lab",
+            "amount": 5000.0,
+            "transaction_fee": 0.02,
+            "transaction_time": 1656764224,
+        }
+        the_transaction = Transaction.load_json(the_transaction_json)
+        custom_transactions = [[the_transaction, "validated"]]
+        custom_new_block.validating_list = [the_transaction]
+        result = Status(
+            custom_first_block=custom_first_block,
+            custom_new_block=custom_new_block,
+            custom_connections=custom_connections,
+            custom_transactions=custom_transactions,
+        )
+        self.assertEqual(result["status"], "Not working")
+        self.assertEqual(result["last_transaction_of_block"],
+                         str(the_transaction.dump_json()))
+        self.assertEqual(
+            result["transactions_of_us"],
+            str([
+                f"{str(i[0].__dict__)} | {str(i[1])}"
+                for i in custom_transactions
+            ]),
+        )
+        self.assertEqual(result["connected_nodes"],
+                         ["127.0.0.1:10001", "127.0.0.1:10002"])
+
+    def test_status_empty(self):
+        custom_first_block = Block("Onur")
+        custom_new_block = Block("Onur")
+        custom_new_block.empty_block_number += 1
+        custom_connections = self.node_0.clients
+        the_transaction_json = {
+            "sequance_number": 1,
+            "signature":
+            "MEUCIHABt7ypkpvFlpqL4SuogwVuzMu2gGynVkrSw6ohZ/GyAiEAg2O3iOei1Ft/vQRpboX7Sm1OOey8a3a67wPJaH/FmVE=",
+            "fromUser":
+            "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE0AYA7B+neqfUA17wKh3OxC67K8UlIskMm9T2qAR+pl+kKX1SleqqvLPM5bGykZ8tqq4RGtAcGtrtvEBrB9DTPg==",
+            "toUser": "onur",
+            "data": "blockchain-lab",
+            "amount": 5000.0,
+            "transaction_fee": 0.02,
+            "transaction_time": 1656764224,
+        }
+        the_transaction = Transaction.load_json(the_transaction_json)
+        custom_transactions = [[the_transaction, "validated"]]
+        custom_new_block.validating_list = [the_transaction]
+        result = Status(
+            custom_first_block=custom_first_block,
+            custom_new_block=custom_new_block,
+            custom_connections=custom_connections,
+            custom_transactions=custom_transactions,
+        )
+        self.assertEqual(result["status"], "Working")
+        self.assertEqual(result["last_transaction_of_block"],
+                         str(the_transaction.dump_json()))
+        self.assertEqual(
+            result["transactions_of_us"],
+            str([
+                f"{str(i[0].__dict__)} | {str(i[1])}"
+                for i in custom_transactions
+            ]),
+        )
+        self.assertEqual(result["connected_nodes"],
+                         ["127.0.0.1:10001", "127.0.0.1:10002"])
+
+    def test_status(self):
+        custom_first_block = Block("Onur")
+        custom_new_block = Block("Onur")
+        custom_new_block.sequance_number += 1
+        custom_connections = self.node_0.clients
+        the_transaction_json = {
+            "sequance_number": 1,
+            "signature":
+            "MEUCIHABt7ypkpvFlpqL4SuogwVuzMu2gGynVkrSw6ohZ/GyAiEAg2O3iOei1Ft/vQRpboX7Sm1OOey8a3a67wPJaH/FmVE=",
+            "fromUser":
+            "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE0AYA7B+neqfUA17wKh3OxC67K8UlIskMm9T2qAR+pl+kKX1SleqqvLPM5bGykZ8tqq4RGtAcGtrtvEBrB9DTPg==",
+            "toUser": "onur",
+            "data": "blockchain-lab",
+            "amount": 5000.0,
+            "transaction_fee": 0.02,
+            "transaction_time": 1656764224,
+        }
+        the_transaction = Transaction.load_json(the_transaction_json)
+        custom_transactions = [[the_transaction, "validated"]]
+        custom_new_block.validating_list = [the_transaction]
+        result = Status(
+            custom_first_block=custom_first_block,
+            custom_new_block=custom_new_block,
+            custom_connections=custom_connections,
+            custom_transactions=custom_transactions,
+        )
+        self.assertEqual(result["status"], "Working")
+        self.assertEqual(result["last_transaction_of_block"],
+                         str(the_transaction.dump_json()))
+        self.assertEqual(
+            result["transactions_of_us"],
+            str([
+                f"{str(i[0].__dict__)} | {str(i[1])}"
+                for i in custom_transactions
+            ]),
+        )
+        self.assertEqual(result["connected_nodes"],
+                         ["127.0.0.1:10001", "127.0.0.1:10002"])
+
 
 unittest.main(exit=False)