Exemplo n.º 1
0
    def test_mercury_configuration_init():
        mc = config.MercuryConfiguration('test', '/tmp/file.yaml')

        sys.argv = ['test']
        mc.add_option('test', '--test', 'TESTVAR', 'test', default='HELLO')
        namespace = mc.scan_options()
        assert namespace.test == 'HELLO'
Exemplo n.º 2
0
    def test_required():
        sys.argv = ['test', '-c', '/tmp/test.yaml']
        new_mc = config.MercuryConfiguration('test', 'test.yaml')

        new_mc.add_option('required', required=True)

        with pytest.raises(
                mercury.common.exceptions.MercuryConfigurationError):
            new_mc.scan_options()
Exemplo n.º 3
0
    def test_mercury_configuration_type_conversions():
        # List
        sys.argv = ['test']

        new_mc = config.MercuryConfiguration('test', 'test.yaml')

        new_mc.add_option('array',
                          '--array',
                          'ARRAY',
                          'none.none',
                          special_type=list,
                          default=[1, 2, 3])

        sys.argv += ['--array=element1,element2,element3']

        namespace = new_mc.scan_options()

        assert isinstance(namespace.array, list)
        assert len(namespace.array) == 3
        assert namespace.array[-1] == 'element3'

        # Bool

        new_mc.add_option('bool',
                          '--bool',
                          'BOOL',
                          'none.none',
                          special_type=bool,
                          default=True)

        namespace = new_mc.scan_options()

        assert namespace.bool

        sys.argv += ['--bool=False']

        namespace = new_mc.scan_options()

        assert not namespace.bool

        # Int & Float

        new_mc.add_option('int', '--int', 'INT', 'none.none', special_type=int)
        new_mc.add_option('float',
                          '--float',
                          'FLOAT',
                          'none.none',
                          special_type=float)

        sys.argv += ['--int=20', '--float=2.5']

        namespace = new_mc.scan_options()

        assert namespace.int == 20
        assert namespace.float == 2.5
Exemplo n.º 4
0
    def test_mercury_configuration_scan_options():
        sys.argv = ['test', '-c', '/tmp/test.yaml']
        patch_path = 'mercury.common.configuration.configuration_from_yaml'
        with mock.patch(patch_path) as mock_load:
            mock_load.return_value = {
                'test_server': {
                    'host': 'test.com',
                    'port': 31137
                },
                'nest1': {
                    'nest2': {
                        'nest3': 'the cake is a lie'
                    }
                }
            }

            new_mc = config.MercuryConfiguration('test', 'test.yaml')
            new_mc.add_option('host',
                              '--host',
                              'HOST',
                              'test_server.host',
                              default='localhost')

            namespace = new_mc.scan_options()

            # From configuration file
            assert namespace.host == 'test.com'

            # From environment variable
            os.environ['HOST'] = 'test2.com'

            namespace = new_mc.scan_options()

            assert namespace.host == 'test2.com'

            # From command line

            sys.argv += ['--host', 'test3.com']

            namespace = new_mc.scan_options()

            assert namespace.host == 'test3.com'

            # Test no configuration
            sys.argv = ['test']
            del os.environ['HOST']
            new_mc.configuration = {}
            namespace = new_mc.scan_options()

            assert namespace.host == 'localhost'
Exemplo n.º 5
0
    def test_mercury_configuration_auto_format():
        sys.argv = ['test', '-c', '/tmp/test.yaml']
        patch_path = 'mercury.common.configuration.configuration_from_yaml'
        with mock.patch(patch_path) as mock_load:
            mock_load.return_value = {
                'server': {
                    'host': 'test.com',
                    'port': 31137
                },
                'nest1': {
                    'nest2': {
                        'nest3': 'the cake is a lie'
                    }
                }
            }

            new_mc = config.MercuryConfiguration('test', 'test.yaml')
            new_mc.add_option('nest1.nest2.nest3', default='default')

            namespace = new_mc.scan_options()

            assert namespace.nest1.nest2.nest3 == 'the cake is a lie'

            os.environ['NEST1_NEST2_NEST3'] = \
                'Is it ok to drink beer while writing tests?'

            namespace = new_mc.scan_options()

            assert namespace.nest1.nest2.nest3 == \
                'Is it ok to drink beer while writing tests?'

            sys.argv += ['--nest1-nest2-nest3', 'Writing tests is FUN...']

            namespace = new_mc.scan_options()

            assert namespace.nest1.nest2.nest3 == 'Writing tests is FUN...'