1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
"""Tests for SQLAlchemy ORM models."""
from sqlalchemy import inspect
def test_base_metadata_has_all_tables():
from shared.sa_models import Base
table_names = set(Base.metadata.tables.keys())
expected = {
"candles",
"signals",
"orders",
"positions",
"portfolio_snapshots",
"news_items",
"symbol_scores",
"market_sentiment",
"stock_selections",
}
assert expected == table_names
class TestCandleRow:
def test_table_name(self):
from shared.sa_models import CandleRow
assert CandleRow.__tablename__ == "candles"
def test_columns(self):
from shared.sa_models import CandleRow
mapper = inspect(CandleRow)
cols = {c.key for c in mapper.column_attrs}
expected = {
"symbol",
"timeframe",
"open_time",
"open",
"high",
"low",
"close",
"volume",
}
assert expected == cols
def test_primary_key(self):
from shared.sa_models import CandleRow
mapper = inspect(CandleRow)
pk_cols = [c.name for c in mapper.mapper.primary_key]
assert pk_cols == ["symbol", "timeframe", "open_time"]
class TestSignalRow:
def test_table_name(self):
from shared.sa_models import SignalRow
assert SignalRow.__tablename__ == "signals"
def test_columns(self):
from shared.sa_models import SignalRow
mapper = inspect(SignalRow)
cols = {c.key for c in mapper.column_attrs}
expected = {
"id",
"strategy",
"symbol",
"side",
"price",
"quantity",
"reason",
"created_at",
}
assert expected == cols
def test_primary_key(self):
from shared.sa_models import SignalRow
mapper = inspect(SignalRow)
pk_cols = [c.name for c in mapper.mapper.primary_key]
assert pk_cols == ["id"]
class TestOrderRow:
def test_table_name(self):
from shared.sa_models import OrderRow
assert OrderRow.__tablename__ == "orders"
def test_columns(self):
from shared.sa_models import OrderRow
mapper = inspect(OrderRow)
cols = {c.key for c in mapper.column_attrs}
expected = {
"id",
"signal_id",
"symbol",
"side",
"type",
"price",
"quantity",
"status",
"created_at",
"filled_at",
}
assert expected == cols
def test_primary_key(self):
from shared.sa_models import OrderRow
mapper = inspect(OrderRow)
pk_cols = [c.name for c in mapper.mapper.primary_key]
assert pk_cols == ["id"]
def test_signal_id_foreign_key(self):
from shared.sa_models import OrderRow
table = OrderRow.__table__
fk_cols = {fk.parent.name: fk.target_fullname for fk in table.foreign_keys}
assert fk_cols == {"signal_id": "signals.id"}
class TestPositionRow:
def test_table_name(self):
from shared.sa_models import PositionRow
assert PositionRow.__tablename__ == "positions"
def test_columns(self):
from shared.sa_models import PositionRow
mapper = inspect(PositionRow)
cols = {c.key for c in mapper.column_attrs}
expected = {
"symbol",
"quantity",
"avg_entry_price",
"current_price",
"updated_at",
}
assert expected == cols
def test_primary_key(self):
from shared.sa_models import PositionRow
mapper = inspect(PositionRow)
pk_cols = [c.name for c in mapper.mapper.primary_key]
assert pk_cols == ["symbol"]
class TestPortfolioSnapshotRow:
def test_table_name(self):
from shared.sa_models import PortfolioSnapshotRow
assert PortfolioSnapshotRow.__tablename__ == "portfolio_snapshots"
def test_columns(self):
from shared.sa_models import PortfolioSnapshotRow
mapper = inspect(PortfolioSnapshotRow)
cols = {c.key for c in mapper.column_attrs}
expected = {
"id",
"total_value",
"realized_pnl",
"unrealized_pnl",
"snapshot_at",
}
assert expected == cols
def test_primary_key(self):
from shared.sa_models import PortfolioSnapshotRow
mapper = inspect(PortfolioSnapshotRow)
pk_cols = [c.name for c in mapper.mapper.primary_key]
assert pk_cols == ["id"]
def test_id_is_autoincrement(self):
from shared.sa_models import PortfolioSnapshotRow
table = PortfolioSnapshotRow.__table__
id_col = table.c.id
assert id_col.autoincrement is True or id_col.autoincrement == "auto"
class TestStatusDefault:
def test_order_status_server_default(self):
from shared.sa_models import OrderRow
table = OrderRow.__table__
status_col = table.c.status
assert status_col.server_default is not None
assert status_col.server_default.arg == "PENDING"
|