-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add test case for Vector with borsh serializer #403
Open
denbite
wants to merge
4
commits into
develop
Choose a base branch
from
add_guest_book_borsh_example_with_tests
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ac08d44
test: add test case for Vector with borsh serializer
denbite ce96d15
Merge branch 'develop' into add_guest_book_borsh_example_with_tests
ailisp ef874fc
Merge branch 'develop' into add_guest_book_borsh_example_with_tests
ailisp a2f4697
test: check messages length
denbite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,35 @@ | ||
import { Worker } from 'near-workspaces'; | ||
import test from 'ava'; | ||
|
||
test.before(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; | ||
|
||
// Deploy the contract. | ||
const guestBook = await root.devDeploy('./build/guest-book-borsh.wasm'); | ||
|
||
// Save state for test runs | ||
t.context.worker = worker; | ||
t.context.accounts = { root, guestBook }; | ||
}); | ||
|
||
test.after.always(async (t) => { | ||
await t.context.worker.tearDown().catch((error) => { | ||
console.log('Failed to tear down the worker:', error); | ||
}); | ||
}); | ||
|
||
test('Root adds non-premium message then gets', async (t) => { | ||
const { guestBook, root } = t.context.accounts; | ||
const messagesBefore = await guestBook.view('get_messages', {}); | ||
t.is(messagesBefore.length, 0); | ||
|
||
await root.call(guestBook, 'add_message', { text: 'Some usual text' }); | ||
|
||
const messagesAfter = await guestBook.view('get_messages', {}); | ||
t.is(messagesAfter.length, 1); | ||
t.is(messagesAfter[0].text, 'Some usual text'); | ||
}); |
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,82 @@ | ||
import { NearBindgen, near, call, view, Vector } from 'near-sdk-js'; | ||
import * as borsh from 'borsh'; | ||
|
||
@NearBindgen({ | ||
serializer(value) { | ||
return borsh.serialize(schema, value); | ||
}, | ||
deserializer(value) { | ||
return borsh.deserialize(schema, value); | ||
}, | ||
}) | ||
class GuestBook { | ||
messages: Vector<PostedMessage> = new Vector<PostedMessage>('v-uid'); | ||
|
||
@call({ payableFunction: true }) | ||
// Public - Adds a new message. | ||
add_message({ text }: { text: string }) { | ||
// If the user attaches more than 0.1N the message is premium | ||
const premium = near.attachedDeposit() >= BigInt(POINT_ONE); | ||
const sender = near.predecessorAccountId(); | ||
|
||
const message: PostedMessage = { premium, sender, text }; | ||
this.messages.push(message, { | ||
serializer: (value) => borsh.serialize(MessageSchema, value), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a manual serialization, can we make use of above
to auto serialize? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. didn't catch that, could you clarify? |
||
}); | ||
} | ||
|
||
@view({}) | ||
// Returns an array of messages. | ||
get_messages({ | ||
from_index = 0, | ||
limit = 10, | ||
}: { | ||
from_index: number; | ||
limit: number; | ||
}): PostedMessage[] { | ||
return this.messages | ||
.toArray({ | ||
deserializer: (value) => borsh.deserialize(MessageSchema, value), | ||
}) | ||
.slice(from_index, from_index + limit); | ||
} | ||
|
||
@view({}) | ||
total_messages(): number { | ||
return this.messages.length; | ||
} | ||
} | ||
|
||
const schema: borsh.Schema = { | ||
struct: { | ||
// Vector's internal info | ||
messages: { | ||
struct: { | ||
prefix: 'string', | ||
length: 'u32', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const POINT_ONE = '100000000000000000000000'; | ||
|
||
class PostedMessage { | ||
premium: boolean; | ||
sender: string; | ||
text: string; | ||
|
||
constructor({ premium, sender, text }: PostedMessage) { | ||
this.premium = premium; | ||
this.sender = sender; | ||
this.text = text; | ||
} | ||
} | ||
|
||
const MessageSchema: borsh.Schema = { | ||
struct: { | ||
premium: 'bool', | ||
sender: 'string', | ||
text: 'string', | ||
}, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
schema is not defined, would result in error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
schema is defined in the bottom