1+ import { IDocumentStore , TimeSeriesAggregationResult , TimeSeriesValue } from "../../src" ;
2+ import { disposeTestDocumentStore , testContext } from "../Utils/TestUtil" ;
3+
4+ import moment = require( "moment" ) ;
5+ import { assertThat } from "../Utils/AssertExtensions" ;
6+
7+ class SymbolPrice {
8+ open : number ;
9+ close : number ;
10+ high : number ;
11+ low : number ;
12+
13+ static TIME_SERIES_VALUES : TimeSeriesValue < SymbolPrice > = [
14+ { field : "open" , name : "Open" } ,
15+ { field : "close" , name : "Close" } ,
16+ { field : "high" , name : "High" } ,
17+ { field : "low" , name : "Low" } ,
18+ ] ;
19+ }
20+
21+ class MarketSymbol {
22+
23+ }
24+
25+ describe ( "RDBC-501" , function ( ) {
26+
27+ let store : IDocumentStore ;
28+
29+ beforeEach ( async function ( ) {
30+ store = await testContext . getDocumentStore ( ) ;
31+ } ) ;
32+
33+ afterEach ( async ( ) =>
34+ await disposeTestDocumentStore ( store ) ) ;
35+
36+ it ( "should properly map typed entries" , async function ( ) {
37+
38+ const baseLine = moment ( ) . startOf ( "day" ) ;
39+
40+ {
41+ const session = store . openSession ( ) ;
42+ const symbol = new MarketSymbol ( ) ;
43+ await session . store ( symbol , "markerSymbols/1-A" ) ;
44+
45+ const price1 = new SymbolPrice ( ) ;
46+ price1 . low = 1 ;
47+ price1 . high = 10 ;
48+ price1 . open = 4 ;
49+ price1 . close = 7 ;
50+
51+ const price2 = new SymbolPrice ( ) ;
52+ price2 . low = 21 ;
53+ price2 . high = 210 ;
54+ price2 . open = 24 ;
55+ price2 . close = 27 ;
56+
57+ const price3 = new SymbolPrice ( ) ;
58+ price3 . low = 321 ;
59+ price3 . high = 310 ;
60+ price3 . open = 34 ;
61+ price3 . close = 37 ;
62+
63+ const tsf = session . timeSeriesFor < SymbolPrice > ( symbol , "history" , SymbolPrice ) ;
64+
65+ tsf . append ( baseLine . clone ( ) . add ( 1 , "hours" ) . toDate ( ) , price1 ) ;
66+ tsf . append ( baseLine . clone ( ) . add ( 2 , "hours" ) . toDate ( ) , price2 ) ;
67+ tsf . append ( baseLine . clone ( ) . add ( 2 , "days" ) . toDate ( ) , price3 ) ;
68+
69+ await session . saveChanges ( ) ;
70+ }
71+
72+ {
73+ const session = store . openSession ( ) ;
74+
75+ const aggregatedHistoryQueryResult = await session
76+ . query ( MarketSymbol )
77+ . selectTimeSeries (
78+ ( builder ) =>
79+ builder . raw (
80+ `from history
81+ group by '1 days'
82+ select first(), last(), min(), max()`
83+ ) ,
84+ TimeSeriesAggregationResult
85+ )
86+ . firstOrNull ( ) ;
87+
88+ assertThat ( aggregatedHistoryQueryResult . results )
89+ . hasSize ( 2 ) ;
90+
91+ const typed = aggregatedHistoryQueryResult . asTypedEntry ( SymbolPrice ) ;
92+ assertThat ( typed . results )
93+ . hasSize ( 2 ) ;
94+
95+ const firstResult = typed . results [ 0 ] ;
96+ assertThat ( firstResult . min . open )
97+ . isEqualTo ( 4 ) ;
98+ assertThat ( firstResult . min . close )
99+ . isEqualTo ( 7 ) ;
100+ assertThat ( firstResult . min . low )
101+ . isEqualTo ( 1 ) ;
102+ assertThat ( firstResult . min . high )
103+ . isEqualTo ( 10 ) ;
104+
105+ assertThat ( firstResult . first . open )
106+ . isEqualTo ( 4 ) ;
107+ assertThat ( firstResult . first . close )
108+ . isEqualTo ( 7 ) ;
109+ assertThat ( firstResult . first . low )
110+ . isEqualTo ( 1 ) ;
111+ assertThat ( firstResult . first . high )
112+ . isEqualTo ( 10 ) ;
113+
114+ const secondResult = typed . results [ 1 ] ;
115+ assertThat ( secondResult . min . open )
116+ . isEqualTo ( 34 ) ;
117+ assertThat ( secondResult . min . close )
118+ . isEqualTo ( 37 ) ;
119+ assertThat ( secondResult . min . low )
120+ . isEqualTo ( 321 ) ;
121+ assertThat ( secondResult . min . high )
122+ . isEqualTo ( 310 ) ;
123+ }
124+ } ) ;
125+ } ) ;
0 commit comments