test_path_parser.py 3.7 KB
Newer Older
G
gaocongli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Test the path_parser module."""
from datetime import datetime
from unittest import TestCase, mock

from mindinsight.datavisual.data_transform.summary_watcher import SummaryWatcher
from mindinsight.lineagemgr.common.path_parser import SummaryPathParser


C
chenchao99 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
MOCK_SUMMARY_DIRS = [
    {
        'relative_path': './relative_path0'
    },
    {
        'relative_path': './'
    },
    {
        'relative_path': './relative_path1'
    }
]
MOCK_SUMMARIES = [
    {
36
        'file_name': 'file0.summary.1',
C
chenchao99 已提交
37 38 39
        'create_time': datetime.fromtimestamp(1582031970)
    },
    {
40
        'file_name': 'file0.summary.1_lineage',
C
chenchao99 已提交
41 42 43
        'create_time': datetime.fromtimestamp(1582031970)
    },
    {
44
        'file_name': 'file1.summary.2',
C
chenchao99 已提交
45 46 47
        'create_time': datetime.fromtimestamp(1582031971)
    },
    {
48
        'file_name': 'file1.summary.2_lineage',
C
chenchao99 已提交
49 50 51 52 53
        'create_time': datetime.fromtimestamp(1582031971)
    }
]


G
gaocongli 已提交
54 55 56 57
class TestSummaryPathParser(TestCase):
    """Test the class of SummaryPathParser."""

    @mock.patch.object(SummaryWatcher, 'list_summaries')
58 59
    def test_get_lineage_summaries(self, *args):
        """Test the function of get_lineage_summaries."""
C
chenchao99 已提交
60
        args[0].return_value = MOCK_SUMMARIES
61
        exp_result = ['file0.summary.1_lineage', 'file1.summary.2_lineage']
G
gaocongli 已提交
62
        summary_dir = '/path/to/summary_dir'
63 64
        result = SummaryPathParser.get_lineage_summaries(summary_dir)
        self.assertEqual(exp_result, result)
G
gaocongli 已提交
65 66 67

        args[0].return_value = [
            {
68
                'file_name': 'file0.summary.1',
G
gaocongli 已提交
69 70 71
                'create_time': datetime.fromtimestamp(1582031970)
            }
        ]
72 73
        result = SummaryPathParser.get_lineage_summaries(summary_dir)
        self.assertEqual([], result)
G
gaocongli 已提交
74 75 76

        args[0].return_value = [
            {
77
                'file_name': 'file0.summary.1_lineage',
G
gaocongli 已提交
78 79 80
                'create_time': datetime.fromtimestamp(1582031970)
            }
        ]
81 82
        result = SummaryPathParser.get_lineage_summaries(summary_dir)
        self.assertEqual(['file0.summary.1_lineage'], result)
G
gaocongli 已提交
83 84 85

        args[0].return_value = [
            {
86
                'file_name': 'file0.summary.3_lineage',
G
gaocongli 已提交
87 88 89
                'create_time': datetime.fromtimestamp(1582031970)
            },
            {
90
                'file_name': 'file0.summary.2_lineage_lineage',
G
gaocongli 已提交
91 92 93
                'create_time': datetime.fromtimestamp(1582031970)
            },
            {
94
                'file_name': 'file1.summary.1_lineage',
G
gaocongli 已提交
95 96 97
                'create_time': datetime.fromtimestamp(1582031971)
            },
            {
98
                'file_name': 'file1.summary.7_lineage_lineage',
G
gaocongli 已提交
99 100 101
                'create_time': datetime.fromtimestamp(1582031971)
            }
        ]
102 103 104 105 106 107
        exp_result = ['file1.summary.1_lineage',
                      'file0.summary.2_lineage_lineage',
                      'file0.summary.3_lineage',
                      'file1.summary.7_lineage_lineage']
        result = SummaryPathParser.get_lineage_summaries(summary_dir, is_sorted=True)
        self.assertEqual(exp_result, result)