view tests/unit.py @ 48:84fb8ad5ba81

ensure that missing values raise the correct exception
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 27 Mar 2012 12:50:16 -0700
parents f09982d47b3c
children 09fbc09455d4
line wrap: on
line source

#!/usr/bin/env python

"""
unit tests
"""

import configuration
import os
import sys
import tempfile
import unittest

try:
    import json
except ImportError:
    import simplejson as json

from example import ExampleConfiguration # example configuration to test

# globals
here = os.path.dirname(os.path.abspath(__file__))

class ConfigurationUnitTest(unittest.TestCase):

    def test_cli(self):
        """test command line interface"""

        example = ExampleConfiguration()

        # parse command line arguments
        options, args = example.parse(['-a', 'ts', '--develop', '-e', '/home/jhammel/bin/firefox'])

        # ensure that the options appropriately get set
        self.assertEqual(bool(args), False) # no arguments
        self.assertEqual(options.develop, True)
        self.assertEqual(options.activeTests, ['ts'])
        self.assertEqual(options.browser_path, '/home/jhammel/bin/firefox')

        # ensure that the configuration appropriately gets updated
        self.assertEqual(example.config['develop'], True)
        self.assertEqual(example.config['activeTests'], ['ts'])
        self.assertEqual(example.config['browser_path'], '/home/jhammel/bin/firefox')

    def test_configuration_providers(self):
        """test file-based configuration providers"""
        # requires json/simplejson to be installed

        example = ExampleConfiguration()

        # see what providers you got
        json_provider = example.configuration_provider('json')
        self.assertTrue(isinstance(json_provider, configuration.JSON))

        # serialize to a temporary file
        filename = tempfile.mktemp(suffix='.json')
        self.assertEqual(example.filename2format(filename), 'json')
        self.assertFalse(os.path.exists(filename))
        config = {'browser_path': '/home/jhammel/bin/firefox',
                  'activeTests':  ['ts']}
        example(config)
        config['test_timeout'] = 1200 # default
        self.assertEqual(config, example.config)
        example.serialize(filename)
        self.assertTrue(os.path.exists(filename))
        serialized = json.loads(file(filename).read())
        self.assertEqual(serialized, config)

        # deserialize
        deserialized = example.deserialize(filename)
        self.assertEqual(deserialized, config)

        # cleanup
        if os.path.exists(filename):
            os.remove(filename)

    def test_missing_values(self):
        """ensure that Configuration raises a missing value exception"""

        example = ExampleConfiguration()

        exception = None
        try:
            example.parse(args=[])
        except Exception, exception:
            pass
        self.assertTrue(isinstance(exception, configuration.TypeCastException))

    def test_required(self):
        """ensure you have to have required values"""

        example = ExampleConfiguration()

        # ensure you get an exception
        missingvalueexception = None
        try:
            example()
        except configuration.MissingValueException, e:
            missingvalueexception = e
        self.assertTrue(isinstance(e, configuration.MissingValueException))

    def test_multiple_configurations(self):
        """test having multiple configurations"""

        example = ExampleConfiguration()

        # simple override
        config1 = {'browser_path': '/home/jhammel/bin/firefox',
                   'activeTests': ['ts']}
        args1 = ['-e', '/opt/bin/firefox']

        #

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