import os
import unittest
from concord_cli.generated.concord.internal.thrift.ttypes import *
from concord_cli.print_graph import *
from testing_utilities import *

class TestPrintGraph(unittest.TestCase):

    def setUp(self):
        # Retrieve static topology metadata
        with open(test_filepath('test_topology_metadata')) as data_file:
            self.test_topology_metadata = data_file.read()

        # Retrieve result pdf generated by static topology metadata
        with open(test_filepath('test_topology_visual_graph')) as data_file:
            self.test_topology_graph = data_file.read()

        self.stubbed_metadata = TopologyMetadata()
        bytes_to_thrift(self.test_topology_metadata, self.stubbed_metadata)

    def test_print_dot(self):
        # Give print dot a filepath to store its result into
        temp_file_name = test_filepath("temporary")
        print_dot(self.stubbed_metadata, temp_file_name)

        # Read the contents of the data generated by print_dot into memory
        temp_file_name = temp_file_name + '.pdf'
        with open(temp_file_name) as data_file:
            test_data = data_file.read()

        try:
            # Assert that the file written by print_dot matches with test pdf
            self.assertEquals(test_data, self.test_topology_graph)
        finally:
            os.unlink(temp_file_name)
