examples: Update tests
This commit is contained in:
parent
06ac7b2b88
commit
178807102d
|
@ -109,10 +109,9 @@ array of all instructions to run **before** the explicitly specified program ins
|
|||
which in this case is `initialize`. Because we are creating `myAccount`, it needs to
|
||||
sign the transaction, as required by the Solana runtime.
|
||||
|
||||
::: details
|
||||
In future work, we can simplify this example further by using something like a *Builder*
|
||||
pattern for constructing common transactions like creating and then initializing an account.
|
||||
:::
|
||||
We can simplify this further.
|
||||
|
||||
<<< @/../examples/tutorial/basic-1/tests/basic-1.js#code-simplified
|
||||
|
||||
As before, we can run the example tests.
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ mod composite {
|
|||
pub fn composite_update(
|
||||
ctx: Context<CompositeUpdate>,
|
||||
dummy_a: u64,
|
||||
dummy_b: String,
|
||||
dummy_b: u64,
|
||||
) -> ProgramResult {
|
||||
let a = &mut ctx.accounts.foo.dummy_a;
|
||||
let b = &mut ctx.accounts.bar.dummy_b;
|
||||
|
@ -61,5 +61,5 @@ pub struct DummyA {
|
|||
|
||||
#[account]
|
||||
pub struct DummyB {
|
||||
pub data: String,
|
||||
pub data: u64,
|
||||
}
|
||||
|
|
|
@ -1,59 +1,50 @@
|
|||
const assert = require('assert');
|
||||
const anchor = require('@project-serum/anchor');
|
||||
const assert = require("assert");
|
||||
const anchor = require("@project-serum/anchor");
|
||||
|
||||
describe('composite', () => {
|
||||
|
||||
const provider = anchor.Provider.local();
|
||||
describe("composite", () => {
|
||||
const provider = anchor.Provider.local();
|
||||
|
||||
// Configure the client to use the local cluster.
|
||||
anchor.setProvider(provider);
|
||||
|
||||
it('Is initialized!', async () => {
|
||||
const program = anchor.workspace.Composite;
|
||||
it("Is initialized!", async () => {
|
||||
const program = anchor.workspace.Composite;
|
||||
|
||||
const dummyA = new anchor.web3.Account();
|
||||
const dummyB = new anchor.web3.Account();
|
||||
const dummyA = new anchor.web3.Account();
|
||||
const dummyB = new anchor.web3.Account();
|
||||
|
||||
const tx = await program.rpc.initialize({
|
||||
const tx = await program.rpc.initialize({
|
||||
accounts: {
|
||||
dummyA: dummyA.publicKey,
|
||||
dummyB: dummyB.publicKey,
|
||||
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
||||
dummyA: dummyA.publicKey,
|
||||
dummyB: dummyB.publicKey,
|
||||
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
||||
},
|
||||
signers: [dummyA, dummyB],
|
||||
signers: [dummyA, dummyB],
|
||||
instructions: [
|
||||
anchor.web3.SystemProgram.createAccount({
|
||||
fromPubkey: provider.wallet.publicKey,
|
||||
newAccountPubkey: dummyA.publicKey,
|
||||
space: 8 + 8,
|
||||
lamports: await provider.connection.getMinimumBalanceForRentExemption(
|
||||
8 + 8
|
||||
),
|
||||
programId: program.programId,
|
||||
}),
|
||||
anchor.web3.SystemProgram.createAccount({
|
||||
fromPubkey: provider.wallet.publicKey,
|
||||
newAccountPubkey: dummyB.publicKey,
|
||||
space: 8 + 100,
|
||||
lamports: await provider.connection.getMinimumBalanceForRentExemption(
|
||||
8 + 100
|
||||
),
|
||||
programId: program.programId,
|
||||
}),
|
||||
await program.account.dummyA.createInstruction(dummyA),
|
||||
await program.account.dummyB.createInstruction(dummyB),
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
await program.rpc.compositeUpdate(new anchor.BN(1234), 'hello', {
|
||||
accounts: {
|
||||
dummyA: dummyA.publicKey,
|
||||
dummyB: dummyB.publicKey,
|
||||
},
|
||||
});
|
||||
await program.rpc.compositeUpdate(
|
||||
new anchor.BN(1234),
|
||||
new anchor.BN(4321),
|
||||
{
|
||||
accounts: {
|
||||
foo: {
|
||||
dummyA: dummyA.publicKey,
|
||||
},
|
||||
bar: {
|
||||
dummyB: dummyB.publicKey,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const dummyAAccount = await program.account.dummyA(dummyA.publicKey);
|
||||
const dummyBAccount = await program.account.dummyB(dummyB.publicKey);
|
||||
const dummyAAccount = await program.account.dummyA(dummyA.publicKey);
|
||||
const dummyBAccount = await program.account.dummyB(dummyB.publicKey);
|
||||
|
||||
assert.ok(dummyAAccount.data.eq(new anchor.BN(1234)));
|
||||
assert.ok(dummyBAccount.data === 'hello');
|
||||
assert.ok(dummyAAccount.data.eq(new anchor.BN(1234)));
|
||||
assert.ok(dummyBAccount.data.eq(new anchor.BN(4321)));
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
const anchor = require('@project-serum/anchor');
|
||||
|
||||
describe('basic-0', () => {
|
||||
const anchor = require("@project-serum/anchor");
|
||||
|
||||
describe("basic-0", () => {
|
||||
// Configure the client to use the local cluster.
|
||||
anchor.setProvider(anchor.Provider.local());
|
||||
|
||||
it('Uses the workspace to invoke the initialize instruction', async () => {
|
||||
it("Uses the workspace to invoke the initialize instruction", async () => {
|
||||
// #region code
|
||||
// Read the deployed program from the workspace.
|
||||
const program = anchor.workspace.Basic0;
|
||||
|
||||
// Execute the RPC.
|
||||
await program.rpc.initialize();
|
||||
// #endregion code
|
||||
// #endregion code
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
const assert = require('assert');
|
||||
const anchor = require('@project-serum/anchor');
|
||||
|
||||
describe('basic-1', () => {
|
||||
const assert = require("assert");
|
||||
const anchor = require("@project-serum/anchor");
|
||||
|
||||
describe("basic-1", () => {
|
||||
// Use a local provider.
|
||||
const provider = anchor.Provider.local()
|
||||
const provider = anchor.Provider.local();
|
||||
|
||||
// Configure the client to use the local cluster.
|
||||
anchor.setProvider(provider);
|
||||
|
||||
it('Creates and initializes an account in two different transactions', async () => {
|
||||
it("Creates and initializes an account in two different transactions", async () => {
|
||||
// The program owning the account to create.
|
||||
const program = anchor.workspace.Basic1;
|
||||
|
||||
|
@ -22,10 +21,12 @@ describe('basic-1', () => {
|
|||
anchor.web3.SystemProgram.createAccount({
|
||||
fromPubkey: provider.wallet.publicKey,
|
||||
newAccountPubkey: myAccount.publicKey,
|
||||
space: 8+8,
|
||||
lamports: await provider.connection.getMinimumBalanceForRentExemption(8+8),
|
||||
space: 8 + 8,
|
||||
lamports: await provider.connection.getMinimumBalanceForRentExemption(
|
||||
8 + 8
|
||||
),
|
||||
programId: program.programId,
|
||||
}),
|
||||
})
|
||||
);
|
||||
|
||||
// Execute the transaction against the cluster.
|
||||
|
@ -51,7 +52,7 @@ describe('basic-1', () => {
|
|||
// Reference to an account to use between multiple tests.
|
||||
let _myAccount = undefined;
|
||||
|
||||
it('Creates and initializes an account in a single atomic transaction', async () => {
|
||||
it("Creates and initializes an account in a single atomic transaction", async () => {
|
||||
// The program to execute.
|
||||
const program = anchor.workspace.Basic1;
|
||||
|
||||
|
@ -70,8 +71,10 @@ describe('basic-1', () => {
|
|||
anchor.web3.SystemProgram.createAccount({
|
||||
fromPubkey: provider.wallet.publicKey,
|
||||
newAccountPubkey: myAccount.publicKey,
|
||||
space: 8+8, // Add 8 for the account discriminator.
|
||||
lamports: await provider.connection.getMinimumBalanceForRentExemption(8+8),
|
||||
space: 8 + 8, // Add 8 for the account discriminator.
|
||||
lamports: await provider.connection.getMinimumBalanceForRentExemption(
|
||||
8 + 8
|
||||
),
|
||||
programId: program.programId,
|
||||
}),
|
||||
],
|
||||
|
@ -83,13 +86,38 @@ describe('basic-1', () => {
|
|||
// Check it's state was initialized.
|
||||
assert.ok(account.data.eq(new anchor.BN(1234)));
|
||||
// #endregion code
|
||||
});
|
||||
|
||||
it("Creates and initializes an account in a single atomic transaction (simplified)", async () => {
|
||||
// The program to execute.
|
||||
const program = anchor.workspace.Basic1;
|
||||
|
||||
// The Account to create.
|
||||
const myAccount = new anchor.web3.Account();
|
||||
|
||||
// Atomically create the new account and initialize it with the program.
|
||||
// #region code-simplified
|
||||
await program.rpc.initialize(new anchor.BN(1234), {
|
||||
accounts: {
|
||||
myAccount: myAccount.publicKey,
|
||||
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
||||
},
|
||||
signers: [myAccount],
|
||||
instructions: [await program.account.myAccount(myAccount)],
|
||||
});
|
||||
// #endregion code-simplified
|
||||
|
||||
// Fetch the newly created account from the cluster.
|
||||
const account = await program.account.myAccount(myAccount.publicKey);
|
||||
|
||||
// Check it's state was initialized.
|
||||
assert.ok(account.data.eq(new anchor.BN(1234)));
|
||||
|
||||
// Store the account for the next test.
|
||||
_myAccount = myAccount;
|
||||
});
|
||||
|
||||
it('Updates a previously created account', async () => {
|
||||
|
||||
it("Updates a previously created account", async () => {
|
||||
const myAccount = _myAccount;
|
||||
|
||||
// #region update-test
|
||||
|
|
Loading…
Reference in New Issue