-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from tsugumi-sys/feature/imple-polygon-financ…
…ials-store Store: Implement polygon financials sqlalchemy sotre
- Loading branch information
Showing
5 changed files
with
179 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...ke/stores/db_migrations/versions/3ae9ebc67811_remove_unnecessary_columns_from_polygon_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Remove unnecessary columns from polygon financials table | ||
Revision ID: 3ae9ebc67811 | ||
Revises: 29fe33d1bd8a | ||
Create Date: 2024-06-06 14:06:43.827791 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "3ae9ebc67811" | ||
down_revision: Union[str, None] = "29fe33d1bd8a" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column( | ||
"polygonapi_financials_data", | ||
sa.Column( | ||
"comprehensive_income_loss_attributable_to_parent", | ||
sa.Float(), | ||
nullable=True, | ||
), | ||
) | ||
op.drop_column( | ||
"polygonapi_financials_data", "income_loss_before_equity_method_investments" | ||
) | ||
op.drop_column( | ||
"polygonapi_financials_data", "loss_attributable_to_noncontrolling_interest" | ||
) | ||
op.drop_column("polygonapi_financials_data", "loss_attributable_to_parent") | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column( | ||
"polygonapi_financials_data", | ||
sa.Column( | ||
"loss_attributable_to_parent", | ||
sa.DOUBLE_PRECISION(precision=53), | ||
autoincrement=False, | ||
nullable=True, | ||
), | ||
) | ||
op.add_column( | ||
"polygonapi_financials_data", | ||
sa.Column( | ||
"loss_attributable_to_noncontrolling_interest", | ||
sa.DOUBLE_PRECISION(precision=53), | ||
autoincrement=False, | ||
nullable=True, | ||
), | ||
) | ||
op.add_column( | ||
"polygonapi_financials_data", | ||
sa.Column( | ||
"income_loss_before_equity_method_investments", | ||
sa.DOUBLE_PRECISION(precision=53), | ||
autoincrement=False, | ||
nullable=True, | ||
), | ||
) | ||
op.drop_column( | ||
"polygonapi_financials_data", "comprehensive_income_loss_attributable_to_parent" | ||
) | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,89 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from stocklake.environment_variables import STOCKLAKE_POLYGON_API_KEY | ||
from stocklake.polygonapi.data_loader import PolygonFinancialsDataLoader | ||
from stocklake.polygonapi.preprocessor import PolygonFinancialsDataPreprocessor | ||
from stocklake.polygonapi.stores import SAVE_ARTIFACTS_DIR, PolygonFinancialsDataStore | ||
from stocklake.polygonapi.stores import ( | ||
SAVE_ARTIFACTS_DIR, | ||
PolygonFinancialsDataSQLAlchemyStore, | ||
PolygonFinancialsDataStore, | ||
) | ||
from stocklake.stores.constants import StoreType | ||
from stocklake.stores.db import models, schemas | ||
from tests.polygonapi.test_data_loader import MockPolygonAPIServer # noqa: F401 | ||
from tests.stores.db.utils import SessionLocal # noqa: F401 | ||
|
||
|
||
def test_polygon_financials_store_local_artifact( | ||
@pytest.fixture | ||
def polygon_financials_data( | ||
MockPolygonAPIServer, # noqa: F811 | ||
monkeypatch, | ||
): | ||
monkeypatch.setenv("STOCKLAKE_POLYGON_API_KEY", "dummy_key") | ||
monkeypatch.setenv(STOCKLAKE_POLYGON_API_KEY.env_name, "dummy_key") | ||
dataloader = PolygonFinancialsDataLoader() | ||
preprocessor = PolygonFinancialsDataPreprocessor() | ||
data = preprocessor.process(dataloader.download(["MSFT"])) | ||
yield data | ||
|
||
|
||
def test_polygon_financials_store_local_artifact( | ||
polygon_financials_data, | ||
): | ||
store = PolygonFinancialsDataStore() | ||
store.save(StoreType.LOCAL_ARTIFACT, data) | ||
store.save(StoreType.LOCAL_ARTIFACT, polygon_financials_data) | ||
assert os.path.exists(os.path.join(SAVE_ARTIFACTS_DIR, "financials_data.csv")) | ||
|
||
|
||
def test_PolygonFinancialsDataSQLAlchemyStore_create( | ||
polygon_financials_data, | ||
SessionLocal, # noqa: F811 | ||
): | ||
data_length = len(polygon_financials_data) | ||
with SessionLocal() as session, session.begin(): | ||
res = session.query(models.PolygonFinancialsData).all() | ||
assert len(res) == 0 | ||
|
||
store = PolygonFinancialsDataSQLAlchemyStore(SessionLocal) | ||
store.create( | ||
[ | ||
schemas.PolygonFinancialsDataCreate(**d.dict()) | ||
for d in polygon_financials_data | ||
] | ||
) | ||
|
||
with SessionLocal() as session, session.begin(): | ||
res = session.query(models.PolygonFinancialsData).all() | ||
assert len(res) == data_length | ||
|
||
|
||
def test_PolygonFinancialsDataSQLAlchemyStore_delete( | ||
polygon_financials_data, | ||
SessionLocal, # noqa: F811 | ||
): | ||
data_length = len(polygon_financials_data) | ||
with SessionLocal() as session, session.begin(): | ||
res = session.query(models.PolygonFinancialsData).all() | ||
assert len(res) == 0 | ||
|
||
store = PolygonFinancialsDataSQLAlchemyStore(SessionLocal) | ||
store.create( | ||
[ | ||
schemas.PolygonFinancialsDataCreate(**d.dict()) | ||
for d in polygon_financials_data | ||
] | ||
) | ||
|
||
# check data is created | ||
with SessionLocal() as session, session.begin(): | ||
res = session.query(models.PolygonFinancialsData).all() | ||
assert len(res) == data_length | ||
|
||
# delete all rows | ||
store.delete() | ||
|
||
# check successfully deleted. | ||
with SessionLocal() as session, session.begin(): | ||
res = session.query(models.PolygonFinancialsData).all() | ||
assert len(res) == 0 |