From aa7dc42b90425017298c1602a4aa080c14bcda2f Mon Sep 17 00:00:00 2001 From: kpozdnikin Date: Wed, 30 Dec 2020 13:12:49 +0000 Subject: [PATCH] setSchema test scenarios --- --- 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", --- 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 { + return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface; +} +async function getCollectionCount(api: ApiPromise): Promise { + // 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; + } }); }); }); --- /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 +} --- a/tests/src/util/helpers.ts +++ b/tests/src/util/helpers.ts @@ -47,7 +47,7 @@ return result; } -function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult { +export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult { let success = false; let collectionId: number = 0; events.forEach(({ phase, event: { data, method, section } }) => { @@ -65,7 +65,7 @@ return result; } -function getCreateItemResult(events: EventRecord[]): CreateItemResult { +export function getCreateItemResult(events: EventRecord[]): CreateItemResult { let success = false; let collectionId: number = 0; let itemId: number = 0; -- gitstuff