|
| 1 | +import { assert, test, describe } from "matchstick-as/assembly/index" |
| 2 | +import { BigInt } from "@graphprotocol/graph-ts" |
| 3 | +import { DAY, HOUR, MONTH, QUARTER, WEEK, YEAR, getIntervalFromTimestamp, getPreviousIntervalFromTimestamp } from "../../src/common/utils/time" |
| 4 | + |
| 5 | +describe("time.getIntervalFromTimestamp", () => { |
| 6 | + test("Support all the different periods", () => { |
| 7 | + const timestamp = BigInt.fromString("1712744972") |
| 8 | + |
| 9 | + // simple periods |
| 10 | + let res = getIntervalFromTimestamp(timestamp, HOUR) |
| 11 | + assert.assertTrue(res.equals(BigInt.fromString("1712743200"))) |
| 12 | + |
| 13 | + res = getIntervalFromTimestamp(timestamp, DAY) |
| 14 | + assert.assertTrue(res.equals(BigInt.fromString("1712707200"))) |
| 15 | + |
| 16 | + res = getIntervalFromTimestamp(timestamp, WEEK) |
| 17 | + assert.assertTrue(res.equals(BigInt.fromString("1712448000"))) |
| 18 | + |
| 19 | + res = getIntervalFromTimestamp(timestamp, MONTH) |
| 20 | + assert.assertTrue(res.equals(BigInt.fromString("1711929600"))) |
| 21 | + |
| 22 | + res = getIntervalFromTimestamp(timestamp, QUARTER) |
| 23 | + assert.assertTrue(res.equals(BigInt.fromString("1711929600"))) |
| 24 | + |
| 25 | + res = getIntervalFromTimestamp(timestamp, YEAR) |
| 26 | + assert.assertTrue(res.equals(BigInt.fromString("1704067200"))) |
| 27 | + }) |
| 28 | + |
| 29 | + test("can query the previous interval as well", () => { |
| 30 | + const timestamp = BigInt.fromString("1712744972") |
| 31 | + |
| 32 | + // simple periods |
| 33 | + let res = getPreviousIntervalFromTimestamp(timestamp, HOUR) |
| 34 | + assert.assertTrue(res.equals(BigInt.fromString("1712739600"))) |
| 35 | + |
| 36 | + res = getPreviousIntervalFromTimestamp(timestamp, DAY) |
| 37 | + assert.assertTrue(res.equals(BigInt.fromString("1712620800"))) |
| 38 | + |
| 39 | + res = getPreviousIntervalFromTimestamp(timestamp, WEEK) |
| 40 | + assert.assertTrue(res.equals(BigInt.fromString("1711843200"))) |
| 41 | + |
| 42 | + res = getPreviousIntervalFromTimestamp(timestamp, MONTH) |
| 43 | + assert.assertTrue(res.equals(BigInt.fromString("1709251200"))) |
| 44 | + |
| 45 | + res = getPreviousIntervalFromTimestamp(timestamp, QUARTER) |
| 46 | + assert.assertTrue(res.equals(BigInt.fromString("1704067200"))) |
| 47 | + |
| 48 | + res = getPreviousIntervalFromTimestamp(timestamp, YEAR) |
| 49 | + assert.assertTrue(res.equals(BigInt.fromString("1672531200"))) |
| 50 | + }) |
| 51 | +}) |
0 commit comments