This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import pytest
|
||||
|
||||
from app.alphas import snapshot_columns, upsert_alpha
|
||||
from tests.conftest import alpha
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", [0.6988, 0.0])
|
||||
async def test_submitted_metrics_map_without_check_history(app, logged_in, value):
|
||||
raw = alpha(status="ACTIVE", **{"is": {"prodCorrelation": value, "selfCorrelation": 0.0,
|
||||
"checks": [{"name": "LOW_2Y_SHARPE", "result": "PASS", "value": 2.1}]},
|
||||
"os": {"checks": [{"name": "PROD_CORRELATION", "result": "PENDING"}]}})
|
||||
async with app.state.sessions.begin() as db:
|
||||
await upsert_alpha(db, raw)
|
||||
detail = (await logged_in.get('/api/v1/alphas/alpha1')).json()
|
||||
assert detail['prod_correlation'] == value
|
||||
assert detail['is_metrics']['selfCorrelation'] == 0.0
|
||||
assert detail['os_metrics']['checks'][0]['result'] == 'PENDING'
|
||||
assert detail['two_year_sharpe'] == 2.1
|
||||
assert detail['check_type'] != 'PASS'
|
||||
|
||||
|
||||
def test_check_value_takes_precedence_over_cached_metric():
|
||||
assert snapshot_columns({}, {'prodCorrelation': 0.4}, [
|
||||
{'name': 'PROD_CORRELATION', 'value': 0.0, 'result': 'PASS'}
|
||||
])['prod_correlation'] == 0.0
|
||||
|
||||
|
||||
def test_migration_recovers_saved_values_without_overwriting_existing():
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
path = Path(__file__).parents[1] / 'migrations/versions/0013_submitted_correlation.py'
|
||||
spec = importlib.util.spec_from_file_location('migration_0013', path)
|
||||
migration = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(migration)
|
||||
engine = sa.create_engine('sqlite://')
|
||||
metadata = sa.MetaData()
|
||||
table = sa.Table('alphas', metadata, sa.Column('id', sa.String, primary_key=True),
|
||||
sa.Column('is_metrics', sa.JSON), sa.Column('prod_correlation', sa.Float))
|
||||
metadata.create_all(engine)
|
||||
with engine.begin() as db:
|
||||
db.execute(table.insert(), [
|
||||
{'id': 'zero', 'is_metrics': {'prodCorrelation': 0.0}, 'prod_correlation': None},
|
||||
{'id': 'saved', 'is_metrics': {'prodCorrelation': 0.6988}, 'prod_correlation': None},
|
||||
{'id': 'existing', 'is_metrics': {'prodCorrelation': 0.4}, 'prod_correlation': 0.2},
|
||||
{'id': 'missing', 'is_metrics': {}, 'prod_correlation': None},
|
||||
])
|
||||
migration.op = SimpleNamespace(get_bind=lambda: db)
|
||||
migration.upgrade()
|
||||
migration.upgrade()
|
||||
assert dict(db.execute(sa.select(table.c.id, table.c.prod_correlation)).all()) == {
|
||||
'zero': 0.0, 'saved': 0.6988, 'existing': 0.2, 'missing': None}
|
||||
engine.dispose()
|
||||
Reference in New Issue
Block a user