-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add more generic serialization
Generalize type branding Add `Date` object serialization and tests
- Loading branch information
1 parent
b10bb1d
commit d843af6
Showing
6 changed files
with
131 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Worker } from "near-workspaces"; | ||
import test from "ava"; | ||
|
||
test.beforeEach(async (t) => { | ||
// Init the worker and start a Sandbox server | ||
const worker = await Worker.init(); | ||
|
||
// Prepare sandbox for tests, create accounts, deploy contracts, etx. | ||
const root = worker.rootAccount; | ||
|
||
// Create and deploy test contract | ||
const dsContract = await root.devDeploy("build/date-serialization.wasm"); | ||
|
||
// Save state for test runs | ||
t.context.worker = worker; | ||
t.context.accounts = { root, dsContract }; | ||
}); | ||
|
||
test.afterEach.always(async (t) => { | ||
await t.context.worker.tearDown().catch((error) => { | ||
console.log("Failed to tear down the worker:", error); | ||
}); | ||
}); | ||
|
||
test("get initial date field value", async (t) => { | ||
const { dsContract } = t.context.accounts; | ||
const dateField = await dsContract.view("getDateField"); | ||
t.is(dateField, new Date(0).toISOString()); | ||
}); | ||
|
||
test("get date field after set", async (t) => { | ||
const { dsContract } = t.context.accounts; | ||
const dateField = await dsContract.view("getDateField"); | ||
t.is(dateField, new Date(0).toISOString()); | ||
|
||
const newDate = new Date(); | ||
await dsContract.call(dsContract, "setDateField", { dateField: newDate }); | ||
const afterSet = await dsContract.view("getDateField"); | ||
t.is(afterSet, newDate.toISOString()); | ||
}); | ||
|
||
test("get date field in milliseconds", async (t) => { | ||
const { dsContract } = t.context.accounts; | ||
const dateField = await dsContract.view("getDateFieldAsMilliseconds"); | ||
t.is(dateField, new Date(0).getTime()); | ||
|
||
const newDate = new Date(); | ||
await dsContract.call(dsContract, "setDateField", { dateField: newDate }); | ||
const afterIncrement = await dsContract.view("getDateFieldAsMilliseconds"); | ||
t.is(afterIncrement, newDate.getTime()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { near, NearBindgen, call, view } from "near-sdk-js"; | ||
|
||
@NearBindgen({}) | ||
export class DateSerializationTest { | ||
dateField: Date; | ||
|
||
constructor() { | ||
this.dateField = new Date(0); | ||
} | ||
|
||
@view({}) | ||
getDateField(): Date { | ||
near.log(`getDateField: ${this.dateField}`); | ||
return this.dateField; | ||
} | ||
|
||
@call({}) | ||
setDateField(args: { dateField: Date }): void { | ||
const dateField = new Date(args.dateField); | ||
near.log(`setDateField: ${dateField}`); | ||
this.dateField = dateField; | ||
} | ||
|
||
@view({}) | ||
getDateFieldAsMilliseconds(): number { | ||
near.log(`getDateFieldAsMilliseconds: ${this.dateField.getTime()}`); | ||
return this.dateField.getTime(); | ||
} | ||
} |