summaryrefslogtreecommitdiffstats
path: root/tools/variations/fieldtrial_to_struct_unittest.py
blob: 7575e833ebfaceffd8a9652878b0eeaa729493f0 (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
# 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 fieldtrial_to_struct
import os


class FieldTrialToStruct(unittest.TestCase):

  def test_FieldTrialToDescription(self):
    config = {
      'Study1': [
        {
          'group_name': 'Group1',
          'params': {
            'x': '1',
            'y': '2'
          },
          'enable_features': ['A', 'B'],
          'disable_features': ['C']
        }
      ],
      'Study2': [{'group_name': 'OtherGroup'}]
    }
    result = fieldtrial_to_struct._FieldTrialConfigToDescription(config)
    expected = {
      'elements': {
        'kFieldTrialConfig': {
          'groups': [
            {
              'study': 'Study1',
              'group_name': 'Group1',
              'params': [
                {'key': 'x', 'value': '1'},
                {'key': 'y', 'value': '2'}
              ],
             'enable_features': ['A',
                                 'B'],
             'disable_features': ['C']
            },
            {
              'study': 'Study2',
              'group_name': 'OtherGroup'
            }
          ]
        }
      }
    }
    self.maxDiff = None
    self.assertEqual(expected, result)

  def test_FieldTrialToStructMain(self):
    schema = (
        '../../chrome/common/variations/fieldtrial_testing_config_schema.json')
    test_ouput_filename = 'test_ouput'
    fieldtrial_to_struct.main([
      '--schema=' + schema,
      '--output=' + test_ouput_filename,
      '--year=2015',
      'unittest_data/test_config.json'
    ])
    header_filename = test_ouput_filename + '.h'
    with open(header_filename, 'r') as header:
      test_header = header.read()
      with open('unittest_data/expected_output.h', 'r') as expected:
        expected_header = expected.read()
        self.assertEqual(expected_header, test_header)
    os.unlink(header_filename)

    cc_filename = test_ouput_filename + '.cc'
    with open(cc_filename, 'r') as cc:
      test_cc = cc.read()
      with open('unittest_data/expected_output.cc', 'r') as expected:
        expected_cc = expected.read()
        self.assertEqual(expected_cc, test_cc)
    os.unlink(cc_filename)

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