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
17 },17 },
18 "scripts": {18 "scripts": {
19 "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",19 "test": "mocha --timeout 9999999 -r ts-node/register ./**/*.test.ts",
20 "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts"20 "load": "mocha --timeout 9999999 -r ts-node/register ./**/*.load.ts",
21 "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",
22 "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",
23 "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts"
21 },24 },
22 "author": "",25 "author": "",
23 "license": "Apache 2.0",26 "license": "Apache 2.0",
modifiedtests/src/setSchemaVersion.test.tsdiffbeforeafterboth
1// https://unique-network.readthedocs.io/en/latest/jsapi.html#setchainlimits
2import { ApiPromise, Keyring } from '@polkadot/api';
1import { BigNumber } from 'bignumber.js';3import { IKeyringPair } from '@polkadot/types/types';
4import BN from 'bn.js';
2import chai from 'chai';5import chai from 'chai';
3import chaiAsPromised from 'chai-as-promised';6import chaiAsPromised from 'chai-as-promised';
7import privateKey from './substrate/privateKey';
8import promisifySubstrate from './substrate/promisify-substrate';
4import usingApi, { submitTransactionAsync } from './substrate/substrate-api';9import usingApi, { submitTransactionAsync } from './substrate/substrate-api';
10import { ICollectionInterface } from './types';
11import { createCollectionExpectSuccess, destroyCollectionExpectSuccess, getCreateItemResult } from './util/helpers';
512
6chai.use(chaiAsPromised);13chai.use(chaiAsPromised);
7const expect = chai.expect;14const expect = chai.expect;
15
16let alice: IKeyringPair;
17let collectionIdForTesting: number;
18
19/*
201. We create collection.
212. Save just created collection id.
223. Use this id for setSchemaVersion.
23*/
24
25async function getDetailedCollectionInfo(api: ApiPromise, collectionId: number)
26 : Promise<ICollectionInterface | null> {
27 return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;
28}
29
30async function getCollectionCount(api: ApiPromise): Promise<number> {
31 // set global object - collectionsCount
32 return (await api.query.nft.collectionCount() as unknown as BN).toNumber();
33}
34
35function utf8Decoder(name: [Uint8Array]) {
36 const collectionNameArr = Array.prototype.slice.call(name);
37 return String.fromCharCode(...collectionNameArr);
38}
39
40describe('hooks', () => {
41 before(async () => {
42 await usingApi(async (api) => {
43 const keyring = new Keyring({ type: 'sr25519' });
44 alice = keyring.addFromUri('//Alice');
45 });
46 });
47 it('choose or create collection for testing', async () => {
48 await usingApi(async (api: ApiPromise) => {
49 const newCollectionId = await createCollectionExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: 'NFT'});
50 console.log('newCollectionId', newCollectionId);
51 collectionIdForTesting = newCollectionId;
52 });
53 });
54});
855
9describe('setSchemaVersion positive', () => {56describe('setSchemaVersion positive', () => {
57 let tx;
58 before(async () => {
59 await usingApi(async (api) => {
60 const keyring = new Keyring({ type: 'sr25519' });
61 alice = keyring.addFromUri('//Alice');
62 });
63 });
10 it('execute setSchemaVersion with image url and unique json ', async () => {64 it('execute setSchemaVersion with image url and unique ', async () => {
11 await usingApi(async api => {65 await usingApi(async (api: ApiPromise) => {
1266 tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Unique');
67 const events = await submitTransactionAsync(alice, tx);
68 const result = getCreateItemResult(events);
69 const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);
70 // tslint:disable-next-line:no-unused-expression
71 expect(result.success).to.be.true;
72 // tslint:disable-next-line:no-unused-expression
73 expect(collectionInfo).to.be.exist;
74 // tslint:disable-next-line:no-unused-expression
75 expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('Unique');
13 });76 });
14 });77 });
1578
16 it('validate schema version with just entered data', async () => {79 it('validate schema version with just entered data', async () => {
17 await usingApi(async api => {80 await usingApi(async (api: ApiPromise) => {
1881 tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'ImageURL');
82 const events = await submitTransactionAsync(alice, tx);
83 const result = getCreateItemResult(events);
84 const collectionInfo = await getDetailedCollectionInfo(api, collectionIdForTesting);
85 // tslint:disable-next-line:no-unused-expression
86 expect(result.success).to.be.true;
87 // tslint:disable-next-line:no-unused-expression
88 expect(collectionInfo).to.be.exist;
89 // tslint:disable-next-line:no-unused-expression
90 expect(collectionInfo ? collectionInfo.SchemaVersion.toString() : '').to.be.equal('ImageURL');
19 });91 });
20 });92 });
21});93});
2294
23describe('setSchemaVersion negative', () => {95describe('setSchemaVersion negative', () => {
96 let tx;
24 it('execute setSchemaVersion for not exists collection', async () => {97 before(async () => {
25 await usingApi(async api => {98 await usingApi(async (api) => {
2699 const keyring = new Keyring({ type: 'sr25519' });
100 alice = keyring.addFromUri('//Alice');
27 });101 });
28 });102 });
29
30 it('execute setSchemaVersion for deleted collection', async () => {103 it('execute setSchemaVersion for not exists collection', async () => {
31 await usingApi(async api => {104 await usingApi(async (api: ApiPromise) => {
32105 const collectionCount = await getCollectionCount(api);
106 const nonExistedCollectionId = collectionCount + 1;
107 tx = api.tx.nft.setSchemaVersion(nonExistedCollectionId, 'ImageURL');
108 try {
109 await submitTransactionAsync(alice, tx);
110 } catch (e) {
111 // tslint:disable-next-line:no-unused-expression
112 expect(e).to.be.exist;
113 }
33 });114 });
34 });115 });
35116
36 it('execute setSchemaVersion with not correct image url', async () => {117 it('execute setSchemaVersion with not correct schema version', async () => {
37 await usingApi(async api => {118 await usingApi(async (api: ApiPromise) => {
38119 try {
120 tx = api.tx.nft.setSchemaVersion(collectionIdForTesting, 'Test');
121 await submitTransactionAsync(alice, tx);
122 } catch (e) {
123 // tslint:disable-next-line:no-unused-expression
124 expect(e).to.be.exist;
125 }
39 });126 });
40 });127 });
41128
42 it('execute setSchemaVersion with not correct unique', async () => {129 it('execute setSchemaVersion for deleted collection', async () => {
43 await usingApi(async api => {130 await usingApi(async (api: ApiPromise) => {
44131 const collectionCount = await getCollectionCount(api);
132 await destroyCollectionExpectSuccess(collectionCount);
133 try {
134 tx = api.tx.nft.setSchemaVersion(collectionCount, 'ImageURL');
135 await submitTransactionAsync(alice, tx);
136 } catch (e) {
137 // tslint:disable-next-line:no-unused-expression
138 expect(e).to.be.exist;
139 }
45 });140 });
46 });141 });
47});142});
addedtests/src/types.tsdiffbeforeafterboth

no changes

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;