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
--- a/tests/src/approve.test.ts
+++ b/tests/src/approve.test.ts
@@ -123,9 +123,6 @@
     // nft
     const nftCollectionId = await createCollectionExpectSuccess();
     await approveExpectFail(nftCollectionId, 2, Alice, Bob);
-    // fungible
-    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});
-    await approveExpectFail(fungibleCollectionId, 2, Alice, Bob);
     // reFungible
     const reFungibleCollectionId =
       await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
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
before · tests/src/contracts.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//56import chai from "chai";7import chaiAsPromised from 'chai-as-promised';8import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";9import fs from "fs";10import { Abi, ContractPromise as Contract } from "@polkadot/api-contract";11import privateKey from "./substrate/privateKey";12import {13  deployFlipper,14  getFlipValue,15  deployTransferContract,16} from "./util/contracthelpers";1718import {19  createCollectionExpectSuccess,20  createItemExpectSuccess,21  getGenericResult22} from "./util/helpers";232425chai.use(chaiAsPromised);26const expect = chai.expect;2728const value = 0;29const gasLimit = 3000n * 1000000n;30const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';3132describe('Contracts', () => {33  it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {34    await usingApi(async api => {35      const [contract, deployer] = await deployFlipper(api);36      const initialGetResponse = await getFlipValue(contract, deployer);3738      const bob = privateKey("//Bob");39      const flip = contract.tx.flip(value, gasLimit);40      await submitTransactionAsync(bob, flip);4142      const afterFlipGetResponse = await getFlipValue(contract, deployer);43      expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');44    });45  });4647  it('Can initialize contract instance', async () => {48    await usingApi(async (api) => {49      const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));50      const abi = new Abi(metadata);51      const newContractInstance = new Contract(api, abi, marketContractAddress);52      expect(newContractInstance).to.have.property('address');53      expect(newContractInstance.address.toString()).to.equal(marketContractAddress);54    });55  });5657  it('Can transfer NFT using smart contract.', async () => {58    await usingApi(async api => {59      const alice = privateKey("//Alice");60      const bob = privateKey("//Bob");6162      // Prep work63      const collectionId = await createCollectionExpectSuccess();64      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');65      const [contract, deployer] = await deployTransferContract(api);66      const tokenBefore: any = await api.query.nft.nftItemList(collectionId, tokenId);67      68      // Transfer69      const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);70      const events = await submitTransactionAsync(alice, transferTx);71      const result = getGenericResult(events);72      const tokenAfter: any = await api.query.nft.nftItemList(collectionId, tokenId);7374      // tslint:disable-next-line:no-unused-expression75      expect(result.success).to.be.true;76      expect(tokenBefore.Owner.toString()).to.be.equal(alice.address);77      expect(tokenAfter.Owner.toString()).to.be.equal(bob.address);78    });79  });80});
after · tests/src/contracts.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//56import chai from "chai";7import chaiAsPromised from 'chai-as-promised';8import usingApi, { submitTransactionAsync, submitTransactionExpectFailAsync } from "./substrate/substrate-api";9import fs from "fs";10import { Abi, ContractPromise as Contract } from "@polkadot/api-contract";11import privateKey from "./substrate/privateKey";12import {13  deployFlipper,14  getFlipValue,15  deployTransferContract,16} from "./util/contracthelpers";1718import {19  createCollectionExpectSuccess,20  createItemExpectSuccess,21  getGenericResult22} from "./util/helpers";232425chai.use(chaiAsPromised);26const expect = chai.expect;2728const value = 0;29const gasLimit = 3000n * 1000000n;30const marketContractAddress = '5CYN9j3YvRkqxewoxeSvRbhAym4465C57uMmX5j4yz99L5H6';3132describe('Contracts', () => {33  it(`Can deploy smart contract Flipper, instantiate it and call it's get and flip messages.`, async () => {34    await usingApi(async api => {35      const [contract, deployer] = await deployFlipper(api);36      const initialGetResponse = await getFlipValue(contract, deployer);3738      const bob = privateKey("//Bob");39      const flip = contract.tx.flip(value, gasLimit);40      await submitTransactionAsync(bob, flip);4142      const afterFlipGetResponse = await getFlipValue(contract, deployer);43      expect(afterFlipGetResponse).not.to.be.eq(initialGetResponse, 'Flipping should change value.');44    });45  });4647  it('Can initialize contract instance', async () => {48    await usingApi(async (api) => {49      const metadata = JSON.parse(fs.readFileSync('./src/flipper/metadata.json').toString('utf-8'));50      const abi = new Abi(metadata);51      const newContractInstance = new Contract(api, abi, marketContractAddress);52      expect(newContractInstance).to.have.property('address');53      expect(newContractInstance.address.toString()).to.equal(marketContractAddress);54    });55  });5657  it('Can transfer NFT using smart contract.', async () => {58    await usingApi(async api => {59      const alice = privateKey("//Alice");60      const bob = privateKey("//Bob");6162      // Prep work63      const collectionId = await createCollectionExpectSuccess();64      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');65      const [contract, deployer] = await deployTransferContract(api);66      const tokenBefore: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();67      68      // Transfer69      const transferTx = contract.tx.transfer(value, gasLimit, bob.address, collectionId, tokenId, 1);70      const events = await submitTransactionAsync(alice, transferTx);71      const result = getGenericResult(events);72      const tokenAfter: any = (await api.query.nft.nftItemList(collectionId, tokenId) as any).unwrap();7374      // tslint:disable-next-line:no-unused-expression75      expect(result.success).to.be.true;76      expect(tokenBefore.Owner.toString()).to.be.equal(alice.address);77      expect(tokenAfter.Owner.toString()).to.be.equal(bob.address);78    });79  });80});
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());
     }
   });
 }