-
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.
feat: ♻️ Refactoring models to own queries against tables
- Loading branch information
Showing
15 changed files
with
235 additions
and
156 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
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,73 @@ | ||
"""Forex Data Model used in TimeScaleDB""" | ||
|
||
from datetime import datetime | ||
|
||
from pydantic import BaseModel | ||
|
||
from foresight.utils.database import TimeScaleService | ||
|
||
|
||
class ForexData(BaseModel): | ||
"""TimescaleDB model for forex data. | ||
Args: | ||
instrument (str): The currency pair. | ||
time (datetime): The time of the record. | ||
bid (float): The bid price. | ||
ask (float): The ask price. | ||
""" | ||
|
||
instrument: str | ||
time: datetime | ||
bid: float | ||
ask: float | ||
|
||
@staticmethod | ||
def create_table(table_name: str = "forex_data") -> str: | ||
"""Create a table in the data store if it does not exist. | ||
Args: | ||
table_name (str): The name of the table to create. | ||
Returns: | ||
str: The name of the table created. | ||
""" | ||
|
||
# Execute SQL queries here | ||
TimeScaleService().create_table( | ||
query=f"""CREATE TABLE IF NOT EXISTS {table_name} ( | ||
instrument VARCHAR(10) NOT NULL, | ||
time TIMESTAMPTZ NOT NULL, | ||
bid FLOAT NOT NULL, | ||
ask FLOAT NOT NULL, | ||
PRIMARY KEY (instrument, time) | ||
)""", | ||
table_name=table_name, | ||
column_name="time", | ||
) | ||
|
||
return table_name | ||
|
||
def insert_forex_data(self, table_name: str = "forex_data"): | ||
"""Insert forex data into the database.""" | ||
TimeScaleService().execute( | ||
query=f"""INSERT INTO {table_name} (instrument, time, bid, ask) | ||
VALUES (%s, %s, %s, %s)""", | ||
params=( | ||
self.instrument, | ||
self.time, | ||
self.bid, | ||
self.ask, | ||
), | ||
) | ||
|
||
@staticmethod | ||
def drop_table(table_name: str = "forex_data"): | ||
"""Drop a table in the data store. | ||
Args: | ||
table_name (str): The name of the table to drop. | ||
""" | ||
|
||
# Execute SQL queries here | ||
TimeScaleService().execute(query=f"DROP TABLE {table_name}") |
File renamed without changes.
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
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
"""Test configuration for Foresight.""" | ||
|
||
import pytest | ||
|
||
from foresight.stream_service.models.forex_data import ForexData | ||
from foresight.utils.database import TimeScaleService | ||
|
||
|
||
@pytest.fixture() | ||
def create_forex_data_table(): | ||
"""Create a table in the TimescaleDB.""" | ||
table_name = ForexData.create_table() | ||
yield table_name | ||
ForexData.drop_table() | ||
|
||
|
||
@pytest.fixture() | ||
def insert_forex_data(): | ||
"""Insert data into the TimescaleDB.""" | ||
# ARRANGE | ||
table_name = create_forex_data_table | ||
data = ForexData( | ||
instrument="EUR_USD", | ||
time="2021-01-01T00:00:00", | ||
bid=1.0, | ||
ask=1.0001, | ||
) | ||
|
||
# ACT | ||
data.insert_forex_data() | ||
|
||
# ASSERT | ||
records = TimeScaleService().execute( | ||
query=f"SELECT * FROM {table_name}", | ||
) | ||
|
||
assert len(records) == 1 # 1 record | ||
assert records[0]["instrument"] == "EUR_USD" | ||
assert records[0]["bid"] == 1.0 | ||
assert records[0]["ask"] == 1.0001 | ||
assert records[0]["time"] == "2021-01-01 00:00:00+00" |
Empty file.
Oops, something went wrong.
89f0813
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Coverage