annotate tests/unit2.py @ 141:c6aea14a3e2b

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