Skip to content

Commit 402a4bb

Browse files
committed
test: add test for lastWeek chart
1 parent 7bb9623 commit 402a4bb

File tree

5 files changed

+90
-7
lines changed

5 files changed

+90
-7
lines changed

features/HistoricalRequest.feature.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,52 @@ Soon I should receive a message on the websocket that is equal to
177177
}
178178
```
179179

180+
## Request historical data for a week
181+
182+
When I connect to the websocket using fingerprint `${fingerprint}`
183+
184+
And I send this message via the websocket
185+
186+
```json
187+
{
188+
"message": "message",
189+
"payload": {
190+
"@context": "https://github.com/hello-nrfcloud/proto/historical-data-request",
191+
"@id": "cc270d6b-725c-4033-ac64-c5dae903f73d",
192+
"type": "lastWeek",
193+
"message": "battery",
194+
"attributes": {
195+
"min": { "attribute": "%", "aggregate": "min" }
196+
}
197+
}
198+
}
199+
```
200+
201+
<!-- @retry:tries=5,initialDelay=1000,delayFactor=2 -->
202+
203+
Given I store
204+
`$toMillis($join([$substring($fromMillis(ts), 0, 13), ":00:00Z"]))` into
205+
`lastHour`
206+
207+
Soon I should receive a message on the websocket that is equal to
208+
209+
```json
210+
{
211+
"@context": "https://github.com/hello-nrfcloud/proto/historical-data-response",
212+
"@id": "cc270d6b-725c-4033-ac64-c5dae903f73d",
213+
"type": "lastWeek",
214+
"message": "battery",
215+
"attributes": {
216+
"min": [
217+
{
218+
"%": 18,
219+
"ts": "$number{lastHour}"
220+
}
221+
]
222+
}
223+
}
224+
```
225+
180226
## Verify I will get error message if the request is invalid
181227

182228
When I connect to the websocket using fingerprint `${fingerprint}`

historicalData/historicalDataRepository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const HistoricalChartTypes = {
2727
},
2828
lastWeek: {
2929
bin: '1hour',
30-
duration: '1week',
30+
duration: '7day',
3131
expires: '5minutes',
3232
},
3333
lastMonth: {

historicalData/queryGenerator.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('queryGenerator', () => {
6262

6363
request.type = 'lastWeek'
6464
expect(getStartPeriod(request, 1688104200000)).toEqual(
65-
'from_milliseconds(1688104200000) - 1week',
65+
'from_milliseconds(1688104200000) - 7day',
6666
)
6767

6868
request.type = 'lastMonth'
@@ -257,6 +257,43 @@ describe('queryGenerator', () => {
257257
expect(result.replace(/\s+/g, ' ').trim()).toEqual(expectedQuery)
258258
})
259259

260+
it('returns the correct query statement for weekly battery request', () => {
261+
request.message = 'battery'
262+
request.type = 'lastWeek'
263+
request.attributes = {
264+
min: { attribute: '%', aggregate: 'min' },
265+
}
266+
267+
const deviceId = 'device123'
268+
const context = new URL(
269+
'https://github.com/hello-nrfcloud/proto/transformed/model/battery',
270+
)
271+
const historicalDataDatabaseName = 'database1'
272+
const historicalDataTableName = 'table1'
273+
274+
const result = getQueryStatement({
275+
request,
276+
deviceId,
277+
context,
278+
historicalDataDatabaseName,
279+
historicalDataTableName,
280+
})
281+
282+
const expectedQuery = `
283+
SELECT deviceId, bin(time, 1hour) as time, min(measure_value::double) as "min"
284+
FROM "database1"."table1"
285+
WHERE deviceId = 'device123'
286+
AND "@context" = 'https://github.com/hello-nrfcloud/proto/transformed/model/battery'
287+
AND time BETWEEN from_milliseconds(1688104200000) - 7day AND from_milliseconds(1688104200000)
288+
GROUP BY deviceId, bin(time, 1hour)
289+
ORDER BY bin(time, 1hour) DESC
290+
`
291+
.replace(/\s+/g, ' ')
292+
.trim()
293+
294+
expect(result.replace(/\s+/g, ' ').trim()).toEqual(expectedQuery)
295+
})
296+
260297
it('returns the correct query statement for location request', () => {
261298
request.message = 'location'
262299
request.type = 'lastMonth'

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
"prettier": "@nordicsemiconductor/asset-tracker-cloud-code-style/.prettierrc",
118118
"dependencies": {
119119
"@aws-lambda-powertools/metrics": "1.11.1",
120-
"@hello.nrfcloud.com/proto": "3.0.4",
120+
"@hello.nrfcloud.com/proto": "3.1.0",
121121
"@middy/core": "4.5.5",
122122
"@nordicsemiconductor/from-env": "2.0.0",
123123
"@nordicsemiconductor/timestream-helpers": "5.0.0",

0 commit comments

Comments
 (0)