annotate tests/unit.py @ 126:82151d79e117

configuration/configuration.py
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 17 Sep 2013 23:35:26 -0700
parents 9d19ed8fd883
children dff886188b55
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 unit tests
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
30
b27a7cb2dd5b stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
7 import configuration
115
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
8 import datetime
0
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import os
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 import sys
32
da4d5c5831c6 serialization testing stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
11 import tempfile
0
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import unittest
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
32
da4d5c5831c6 serialization testing stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
14 try:
da4d5c5831c6 serialization testing stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
15 import json
da4d5c5831c6 serialization testing stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
16 except ImportError:
da4d5c5831c6 serialization testing stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
17 import simplejson as json
da4d5c5831c6 serialization testing stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 30
diff changeset
18
19
cadc9514f60a we have a legitimately failing test!
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
19 from example import ExampleConfiguration # example configuration to test
11
e00afe2c83bf stubbing configuration parser
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
20
0
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 # globals
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 here = os.path.dirname(os.path.abspath(__file__))
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
24
39f2611db9be rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
24 class ConfigurationUnitTest(unittest.TestCase):
0
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25
24
39f2611db9be rename a bunch of things and begin to work on the sanity of validation
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
26 def test_cli(self):
28
c516ab813079 begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
27 """test command line interface"""
c516ab813079 begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
28
19
cadc9514f60a we have a legitimately failing test!
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
29 example = ExampleConfiguration()
cadc9514f60a we have a legitimately failing test!
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
30
cadc9514f60a we have a legitimately failing test!
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
31 # parse command line arguments
65
2a2e9aee5bc6 refactor completed and tests pass again
Jeff Hammel <jhammel@mozilla.com>
parents: 58
diff changeset
32 options, args = example.parse_args(['-a', 'ts', '--develop', '-e', '/home/jhammel/bin/firefox'])
25
b39e550402ea we now update configuration correctly
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
33
b39e550402ea we now update configuration correctly
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
34 # ensure that the options appropriately get set
19
cadc9514f60a we have a legitimately failing test!
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
35 self.assertEqual(bool(args), False) # no arguments
cadc9514f60a we have a legitimately failing test!
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
36 self.assertEqual(options.develop, True)
25
b39e550402ea we now update configuration correctly
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
37 self.assertEqual(options.activeTests, ['ts'])
b39e550402ea we now update configuration correctly
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
38 self.assertEqual(options.browser_path, '/home/jhammel/bin/firefox')
b39e550402ea we now update configuration correctly
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
39
b39e550402ea we now update configuration correctly
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
40 # ensure that the configuration appropriately gets updated
b39e550402ea we now update configuration correctly
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
41 self.assertEqual(example.config['develop'], True)
b39e550402ea we now update configuration correctly
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
42 self.assertEqual(example.config['activeTests'], ['ts'])
b39e550402ea we now update configuration correctly
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
43 self.assertEqual(example.config['browser_path'], '/home/jhammel/bin/firefox')
0
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44
108
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
45 def test_dict(self):
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
46 """test dictionary parsing from the "command line" """
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
47
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
48
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
49 # test adding a preference
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
50 example = ExampleConfiguration()
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
51 options, args = example.parse_args(['-a', 'ts', '--develop', '-e', '/home/jhammel/bin/firefox',
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
52 '--pref', 'hangmonitor.timeout=0'])
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
53 self.assertTrue('hangmonitor.timeout' in options.preferences)
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
54
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
55 # test overriding a preference
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
56 example = ExampleConfiguration()
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
57 options, args = example.parse_args(['-a', 'ts', '--develop', '-e', '/home/jhammel/bin/firefox',
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
58 '--pref', 'browser.bookmarks.max_backups=1'])
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
59 self.assertTrue(options.preferences['browser.bookmarks.max_backups'] == '1')
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
60
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
61
a2184db43fe2 fix dict command line processing
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
62
28
c516ab813079 begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
63 def test_configuration_providers(self):
c516ab813079 begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
64 """test file-based configuration providers"""
39
3c91bf85adb5 actually test serialization
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
65 # requires json/simplejson to be installed
30
b27a7cb2dd5b stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
66
b27a7cb2dd5b stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
67 example = ExampleConfiguration()
b27a7cb2dd5b stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
68
b27a7cb2dd5b stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
69 # see what providers you got
b27a7cb2dd5b stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
70 json_provider = example.configuration_provider('json')
b27a7cb2dd5b stub test for configuration providers
Jeff Hammel <jhammel@mozilla.com>
parents: 28
diff changeset
71 self.assertTrue(isinstance(json_provider, configuration.JSON))
28
c516ab813079 begin stubbing serialization/deserialization
Jeff Hammel <jhammel@mozilla.com>
parents: 25
diff changeset
72
33
b7d6a90b0bb5 setup for serialization testing
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
73 # serialize to a temporary file
37
a1f8dec4d4f9 we has another broken test
Jeff Hammel <jhammel@mozilla.com>
parents: 36
diff changeset
74 filename = tempfile.mktemp(suffix='.json')
38
8909ae1cc4ea fix serialization (stub) tests
Jeff Hammel <jhammel@mozilla.com>
parents: 37
diff changeset
75 self.assertEqual(example.filename2format(filename), 'json')
37
a1f8dec4d4f9 we has another broken test
Jeff Hammel <jhammel@mozilla.com>
parents: 36
diff changeset
76 self.assertFalse(os.path.exists(filename))
34
79aca36abd9a fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
77 config = {'browser_path': '/home/jhammel/bin/firefox',
79aca36abd9a fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
78 'activeTests': ['ts']}
79aca36abd9a fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
79 example(config)
79aca36abd9a fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
80 config['test_timeout'] = 1200 # default
102
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
81 config['preferences'] = {"browser.bookmarks.max_backups": 0,
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
82 "browser.cache.disk.smart_size.enabled": False}
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
83
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
84 # ensure they are equal
34
79aca36abd9a fill in defaults if not present
Jeff Hammel <jhammel@mozilla.com>
parents: 33
diff changeset
85 self.assertEqual(config, example.config)
37
a1f8dec4d4f9 we has another broken test
Jeff Hammel <jhammel@mozilla.com>
parents: 36
diff changeset
86 example.serialize(filename)
a1f8dec4d4f9 we has another broken test
Jeff Hammel <jhammel@mozilla.com>
parents: 36
diff changeset
87 self.assertTrue(os.path.exists(filename))
39
3c91bf85adb5 actually test serialization
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
88 serialized = json.loads(file(filename).read())
3c91bf85adb5 actually test serialization
Jeff Hammel <jhammel@mozilla.com>
parents: 38
diff changeset
89 self.assertEqual(serialized, config)
42
75886253ee73 test deserialization
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
90
75886253ee73 test deserialization
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
91 # deserialize
75886253ee73 test deserialization
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
92 deserialized = example.deserialize(filename)
75886253ee73 test deserialization
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
93 self.assertEqual(deserialized, config)
75886253ee73 test deserialization
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
94
75886253ee73 test deserialization
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
95 # cleanup
33
b7d6a90b0bb5 setup for serialization testing
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
96 if os.path.exists(filename):
b7d6a90b0bb5 setup for serialization testing
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
97 os.remove(filename)
b7d6a90b0bb5 setup for serialization testing
Jeff Hammel <jhammel@mozilla.com>
parents: 32
diff changeset
98
48
84fb8ad5ba81 ensure that missing values raise the correct exception
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
99 def test_missing_values(self):
84fb8ad5ba81 ensure that missing values raise the correct exception
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
100 """ensure that Configuration raises a missing value exception"""
84fb8ad5ba81 ensure that missing values raise the correct exception
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
101
84fb8ad5ba81 ensure that missing values raise the correct exception
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
102 example = ExampleConfiguration()
84fb8ad5ba81 ensure that missing values raise the correct exception
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
103
89
0804a706d6bf handle CLI errors with optparse
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
104 # monkey-patch the error method from optparse.OptionParser
0804a706d6bf handle CLI errors with optparse
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
105 error_msg = []
0804a706d6bf handle CLI errors with optparse
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
106 def error(msg):
0804a706d6bf handle CLI errors with optparse
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
107 error_msg.append(msg)
0804a706d6bf handle CLI errors with optparse
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
108 example.error = error
0804a706d6bf handle CLI errors with optparse
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
109
0804a706d6bf handle CLI errors with optparse
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
110 # trigger it
0804a706d6bf handle CLI errors with optparse
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
111 example.parse_args(args=[])
0804a706d6bf handle CLI errors with optparse
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
112 self.assertEqual(error_msg, ['Parameter browser_path is required but not present'])
48
84fb8ad5ba81 ensure that missing values raise the correct exception
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
113
36
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
114 def test_required(self):
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
115 """ensure you have to have required values"""
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
116
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
117 example = ExampleConfiguration()
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
118
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
119 # ensure you get an exception
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
120 missingvalueexception = None
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
121 try:
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
122 example()
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
123 except configuration.MissingValueException, e:
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
124 missingvalueexception = e
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
125 self.assertTrue(isinstance(e, configuration.MissingValueException))
d218df4d0b4c add test for missing values
Jeff Hammel <jhammel@mozilla.com>
parents: 34
diff changeset
126
102
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
127
40
056280e7a6ea stub munging configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 39
diff changeset
128 def test_multiple_configurations(self):
056280e7a6ea stub munging configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 39
diff changeset
129 """test having multiple configurations"""
056280e7a6ea stub munging configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 39
diff changeset
130
056280e7a6ea stub munging configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 39
diff changeset
131 # simple override
056280e7a6ea stub munging configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 39
diff changeset
132 args1 = ['-e', '/opt/bin/firefox']
056280e7a6ea stub munging configuration
Jeff Hammel <jhammel@mozilla.com>
parents: 39
diff changeset
133
55
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
134 # simple serialized file
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
135 json_file = os.path.join(here, 'base.json')
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
136 assert os.path.exists(json_file)
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
137 json_config = json.loads(file(json_file).read())
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
138
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
139 # parse the json file
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
140 example = ExampleConfiguration()
65
2a2e9aee5bc6 refactor completed and tests pass again
Jeff Hammel <jhammel@mozilla.com>
parents: 58
diff changeset
141 example.parse_args([json_file])
55
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
142 self.assertEqual(example.config, json_config)
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
143
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
144 # parse the json file with overrides
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
145 example = ExampleConfiguration()
65
2a2e9aee5bc6 refactor completed and tests pass again
Jeff Hammel <jhammel@mozilla.com>
parents: 58
diff changeset
146 example.parse_args([json_file] + args1)
55
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
147 config = json_config.copy()
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
148 config['browser_path'] = '/opt/bin/firefox'
87bd539a21d3 test with overrides
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
149 self.assertEqual(example.config, config)
43
f09982d47b3c add --dump option
Jeff Hammel <jhammel@mozilla.com>
parents: 42
diff changeset
150
56
7f8e9c5d1606 introduce a somewhat redundant test
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
151 # it shouldn't matter in which order the arguments are
7f8e9c5d1606 introduce a somewhat redundant test
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
152 example = ExampleConfiguration()
65
2a2e9aee5bc6 refactor completed and tests pass again
Jeff Hammel <jhammel@mozilla.com>
parents: 58
diff changeset
153 example.parse_args(args1 + [json_file])
56
7f8e9c5d1606 introduce a somewhat redundant test
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
154 self.assertEqual(example.config, config)
7f8e9c5d1606 introduce a somewhat redundant test
Jeff Hammel <jhammel@mozilla.com>
parents: 55
diff changeset
155
58
f7dc5cf2540c add complex test, now we can refactor
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
156 # Now a tricky case:
f7dc5cf2540c add complex test, now we can refactor
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
157 # the default value for test_timeout is 1200:
f7dc5cf2540c add complex test, now we can refactor
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
158 example = ExampleConfiguration()
f7dc5cf2540c add complex test, now we can refactor
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
159 self.assertEqual(example.options['test_timeout']['default'], 1200)
f7dc5cf2540c add complex test, now we can refactor
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
160 # The value from base.json is 60:
f7dc5cf2540c add complex test, now we can refactor
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
161 self.assertEqual(json_config['test_timeout'], 60)
f7dc5cf2540c add complex test, now we can refactor
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
162 self.assertEqual(config['test_timeout'], 60)
f7dc5cf2540c add complex test, now we can refactor
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
163 # but we can override it back from the "command line"
65
2a2e9aee5bc6 refactor completed and tests pass again
Jeff Hammel <jhammel@mozilla.com>
parents: 58
diff changeset
164 example.parse_args(args1 + [json_file, '--test_timeout', '1200'])
58
f7dc5cf2540c add complex test, now we can refactor
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
165 config['test_timeout'] = 1200
f7dc5cf2540c add complex test, now we can refactor
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
166 self.assertEqual(example.config, config)
f7dc5cf2540c add complex test, now we can refactor
Jeff Hammel <jhammel@mozilla.com>
parents: 56
diff changeset
167
102
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
168 def test_extend(self):
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
169
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
170 # default preferences
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
171 example = ExampleConfiguration()
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
172 default_prefs = {"browser.bookmarks.max_backups": 0,
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
173 "browser.cache.disk.smart_size.enabled": False}
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
174 example.parse_args(['-a', 'ts', '-e', '/opt/bin/firefox'])
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
175 self.assertEqual(example.config['preferences'], default_prefs)
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
176
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
177 # now extend them
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
178 example = ExampleConfiguration()
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
179 default_prefs['network.dns.ipv4OnlyDomains'] = 'localhost'
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
180 tf = tempfile.mktemp()
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
181 f = file(tf, 'w')
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
182 f.write(json.dumps({'preferences': {'network.dns.ipv4OnlyDomains': 'localhost'}}))
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
183 f.close()
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
184 example.parse_args(['-a', 'ts', '-e', '/opt/bin/firefox', tf])
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
185 self.assertEqual(example.config['preferences'], default_prefs)
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
186 os.remove(tf)
c530f6265deb allow extensible configuration; also start using deepcopy heavily since otherwise you have artefacts
Jeff Hammel <jhammel@mozilla.com>
parents: 89
diff changeset
187
115
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
188 def test_typecast(self):
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
189 """casting example"""
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
190
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
191 def todate(string):
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
192 return datetime.datetime.strptime(string, "%Y%m%d")
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
193
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
194 # make an example class
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
195 class TypecastExample(configuration.Configuration):
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
196 options = {'datestring': {'type': todate}}
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
197 example = TypecastExample()
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
198
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
199 # parse a date string
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
200 example({'datestring': "20120704"})
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
201
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
202 # ensure it works correctly
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
203 expected = datetime.datetime(2012, 7, 4, 0, 0)
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
204 self.assertEqual(example.config['datestring'], expected)
56db0b2b90af fix casting
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
205
116
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
206
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
207 def test_added(self):
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
208 """test that we keep track of things added to the configuration"""
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
209
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
210 # make an example class
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
211 class AddedExample(configuration.Configuration):
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
212 options = {'foo': {},
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
213 'bar': {}}
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
214
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
215 # parse it; there should be nothing
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
216 instance = AddedExample()
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
217 instance()
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
218 self.assertEqual(instance.added, set())
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
219
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
220 # parse it; there should be one thing
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
221 instance = AddedExample()
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
222 instance({'foo': 'foo'})
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
223 self.assertEqual(instance.added, set(['foo']))
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
224
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
225 # parse it; there should be two things
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
226 instance = AddedExample()
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
227 instance({'foo': 'foo'}, {'foo': 'FOO', 'bar': 'bar'})
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
228 self.assertEqual(instance.added, set(['foo', 'bar']))
9d19ed8fd883 https://bugzilla.mozilla.org/show_bug.cgi?id=796196
Jeff Hammel <jhammel@mozilla.com>
parents: 115
diff changeset
229
0
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
230 if __name__ == '__main__':
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
231 unittest.main()