This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
SnapshotTest.js
100 lines (80 loc) · 3.29 KB
/
SnapshotTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import gasCost from "./helpers/gasCost";
import { latestTimestamp } from "./helpers/latestTime";
import increaseTime from "./helpers/increaseTime";
import moment from "moment";
import expectThrow from "./helpers/expectThrow";
const SnapshotTest = artifacts.require("./test/SnapshotTest.sol");
const day = 24 * 3600;
contract("Snapshot", accounts => {
let value;
const createSnapshot = async () => {
const r = await value.createSnapshot();
assert.equal(r.logs.length, 1);
assert.equal(r.logs[0].event, "SnapshotCreated");
return r.logs[0].args.snapshot;
};
beforeEach(async () => {
value = await SnapshotTest.new();
});
it("should deploy", async () => {
console.log(`\tSnapshot took ${gasCost(value)}.`);
});
it("should be initially unset", async () => {
assert.isFalse(await value.hasValue.call());
});
it("should initially return default", async () => {
assert.isFalse(await value.hasValue.call());
});
it("should initially return default", async () => {
assert.equal(12, await value.getValue.call(12));
assert.equal(42, await value.getValue.call(42));
});
it("should create a snapshot", async () => {
const r = await value.createSnapshot();
console.log(`\tcreateSnapshot took ${gasCost(r)}`);
});
it("should set a new value", async () => {
const r = await value.setValue(1234);
console.log(`\tsetValue took ${gasCost(r)}`);
assert.equal(1234, await value.getValue.call(12));
assert.isTrue(await value.hasValue.call());
});
it("should reset value", async () => {
await value.setValue(1234);
const r = await value.setValue(12345);
console.log(`\tsetValue took ${gasCost(r)}`);
assert.equal(12345, await value.getValue.call(12));
assert.isTrue(await value.hasValue.call());
});
it("should keep values in snapshots", async () => {
const before = await createSnapshot();
await value.setValue(100);
const middle = await createSnapshot();
await value.setValue(200);
const after = await createSnapshot();
assert.isFalse(await value.hasValueAt.call(before));
assert.isTrue(await value.hasValueAt.call(middle));
assert.isTrue(await value.hasValueAt.call(after));
assert.equal(41, await value.getValueAt.call(before, 41));
assert.equal(100, await value.getValueAt.call(middle, 41));
assert.equal(200, await value.getValueAt.call(after, 41));
});
it("should create daily snapshots", async () => {
const day0 = await value.snapshotAt.call(latestTimestamp() + 0 * day);
const day1 = await value.snapshotAt.call(latestTimestamp() + 1 * day);
const day2 = await value.snapshotAt.call(latestTimestamp() + 2 * day);
const day3 = await value.snapshotAt.call(latestTimestamp() + 3 * day);
await value.setValue(100);
await increaseTime(moment.duration({ days: 1 }));
await value.setValue(200);
await increaseTime(moment.duration({ days: 1 }));
await value.setValue(300);
assert.equal(41, await value.getValueAt.call(day0, 41));
assert.equal(100, await value.getValueAt.call(day1, 41));
assert.equal(200, await value.getValueAt.call(day2, 41));
});
it("should throw when queried in the future", async () => {
const day1 = await value.snapshotAt.call(latestTimestamp() + 1 * day);
await expectThrow(value.getValueAt.call(day1, 41));
});
});