difftreelog
Merge pull request #101 from usetech-llc/feature/NFT-253_setOffchainSchema
in: master
Add tests for setOffchainSchema
3 files changed
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -41,6 +41,7 @@
"testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
"testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",
"testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",
+ "testSetOffchainSchema": "mocha --timeout 9999999 -r ts-node/register ./**/setOffchainSchema.test.ts",
"testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts"
},
"author": "",
tests/src/setOffchainSchema.test.tsdiffbeforeafterboth1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//56import { IKeyringPair } from '@polkadot/types/types';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import privateKey from './substrate/privateKey';10import usingApi from './substrate/substrate-api';11import {12 createCollectionExpectSuccess,13 destroyCollectionExpectSuccess,14 findNotExistingCollection,15 queryCollectionExpectSuccess,16 setOffchainSchemaExpectFailure,17 setOffchainSchemaExpectSuccess,18} from './util/helpers';1920chai.use(chaiAsPromised);21const expect = chai.expect;2223const DATA = [1, 2, 3, 4];2425describe('Integration Test setOffchainSchema', () => {26 let alice: IKeyringPair;2728 before(async () => {29 await usingApi(async () => {30 alice = privateKey('//Alice');31 });32 });3334 it('execute setOffchainSchema, verify data was set', async () => {35 const collectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });36 await setOffchainSchemaExpectSuccess(alice, collectionId, DATA);37 const collection = await queryCollectionExpectSuccess(collectionId);3839 expect(Array.from(collection.OffchainSchema)).to.be.deep.equal(DATA);40 });41});4243describe('Negative Integration Test setOffchainSchema', () => {44 let alice: IKeyringPair;45 let bob: IKeyringPair;4647 let validCollectionId: number;4849 before(async () => {50 await usingApi(async () => {51 alice = privateKey('//Alice');52 bob = privateKey('//Bob');5354 validCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });55 });56 });5758 it('fails on not existing collection id', async () => {59 const nonExistingCollectionId = await usingApi(findNotExistingCollection);6061 await setOffchainSchemaExpectFailure(alice, nonExistingCollectionId, DATA);62 });6364 it('fails on destroyed collection id', async () => {65 const destroyedCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });66 await destroyCollectionExpectSuccess(destroyedCollectionId);6768 await setOffchainSchemaExpectFailure(alice, destroyedCollectionId, DATA);69 });7071 it('fails on too long data', async () => {72 const tooLongData = new Array(4097).fill(0xff);7374 await setOffchainSchemaExpectFailure(alice, validCollectionId, tooLongData);75 });7677 it('fails on execution by non-owner', async () => {78 await setOffchainSchemaExpectFailure(bob, validCollectionId, DATA);79 });80});tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -488,6 +488,23 @@
});
}
+export async function setOffchainSchemaExpectSuccess(sender: IKeyringPair, collectionId: number, data: number[]) {
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));
+ const events = await submitTransactionAsync(sender, tx);
+ const result = getGenericResult(events);
+
+ expect(result.success).to.be.true;
+ });
+}
+
+export async function setOffchainSchemaExpectFailure(sender: IKeyringPair, collectionId: number, data: number[]) {
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));
+ await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
+ });
+}
+
export interface CreateFungibleData {
readonly Value: bigint;
}
@@ -862,3 +879,9 @@
// set global object - collectionsCount
return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();
};
+
+export async function queryCollectionExpectSuccess(collectionId: number): Promise<ICollectionInterface> {
+ return await usingApi(async (api) => {
+ return (await api.query.nft.collection(collectionId)) as unknown as ICollectionInterface;
+ });
+}