Beispiel #1
0
def test_connect(credentials, dbapi):
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        b = Database(dbapi=dbapi, **credentials)
        b._connect()
        mock_connect.assert_called_with(
            host="host", user="******", port="port", password="******", database="database"
        )

    credentials["extra"] = 123
    credentials["another"] = 321
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        b = Database(dbapi=dbapi, **credentials)
        b._connect()
        mock_connect.assert_called_with(
            host="host",
            user="******",
            port="port",
            password="******",
            database="database",
            extra=123,
            another=321,
        )

    # side effect exception
    mock_connect.side_effect = Exception("Connect Exception")
    with pytest.raises(DBError):
        b._connect()
Beispiel #2
0
def test_get_column_names(credentials, dbapi):
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        mock_connect.return_value.cursor.return_value.description = [["COL1"], ["COL2"]]
        with Database(dbapi=dbapi, **credentials) as test:
            assert test.column_names() == ["col1", "col2"]

        mock_connect.return_value.cursor.return_value.description = [("COL1",), ("COL2",)]
        with Database(dbapi=dbapi, **credentials) as test:
            assert test.column_names() == ["col1", "col2"]

        mock_connect.return_value.cursor.return_value.description = (("COL1",), ("COL2",))
        with Database(dbapi=dbapi, **credentials) as test:
            assert test.column_names() == ["col1", "col2"]
Beispiel #3
0
def test_disconnect(credentials, dbapi):
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        b = Database(dbapi=dbapi, **credentials)
        b._connect()
        b._disconnect()
        b.conn.close.assert_called_with()
        b.cursor.close.assert_called_with()

    # side effect exception
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        b._connect()
        b.conn.close.side_effect = Exception("Disconnect Exception")
        with pytest.raises(DBError):
            b._disconnect()
def test_to_dict(credentials, dbapi):
    def cols():
        return ["col1", "col2"]

    test = Database(dbapi=dbapi, **credentials)

    test.column_names = cols
    test.cursor = [(1, 2), (2, 3), (3, )]
    assert list(test.to_dict()) == [{
        "col1": 1,
        "col2": 2
    }, {
        "col1": 2,
        "col2": 3
    }, {
        "col1": 3
    }]

    test.column_names = cols
    test.cursor = [("a", 2), (2, 3), ("b", )]
    assert list(test.to_dict()) == [{
        "col1": "a",
        "col2": 2
    }, {
        "col1": 2,
        "col2": 3
    }, {
        "col1": "b"
    }]
Beispiel #5
0
def test_database_constructor(credentials, dbapi):
    d = Database(dbapi=dbapi, **credentials)
    assert d.connection["host"] == "host"
    assert d.connection["port"] == "port"
    assert d.connection["database"] == "database"
    assert d.connection["user"] == "user"
    assert d.connection["password"] == "password"
Beispiel #6
0
def test_to_dataframe_none(mock_pandas, credentials, dbapi):
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        mock_connect.return_value.cursor.return_value.fetchmany.return_value = []
        with Database(dbapi=dbapi, **credentials) as test:
            test.execute("SELECT 'hello world' WHERE 1=0")
            assert test.to_dataframe(size=5) is None
            mock_pandas.assert_not_called()
Beispiel #7
0
def test_database_constructor_kwargs(dbapi):
    d = Database(
        dbapi=dbapi, host="host", port="port", database="database", user="******", password="******"
    )
    assert d.connection["host"] == "host"
    assert d.connection["port"] == "port"
    assert d.connection["database"] == "database"
    assert d.connection["user"] == "user"
    assert d.connection["password"] == "password"
Beispiel #8
0
def test_to_dataframe_all(mock_pandas, credentials, dbapi):
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        mock_connect.return_value.cursor.return_value.fetchall.return_value = [(1, 2), (2, 3), (3,)]
        with Database(dbapi=dbapi, **credentials) as test:
            test.execute("SELECT 'hello world' AS fld")
            df = test.to_dataframe()

    assert mock_connect.return_value.cursor.return_value.fetchall.called
    mock_pandas.assert_called_with(test.cursor.fetchall(), columns=[])
Beispiel #9
0
def test_database_constructor_yaml(dbapi):
    d = Database(dbapi=dbapi, config_yaml="some_config.yml")
    assert d.connection["host"] == "host"
    assert d.connection["port"] == 1234
    assert d.connection["database"] == "database"
    assert d.connection["user"] == "id"
    assert d.connection["password"] == "pass"
    assert d.connection["other"] == "stuff"
    assert d.connection["extra"] == 123
    assert d.connection["another"] == 321
Beispiel #10
0
def test_database_constructor_with_extras(credentials, dbapi):
    credentials["extra"] = 123
    credentials["another"] = 321
    d = Database(dbapi=dbapi, **credentials)
    assert d.connection["host"] == "host"
    assert d.connection["port"] == "port"
    assert d.connection["database"] == "database"
    assert d.connection["user"] == "user"
    assert d.connection["password"] == "password"
    assert d.connection["extra"] == 123
    assert d.connection["another"] == 321
Beispiel #11
0
def test_database_constructor_kwargs_and_yaml(dbapi):
    with pytest.raises(CredentialsError):
        Database(
            dbapi=dbapi,
            config_yaml="some_config.yml",
            host="host",
            port="port",
            database="database",
            user="******",
            password="******",
        )
Beispiel #12
0
def test_execute_no_connection_exception(credentials, dbapi):
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        test = Database(dbapi=dbapi, **credentials)
        test.conn = None
        test.cursor = None
        with pytest.raises(DBError):
            test.execute("SELECT * FROM some_table")
Beispiel #13
0
def test_execute_sql_exception(credentials, dbapi):
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        with Database(dbapi=dbapi, **credentials) as test:
            test.cursor.execute.side_effect = Exception("SQL Exception")
            with pytest.raises(DBError):
                test.execute("SELECT * FROM some_table")
Beispiel #14
0
def test_execute(credentials, dbapi):
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        with Database(dbapi=dbapi, **credentials) as test:
            print(test)
            test.execute("SELECT * FROM some_table")
            assert test.cursor.execute.called is True
Beispiel #15
0
def test_disconnect_no_conn(credentials, dbapi):
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        b = Database(dbapi=dbapi, **credentials)
        b._disconnect()
Beispiel #16
0
def test_is_connected(credentials, dbapi):
    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        b = Database(dbapi=dbapi, **credentials)
        assert b._is_connected() is False

    with mock.patch(dbapi.__name__ + ".connect") as mock_connect:
        b = Database(dbapi=dbapi, **credentials)
        b._connect()
        assert b._is_connected() is True

    # throws exception in _is_connected
    b = Database(dbapi=dbapi, **credentials)
    del b.conn
    assert b._is_connected() is False