91 lines
2.2 KiB
Dart
91 lines
2.2 KiB
Dart
@Tags(['integration', 'postgres'])
|
|
library;
|
|
|
|
import 'package:postgres/postgres.dart';
|
|
import 'package:test/test.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import '../helpers/test_db.dart';
|
|
|
|
void main() {
|
|
const Uuid uuid = Uuid();
|
|
TestDb? testDb;
|
|
|
|
setUpAll(() async {
|
|
testDb = await TestDb.open();
|
|
});
|
|
|
|
tearDown(() async {
|
|
if (testDb != null) {
|
|
await testDb!.truncateTradingTables();
|
|
}
|
|
});
|
|
|
|
tearDownAll(() async {
|
|
await testDb?.close();
|
|
});
|
|
|
|
test('trading tables enforce FK to users', () async {
|
|
if (testDb == null) {
|
|
markTestSkipped('Set DATABASE_URL or TEST_DATABASE_URL for integration tests');
|
|
return;
|
|
}
|
|
|
|
const String uid = 'trading-schema-test-uid';
|
|
await testDb!.seedUser(uid);
|
|
final Connection connection = testDb!.connection;
|
|
final DateTime asOf = DateTime.utc(2026, 5, 23, 12);
|
|
|
|
await connection.execute(
|
|
Sql.named(
|
|
'''
|
|
INSERT INTO market_data_snapshots (symbol, metric, price, as_of)
|
|
VALUES ('SPY', 'last_trade', 492, @as_of)
|
|
''',
|
|
),
|
|
parameters: <String, dynamic>{'as_of': asOf},
|
|
);
|
|
|
|
await connection.execute(
|
|
Sql.named(
|
|
'''
|
|
INSERT INTO user_trading_config (firebase_uid, enabled, config)
|
|
VALUES (@uid, true, '{}'::jsonb)
|
|
''',
|
|
),
|
|
parameters: <String, dynamic>{'uid': uid},
|
|
);
|
|
|
|
final String orderId = uuid.v4();
|
|
await connection.execute(
|
|
Sql.named(
|
|
'''
|
|
INSERT INTO trade_orders (
|
|
id, firebase_uid, client_order_id, symbol, side, order_type, status
|
|
) VALUES (
|
|
@id::uuid, @uid, @client_order_id, 'SPY', 'buy', 'market', 'pending'
|
|
)
|
|
''',
|
|
),
|
|
parameters: <String, dynamic>{
|
|
'id': orderId,
|
|
'uid': uid,
|
|
'client_order_id': 'test-$orderId',
|
|
},
|
|
);
|
|
|
|
await expectLater(
|
|
connection.execute(
|
|
Sql.named(
|
|
'''
|
|
INSERT INTO user_trading_config (firebase_uid, enabled, config)
|
|
VALUES (@uid, true, '{}'::jsonb)
|
|
''',
|
|
),
|
|
parameters: <String, dynamic>{'uid': 'missing-user-uid'},
|
|
),
|
|
throwsA(isA<ServerException>()),
|
|
);
|
|
});
|
|
}
|