import unittest

from Processing import process_main
from import_from_json import input_from_json
from update_graph import check_graph_updates

class ProcessingTest(unittest.TestCase):
     maxDiff = None


     def testCycle(self):
         nodes, edges = process_main(['doiz1'],1,1,True)
         doi_nodes = keep_only_dois(nodes)
         self.assertCountEqual(doi_nodes, ['doiz1', 'doiz2'])
         self.assertCountEqual(edges, [['doiz1', 'doiz2'], ['doiz2', 'doiz1']])

         nodes, edges = process_main(['doiz1'],2,2,True)
         doi_nodes = keep_only_dois(nodes)
         self.assertCountEqual(doi_nodes, ['doiz1', 'doiz2'])
         self.assertCountEqual(edges, [['doiz2', 'doiz1'], ['doiz1', 'doiz2']])

    #def testBigCycle(self):

    #def testEmptyHeight(self):

    #def testEmptyDepth(self):

     def testEmptyDepthHeight(self):
         nodes, edges = process_main(['doi1'],0,0,True)
         doi_nodes = keep_only_dois(nodes)
         self.assertCountEqual(doi_nodes,['doi1'])
         self.assertCountEqual(edges, [])

         nodes, edges = process_main(['doi1', 'doi2'],0,0,True)
         doi_nodes = keep_only_dois(nodes)
         self.assertCountEqual(doi_nodes, ['doi1','doi2'])
         self.assertCountEqual(edges, [['doi1', 'doi2']])

         nodes, edges = process_main(['doi1', 'doi2', 'doi3'],0,0,True)
         doi_nodes = keep_only_dois(nodes)
         self.assertCountEqual(doi_nodes, ['doi1','doi2', 'doi3'])
         self.assertCountEqual(edges, [['doi3', 'doi1'], ['doi1', 'doi2']])


     def testInnerEdges(self):
        nodes, edges = process_main(['doi_ie1'],1,1,True)
        doi_nodes = keep_only_dois(nodes)
        self.assertCountEqual(doi_nodes,['doi_ie1','doi_ie2','doi_ie3'])
        self.assertCountEqual(edges,[['doi_ie1','doi_ie2'],['doi_ie3','doi_ie1'],['doi_ie3','doi_ie2']])
     
     def testRightHeight(self):
          nodes, edges = process_main(['doi_h01'],1,0,True)
          doi_nodes = keep_only_dois(nodes)
          self.assertCountEqual(doi_nodes,['doi_h01'])
          self.assertCountEqual(edges, [])

          nodes, edges = process_main(['doi_h02'],1,0,True)
          doi_nodes = keep_only_dois(nodes)
          self.assertCountEqual(doi_nodes,['doi_h02','doi_h1'])
          self.assertCountEqual(edges, [['doi_h1','doi_h02']])

          nodes, edges = process_main(['doi_h02'],2,0,True)
          doi_nodes = keep_only_dois(nodes)
          self.assertCountEqual(doi_nodes,['doi_h02','doi_h1','doi_h2'])
          self.assertCountEqual(edges, [['doi_h1','doi_h02'], ['doi_h2','doi_h1']])

     def testRightDepth(self):
          nodes, edges = process_main(['doi_d01'],0,1,True)
          doi_nodes = keep_only_dois(nodes)
          self.assertCountEqual(doi_nodes,['doi_d01'])
          self.assertCountEqual(edges, [])

          nodes, edges = process_main(['doi_d02'],0,1,True)
          doi_nodes = keep_only_dois(nodes)
          self.assertCountEqual(doi_nodes,['doi_d02','doi_d1'])
          self.assertCountEqual(edges, [['doi_d02','doi_d1']])

          nodes, edges = process_main(['doi_d02'],0,2,True)
          doi_nodes = keep_only_dois(nodes)
          self.assertCountEqual(doi_nodes,['doi_d02','doi_d1','doi_d2'])
          self.assertCountEqual(edges, [['doi_d02','doi_d1'], ['doi_d1','doi_d2']])

     def test_import_from_json(self):
          nodes_old, edges_old = process_main(['doi_lg_1_i'],2,2,True)
          nodes_new, edges_new = input_from_json('test_output.json')
          self.assertCountEqual(nodes_old,nodes_new)
          self.assertCountEqual(edges_old, edges_new)

     def test_deleted_input_dois(self):
          nodes_old_single, edges_old_single = process_main(['doi_lg_1_i'],2,2,True)
          nodes_old_both, edges_old_both = process_main(['doi_lg_1_i','doi_lg_2_i'],2,2,True)
          nodes_new_both, edges_new_both = input_from_json('test_output.json')
          nodes_new_single, edges_new_single = check_graph_updates(['doi_lg_1_i'], nodes_old_both, edges_old_both, True)
          self.assertCountEqual(nodes_old_single,nodes_new_single)
          self.assertCountEqual(edges_old_single, edges_new_single)




def keep_only_dois(nodes):
     '''
          :param nodes:  input list of nodes of type Publication
          :type nodes:   List[Publication]

          gets nodes of type pub and return only their doi
    '''
     doi_list = []
     for node in nodes:
          doi_list.append(node.doi_url)
     return doi_list


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