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

difftreelog

setSchema test scenarios

kpozdnikin2020-12-30parent: #aabcd75.patch.diff
in: master

4 files changed

modifiedtests/package.jsondiffbeforeafterboth
--- a/tests/package.json
+++ b/tests/package.json
@@ -17,7 +17,10 @@
   },
   "scripts": {
     "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",
-    "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts"
+    "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",
+    "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",
+    "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",
+    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"
   },
   "author": "",
   "license": "Apache 2.0",
modifiedtests/src/setSchemaVersion.test.tsdiffbeforeafterboth
--- a/tests/src/setSchemaVersion.test.ts
+++ b/tests/src/setSchemaVersion.test.ts
@@ -1,47 +1,142 @@
-import { BigNumber } from 'bignumber.js';
+// https://unique-network.readthedocs.io/en/latest/jsapi.html#setchainlimits
+import { ApiPromise, Keyring } from '@polkadot/api';
+import { IKeyringPair } from '@polkadot/types/types';
+import BN from 'bn.js';
 import chai from 'chai';
 import chaiAsPromised from 'chai-as-promised';
+import privateKey from './substrate/privateKey';
+import promisifySubstrate from './substrate/promisify-substrate';
 import usingApi, { submitTransactionAsync } from './substrate/substrate-api';
+import { ICollectionInterface } from './types';
+import { createCollectionExpectSuccess, destroyCollectionExpectSuccess, getCreateItemResult } from './util/helpers';
 
 chai.use(chaiAsPromised);
 const expect = chai.expect;
 
-describe('setSchemaVersion positive', () => {
-  it('execute setSchemaVersion with image url and unique json ', async () => {
-    await usingApi(async api => {
+let alice: IKeyringPair;
+let collectionIdForTesting: number;
+
+/*
+1. We create collection.
+2. Save just created collection id.
+3. Use this id for setSchemaVersion.
+*/
+
+async function getDetailedCollectionInfo(api: ApiPromise, collectionId: number)
+  : Promise<ICollectionInterface | null> {
+  return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;
+}
 
+async function getCollectionCount(api: ApiPromise): Promise<number> {
+  // set global object - collectionsCount
+  return (await api.query.nft.collectionCount() as unknown as BN).toNumber();
+}
+
+function utf8Decoder(name: [Uint8Array]) {
+  const collectionNameArr = Array.prototype.slice.call(name);
+  return String.fromCharCode(...collectionNameArr);
+}
+
+describe('hooks', () => {
+  before(async () => {
+    await usingApi(async (api) => {
+      const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri('//Alice');
     });
   });
+  it('choose or create collection for testing', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const newCollectionId = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});
+      console.log('newCollectionId', newCollectionId);
+      collectionIdForTesting = newCollectionId;
+    });
+  });
+});
 
-  it('validate schema version with just entered data', async () => {
-    await usingApi(async api => {
+describe('setSchemaVersion positive', () => {
+  let tx;
+  before(async () => {
+    await usingApi(async (api) => {
+      const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri('//Alice');
+    });
+  });
+  it('execute setSchemaVersion with image url and unique ', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Unique');
+      const events = await submitTransactionAsync(alice, tx);
+      const result = getCreateItemResult(events);
+      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);
+      // tslint:disable-next-line:no-unused-expression
+      expect(result.success).to.be.true;
+      // tslint:disable-next-line:no-unused-expression
+      expect(collectionInfo).to.be.exist;
+      // tslint:disable-next-line:no-unused-expression
+      expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('Unique');
+    });
+  });
 
+  it('validate schema version with just entered data', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');
+      const events = await submitTransactionAsync(alice, tx);
+      const result = getCreateItemResult(events);
+      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);
+      // tslint:disable-next-line:no-unused-expression
+      expect(result.success).to.be.true;
+      // tslint:disable-next-line:no-unused-expression
+      expect(collectionInfo).to.be.exist;
+      // tslint:disable-next-line:no-unused-expression
+      expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('ImageURL');
     });
   });
 });
 
 describe('setSchemaVersion negative', () => {
-  it('execute setSchemaVersion for not exists collection', async () => {
-    await usingApi(async api => {
-
+  let tx;
+  before(async () => {
+    await usingApi(async (api) => {
+      const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri('//Alice');
     });
   });
-
-  it('execute setSchemaVersion for deleted collection', async () => {
-    await usingApi(async api => {
-
+  it('execute setSchemaVersion for not exists collection', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const collectionCount = await getCollectionCount(api);
+      const nonExistedCollectionId = collectionCount + 1;
+      tx = api.tx.nft.setSchemaVersion(nonExistedCollectionId, 'ImageURL');
+      try {
+        await submitTransactionAsync(alice, tx);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
     });
   });
 
-  it('execute setSchemaVersion with not correct image url', async () => {
-    await usingApi(async api => {
-
+  it('execute setSchemaVersion with not correct schema version', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      try {
+        tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Test');
+        await submitTransactionAsync(alice, tx);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
     });
   });
 
-  it('execute setSchemaVersion with not correct unique', async () => {
-    await usingApi(async api => {
-
+  it('execute setSchemaVersion for deleted collection', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const collectionCount = await getCollectionCount(api);
+      await destroyCollectionExpectSuccess(collectionCount);
+      try {
+        tx = api.tx.nft.setSchemaVersion(collectionCount, 'ImageURL');
+        await submitTransactionAsync(alice, tx);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
     });
   });
 });
addedtests/src/types.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/types.ts
@@ -0,0 +1,23 @@
+import BN from 'bn.js';
+
+export interface ICollectionInterface {
+  Access: string;
+  id: number;
+  DecimalPoints: BN;
+  // constOnChainSchema
+  Description: [BN, BN]; // utf16
+  isReFungible: boolean;
+  MintMode: boolean;
+  Mode: {
+    Nft: null;
+  };
+  Name: [BN, BN]; // utf16
+  OffchainSchema: [Uint8Array];
+  SchemaVersion: string;
+  Owner: [Uint8Array];
+  // prefix
+  // sponsor
+  // tokenPrefix
+  // unconfirmedSponsor
+  // variableOnChainSchema
+}
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
47 return result;47 return result;
48}48}
4949
50function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {50export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {
51 let success = false;51 let success = false;
52 let collectionId: number = 0;52 let collectionId: number = 0;
53 events.forEach(({ phase, event: { data, method, section } }) => {53 events.forEach(({ phase, event: { data, method, section } }) => {
65 return result;65 return result;
66}66}
6767
68function getCreateItemResult(events: EventRecord[]): CreateItemResult {68export function getCreateItemResult(events: EventRecord[]): CreateItemResult {
69 let success = false;69 let success = false;
70 let collectionId: number = 0;70 let collectionId: number = 0;
71 let itemId: number = 0;71 let itemId: number = 0;