-
Notifications
You must be signed in to change notification settings - Fork 49
refactor: tests #97
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
Open
skywardboundd
wants to merge
12
commits into
main
Choose a base branch
from
96-update-tests
base: main
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
refactor: tests #97
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
812bd09
refator: tests
skywardboundd dfbadf9
small refactor
skywardboundd a287d0a
Merge remote-tracking branch 'origin/main' into 96-update-tests
skywardboundd 761d920
fmt
skywardboundd 25bafa9
review
skywardboundd 75a78fc
fmt
skywardboundd ffd610b
upd
skywardboundd 167a734
fmt
skywardboundd c4b2bed
fix
skywardboundd f5e8ed4
small fix
skywardboundd 589a306
fmt :)
skywardboundd 00b1b28
Merge branch 'main' into 96-update-tests
skywardboundd 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 hidden or 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 |
|---|---|---|
| @@ -1,39 +1,6 @@ | ||
| import { toNano } from "@ton/core"; | ||
| import { Blockchain } from "@ton/sandbox"; | ||
| import "@ton/test-utils"; | ||
| import { testContract } from "./contract"; | ||
| import { SampleTactContract } from "./output/sample_SampleTactContract"; | ||
|
|
||
| describe("contract", () => { | ||
| it("should deploy correctly", async () => { | ||
| // Create Sandbox and deploy contract | ||
| const system = await Blockchain.create(); | ||
| const owner = await system.treasury("owner"); | ||
| const nonOwner = await system.treasury("non-owner"); | ||
| const contract = system.openContract(await SampleTactContract.fromInit(owner.address, 0n)); | ||
| const deployResult = await contract.send(owner.getSender(), { value: toNano(1) }, null); | ||
| expect(deployResult.transactions).toHaveTransaction({ | ||
| from: owner.address, | ||
| to: contract.address, | ||
| deploy: true, | ||
| success: true, | ||
| }); | ||
| // Check counter | ||
| expect(await contract.getCounter()).toEqual(0n); | ||
|
|
||
| // Increment counter | ||
| await contract.send(owner.getSender(), { value: toNano(1) }, { $$type: "Increment" }); | ||
|
|
||
| // Check counter | ||
| expect(await contract.getCounter()).toEqual(1n); | ||
|
|
||
| // Non-owner | ||
| const nonOwnerResult = await contract.send(nonOwner.getSender(), { value: toNano(1) }, { $$type: "Increment" }); | ||
| const accessDeniedExitCode = 132; | ||
| expect(nonOwnerResult.transactions).toHaveTransaction({ | ||
| from: nonOwner.address, | ||
| to: contract.address, | ||
| success: false, | ||
| exitCode: accessDeniedExitCode, | ||
| }); | ||
| }); | ||
| describe("SampleTactContract", () => { | ||
| testContract(); | ||
| }); |
This file contains hidden or 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,137 @@ | ||
| import { toNano, Dictionary } from "@ton/core"; | ||
| import { Blockchain, SandboxContract } from "@ton/sandbox"; | ||
| import { updateConfig } from "@ton/sandbox"; | ||
| import { StoragePrices } from "@ton/sandbox/dist/config/config.tlb-gen"; | ||
| import "@ton/test-utils"; | ||
| import { SampleTactContract } from "./output/sample_SampleTactContract"; | ||
|
|
||
| const globalSetup = async () => { | ||
| const blockchain = await Blockchain.create(); | ||
|
|
||
| // --- Set storage prices to 0 if they are not being tested --- | ||
| const storagePricesDict = Dictionary.empty<number, StoragePrices>(); | ||
|
|
||
| storagePricesDict.set(0, { | ||
| kind: "StoragePrices", | ||
| utime_since: 0, | ||
| bit_price_ps: 0n, | ||
| _cell_price_ps: 0n, | ||
| mc_bit_price_ps: 0n, | ||
| mc_cell_price_ps: 0n, | ||
| }); | ||
|
|
||
| const updatedConfig = updateConfig(blockchain.config, { | ||
| kind: "ConfigParam__18", | ||
| anon0: storagePricesDict, | ||
| }); | ||
|
|
||
| blockchain.setConfig(updatedConfig); | ||
| // --- | ||
|
|
||
| const owner = await blockchain.treasury("owner"); | ||
| const nonOwner = await blockchain.treasury("non-owner"); | ||
|
|
||
| const contract: SandboxContract<SampleTactContract> = blockchain.openContract( | ||
| await SampleTactContract.fromInit(owner.address, 0n) | ||
| ); | ||
|
|
||
| const deployResult = await contract.send(owner.getSender(), { value: toNano(1) }, null); | ||
|
|
||
| expect(deployResult.transactions).toHaveTransaction({ | ||
| from: owner.address, | ||
| to: contract.address, | ||
| deploy: true, | ||
| success: true, | ||
| }); | ||
|
|
||
| return { blockchain, owner, nonOwner, contract }; | ||
| }; | ||
|
|
||
| const testDeploy = () => { | ||
| describe("test deploy", () => { | ||
| const setup = async () => { | ||
| return await globalSetup(); | ||
| }; | ||
|
|
||
| it("should deploy correctly", async () => { | ||
| const { contract } = await setup(); | ||
|
|
||
| const counter = await contract.getCounter(); | ||
| expect(counter).toEqual(0n); | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| const testIncrement = () => { | ||
| const setup = async () => { | ||
| return await globalSetup(); | ||
| }; | ||
|
|
||
| describe("test increment", () => { | ||
| it("should increment counter by owner", async () => { | ||
| const { owner, contract } = await setup(); | ||
|
|
||
| // Increment counter | ||
| await contract.send(owner.getSender(), { value: toNano(1) }, { $$type: "Increment" }); | ||
|
|
||
| // Check counter | ||
| expect(await contract.getCounter()).toEqual(1n); | ||
| }); | ||
|
|
||
| it("should not increment counter by non-owner", async () => { | ||
| const { nonOwner, contract } = await setup(); | ||
|
|
||
| // Increment counter | ||
| const result = await contract.send(nonOwner.getSender(), { value: toNano(1) }, { $$type: "Increment" }); | ||
|
|
||
| expect(result.transactions).toHaveTransaction({ | ||
| from: nonOwner.address, | ||
| to: contract.address, | ||
| success: false, | ||
| exitCode: SampleTactContract.errors["Access denied"], | ||
| }); | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| const testAdd = () => { | ||
| const setup = async () => { | ||
| return await globalSetup(); | ||
| }; | ||
|
|
||
| describe("test add", () => { | ||
| it("should add amount by owner", async () => { | ||
| const { owner, contract } = await setup(); | ||
|
|
||
| // Add amount | ||
| await contract.send(owner.getSender(), { value: toNano(10) }, { $$type: "Add", amount: 10n }); | ||
|
|
||
| // Check counter | ||
| expect(await contract.getCounter()).toEqual(10n); | ||
| }); | ||
|
|
||
| it("should not add amount by non-owner", async () => { | ||
| const { nonOwner, contract } = await setup(); | ||
|
|
||
| // Add amount | ||
| const result = await contract.send( | ||
| nonOwner.getSender(), | ||
| { value: toNano(10) }, | ||
| { $$type: "Add", amount: 10n } | ||
| ); | ||
|
|
||
| expect(result.transactions).toHaveTransaction({ | ||
| from: nonOwner.address, | ||
| to: contract.address, | ||
| success: false, | ||
| exitCode: SampleTactContract.errors["Access denied"], | ||
| }); | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| export const testContract = () => { | ||
| testDeploy(); | ||
| testIncrement(); | ||
| testAdd(); | ||
| }; | ||
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.
To me, this split into two files isn't justified, as it moves contents from one to another — and now we have two files to take care about. Complexity demon bad :)
Perhaps, it's better to add reusable helper testing utilities to
contract.ts, such that it won't be coupled to the given contract AND it'd be easy to use those functions from the*.spec.tsfiles.