Skip to content

Commit 0c3de58

Browse files
committed
RDBC-501 TimeSeriesRangeAggregation.asTypedEntry not producing expected results
1 parent 0488b6c commit 0c3de58

File tree

3 files changed

+134
-9
lines changed

3 files changed

+134
-9
lines changed

src/Documents/Queries/TimeSeries/TimeSeriesAggregationResult.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { TimeSeriesQueryResult } from "./TimeSeriesQueryResult";
22
import { TimeSeriesRangeAggregation } from "./TimeSeriesRangeAggregation";
3-
import { ClassConstructor } from "../../../Types";
3+
import { EntityConstructor } from "../../../Types";
44
import { TypedTimeSeriesAggregationResult } from "./TypedTimeSeriesAggregationResult";
55

66
export class TimeSeriesAggregationResult extends TimeSeriesQueryResult {
77
public results: TimeSeriesRangeAggregation[];
88

9-
public asTypedEntry<T extends object>(clazz: ClassConstructor<T>) {
9+
public asTypedEntry<T extends object>(clazz: EntityConstructor<T>) {
1010
const result = new TypedTimeSeriesAggregationResult<T>();
1111
result.count = this.count;
1212
result.results = this.results.map(x => x.asTypedEntry(clazz));

src/Documents/Queries/TimeSeries/TimeSeriesRangeAggregation.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ export class TimeSeriesRangeAggregation {
1818

1919
typedEntry.from = this.from;
2020
typedEntry.to = this.to;
21-
typedEntry.min = this.min ? TimeSeriesValuesHelper.setFields(clazz, this.min, true) : null;
22-
typedEntry.max = this.max ? TimeSeriesValuesHelper.setFields(clazz, this.max, true) : null;
23-
typedEntry.first = this.first ? TimeSeriesValuesHelper.setFields(clazz, this.first, true) : null;
24-
typedEntry.last = this.last ? TimeSeriesValuesHelper.setFields(clazz, this.last, true) : null;
25-
typedEntry.sum = this.sum ? TimeSeriesValuesHelper.setFields(clazz, this.sum, true) : null;
26-
typedEntry.count = this.count ? TimeSeriesValuesHelper.setFields(clazz, this.count, true) : null;
27-
typedEntry.average = this.average ? TimeSeriesValuesHelper.setFields(clazz, this.average, true) : null;
21+
typedEntry.min = this.min ? TimeSeriesValuesHelper.setFields(clazz, this.min, false) : null;
22+
typedEntry.max = this.max ? TimeSeriesValuesHelper.setFields(clazz, this.max, false) : null;
23+
typedEntry.first = this.first ? TimeSeriesValuesHelper.setFields(clazz, this.first, false) : null;
24+
typedEntry.last = this.last ? TimeSeriesValuesHelper.setFields(clazz, this.last, false) : null;
25+
typedEntry.sum = this.sum ? TimeSeriesValuesHelper.setFields(clazz, this.sum, false) : null;
26+
typedEntry.count = this.count ? TimeSeriesValuesHelper.setFields(clazz, this.count, false) : null;
27+
typedEntry.average = this.average ? TimeSeriesValuesHelper.setFields(clazz, this.average, false) : null;
2828

2929
return typedEntry;
3030
}

test/Issues/RDBC_501.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)