summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/tests/python/test_histogramtools_non_strict.py
blob: 275690735cef738f84f18eb1db0c79da2727d89c (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# 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 sys
import unittest
from os import path

import mozunit

TELEMETRY_ROOT_PATH = path.abspath(
    path.join(path.dirname(__file__), path.pardir, path.pardir)
)
sys.path.append(TELEMETRY_ROOT_PATH)
# The parsers live in a subdirectory of "build_scripts", account for that.
# NOTE: if the parsers are moved, this logic will need to be updated.
sys.path.append(path.join(TELEMETRY_ROOT_PATH, "build_scripts"))
from mozparsers import parse_histograms  # noqa: E402


def load_histogram(histograms):
    """Parse the passed Histogram and return a dictionary mapping histogram
    names to histogram parameters.

    :param histogram: Histogram as a python dictionary
    :returns: Parsed Histogram dictionary mapping histogram names to histogram parameters
    """

    def hook(ps):
        return parse_histograms.load_histograms_into_dict(ps, strict_type_checks=False)

    return json.loads(json.dumps(histograms), object_pairs_hook=hook)


class TestParser(unittest.TestCase):
    def test_unknown_field(self):
        SAMPLE_HISTOGRAM = {
            "A11Y_INSTANTIATED_FLAG": {
                "record_in_processes": ["main", "content"],
                "expires_in_version": "never",
                "kind": "flag",
                "description": "has accessibility support been instantiated",
                "new_field": "Its a new field",
            }
        }
        histograms = load_histogram(SAMPLE_HISTOGRAM)

        hist = parse_histograms.Histogram(
            "A11Y_INSTANTIATED_FLAG",
            histograms["A11Y_INSTANTIATED_FLAG"],
            strict_type_checks=False,
        )
        self.assertEqual(hist.expiration(), "never")
        self.assertEqual(hist.kind(), "flag")
        self.assertEqual(hist.record_in_processes(), ["main", "content"])

    def test_non_numeric_expressions(self):
        SAMPLE_HISTOGRAM = {
            "TEST_NON_NUMERIC_HISTOGRAM": {
                "kind": "linear",
                "description": "sample",
                "n_buckets": "JS::GCReason::NUM_TELEMETRY_REASONS",
                "high": "mozilla::StartupTimeline::MAX_EVENT_ID",
            }
        }

        histograms = load_histogram(SAMPLE_HISTOGRAM)
        hist = parse_histograms.Histogram(
            "TEST_NON_NUMERIC_HISTOGRAM",
            histograms["TEST_NON_NUMERIC_HISTOGRAM"],
            strict_type_checks=False,
        )

        # expected values come off parse_histograms.py
        self.assertEqual(hist.n_buckets(), 101)
        self.assertEqual(hist.high(), 12)

    def test_devtools_database_parsing(self):
        db = path.join(
            TELEMETRY_ROOT_PATH,
            path.pardir,
            path.pardir,
            path.pardir,
            "devtools",
            "shared",
            "css",
            "generated",
            "properties-db.js",
        )

        histograms = list(parse_histograms.from_files([db], strict_type_checks=False))
        histograms = [h.name() for h in histograms]

        # Test a shorthand (animation)
        self.assertTrue("USE_COUNTER2_CSS_PROPERTY_Animation_DOCUMENT" in histograms)

        # Test a shorthand alias (-moz-animation).
        self.assertTrue("USE_COUNTER2_CSS_PROPERTY_MozAnimation_DOCUMENT" in histograms)

        # Test a longhand (animation-name)
        self.assertTrue(
            "USE_COUNTER2_CSS_PROPERTY_AnimationName_DOCUMENT" in histograms
        )

        # Test a longhand alias (-moz-animation-name)
        self.assertTrue(
            "USE_COUNTER2_CSS_PROPERTY_MozAnimationName_DOCUMENT" in histograms
        )

    def test_current_histogram(self):
        HISTOGRAMS_PATH = path.join(TELEMETRY_ROOT_PATH, "Histograms.json")
        all_histograms = list(
            parse_histograms.from_files([HISTOGRAMS_PATH], strict_type_checks=False)
        )
        test_histogram = [
            i for i in all_histograms if i.name() == "TELEMETRY_TEST_FLAG"
        ][0]

        self.assertEqual(test_histogram.expiration(), "never")
        self.assertEqual(test_histogram.kind(), "flag")
        self.assertEqual(test_histogram.record_in_processes(), ["main", "content"])
        self.assertEqual(test_histogram.keyed(), False)

    def test_no_products(self):
        SAMPLE_HISTOGRAM = {
            "TEST_EMPTY_PRODUCTS": {
                "kind": "flag",
                "description": "sample",
            }
        }

        histograms = load_histogram(SAMPLE_HISTOGRAM)
        hist = parse_histograms.Histogram(
            "TEST_EMPTY_PRODUCTS",
            histograms["TEST_EMPTY_PRODUCTS"],
            strict_type_checks=False,
        )

        self.assertEqual(hist.kind(), "flag")
        # bug 1486072: absent `product` key becomes None instead of ["all"]
        self.assertEqual(hist.products(), None)


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