git.delta.rocks / unique-network / refs/commits / dc53bbca9772

difftreelog

Merge pull request #72 from usetech-llc/feature/NFTPAR-255_setVariableMetadata

usetech-llc2021-01-26parents: #1b4fb71 #01e7dd2.patch.diff
in: master
Add tests for setVariableMetadata

3 files changed

modifiedtests/package.jsondiffbeforeafterboth
before · tests/package.json
1{2  "name": "NftTests",3  "version": "1.0.0",4  "description": "Substrate Nft tests",5  "main": "",6  "devDependencies": {7    "@polkadot/dev": "^0.52.11",8    "@polkadot/ts": "^0.3.41",9    "@types/chai": "^4.2.12",10    "@types/chai-as-promised": "^7.1.3",11    "@types/mocha": "^8.0.3",12    "chai": "^4.2.0",13    "mocha": "^8.1.1",14    "ts-node": "^9.0.0",15    "tslint": "^5.20.1",16    "typescript": "^3.9.7"17  },18  "scripts": {19    "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",20    "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",21    "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",22    "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",23    "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",24    "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",25    "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",26    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",27    "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",28    "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",29    "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",30    "testToggleContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/toggleContractWhiteList.test.ts",31    "testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",32    "testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",33    "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts"34  },35  "author": "",36  "license": "Apache 2.0",37  "homepage": "",38  "dependencies": {39    "@polkadot/api": "^2.3.1",40    "@polkadot/api-contract": "^2.3.1",41    "@polkadot/types": "^2.3.1",42    "@polkadot/util": "^3.4.1",43    "bignumber.js": "^9.0.0",44    "chai-as-promised": "^7.1.1"45  },46  "standard": {47    "globals": [48      "it",49      "assert",50      "beforeEach",51      "afterEach",52      "describe",53      "contract",54      "artifacts"55    ]56  }57}
after · tests/package.json
1{2  "name": "NftTests",3  "version": "1.0.0",4  "description": "Substrate Nft tests",5  "main": "",6  "devDependencies": {7    "@polkadot/dev": "^0.52.11",8    "@polkadot/ts": "^0.3.41",9    "@types/chai": "^4.2.12",10    "@types/chai-as-promised": "^7.1.3",11    "@types/mocha": "^8.0.3",12    "chai": "^4.2.0",13    "mocha": "^8.1.1",14    "ts-node": "^9.0.0",15    "tslint": "^5.20.1",16    "typescript": "^3.9.7"17  },18  "scripts": {19    "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",20    "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",21    "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",22    "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",23    "testSetVariableMetaData": "mocha --timeout 9999999 -r ts-node/register ./**/setVariableMetaData.test.ts",24    "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",25    "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",26    "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",27    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",28    "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",29    "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",30    "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",31    "testToggleContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/toggleContractWhiteList.test.ts",32    "testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",33    "testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",34    "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts"35  },36  "author": "",37  "license": "Apache 2.0",38  "homepage": "",39  "dependencies": {40    "@polkadot/api": "^2.3.1",41    "@polkadot/api-contract": "^2.3.1",42    "@polkadot/types": "^2.3.1",43    "@polkadot/util": "^3.4.1",44    "bignumber.js": "^9.0.0",45    "chai-as-promised": "^7.1.1"46  },47  "standard": {48    "globals": [49      "it",50      "assert",51      "beforeEach",52      "afterEach",53      "describe",54      "contract",55      "artifacts"56    ]57  }58}
addedtests/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);
+  });
+});
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -253,6 +253,12 @@
   return unused;
 }
 
+export async function findNotExistingCollection(api: ApiPromise): Promise<number> {
+  const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;
+  const newCollection: number = totalNumber + 1;
+  return newCollection;
+}
+
 function getDestroyResult(events: EventRecord[]): boolean {
   let success: boolean = false;
   events.forEach(({ phase, event: { data, method, section } }) => {
@@ -378,6 +384,23 @@
   });
 }
 
+export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {
+  await usingApi(async (api) => {
+    const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '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 setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {
+  await usingApi(async (api) => {
+    const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));
+    await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
+  });
+}
+
 export interface CreateFungibleData extends Struct {
   readonly value: u128;
 }