git.delta.rocks / unique-network / refs/commits / 39dc916474d4

difftreelog

tests: fix tests for optional collections

Yaroslav Bolyukin2021-03-22parent: #6a8b360.patch.diff
in: master

10 files changed

modifiedtests/package.jsondiffbeforeafterboth
--- a/tests/package.json
+++ b/tests/package.json
@@ -32,6 +32,7 @@
     "testRemoveFromWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/removeFromWhiteList.test.ts",
     "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",
     "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",
+    "testContracts": "mocha --timeout 9999999 -r ts-node/register ./**/contracts.test.ts",
     "testCreateItem": "mocha --timeout 9999999 -r ts-node/register ./**/createItem.test.ts",
     "testCreateMultipleItems": "mocha --timeout 9999999 -r ts-node/register ./**/createMultipleItems.test.ts",
     "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",
@@ -44,6 +45,7 @@
     "testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.test.ts",
     "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
     "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",
+    "testRemoveFromContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/removeFromContractWhiteList.test.ts",
     "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",
     "testSetOffchainSchema": "mocha --timeout 9999999 -r ts-node/register ./**/setOffchainSchema.test.ts",
     "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts",
modifiedtests/src/approve.test.tsdiffbeforeafterboth
before · tests/src/approve.test.ts
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//5import { IKeyringPair } from '@polkadot/types/types';6import { ApiPromise } from '@polkadot/api';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import privateKey from './substrate/privateKey';10import { default as usingApi } from './substrate/substrate-api';11import {12  approveExpectFail,13  approveExpectSuccess,14  createCollectionExpectSuccess,15  createItemExpectSuccess,16  destroyCollectionExpectSuccess,17  setCollectionLimitsExpectSuccess,18  transferExpectSuccess,19} from './util/helpers';2021chai.use(chaiAsPromised);2223describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {24  let Alice: IKeyringPair;25  let Bob: IKeyringPair;26  let Charlie: IKeyringPair;2728  before(async () => {29    await usingApi(async () => {30      Alice = privateKey('//Alice');31      Bob = privateKey('//Bob');32      Charlie = privateKey('//Charlie');33    });34  });3536  it('Execute the extrinsic and check approvedList', async () => {37    const nftCollectionId = await createCollectionExpectSuccess();38    // nft39    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');40    await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);41    // fungible42    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});43    const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');44    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);45    // reFungible46    const reFungibleCollectionId =47      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});48    const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');49    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);50  });5152  it('Remove approval by using 0 amount', async () => {53    const nftCollectionId = await createCollectionExpectSuccess();54    // nft55    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');56    await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);57    await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);58    // fungible59    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});60    const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');61    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);62    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);63    // reFungible64    const reFungibleCollectionId =65      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});66    const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');67    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);68    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);69  });7071  it('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async () => {72    const collectionId = await createCollectionExpectSuccess();73    const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Bob.address);7475    await approveExpectSuccess(collectionId, itemId, Alice, Charlie);76  });77});7879describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {80  let Alice: IKeyringPair;81  let Bob: IKeyringPair;82  let Charlie: IKeyringPair;8384  before(async () => {85    await usingApi(async (api) => {86      Alice = privateKey('//Alice');87      Bob = privateKey('//Bob');88      Charlie = privateKey('//Charlie');89    });90  });9192  it('Approve for a collection that does not exist', async () => {93    await usingApi(async (api: ApiPromise) => {94      // nft95      const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;96      await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);97      // fungible98      const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;99      await approveExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob);100      // reFungible101      const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;102      await approveExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob);103    });104  });105106  it('Approve for a collection that was destroyed', async () => {107    // nft108    const nftCollectionId = await createCollectionExpectSuccess();109    await destroyCollectionExpectSuccess(nftCollectionId);110    await approveExpectFail(nftCollectionId, 1, Alice, Bob);111    // fungible112    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});113    await destroyCollectionExpectSuccess(fungibleCollectionId);114    await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);115    // reFungible116    const reFungibleCollectionId =117      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});118    await destroyCollectionExpectSuccess(reFungibleCollectionId);119    await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);120  });121122  it('Approve transfer of a token that does not exist', async () => {123    // nft124    const nftCollectionId = await createCollectionExpectSuccess();125    await approveExpectFail(nftCollectionId, 2, Alice, Bob);126    // fungible127    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});128    await approveExpectFail(fungibleCollectionId, 2, Alice, Bob);129    // reFungible130    const reFungibleCollectionId =131      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});132    await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);133  });134135  it('Approve using the address that does not own the approved token', async () => {136    const nftCollectionId = await createCollectionExpectSuccess();137    // nft138    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');139    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);140    // fungible141    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});142    const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');143    await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice);144    // reFungible145    const reFungibleCollectionId =146      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});147    const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');148    await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);149  });150151  it('should fail if approved more NFTs than owned', async () => {152    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });153    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');154    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1, 'NFT');155    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice);156    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);157  });158159  it('should fail if approved more ReFungibles than owned', async () => {160    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'ReFungible' } });161    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'ReFungible');162    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 100, 'ReFungible');163    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 100);164    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);165  });166167  it('should fail if approved more Fungibles than owned', async () => {168    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });169    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'Fungible');170    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 10, 'Fungible');171    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 10);172    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);173  });174175  it('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {176    const collectionId = await createCollectionExpectSuccess();177    const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Bob.address);178    await setCollectionLimitsExpectSuccess(Alice, collectionId, { OwnerCanTransfer: false });179180    await approveExpectFail(collectionId, itemId, Alice, Charlie);181  });182});
modifiedtests/src/burnItem.test.tsdiffbeforeafterboth
--- a/tests/src/burnItem.test.ts
+++ b/tests/src/burnItem.test.ts
@@ -46,8 +46,7 @@
       // tslint:disable-next-line:no-unused-expression
       expect(result.success).to.be.true;
       // tslint:disable-next-line:no-unused-expression
-      expect(item).to.be.not.null;
-      expect(item.Owner).to.be.equal(nullPublicKey);
+      expect(item).to.be.null;
     });
 
   });
@@ -88,8 +87,7 @@
       
       // What to expect
       expect(result.success).to.be.true;
-      expect(balance).to.be.not.null;
-      expect(balance.Owner.length).to.be.equal(0);
+      expect(balance).to.be.null;
     });
 
   });
modifiedtests/src/contracts.test.tsdiffbeforeafterboth
--- a/tests/src/contracts.test.ts
+++ b/tests/src/contracts.test.ts
@@ -63,13 +63,13 @@
       const collectionId = await createCollectionExpectSuccess();
       const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');
       const [contract, deployer] = await deployTransferContract(api);
-      const tokenBefore: any = await api.query.nft.nftItemList(collectionId, tokenId);
+      const tokenBefore: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();
       
       // Transfer
       const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);
       const events = await submitTransactionAsync(alice, transferTx);
       const result = getGenericResult(events);
-      const tokenAfter: any = await api.query.nft.nftItemList(collectionId, tokenId);
+      const tokenAfter: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();
 
       // tslint:disable-next-line:no-unused-expression
       expect(result.success).to.be.true;
modifiedtests/src/createMultipleItems.test.tsdiffbeforeafterboth
--- a/tests/src/createMultipleItems.test.ts
+++ b/tests/src/createMultipleItems.test.ts
@@ -38,9 +38,9 @@
       await submitTransactionAsync(Alice, createMultipleItemsTx);
       const itemsListIndexAfter = await api.query.nft.itemListIndex(collectionId) as unknown as BN;
       expect(itemsListIndexAfter.toNumber()).to.be.equal(3);
-      const token1Data = await api.query.nft.nftItemList(collectionId, 1) as unknown as ITokenDataType;
-      const token2Data = await api.query.nft.nftItemList(collectionId, 2) as unknown as ITokenDataType;
-      const token3Data = await api.query.nft.nftItemList(collectionId, 3) as unknown as ITokenDataType;
+      const token1Data = (await api.query.nft.nftItemList(collectionId, 1)).toJSON() as unknown as ITokenDataType;
+      const token2Data = (await api.query.nft.nftItemList(collectionId, 2)).toJSON() as unknown as ITokenDataType;
+      const token3Data = (await api.query.nft.nftItemList(collectionId, 3)).toJSON() as unknown as ITokenDataType;
 
       expect(token1Data.Owner.toString()).to.be.equal(Alice.address);
       expect(token2Data.Owner.toString()).to.be.equal(Alice.address);
@@ -72,9 +72,9 @@
       await submitTransactionAsync(Alice, createMultipleItemsTx);
       const itemsListIndexAfter = await api.query.nft.itemListIndex(collectionId) as unknown as BN;
       expect(itemsListIndexAfter.toNumber()).to.be.equal(3);
-      const token1Data = await api.query.nft.reFungibleItemList(collectionId, 1) as unknown as IReFungibleTokenDataType;
-      const token2Data = await api.query.nft.reFungibleItemList(collectionId, 2) as unknown as IReFungibleTokenDataType;
-      const token3Data = await api.query.nft.reFungibleItemList(collectionId, 3) as unknown as IReFungibleTokenDataType;
+      const token1Data = (await api.query.nft.reFungibleItemList(collectionId, 1) as any).unwrap() as unknown as IReFungibleTokenDataType;
+      const token2Data = (await api.query.nft.reFungibleItemList(collectionId, 2) as any).unwrap() as unknown as IReFungibleTokenDataType;
+      const token3Data = (await api.query.nft.reFungibleItemList(collectionId, 3) as any).unwrap() as unknown as IReFungibleTokenDataType;
 
       expect(token1Data.Owner[0].Owner.toString()).to.be.equal(Alice.address);
       expect(token1Data.Owner[0].Fraction.toNumber()).to.be.equal(1);
modifiedtests/src/removeCollectionAdmin.test.tsdiffbeforeafterboth
--- a/tests/src/removeCollectionAdmin.test.ts
+++ b/tests/src/removeCollectionAdmin.test.ts
@@ -36,6 +36,19 @@
       expect(adminListAfterRemoveAdmin).not.to.be.contains(Bob.address);
     });
   });
+
+  it('Remove admin from collection that has no admins', async () => {
+    await usingApi(async (api: ApiPromise) => {
+      const Alice = privateKey('//Alice');
+      const collectionId = await createCollectionExpectSuccess();
+
+      const adminListBeforeAddAdmin: any = (await api.query.nft.adminList(collectionId));
+      expect(adminListBeforeAddAdmin).to.have.lengthOf(0);
+
+      const tx = api.tx.nft.removeCollectionAdmin(collectionId, Alice.address);
+      await submitTransactionAsync(Alice, tx);
+    });
+  });
 });
 
 describe('Negative Integration Test removeCollectionAdmin(collection_id, account_id):', () => {
@@ -68,19 +81,6 @@
 
       // Verifying that nothing bad happened (network is live, new collections can be created, etc.)
       await createCollectionExpectSuccess();
-    });
-  });
-
-  it('Remove admin from collection that has no admins', async () => {
-    await usingApi(async (api: ApiPromise) => {
-      const Alice = privateKey('//Alice');
-      const collectionId = await createCollectionExpectSuccess();
-
-      const adminListBeforeAddAdmin: any = (await api.query.nft.adminList(collectionId));
-      expect(adminListBeforeAddAdmin).to.have.lengthOf(0);
-
-      const tx = api.tx.nft.removeCollectionAdmin(collectionId, Alice.address);
-      await expect(submitTransactionExpectFailAsync(Alice, tx)).to.be.rejected;
     });
   });
 });
modifiedtests/src/removeFromContractWhiteList.test.tsdiffbeforeafterboth
--- a/tests/src/removeFromContractWhiteList.test.ts
+++ b/tests/src/removeFromContractWhiteList.test.ts
@@ -13,8 +13,10 @@
 describe('Integration Test removeFromContractWhiteList', () => {
     let bob: IKeyringPair;
 
-    before(() => {
-        bob = privateKey('//Bob');
+    before(async () => {
+        await usingApi(async () => {
+            bob = privateKey('//Bob');
+        });
     });
 
     it('user is no longer whitelisted after removal', async () => {
@@ -56,9 +58,11 @@
     let alice: IKeyringPair;
     let bob: IKeyringPair;
 
-    before(() => {
-        alice = privateKey('//Alice');
-        bob = privateKey('//Bob');
+    before(async () => {
+        await usingApi(async () => {
+            alice = privateKey('//Alice');
+            bob = privateKey('//Bob');
+        });
     });
 
     it('fails when called with non-contract address', async () => {
modifiedtests/src/setVariableMetaData.test.tsdiffbeforeafterboth
--- a/tests/src/setVariableMetaData.test.ts
+++ b/tests/src/setVariableMetaData.test.ts
@@ -41,7 +41,7 @@
 
   it('verify data was set', async () => {
     await usingApi(async api => {
-      const item: any = await api.query.nft.nftItemList(collectionId, tokenId);
+      const item: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();
 
       expect(Array.from(item.VariableData)).to.deep.equal(Array.from(data));
     });
modifiedtests/src/transferFrom.test.tsdiffbeforeafterboth
--- a/tests/src/transferFrom.test.ts
+++ b/tests/src/transferFrom.test.ts
@@ -237,7 +237,7 @@
       const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
       const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');
       await burnItemExpectSuccess(Alice, fungibleCollectionId, 1, 10);
-      await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
+      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
       await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice, Charlie, 1);
           
     });
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -599,8 +599,7 @@
     // tslint:disable-next-line:no-unused-expression
     expect(result.success).to.be.true;
     // tslint:disable-next-line:no-unused-expression
-    expect(item).to.be.not.null;
-    expect(item.Owner).to.be.equal(nullPublicKey);
+    expect(item).to.be.null;
   });
 }
 
@@ -641,16 +640,16 @@
     // tslint:disable-next-line:no-unused-expression
     expect(result.success).to.be.true;
     if (type === 'NFT') {
-      const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;
+      const nftItemData = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap() as ITokenDataType;
       expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);
     }
     if (type === 'Fungible') {
-      const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;
+      const balanceAfter = (await api.query.nft.fungibleItemList(collectionId, accountTo.address) as any).Value as unknown as BN;
       expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());
     }
     if (type === 'ReFungible') {
       const nftItemData =
-        await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;
+        (await api.query.nft.reFungibleItemList(collectionId, tokenId) as any).unwrap() as IReFungibleTokenDataType;
       expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);
       expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);
     }
@@ -697,18 +696,18 @@
     expect(result.recipient).to.be.equal(recipient.address);
     expect(result.value.toString()).to.be.equal(value.toString());
     if (type === 'NFT') {
-      const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;
+      const nftItemData = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON() as unknown as ITokenDataType;
       expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);
     }
     if (type === 'Fungible') {
-      const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;
+      const balanceAfter = (await api.query.nft.fungibleItemList(collectionId, recipient.address) as any).Value as unknown as BN;
       expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());
     }
     if (type === 'ReFungible') {
       const nftItemData =
-        await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;
+        (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON() as unknown as IReFungibleTokenDataType;
       expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);
-      expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);
+      expect(nftItemData.Owner[0].Fraction.toString()).to.be.equal(value.toString());
     }
   });
 }