import unittest
import manage_sys_paths
import json

from db.repository import Repository
from db.entities.layer import Layer
from messaging.MessageHandler import MessageHandler
from messaging.DummyMessageManager import DummyMessageManager as DummyMessageSender
from typing import List

class DummyMongoRepo:
    '''Dummy class to be used for testing the MessageHandler'''
    last_trace = None
    layernodes = []
    def insert_trace(self, trace):
        self.last_trace = trace
    
    def delete_layer(self, layer):
        pass
    
    def add_layer(self, layer):
        pass
    
    def get_layers_for_use_case_and_table(self, use_case, table):
        return [
            Layer(
                layer_info={
                    "layer_name": "pizza_layer_test",
                    "properties": ["UniqueID"],
                    "total_properties": ["UniqueID"],
                    "use_case": "debug",
                    "table": "pizza"
                }
            )
        ]

    def add_layer_nodes(self, nodes: List):
        self.layernodes.extend(nodes)
        return
        

class Test_Pipeline(unittest.TestCase):
    handler = None
    repo = None
    msg_sender = None

    def setUp(self):
        self.repo = DummyMongoRepo()
        self.msg_sender = DummyMessageSender.get_instance()
        self.handler = MessageHandler(self.repo)
    
    def _buildTraceMessage(self):
        return {
            "type": "new-trace", 
            "content": {
                "use_case": "debug", 
                "table": "pizza", 
                "id": "9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0", 
                'properties': {
                    "UniqueID": "9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0",
                    "doughType": "wheat",
                    "firstTopping": "Tomato Sauce",
                    "firstToppingPrice": 1.00,
                    "hasSpinach": False,
                    "name": "Diavolo",
                    "toppingInfo": "Tomato Sauce1.00",
                }
            }
        }
#original name testTraceProcessing
    def test_handle_new_trace_newTraceMsg_correctlyInserted(self):
        msg = self._buildTraceMessage()
        self.handler.handle_new_trace(msg["content"])
        self.assertEqual(len(self.handler._repository.layernodes),1)
    

if __name__ == '__main__':
    unittest.main()