summaryrefslogtreecommitdiffstats
path: root/tools/metrics/rappor/pretty_print_test.py
blob: f69b7df9bdbae240d52ac93c0c2864bccb55581f (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
#!/usr/bin/env python
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest

import pretty_print


PRETTY_XML = """
<!-- Comment1 -->

<rappor-configuration>
<!-- Comment2 -->

<rappor-parameter-types>
<!-- Comment3 -->

<rappor-parameters name="TEST_RAPPOR_TYPE">
  <summary>
    Fake type for tests.
  </summary>
  <parameters num-cohorts="128" bytes="1" hash-functions="2" fake-prob="0.5"
      fake-one-prob="0.5" one-coin-prob="0.75" zero-coin-prob="0.25"
      reporting-level="COARSE"/>
</rappor-parameters>

</rappor-parameter-types>

<rappor-metrics>
<!-- Comment4 -->

<rappor-metric name="Test.Rappor.Metric" type="TEST_RAPPOR_TYPE">
  <owner>user1@chromium.org</owner>
  <owner>user2@chromium.org</owner>
  <summary>
    A fake metric summary.
  </summary>
</rappor-metric>

<rappor-metric name="Test.Rappor.Metric2" type="TEST_RAPPOR_TYPE">
  <owner>user1@chromium.org</owner>
  <owner>user2@chromium.org</owner>
  <summary>
    A fake metric summary.
  </summary>
  <string-field name="Url">
    <summary>
      The url of the event.
    </summary>
  </string-field>
  <flags-field name="Flags">
    <flag>Flag bit #1</flag>
    <flag>Flag bit #2</flag>
  </flags-field>
</rappor-metric>

</rappor-metrics>

</rappor-configuration>
""".strip()

BASIC_METRIC = {
  'comments': [],
  'name': 'Test.Rappor.Metric',
  'type': 'TEST_RAPPOR_TYPE',
  'owners': ['user1@chromium.org', 'user2@chromium.org'],
  'summary': 'A fake metric summary.',
  'flags': [],
  'strings': [],
}

MULTI_FIELD_METRIC = {
  'comments': [],
  'name': 'Test.Rappor.Metric2',
  'type': 'TEST_RAPPOR_TYPE',
  'owners': ['user1@chromium.org', 'user2@chromium.org'],
  'summary': 'A fake metric summary.',
  'strings': [{
    'comments': [],
    'name': 'Url',
    'summary': 'The url of the event.',
  }],
  'flags': [{
    'comments': [],
    'name': 'Flags',
    'flags': [
      'Flag bit #1',
      'Flag bit #2',
    ]
  }]
}


class ActionXmlTest(unittest.TestCase):

  def testIsPretty(self):
    result = pretty_print.UpdateXML(PRETTY_XML)
    self.assertMultiLineEqual(PRETTY_XML, result.strip())

  def testParsing(self):
    comments, config = pretty_print.RAPPOR_XML_TYPE.Parse(PRETTY_XML)
    self.assertEqual(BASIC_METRIC, config['metrics']['metrics'][0])
    self.assertEqual(MULTI_FIELD_METRIC, config['metrics']['metrics'][1])
    self.assertEqual(set(['TEST_RAPPOR_TYPE']),
                     pretty_print.GetTypeNames(config))

  def testMissingOwners(self):
    self.assertFalse(pretty_print.GetMissingOwnerErrors([BASIC_METRIC]))
    no_owners = BASIC_METRIC.copy()
    no_owners['owners'] = []
    self.assertTrue(pretty_print.GetMissingOwnerErrors([no_owners]))

  def testInvalidTypes(self):
    self.assertFalse(pretty_print.GetInvalidTypeErrors(
        set(['TEST_RAPPOR_TYPE']), [BASIC_METRIC]))
    self.assertTrue(pretty_print.GetInvalidTypeErrors(
        set(['OTHER_TYPE']), [BASIC_METRIC]))


if __name__ == '__main__':
  unittest.main()