summaryrefslogtreecommitdiffstats
path: root/python/mozperftest/mozperftest/tests/test_metrics_utils.py
blob: 0e6bdd71ac14920b388c2904f17bedc1d53860a4 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import json

import mozunit
import pytest

from mozperftest.metrics.utils import metric_fields, open_file
from mozperftest.tests.support import temp_file


def test_open_file():
    data = json.dumps({"1": 2})

    with temp_file(name="data.json", content=data) as f:
        res = open_file(f)
        assert res == {"1": 2}

    with temp_file(name="data.txt", content="yeah") as f:
        assert open_file(f) == "yeah"


def test_metric_fields_old_format():
    assert metric_fields("firstPaint") == {"name": "firstPaint"}


@pytest.mark.parametrize(
    "metrics, expected",
    [
        [
            "name:foo,extraOptions:bar",
            {"name": "foo", "extraOptions": "bar"},
        ],
        ["name:foo", {"name": "foo"}],
    ],
)
def test_metric_fields_simple(metrics, expected):
    assert metric_fields(metrics) == expected


@pytest.mark.parametrize(
    "metrics, expected",
    [
        [
            "name:foo,extraOptions:['1', '2', '3', 2]",
            {"name": "foo", "extraOptions": ["1", "2", "3", 2]},
        ],
        [
            """name:foo,extraOptions:['1', '2', '3', 2, "3", "hello,world"] """,
            {"name": "foo", "extraOptions": ["1", "2", "3", 2, "3", "hello,world"]},
        ],
        [
            """name:foo,extraOptions:['1', '2', '3', 2, "3", "hello,world"],"""
            """alertThreshold:['1',2,"hello"] """,
            {
                "name": "foo",
                "extraOptions": ["1", "2", "3", 2, "3", "hello,world"],
                "alertThreshold": ["1", 2, "hello"],
            },
        ],
        [
            """name:foo,extraOptions:['1', '2', '3', 2, "3", "hello,world"],"""
            """value:foo,alertThreshold:['1',2,"hello"],framework:99 """,
            {
                "name": "foo",
                "extraOptions": ["1", "2", "3", 2, "3", "hello,world"],
                "alertThreshold": ["1", 2, "hello"],
                "value": "foo",
                "framework": 99,
            },
        ],
    ],
)
def test_metric_fields_complex(metrics, expected):
    assert metric_fields(metrics) == expected


@pytest.mark.parametrize(
    "metrics",
    [
        """name:foo,extraOptions:['1', '2', '3', 2, "3", "hello,world"],"""
        """value:foo,alertThreshold:['1',2,"hello"],framework:99,"""
        """shouldAlert:[99,100,["hello", "world"],0] """,
        """name:foo,extraOptions:['1', '2', '3', 2, "3", "hello,world"],"""
        """value:foo,alertThreshold:['1',2,"hello"],framework:99,"""
        """shouldAlert:[99,100,["hello:", "world:"],0] """,
    ],
)
def test_metric_fields_complex_failures(metrics):
    with pytest.raises(Exception):
        metric_fields(metrics)


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