Beispiel #1
0
class GraphsTestCase(unittest.TestCase):
    def setUp(self):
        self.graphs = Graphs()
        self.test_methods = TestMethods()
        self.data_processing = DataProcessing()

    def test_write_grap(self):
        title = "write_test"
        file = "templates/graphs/write_test.html"
        self.graphs.write_graph_to_html(figure=go.Figure(), title=title)
        search = os.path.abspath(file)
        self.assertIn(title, search)
        self.assertTrue(os.path.exists(search))
        os.remove(file)
        self.assertFalse(os.path.isfile(file))

    def test_figure(self):
        country_id = self.test_methods.get_country_id()
        self.assertTrue(isinstance(country_id, int))
        data = self.data_processing.all_cases_per_day_where_country_id_equal(
            country_id=country_id)
        self.assertTrue(isinstance(data, list))
        self.assertTrue(isinstance(data[0], tuple))
        assert len(data) > 0
        dataframe = self.data_processing.get_dateframe(data=data)
        self.assertTrue(isinstance(dataframe, pandas.DataFrame))
        alpha_3_code = ConnectToDb().select_one_record(
            query=
            r"SELECT co.alpha_3_code from countries as co join cases as ca on co.country_id = ca.country_id group by co.country_id having co.country_id = ?",
            parameter=(country_id, ),
        )
        self.assertTrue(isinstance(alpha_3_code[0], str))
        figure = self.graphs.creating_figure_with_data(
            figure=go.Figure(),
            dataframe=dataframe,
            alpha_3_code=alpha_3_code[0])
        self.assertTrue(isinstance(figure, plotly.graph_objects.Figure))

    def test_graph(self):
        country_id = self.test_methods.get_country_id()
        data = self.data_processing.all_cases_per_day_where_country_id_equal(
            country_id=country_id)
        dataframe = self.data_processing.get_dateframe(data=data)
        graph = self.graphs.get_graph(country_id=country_id,
                                      dataframe=dataframe,
                                      diff=False,
                                      write=False)
        self.assertTrue(isinstance(graph, tuple))
        self.assertTrue(isinstance(graph[1], str))
        self.assertTrue(isinstance(graph[0], plotly.graph_objects.Figure))

    def test_diff_graph(self):
        country_id = self.test_methods.get_country_id()
        data = self.data_processing.all_cases_per_day_where_country_id_equal(
            country_id=country_id)
        dataframe = self.data_processing.get_dateframe_diff(data=data)
        path = "tests/test-diff.csv"
        dataframe.to_csv(path_or_buf=path, encoding="utf-8")
        search = os.path.abspath(path)
        self.assertTrue(os.path.exists(search))
        os.remove(search)
        graph = self.graphs.get_graph(country_id=country_id,
                                      dataframe=dataframe,
                                      diff=True,
                                      write=False)
        self.assertTrue(isinstance(graph, tuple))
        self.assertTrue(isinstance(graph[1], str))
        self.assertTrue(isinstance(graph[0], plotly.graph_objects.Figure))

    def test_join_graphs(self):
        country_id1 = self.test_methods.get_country_id()
        country_id2 = self.test_methods.get_country_id()
        graph = self.graphs.join_two_graphs(first_country_id=country_id1,
                                            second_country_id=country_id2)
        self.assertTrue(isinstance(graph, tuple))
        self.assertTrue(isinstance(graph[1], str))
        self.assertTrue(isinstance(graph[0], plotly.graph_objects.Figure))
        os.remove("templates/graphs/" + graph[1] + ".html")

    def test_cases_world(self):
        world = self.graphs.cases_of_the_world(write=False)
        self.assertTrue(world)
        self.assertTrue(isinstance(world, tuple))
        self.assertTrue(isinstance(world[0], plotly.graph_objects.Figure))