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