@quantform/sqlite 0.6.6 → 0.7.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,157 +0,0 @@
1
- import {
2
- Asset,
3
- BacktesterStreamer,
4
- Commission,
5
- d,
6
- Feed,
7
- Instrument,
8
- Store,
9
- TradePatchEvent
10
- } from '@quantform/core';
11
- import { existsSync, unlinkSync } from 'fs';
12
-
13
- import { SQLiteStorage } from './sqlite-storage';
14
-
15
- describe('sqlite feed tests', () => {
16
- const dbName = 'test.db';
17
- const instrument = new Instrument(
18
- 0,
19
- new Asset('btc', 'binance', 8),
20
- new Asset('usdt', 'binance', 2),
21
- '',
22
- Commission.Zero
23
- );
24
-
25
- afterEach(() => {
26
- if (existsSync(dbName)) {
27
- unlinkSync(dbName);
28
- }
29
- });
30
-
31
- test('should create db file in user directory', async () => {
32
- const feed = new Feed(new SQLiteStorage(dbName));
33
-
34
- const input = [
35
- new TradePatchEvent(instrument, d(1234.56789), d(1.12345678), 1616175004063)
36
- ];
37
-
38
- await feed.save(input);
39
-
40
- const output = await feed.query(instrument, {
41
- from: 1616175004062,
42
- to: 1616234152109,
43
- count: 100
44
- });
45
-
46
- expect(output.length).toBe(1);
47
- });
48
-
49
- test('should insert multiple rows', async () => {
50
- const feed = new Feed(new SQLiteStorage(dbName));
51
-
52
- const input = [
53
- new TradePatchEvent(instrument, d(1234.56789), d(1.12345678), 1616175004063),
54
- new TradePatchEvent(instrument, d(1234.56789), d(2.12345678), 1616221874143),
55
- new TradePatchEvent(instrument, d(1234.56789), d(3.12345678), 1616234152108)
56
- ];
57
-
58
- await feed.save(input);
59
-
60
- const output = await feed.query(instrument, {
61
- from: 1616175004062,
62
- to: 1616234152109,
63
- count: 100
64
- });
65
-
66
- expect(output.length).toBe(3);
67
-
68
- for (let i = 0; i < 3; i++) {
69
- expect(output[i]).toEqual(input[i]);
70
- }
71
- });
72
-
73
- test('should limit result', async () => {
74
- const feed = new Feed(new SQLiteStorage(dbName));
75
-
76
- const input = [
77
- new TradePatchEvent(instrument, d(1234.56789), d(1.12345678), 1616175004063),
78
- new TradePatchEvent(instrument, d(1234.56789), d(2.12345678), 1616221874143),
79
- new TradePatchEvent(instrument, d(1234.56789), d(3.12345678), 1616234152108)
80
- ];
81
-
82
- await feed.save(input);
83
-
84
- const output = await feed.query(instrument, {
85
- from: 1616175004062,
86
- to: 1616234152109,
87
- count: 2
88
- });
89
-
90
- expect(output.length).toBe(2);
91
- expect(output[0].timestamp).toBe(input[0].timestamp);
92
- expect(output[1].timestamp).toBe(input[1].timestamp);
93
- });
94
-
95
- test('should override duplicated rows', async () => {
96
- const feed = new Feed(new SQLiteStorage(dbName));
97
-
98
- await feed.save([
99
- new TradePatchEvent(instrument, d(1234.56789), d(1.12345678), 1616175004063),
100
- new TradePatchEvent(instrument, d(1234.56789), d(2.12345678), 1616221874143),
101
- new TradePatchEvent(instrument, d(1234.56789), d(3.12345678), 1616234152108)
102
- ]);
103
-
104
- const result = await feed.query(instrument, {
105
- from: 1616175004062,
106
- to: 1616175004064,
107
- count: 100
108
- });
109
-
110
- expect(result.length).toBe(1);
111
- });
112
-
113
- test('should patch a store with events', done => {
114
- const feed = new Feed(new SQLiteStorage(dbName));
115
- const store = new Store();
116
-
117
- store.snapshot.universe.instrument.upsert(instrument);
118
- store.snapshot.subscription.instrument.upsert(instrument);
119
-
120
- const streamer = new BacktesterStreamer(
121
- store,
122
- feed,
123
- {
124
- from: 0,
125
- to: 10
126
- },
127
- {
128
- onBacktestCompleted: () => {
129
- const trade = store.snapshot.trade.get(instrument.id) ?? fail();
130
-
131
- expect(trade.timestamp).toEqual(8);
132
- expect(trade.rate).toEqual(d(8));
133
- expect(trade.quantity).toEqual(d(8));
134
- expect(store.snapshot.timestamp).toEqual(8);
135
-
136
- done();
137
- }
138
- }
139
- );
140
-
141
- feed
142
- .save([
143
- new TradePatchEvent(instrument, d(1), d(1), 1),
144
- new TradePatchEvent(instrument, d(2), d(2), 2),
145
- new TradePatchEvent(instrument, d(3), d(3), 3),
146
- new TradePatchEvent(instrument, d(4), d(4), 4),
147
- new TradePatchEvent(instrument, d(5), d(5), 5),
148
- new TradePatchEvent(instrument, d(6), d(6), 6),
149
- new TradePatchEvent(instrument, d(7), d(7), 7),
150
- new TradePatchEvent(instrument, d(8), d(8), 8)
151
- ])
152
- .then(() => {
153
- streamer.subscribe(instrument);
154
- streamer.tryContinue();
155
- });
156
- });
157
- });
@@ -1,150 +0,0 @@
1
- import { Measurement } from '@quantform/core';
2
- import { existsSync, unlinkSync } from 'fs';
3
-
4
- import { SQLiteStorage } from './sqlite-storage';
5
-
6
- describe('sqlite measurement tests', () => {
7
- const dbName = 'measurement.db';
8
-
9
- beforeEach(() => {
10
- if (existsSync(dbName)) {
11
- unlinkSync(dbName);
12
- }
13
- });
14
-
15
- afterAll(() => {
16
- if (existsSync(dbName)) {
17
- unlinkSync(dbName);
18
- }
19
- });
20
-
21
- test('should return empty array for unknown session', async () => {
22
- const measurement = new Measurement(new SQLiteStorage(dbName));
23
-
24
- const measure = await measurement.query(0, {
25
- from: 0,
26
- count: 100
27
- });
28
-
29
- expect(measure).toEqual([]);
30
- });
31
-
32
- test('should list written sessions', async () => {
33
- const measurement = new Measurement(new SQLiteStorage(dbName));
34
-
35
- await measurement.save(
36
- 1,
37
- [...Array(10).keys()].map(it => ({
38
- timestamp: it + 1,
39
- kind: 'spread',
40
- payload: { value: it + 1 }
41
- }))
42
- );
43
-
44
- await measurement.save(
45
- 2,
46
- [...Array(10).keys()].map(it => ({
47
- timestamp: it + 1,
48
- kind: 'spread',
49
- payload: { value: it + 1 }
50
- }))
51
- );
52
-
53
- const index = await measurement.index();
54
-
55
- expect(index.length).toBe(2);
56
- expect(index[0]).toBe(1);
57
- expect(index[1]).toBe(2);
58
- });
59
-
60
- test('should read and write measurement', async () => {
61
- const measurement = new Measurement(new SQLiteStorage(dbName));
62
-
63
- const session = 1;
64
-
65
- await measurement.save(
66
- session,
67
- [...Array(10).keys()].map(it => ({
68
- timestamp: it + 1,
69
- kind: 'spread',
70
- payload: { value: it + 1 }
71
- }))
72
- );
73
-
74
- const after = await measurement.query(session, {
75
- from: 5,
76
- count: 100
77
- });
78
-
79
- expect(after.length).toBe(5);
80
- expect(after[0].timestamp).toBe(6);
81
- expect(after[0].kind).toBe('spread');
82
- expect(after[0].payload.value).toBe(6);
83
-
84
- expect(after[1].timestamp).toBe(7);
85
- expect(after[1].kind).toBe('spread');
86
- expect(after[1].payload.value).toBe(7);
87
-
88
- expect(after[2].timestamp).toBe(8);
89
- expect(after[2].kind).toBe('spread');
90
- expect(after[2].payload.value).toBe(8);
91
-
92
- const before = await measurement.query(session, {
93
- to: 6,
94
- count: 100
95
- });
96
-
97
- expect(before.length).toBe(5);
98
- expect(before[0].timestamp).toBe(1);
99
- expect(before[0].kind).toBe('spread');
100
- expect(before[0].payload.value).toBe(1);
101
-
102
- expect(before[1].timestamp).toBe(2);
103
- expect(before[1].kind).toBe('spread');
104
- expect(before[1].payload.value).toBe(2);
105
-
106
- expect(before[2].timestamp).toBe(3);
107
- expect(before[2].kind).toBe('spread');
108
- expect(before[2].payload.value).toBe(3);
109
- });
110
-
111
- test('should read and write specific measurement (state)', async () => {
112
- const measurement = new Measurement(new SQLiteStorage(dbName));
113
-
114
- const session = 1;
115
-
116
- await measurement.save(session, [
117
- {
118
- timestamp: 1,
119
- kind: 'order-completed',
120
- payload: { rate: 100 }
121
- },
122
- {
123
- timestamp: 5,
124
- kind: 'order-completed',
125
- payload: { rate: 105 }
126
- }
127
- ]);
128
-
129
- let measure = await measurement.query(session, {
130
- to: 2,
131
- count: 1,
132
- kind: 'order-completed'
133
- });
134
-
135
- expect(measure.length).toBe(1);
136
- expect(measure[0].timestamp).toBe(1);
137
- expect(measure[0].kind).toBe('order-completed');
138
- expect(measure[0].payload.rate).toBe(100);
139
-
140
- measure = await measurement.query(session, {
141
- to: 6,
142
- count: 1
143
- });
144
-
145
- expect(measure.length).toBe(1);
146
- expect(measure[0].timestamp).toBe(5);
147
- expect(measure[0].kind).toBe('order-completed');
148
- expect(measure[0].payload.rate).toBe(105);
149
- });
150
- });