def test_get_config_empty(drivers):
    """
    Test that get_config raises an error when given an empty dictionary.
    """
    with pytest.raises(ModuleError) as error:
        get_config({})
    assert 'Missing configuration parameter' in str(error.value)
def test_key_not_required(drivers, key):
    """
    Check that the port is not required, even though it can be passed as part
    of the config dictionary.
    """
    config = PARAM_CONFIG.copy()
    expect = INTERNAL_CONFIG.copy()
    expect['driver'] = sql_query.DRIVERS['mysql']

    # Specify the port directly
    config[key] = expect[key] = key
    assert get_config(config) == expect

    # Specify the port in config dict
    assert get_config({'config': config}) == expect

    # Specify the port in config and override it
    assert get_config({'config': config, key: key}) == expect

    # Try a config with no port number
    config.pop(key, None)
    expect.pop(key, None)
    assert get_config({'config': config}) == expect

    # Try with direct arguments and no port number
    assert get_config(config) == expect
def test_get_config_invalid_driver(monkeypatch):
    """
    Check that get_config raises an error when using a dbtype that has no
    associated driver.
    """
    monkeypatch.setitem(sql_query.DRIVERS, 'mssql', '')
    with pytest.raises(ModuleError) as error:
        get_config(PARAM_CONFIG.copy())
        assert 'no driver' in str(error.value).lower()
def test_get_config_invalid_database():
    """
    Check that get_config raises an error when passing an unknown dbtype.
    """
    db = 'this is not a valid database'
    config = PARAM_CONFIG.copy()
    config['dbtype'] = db
    with pytest.raises(ModuleError) as error:
        get_config(config)
    assert 'must be one of' in str(error.value)
def test_get_config_missing_required(key, drivers):
    """
    Check that get_config raises an error when a required key is missing.
    """
    config = PARAM_CONFIG.copy()
    config.pop(key)
    with pytest.raises(ModuleError) as error:
        get_config(config)
    assert 'Missing configuration parameter' in str(error.value)
    assert key in str(error.value)
def test_get_config(drivers):
    config = PARAM_CONFIG.copy()
    expect = INTERNAL_CONFIG.copy()
    expect['driver'] = sql_query.DRIVERS['mysql']
    assert get_config(config) == expect
    assert get_config({'config': config}) == expect

    username = '******'
    other_config = config.copy()
    other_expect = expect.copy()
    other_config['user'] = username
    other_expect['uid'] = username
    assert get_config({'config': config, 'username': username}) == other_expect
def test_dsn_config(config, drivers):
    parsed = get_config(config)
    connstr = connection_string(parsed).lower() + ';'
    assert 'dsn' in connstr
    for key, value in config.items():
        key = ARG_MAPPING[key]
        assert '{}={};'.format(key, value) in connstr
def test_oracle_string_port(drivers):
    config = PARAM_CONFIG.copy()
    config['dbtype'] = 'oracle'
    config['port'] = 12345

    parsed = get_config(config)
    assert parsed['port'] == 12345
    connstr = connection_string(parsed).lower()
    assert 'port=12345' in connstr
def test_oracle_string(drivers):
    config = PARAM_CONFIG.copy()
    config['dbtype'] = 'oracle'
    parsed = get_config(config)
    for key, value in config.items():
        assert parsed[ARG_MAPPING[key]] == value

    connstr = connection_string(parsed).lower()
    arg_mapping = ARG_MAPPING.copy()
    arg_mapping['database'] = 'sid'
    arg_mapping['servername'] = 'host'
    for key, value in config.items():
        key = arg_mapping[key]
        assert '{}={}'.format(key, value) in connstr
    assert 'port=1521' in connstr
def test_odbc_opts_config(drivers):
    config = PARAM_CONFIG.copy()
    opts = {'ansinpw': 1, 'tds_version': '7.0'}
    config['config'] = {'odbc_opts': opts}
    assert_in_config('ansinpw', 1, config)
    assert_in_config('tds_version', '7.0', config)

    config['config'] = {'odbc_opts': {'ansinpw': 1}}
    config['odbc_opts'] = {'ansinpw': 0}
    assert_in_config('ansinpw', 0, config)

    config['config'] = {'odbc_opts': {'ansinpw': 1}}
    config['odbc_opts'] = {'tds_version': '7.0'}
    assert_in_config('tds_version', '7.0', config)
    assert 'ansinpw' not in get_config(config)
def assert_in_config(key, value, config):
    parsed = get_config(config)
    assert parsed[key] == value
    connstr = ';' + connection_string(parsed).lower() + ';'
    assert ';{}={};'.format(key, value) in connstr
def test_dsn_config_error(config):
    with pytest.raises(ModuleError):
        get_config(config)