Add tests for commands to fetch bond
All checks were successful
Tests / cli_tests (18.x) (pull_request) Successful in 5m19s

This commit is contained in:
Prathamesh Musale 2024-01-24 18:22:03 +05:30
parent 55854afbab
commit 0221d75b86

View File

@ -1,16 +1,6 @@
import { spawnSync } from 'child_process'; import { spawnSync } from 'child_process';
describe('Test laconic CLI commands', () => { describe('Test laconic CLI commands', () => {
beforeAll(async () => {
// TODO: Install laconic binary (create symlink in path)
// TODO: Create a temp directory to run the tests in and cd into that
// TODO: Create the required config
});
afterAll(async () => {
// TODO: Cleanup temp dir
});
test('laconic', async () => { test('laconic', async () => {
const result = spawnSync('laconic'); const result = spawnSync('laconic');
@ -52,6 +42,7 @@ describe('Test laconic CLI commands', () => {
expect(outputObj).toHaveProperty('node'); expect(outputObj).toHaveProperty('node');
expect(outputObj).toHaveProperty('node.network', 'laconic_9000-1'); expect(outputObj).toHaveProperty('node.network', 'laconic_9000-1');
expect(outputObj).toHaveProperty('sync'); expect(outputObj).toHaveProperty('sync');
expect(Number(outputObj.sync.latest_block_height)).toBeGreaterThan(0);
expect(outputObj).toHaveProperty('validator'); expect(outputObj).toHaveProperty('validator');
expect(outputObj).toHaveProperty('validators'); expect(outputObj).toHaveProperty('validators');
expect(outputObj).toHaveProperty('num_peers'); expect(outputObj).toHaveProperty('num_peers');
@ -63,10 +54,12 @@ describe('Test laconic CLI commands', () => {
}); });
describe('laconic cns bond <command>', () => { describe('laconic cns bond <command>', () => {
let createdBondId: string; const bondBalance = 1000000000;
let bondId: string;
let bondOwner: string;
test('laconic cns bond create', async () => { test('laconic cns bond create', async () => {
const result = spawnSync('laconic', ['cns', 'bond', 'create', '--type', 'aphoton', '--quantity', '1000000000', '--gas', '200000', '--fees', '200000aphoton']); const result = spawnSync('laconic', ['cns', 'bond', 'create', '--type', 'aphoton', '--quantity', bondBalance.toString(), '--gas', '200000', '--fees', '200000aphoton']);
const output = result.stdout.toString().trim(); const output = result.stdout.toString().trim();
const errorOutput = result.stderr.toString().trim(); const errorOutput = result.stderr.toString().trim();
@ -79,7 +72,8 @@ describe('Test laconic CLI commands', () => {
// Expect output object to resultant bond id // Expect output object to resultant bond id
expect(outputObj).toHaveProperty('bondId'); expect(outputObj).toHaveProperty('bondId');
createdBondId = outputObj.bondId;
bondId = outputObj.bondId;
}); });
test('laconic cns bond list', async () => { test('laconic cns bond list', async () => {
@ -94,13 +88,75 @@ describe('Test laconic CLI commands', () => {
expect(output.length).toBeGreaterThan(0); expect(output.length).toBeGreaterThan(0);
const outputObj = Array.from<any>(JSON.parse(output)); const outputObj = Array.from<any>(JSON.parse(output));
// Expect output object to an array of bonds // Expected bond
const expectedBond = getExpectedBond({ id: bondId, balance: bondBalance });
expect(outputObj.length).toEqual(1); expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toHaveProperty('id', createdBondId);
expect(outputObj[0]).toHaveProperty('owner'); expect(outputObj[0]).toHaveProperty('owner');
expect(outputObj[0]).toHaveProperty('balance'); expect(outputObj[0]).toMatchObject(expectedBond);
bondOwner = outputObj[0].owner;
});
test('laconic cns bond list --owner <owner_address>', async () => {
const result = spawnSync('laconic', ['cns', 'bond', 'list', '--owner', bondOwner]);
const output = result.stdout.toString().trim();
const errorOutput = result.stderr.toString().trim();
expect(errorOutput).toBe('');
expect(result.status).toBe(0);
expect(output.length).toBeGreaterThan(0);
const outputObj = Array.from<any>(JSON.parse(output));
// Expected bond
const expectedBond = getExpectedBond({ id: bondId, owner: bondOwner, balance: bondBalance });
expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toEqual(expectedBond);
});
test('laconic cns bond get --id <bond_id>', async () => {
const result = spawnSync('laconic', ['cns', 'bond', 'get', '--id', bondId]);
const output = result.stdout.toString().trim();
const errorOutput = result.stderr.toString().trim();
expect(errorOutput).toBe('');
expect(result.status).toBe(0);
expect(output.length).toBeGreaterThan(0);
const outputObj = Array.from<any>(JSON.parse(output));
// Expected bond
const expectedBond = getExpectedBond({ id: bondId, owner: bondOwner, balance: bondBalance });
expect(outputObj.length).toEqual(1);
expect(outputObj[0]).toEqual(expectedBond);
}); });
}); });
}); });
}); });
}); });
// Helper methods
function getExpectedBond(params: { id: string, balance: number, owner?: string }): any {
const bond: any = {
id: params.id,
balance: [
{
type: "aphoton",
quantity: params.balance
}
]
};
// Add owner only if passed
if (params.owner) {
bond.owner = params.owner;
}
return bond;
}