Replaced the bind with the connection method

This commit is contained in:
2024-01-15 20:24:38 +01:00
parent 3af9ff58d6
commit 27a95c1c23
7 changed files with 26 additions and 48 deletions

View File

@@ -11,7 +11,6 @@ import pytest
import sqlalchemy as sa
from _pytest.monkeypatch import MonkeyPatch
from pytest_mock import MockerFixture
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session
from aki_prj23_transparenzregister.models.company import (
@@ -714,10 +713,8 @@ def test_add_relationships_none(empty_relations: list, full_db: Session) -> None
def test_relationships(documents: list[dict[str, Any]], full_db: Session) -> None:
"""Testing to add lots of relations."""
data_transfer.add_relationships(documents, full_db)
bind = full_db.bind
assert isinstance(bind, Engine)
pd.testing.assert_frame_equal(
pd.read_sql_table("company", bind),
pd.read_sql_table("company", full_db.connection()),
pd.DataFrame(
{
"id": {0: 1, 1: 2, 2: 3},
@@ -750,13 +747,13 @@ def test_relationships(documents: list[dict[str, Any]], full_db: Session) -> Non
}
),
)
assert len(pd.read_sql_table("company_relation", bind).index) == 0
assert len(pd.read_sql_table("company_relation", full_db.connection()).index) == 0
pd.testing.assert_frame_equal(
pd.read_sql_table("person_relation", bind),
pd.read_sql_table("person_relation", full_db.connection()),
pd.DataFrame({"id": {0: 1, 1: 2}, "person_id": {0: 6, 1: 7}}),
)
pd.testing.assert_frame_equal(
pd.read_sql_table("relation", bind),
pd.read_sql_table("relation", full_db.connection()),
pd.DataFrame(
{
"id": {0: 1, 1: 2},
@@ -768,7 +765,7 @@ def test_relationships(documents: list[dict[str, Any]], full_db: Session) -> Non
),
)
pd.testing.assert_frame_equal(
pd.read_sql_table("person", bind),
pd.read_sql_table("person", full_db.connection()),
pd.DataFrame(
{
"id": {0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7},
@@ -976,7 +973,7 @@ def test_add_annual_report_empty(
) -> None:
"""Testing if the correct warning is thrown when the financial and auditor records are empty."""
df_prior = pd.read_sql_table(
entities.AnnualFinanceStatement.__tablename__, full_db.bind # type: ignore
str(entities.AnnualFinanceStatement.__tablename__), full_db.connection() # type: ignore
)
spy_warning = mocker.spy(data_transfer.logger, "debug")
@@ -985,7 +982,9 @@ def test_add_annual_report_empty(
spy_warning.assert_called_once()
pd.testing.assert_frame_equal(
df_prior,
pd.read_sql_table(entities.AnnualFinanceStatement.__tablename__, full_db.bind), # type: ignore
pd.read_sql_table(
str(entities.AnnualFinanceStatement.__tablename__), full_db.connection() # type: ignore
),
)
@@ -1031,7 +1030,7 @@ def test_add_annual_report(
)
full_db.commit()
df_prior = pd.read_sql_table(
entities.AnnualFinanceStatement.__tablename__, full_db.bind # type: ignore
str(entities.AnnualFinanceStatement.__tablename__), full_db.connection() # type: ignore
)
expected_results = pd.DataFrame(
finance_statements
@@ -1158,7 +1157,7 @@ def test_company_relation_missing(empty_db: Session) -> None:
pd.testing.assert_frame_equal(
pd.read_sql_table(
entities.MissingCompany.__tablename__, empty_db.bind # type: ignore
entities.MissingCompany.__tablename__, empty_db.connection()
).set_index("name"),
pd.DataFrame(
[
@@ -1204,7 +1203,7 @@ def test_company_relation_missing_reset(empty_db: Session) -> None:
empty_db.commit()
data_transfer.reset_relation_counter(empty_db)
queried_df = pd.read_sql_table(
entities.MissingCompany.__tablename__, empty_db.bind # type: ignore
entities.MissingCompany.__tablename__, empty_db.connection()
).set_index("name")
pd.testing.assert_frame_equal(
queried_df,

View File

@@ -45,15 +45,11 @@ def destination_db() -> Generator[Session, None, None]:
def test_transfer_db(full_db: Session, destination_db: Session) -> None:
"""Tests if the data transfer between two sql tables works."""
transfer_db_function(source=full_db, destination=destination_db)
sbind = full_db.bind
dbind = destination_db.bind
assert isinstance(sbind, Engine)
assert isinstance(dbind, Engine)
assert Base.metadata.sorted_tables
for table in Base.metadata.sorted_tables + ["company"]:
pd.testing.assert_frame_equal(
pd.read_sql_table(str(table), dbind),
pd.read_sql_table(str(table), sbind),
pd.read_sql_table(str(table), destination_db.connection()),
pd.read_sql_table(str(table), full_db.connection()),
)

View File

@@ -13,10 +13,10 @@ def test_reset_sql_all(full_db: Session) -> None:
"""Tests if all sql tables are reset."""
reset_sql.reset_tables(all_tables=True, db=full_db)
assert pd.read_sql_table(
entities.MissingCompany.__tablename__, con=full_db.bind # type:ignore
entities.MissingCompany.__tablename__, con=full_db.connection()
).empty
assert pd.read_sql_table(
entities.Company.__tablename__, con=full_db.bind # type:ignore
entities.Company.__tablename__, con=full_db.connection()
).empty
@@ -24,10 +24,10 @@ def test_reset_sql(full_db: Session) -> None:
"""Tests if only most sql tables are reset."""
reset_sql.reset_tables(all_tables=False, db=full_db)
assert pd.read_sql_table(
entities.Company.__tablename__, con=full_db.bind # type:ignore
entities.Company.__tablename__, con=full_db.connection()
).empty
assert not pd.read_sql_table(
entities.MissingCompany.__tablename__, con=full_db.bind # type:ignore
entities.MissingCompany.__tablename__, con=full_db.connection()
).empty

View File

@@ -130,7 +130,7 @@ def test_transfer_news_to_sql(full_db: Session, monkeypatch: MonkeyPatch) -> Non
lambda _: NEWS_TEXTS,
)
transfer_news._transfer_news_to_sql(None, full_db) # type: ignore
articles = pd.read_sql_table(entities.News.__tablename__, full_db.bind) # type: ignore
articles = pd.read_sql_table(entities.News.__tablename__, full_db.connection()) # type: ignore
assert "text" in articles.columns
del articles["text"]
assert articles.to_dict(orient="records") == [
@@ -158,7 +158,7 @@ def test_transfer_news_to_sql(full_db: Session, monkeypatch: MonkeyPatch) -> Non
},
]
pd.testing.assert_frame_equal(
pd.read_sql_table(entities.Sentiment.__tablename__, full_db.bind), # type: ignore
pd.read_sql_table(entities.Sentiment.__tablename__, full_db.connection()),
pd.DataFrame(
[
{