def test_connection_string_nodriver():
    """
    Check that connection_string() raises an AssertionError when no driver is
    specified (which shouldn't happen, by the way).
    """
    with pytest.raises(AssertionError):
        connection_string({})

    with pytest.raises(AssertionError):
        connection_string({'server': 's', 'username': '******'})
def test_connection_string_mssql(drivers):
    """
    Check that some keys are present in a mssql connection string.
    """
    driver = sql_query.DRIVERS['mssql']
    string = connection_string({'driver': driver, 'uid': 'someuser'}).lower()
    assert 'driver={}'.format(driver) in string
    assert 'uid=someuser' in string
    assert 'disable loopback check' not in string

    string = connection_string({'driver': driver, 'uid': 'dom\\user'}).lower()
    assert 'driver={}'.format(driver) in string
    assert 'uid=dom\\user' in string
    assert 'disable loopback check=yes' in string
def test_connection_string_emptydrivers():
    """
    Check that connection_string() doesn't fail when the driver list is empty.
    """
    string = connection_string({'driver': 'd', 'server': 's'})
    string = string.lower()
    assert string in ('driver=d;server=s', 'server=s;driver=d')
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_connect(monkeypatch, drivers):
    """
    Check that the connection function returns a "valid" cursor.

    Sadly, there is no way to test the actual connection.
    """

    def fake_connect(conn_str, *args, **kwargs):
        cur = FakeCursor()
        cur.connection_string = conn_str
        cur.__dict__.update(kwargs)
        return cur

    config = INTERNAL_CONFIG.copy()
    monkeypatch.setattr(pyodbc, 'connect', fake_connect)
    with sql_query.connect(config) as conn:
        assert conn
        assert conn.connection_string == connection_string(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