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

difftreelog

set limits test scenarios

kpozdnikin2020-12-31parent: #49640f3.patch.diff
in: master

5 files changed

modifiedtests/package.jsondiffbeforeafterboth
--- a/tests/package.json
+++ b/tests/package.json
@@ -19,6 +19,7 @@
     "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",
     "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",
     "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",
+    "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",
     "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",
     "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"
   },
addedtests/src/setCollectionLimits.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/setCollectionLimits.test.ts
@@ -0,0 +1,145 @@
+// https://unique-network.readthedocs.io/en/latest/jsapi.html#setchainlimits
+import { ApiPromise, Keyring } from '@polkadot/api';
+import { IKeyringPair } from '@polkadot/types/types';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import usingApi, { submitTransactionAsync } from './substrate/substrate-api';
+import { ICollectionInterface } from './types';
+import {
+  createCollectionExpectSuccess, getCreatedCollectionCount,
+  getCreateItemResult,
+  getDetailedCollectionInfo,
+} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+let alice: IKeyringPair;
+let bob: IKeyringPair;
+let collectionIdForTesting: number;
+
+const accountTokenOwnershipLimit = 0;
+const sponsoredDataSize = 0;
+const sponsoredMintSize = 0;
+const tokenLimit = 0;
+
+describe('hooks', () => {
+  before(async () => {
+    await usingApi(async () => {
+      const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri('//Alice');
+    });
+  });
+  it('choose or create collection for testing', async () => {
+    await usingApi(async () => {
+      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});
+      console.log('collectionIdForTesting', collectionIdForTesting);
+    });
+  });
+});
+
+describe('setCollectionLimits positive', () => {
+  let tx;
+  before(async () => {
+    await usingApi(async () => {
+      const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri('//Alice');
+    });
+  });
+  it('execute setCollectionLimits with predefined params ', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      tx = api.tx.nft.setCollectionLimits(
+        collectionIdForTesting,
+        {
+          accountTokenOwnershipLimit,
+          sponsoredDataSize,
+          sponsoredMintSize,
+          tokenLimit,
+        },
+      );
+      const events = await submitTransactionAsync(alice, tx);
+      const result = getCreateItemResult(events);
+      // tslint:disable-next-line:no-unused-expression
+      expect(result.success).to.be.true;
+    });
+  });
+  it('get collection limits defined in previous test', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;
+      expect(collectionInfo.Limits.AccountTokenOwnershipLimit.toNumber()).to.be.equal(accountTokenOwnershipLimit);
+      expect(collectionInfo.Limits.SponsoredMintSize.toNumber()).to.be.equal(sponsoredMintSize);
+      expect(collectionInfo.Limits.TokenLimit.toNumber()).to.be.equal(tokenLimit);
+      expect(collectionInfo.Limits.SponsorTimeout.toNumber()).to.be.equal(sponsoredDataSize);
+    });
+  });
+});
+
+describe('setCollectionLimits negative', () => {
+  let tx;
+  before(async () => {
+    await usingApi(async () => {
+      const keyring = new Keyring({ type: 'sr25519' });
+      alice = keyring.addFromUri('//Alice');
+      bob = keyring.addFromUri('//Bob');
+    });
+  });
+  it('execute setCollectionLimits for not exists collection', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const collectionCount = await getCreatedCollectionCount(api);
+      const nonExistedCollectionId = collectionCount + 1;
+      tx = api.tx.nft.setCollectionLimits(
+        nonExistedCollectionId,
+        {
+          accountTokenOwnershipLimit,
+          sponsoredDataSize,
+          sponsoredMintSize,
+          tokenLimit,
+        },
+      );
+      try {
+        await submitTransactionAsync(alice, tx);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
+    });
+  });
+  it('execute setCollectionLimits from user who is not owner of this collection', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      tx = api.tx.nft.setCollectionLimits(
+        collectionIdForTesting,
+        {
+          accountTokenOwnershipLimit,
+          sponsoredDataSize,
+          sponsoredMintSize,
+          tokenLimit,
+        },
+      );
+      try {
+        await submitTransactionAsync(bob, tx);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
+    });
+  });
+  it('execute setCollectionLimits with incorrect limits', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      tx = api.tx.nft.setCollectionLimits(
+        collectionIdForTesting,
+        {
+          accountTokenOwnershipLimit: 'awdawd',
+          sponsorTransferTimeout: 'awd',
+          sponsoredDataSize: '12312312312312312',
+          tokenLimit: '-100',
+        },
+      );
+      try {
+        await submitTransactionAsync(alice, tx);
+      } catch (e) {
+        // tslint:disable-next-line:no-unused-expression
+        expect(e).to.be.exist;
+      }
+    });
+  });
+});
modifiedtests/src/setSchemaVersion.test.tsdiffbeforeafterboth
before · tests/src/setSchemaVersion.test.ts
1// https://unique-network.readthedocs.io/en/latest/jsapi.html#setchainlimits2import { ApiPromise, Keyring } from '@polkadot/api';3import { IKeyringPair } from '@polkadot/types/types';4import BN from 'bn.js';5import chai from 'chai';6import chaiAsPromised from 'chai-as-promised';7import usingApi, { submitTransactionAsync } from './substrate/substrate-api';8import { ICollectionInterface } from './types';9import { createCollectionExpectSuccess, destroyCollectionExpectSuccess, getCreateItemResult } from './util/helpers';1011chai.use(chaiAsPromised);12const expect = chai.expect;1314let alice: IKeyringPair;15let collectionIdForTesting: number;1617/*181. We create collection.192. Save just created collection id.203. Use this id for setSchemaVersion.21*/2223const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)24  : Promise<ICollectionInterface | null> => {25  return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;26};2728const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {29  // set global object - collectionsCount30  return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();31};3233describe('hooks', () => {34  before(async () => {35    await usingApi(async () => {36      const keyring = new Keyring({ type: 'sr25519' });37      alice = keyring.addFromUri('//Alice');38    });39  });40  it('choose or create collection for testing', async () => {41    await usingApi(async () => {42      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});43    });44  });45});4647describe('setSchemaVersion positive', () => {48  let tx;49  before(async () => {50    await usingApi(async () => {51      const keyring = new Keyring({ type: 'sr25519' });52      alice = keyring.addFromUri('//Alice');53    });54  });55  it('execute setSchemaVersion with image url and unique ', async () => {56    await usingApi(async (api: ApiPromise) => {57      tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Unique');58      const events = await submitTransactionAsync(alice, tx);59      const result = getCreateItemResult(events);60      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);61      // tslint:disable-next-line:no-unused-expression62      expect(result.success).to.be.true;63      // tslint:disable-next-line:no-unused-expression64      expect(collectionInfo).to.be.exist;65      // tslint:disable-next-line:no-unused-expression66      expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('Unique');67    });68  });6970  it('validate schema version with just entered data', async () => {71    await usingApi(async (api: ApiPromise) => {72      tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');73      const events = await submitTransactionAsync(alice, tx);74      const result = getCreateItemResult(events);75      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);76      // tslint:disable-next-line:no-unused-expression77      expect(result.success).to.be.true;78      // tslint:disable-next-line:no-unused-expression79      expect(collectionInfo).to.be.exist;80      // tslint:disable-next-line:no-unused-expression81      expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('ImageURL');82    });83  });84});8586describe('setSchemaVersion negative', () => {87  let tx;88  before(async () => {89    await usingApi(async () => {90      const keyring = new Keyring({ type: 'sr25519' });91      alice = keyring.addFromUri('//Alice');92    });93  });94  it('execute setSchemaVersion for not exists collection', async () => {95    await usingApi(async (api: ApiPromise) => {96      const collectionCount = await getCreatedCollectionCount(api);97      const nonExistedCollectionId = collectionCount + 1;98      tx = api.tx.nft.setSchemaVersion(nonExistedCollectionId, 'ImageURL');99      try {100        await submitTransactionAsync(alice, tx);101      } catch (e) {102        // tslint:disable-next-line:no-unused-expression103        expect(e).to.be.exist;104      }105    });106  });107108  it('execute setSchemaVersion with not correct schema version', async () => {109    await usingApi(async (api: ApiPromise) => {110      try {111        tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Test');112        await submitTransactionAsync(alice, tx);113      } catch (e) {114        // tslint:disable-next-line:no-unused-expression115        expect(e).to.be.exist;116      }117    });118  });119120  it('execute setSchemaVersion for deleted collection', async () => {121    await usingApi(async (api: ApiPromise) => {122      await destroyCollectionExpectSuccess(collectionIdForTesting);123      try {124        tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');125        await submitTransactionAsync(alice, tx);126      } catch (e) {127        // tslint:disable-next-line:no-unused-expression128        expect(e).to.be.exist;129      }130    });131  });132});
after · tests/src/setSchemaVersion.test.ts
1// https://unique-network.readthedocs.io/en/latest/jsapi.html#setschemaversion2import { ApiPromise, Keyring } from '@polkadot/api';3import { IKeyringPair } from '@polkadot/types/types';4import BN from 'bn.js';5import chai from 'chai';6import chaiAsPromised from 'chai-as-promised';7import usingApi, { submitTransactionAsync } from './substrate/substrate-api';8import { ICollectionInterface } from './types';9import {10  createCollectionExpectSuccess,11  destroyCollectionExpectSuccess,12  getCreatedCollectionCount,13  getCreateItemResult,14  getDetailedCollectionInfo,15} from './util/helpers';1617chai.use(chaiAsPromised);18const expect = chai.expect;1920let alice: IKeyringPair;21let collectionIdForTesting: number;2223/*241. We create collection.252. Save just created collection id.263. Use this id for setSchemaVersion.27*/2829describe('hooks', () => {30  before(async () => {31    await usingApi(async () => {32      const keyring = new Keyring({ type: 'sr25519' });33      alice = keyring.addFromUri('//Alice');34    });35  });36  it('choose or create collection for testing', async () => {37    await usingApi(async () => {38      collectionIdForTesting = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});39    });40  });41});4243describe('setSchemaVersion positive', () => {44  let tx;45  before(async () => {46    await usingApi(async () => {47      const keyring = new Keyring({ type: 'sr25519' });48      alice = keyring.addFromUri('//Alice');49    });50  });51  it('execute setSchemaVersion with image url and unique ', async () => {52    await usingApi(async (api: ApiPromise) => {53      tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Unique');54      const events = await submitTransactionAsync(alice, tx);55      const result = getCreateItemResult(events);56      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;57      // tslint:disable-next-line:no-unused-expression58      expect(result.success).to.be.true;59      // tslint:disable-next-line:no-unused-expression60      expect(collectionInfo).to.be.exist;61      // tslint:disable-next-line:no-unused-expression62      expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('Unique');63    });64  });6566  it('validate schema version with just entered data', async () => {67    await usingApi(async (api: ApiPromise) => {68      tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');69      const events = await submitTransactionAsync(alice, tx);70      const result = getCreateItemResult(events);71      const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting) as ICollectionInterface;72      // tslint:disable-next-line:no-unused-expression73      expect(result.success).to.be.true;74      // tslint:disable-next-line:no-unused-expression75      expect(collectionInfo).to.be.exist;76      // tslint:disable-next-line:no-unused-expression77      expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('ImageURL');78    });79  });80});8182describe('setSchemaVersion negative', () => {83  let tx;84  before(async () => {85    await usingApi(async () => {86      const keyring = new Keyring({ type: 'sr25519' });87      alice = keyring.addFromUri('//Alice');88    });89  });90  it('execute setSchemaVersion for not exists collection', async () => {91    await usingApi(async (api: ApiPromise) => {92      const collectionCount = await getCreatedCollectionCount(api);93      const nonExistedCollectionId = collectionCount + 1;94      tx = api.tx.nft.setSchemaVersion(nonExistedCollectionId, 'ImageURL');95      try {96        await submitTransactionAsync(alice, tx);97      } catch (e) {98        // tslint:disable-next-line:no-unused-expression99        expect(e).to.be.exist;100      }101    });102  });103104  it('execute setSchemaVersion with not correct schema version', async () => {105    await usingApi(async (api: ApiPromise) => {106      try {107        tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Test');108        await submitTransactionAsync(alice, tx);109      } catch (e) {110        // tslint:disable-next-line:no-unused-expression111        expect(e).to.be.exist;112      }113    });114  });115116  it('execute setSchemaVersion for deleted collection', async () => {117    await usingApi(async (api: ApiPromise) => {118      await destroyCollectionExpectSuccess(collectionIdForTesting);119      try {120        tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');121        await submitTransactionAsync(alice, tx);122      } catch (e) {123        // tslint:disable-next-line:no-unused-expression124        expect(e).to.be.exist;125      }126    });127  });128});
modifiedtests/src/types.tsdiffbeforeafterboth
--- a/tests/src/types.ts
+++ b/tests/src/types.ts
@@ -7,14 +7,20 @@
   // constOnChainSchema
   Description: [BN, BN]; // utf16
   isReFungible: boolean;
+  Limits: {
+    AccountTokenOwnershipLimit: BN;
+    SponsoredMintSize: BN;
+    TokenLimit: BN;
+    SponsorTimeout: BN;
+  };
   MintMode: boolean;
   Mode: {
     Nft: null;
   };
   Name: [BN, BN]; // utf16
   OffchainSchema: [Uint8Array];
+  Owner: [Uint8Array];
   SchemaVersion: string;
-  Owner: [Uint8Array];
   // prefix
   // sponsor
   // tokenPrefix
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -10,11 +10,13 @@
 import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from "../substrate/substrate-api";
 import privateKey from '../substrate/privateKey';
 import { alicesPublicKey, nullPublicKey } from "../accounts";
-import { strToUTF16, utf16ToStr, hexToStr } from '../util/util';
-import { IKeyringPair } from "@polkadot/types/types";
+import { strToUTF16, utf16ToStr, hexToStr } from './util';
+import { IKeyringPair } from '@polkadot/types/types';
 import { BigNumber } from 'bignumber.js';
 import { Struct, Enum } from '@polkadot/types/codec';
 import { u128 } from '@polkadot/types/primitive';
+import { ICollectionInterface } from '../types';
+import BN from "bn.js";
 
 chai.use(chaiAsPromised);
 const expect = chai.expect;
@@ -399,3 +401,12 @@
   });
 }
 
+export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)
+  : Promise<ICollectionInterface | null> => {
+  return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;
+};
+
+export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {
+  // set global object - collectionsCount
+  return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();
+};