test_file_handler.py 2.19 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""Tests for file handler module in analyzer."""

import unittest
from pathlib import Path

import pandas as pd

import superbench.analyzer.file_handler as file_handler


class TestFileHandler(unittest.TestCase):
    """Test for file handler."""
    def setUp(self):
        """Method called to prepare the test fixture."""
        self.parent_path = Path(__file__).parent
        self.test_rule_file_fake = str(self.parent_path / 'test_rules_fake.yaml')

    def tearDown(self):
        """Method called after the test method has been called and the result recorded."""
        for file in [self.test_rule_file_fake]:
            p = Path(file)
            if p.is_file():
                p.unlink()

    def test_file_handler(self):
        """Test for the file handler."""
        test_raw_data = str(self.parent_path / 'test_results.jsonl')
        test_rule_file = str(self.parent_path / 'test_rules.yaml')
        test_baseline_file = str(self.parent_path / 'test_baseline.json')
        test_raw_data_fake = str(self.parent_path / 'test_results_fake.jsonl')
        test_rule_file_fake = str(self.parent_path / 'test_rules_fake.yaml')
        test_aseline_file_fake = str(self.parent_path / 'test_baseline_fake.json')
        # Test - read_raw_data
        raw_data_df = file_handler.read_raw_data(test_raw_data)
        assert (not raw_data_df.empty)
        raw_data_df = file_handler.read_raw_data(test_raw_data_fake)
        assert (raw_data_df.empty)
        # Test - read rules
        rules = file_handler.read_rules(test_rule_file_fake)
        assert (not rules)
        rules = file_handler.read_rules(test_rule_file)
        assert (rules)
        # Test - read baseline
        baseline = file_handler.read_baseline(test_aseline_file_fake)
        assert (not baseline)
        baseline = file_handler.read_baseline(test_baseline_file)
        assert (baseline)
51
        # Test - generate_md_table
52
        data_df = pd.DataFrame([[1, 2], [3, 4]])
53
        lines = file_handler.generate_md_table(data_df, header=['A', 'B'])
54
55
        expected_lines = ['| A | B |\n', '| --- | --- |\n', '| 1 | 2 |\n', '| 3 | 4 |\n']
        assert (lines == expected_lines)