difftreelog
Merge pull request #72 from usetech-llc/feature/NFTPAR-255_setVariableMetadata
in: master
Add tests for setVariableMetadata
3 files changed
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -20,6 +20,7 @@
"load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",
"testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",
"testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",
+ "testSetVariableMetaData": "mocha --timeout 9999999 -r ts-node/register ./**/setVariableMetaData.test.ts",
"testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",
"testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",
"testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",
tests/src/setVariableMetaData.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/setVariableMetaData.test.ts
@@ -0,0 +1,104 @@
+import { IKeyringPair } from '@polkadot/types/types';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import usingApi from './substrate/substrate-api';
+import {
+ burnItemExpectSuccess,
+ createCollectionExpectSuccess,
+ createItemExpectSuccess,
+ destroyCollectionExpectSuccess,
+ findNotExistingCollection,
+ setVariableMetaDataExpectFailure,
+ setVariableMetaDataExpectSuccess,
+} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+describe('Integration Test setVariableMetaData', () => {
+ const data = [1, 2, 254, 255];
+
+ let alice: IKeyringPair;
+ let collectionId: number;
+ let tokenId: number;
+ before(async () => {
+ await usingApi(async () => {
+ alice = privateKey('//Alice');
+ collectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+ tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');
+ });
+ });
+
+ it('execute setVariableMetaData', async () => {
+ await setVariableMetaDataExpectSuccess(alice, collectionId, tokenId, data);
+ });
+
+ it('verify data was set', async () => {
+ await usingApi(async api => {
+ const item: any = await api.query.nft.nftItemList(collectionId, tokenId);
+
+ expect(Array.from(item.VariableData)).to.deep.equal(Array.from(data));
+ });
+ });
+});
+
+describe('Negative Integration Test setVariableMetaData', () => {
+ let data = [1];
+
+ let alice: IKeyringPair;
+ let bob: IKeyringPair;
+
+ let validCollectionId: number;
+ let validTokenId: number;
+
+ before(async () => {
+ await usingApi(async () => {
+ alice = privateKey('//Alice');
+ bob = privateKey('//Bob');
+
+ validCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+ validTokenId = await createItemExpectSuccess(alice, validCollectionId, 'NFT');
+ });
+ });
+
+ it('fails on not existing collection id', async () => {
+ await usingApi(async api => {
+ let nonExistingCollectionId = await findNotExistingCollection(api);
+ await setVariableMetaDataExpectFailure(alice, nonExistingCollectionId, 1, data);
+ });
+ });
+ it('fails on removed collection id', async () => {
+ const removedCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+ const removedCollectionTokenId = await createItemExpectSuccess(alice, removedCollectionId, 'NFT');
+
+ await destroyCollectionExpectSuccess(removedCollectionId);
+ await setVariableMetaDataExpectFailure(alice, removedCollectionId, removedCollectionTokenId, data);
+ });
+ it('fails on removed token', async () => {
+ const removedTokenCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });
+ const removedTokenId = await createItemExpectSuccess(alice, removedTokenCollectionId, 'NFT');
+ await burnItemExpectSuccess(alice, removedTokenCollectionId, removedTokenId);
+
+ await setVariableMetaDataExpectFailure(alice, removedTokenCollectionId, removedTokenId, data);
+ });
+ it('fails on not existing token', async () => {
+ const nonExistingTokenId = validTokenId + 1;
+
+ await setVariableMetaDataExpectFailure(alice, validCollectionId, nonExistingTokenId, data);
+ });
+ it('fails on too long data', async () => {
+ const tooLongData = new Array(4097).fill(0xff);
+
+ await setVariableMetaDataExpectFailure(alice, validCollectionId, validTokenId, tooLongData);
+ });
+ it('fails on fungible token', async () => {
+ const fungibleCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });
+ const fungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');
+
+ await setVariableMetaDataExpectFailure(alice, fungibleCollectionId, fungibleTokenId, data);
+ });
+ it('fails on bad sender', async () => {
+ await setVariableMetaDataExpectFailure(bob, validCollectionId, validTokenId, data);
+ });
+});
tests/src/util/helpers.tsdiffbeforeafterboth253 return unused;253 return unused;254}254}255256export async function findNotExistingCollection(api: ApiPromise): Promise<number> {257 const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;258 const newCollection: number = totalNumber + 1;259 return newCollection;260}255261256function getDestroyResult(events: EventRecord[]): boolean {262function getDestroyResult(events: EventRecord[]): boolean {257 let success: boolean = false;263 let success: boolean = false;378 });384 });379}385}386387export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {388 await usingApi(async (api) => {389 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));390 const events = await submitTransactionAsync(sender, tx);391 const result = getGenericResult(events);392393 expect(result.success).to.be.true;394 });395}396397export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {398 await usingApi(async (api) => {399 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));400 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;401 });402}380403381export interface CreateFungibleData extends Struct {404export interface CreateFungibleData extends Struct {382 readonly value: u128;405 readonly value: u128;