From 91cbc925c4f73dfcb4db197b8ae67901f5466455 Mon Sep 17 00:00:00 2001 From: Miljan Milidrag Date: Fri, 21 Jul 2023 10:03:30 +0200 Subject: [PATCH] Add tests for usageEngine and usage routes --- test/mocks/server.js | 2 + test/mocks/store.js | 5 ++ test/unit/index.js | 1 + test/unit/responses.js | 121 ++++++++++++++++++++++++++++++++++++++ test/unit/routes/index.js | 1 + test/unit/routes/usage.js | 23 ++++++++ test/unit/usageEngine.js | 52 ++++++++++++++++ 7 files changed, 205 insertions(+) create mode 100644 test/unit/routes/usage.js create mode 100644 test/unit/usageEngine.js diff --git a/test/mocks/server.js b/test/mocks/server.js index e97d8b63..b7c39d27 100644 --- a/test/mocks/server.js +++ b/test/mocks/server.js @@ -4,6 +4,7 @@ const express = require('express'); const quoteRouter = require('../../src/routes/quote'); const capacityRouter = require('../../src/routes/capacity'); +const usageRouter = require('../../src/routes/usage'); const mockStore = require('./store'); @@ -27,6 +28,7 @@ const main = () => { // initiate routes app.use('/v2', capacityRouter); app.use('/v2', quoteRouter); + app.use('/v2', usageRouter); return app; }; diff --git a/test/mocks/store.js b/test/mocks/store.js index 6cbfc4a4..ed18a12d 100644 --- a/test/mocks/store.js +++ b/test/mocks/store.js @@ -224,6 +224,11 @@ const store = { 1: [1, 2], 2: [1, 2], }, + poolProductIds: { + 1: [0, 1, 2], + 2: [1, 2], + 3: [0], + }, products: { 0: { productType: 0, diff --git a/test/unit/index.js b/test/unit/index.js index 7cacd88c..93dbef38 100644 --- a/test/unit/index.js +++ b/test/unit/index.js @@ -1,6 +1,7 @@ describe('unit tests', function () { require('./capacityEngine'); require('./quoteEngine'); + require('./usageEngine'); require('./routes'); require('./calculateOptimalPoolAllocation'); }); diff --git a/test/unit/responses.js b/test/unit/responses.js index b610325d..b8e85683 100644 --- a/test/unit/responses.js +++ b/test/unit/responses.js @@ -91,7 +91,128 @@ const quote = { ], }; +const usage = [ + { + poolId: '1', + products: [ + { + productId: 0, + capacityUsed: [ + { + assetId: 0, + amount: '0', + }, + { + assetId: 1, + amount: '0', + }, + { + assetId: 255, + amount: '0', + }, + ], + }, + { + productId: 1, + capacityUsed: [ + { + assetId: 0, + amount: '0', + }, + { + assetId: 1, + amount: '0', + }, + { + assetId: 255, + amount: '0', + }, + ], + }, + { + productId: 2, + capacityUsed: [ + { + assetId: 0, + amount: '0', + }, + { + assetId: 1, + amount: '0', + }, + { + assetId: 255, + amount: '0', + }, + ], + }, + ], + }, + { + poolId: '2', + products: [ + { + productId: 1, + capacityUsed: [ + { + assetId: 0, + amount: '0', + }, + { + assetId: 1, + amount: '0', + }, + { + assetId: 255, + amount: '0', + }, + ], + }, + { + productId: 2, + capacityUsed: [ + { + assetId: 0, + amount: '0', + }, + { + assetId: 1, + amount: '0', + }, + { + assetId: 255, + amount: '0', + }, + ], + }, + ], + }, + { + poolId: '3', + products: [ + { + productId: 0, + capacityUsed: [ + { + assetId: 0, + amount: '3750158703091230720', + }, + { + assetId: 1, + amount: '10478675352241508148979', + }, + { + assetId: 255, + amount: '364800000000000000000', + }, + ], + }, + ], + }, +]; + module.exports = { capacities, quote, + usage, }; diff --git a/test/unit/routes/index.js b/test/unit/routes/index.js index 25e6fbea..69226a52 100644 --- a/test/unit/routes/index.js +++ b/test/unit/routes/index.js @@ -1,4 +1,5 @@ describe('routes', function () { require('./quote'); require('./capcaity'); + require('./usage'); }); diff --git a/test/unit/routes/usage.js b/test/unit/routes/usage.js new file mode 100644 index 00000000..6e2445f8 --- /dev/null +++ b/test/unit/routes/usage.js @@ -0,0 +1,23 @@ +const { expect } = require('chai'); +const supertest = require('supertest'); +const initApp = require('../../mocks/server'); +const { usage } = require('../responses'); + +describe('GET /usage', async () => { + let server; + before(() => { + const app = initApp(); + server = supertest(app); + }); + + it('should get all usage for pools', async function () { + const { body: response } = await server.get('/v2/usage').expect('Content-Type', /json/).expect(200); + expect(response).to.be.deep.equal(usage); + }); + + it('should get all usage for one pool', async function () { + const poolId = 1; + const { body: response } = await server.get(`/v2/usage/${poolId}`).expect('Content-Type', /json/).expect(200); + expect(response).to.be.deep.equal(usage[0]); + }); +}); diff --git a/test/unit/usageEngine.js b/test/unit/usageEngine.js new file mode 100644 index 00000000..f9ff0887 --- /dev/null +++ b/test/unit/usageEngine.js @@ -0,0 +1,52 @@ +const sinon = require('sinon'); +const { expect } = require('chai'); +const { BigNumber } = require('ethers'); + +const usageEngine = require('../../src/lib/usageEngine'); +const mockStore = require('../mocks/store'); +const { usage } = require('./responses'); +describe.only('Capacity Engine tests', () => { + const store = { getState: () => null }; + + afterEach(function () { + sinon.restore(); + }); + + it('should return usage for all pools', () => { + sinon.stub(store, 'getState').callsFake(() => mockStore); + const response = usageEngine(store, []); + + response.forEach((pool, i) => { + expect(pool.poolId).to.be.equal(usage[i].poolId); + pool.products.forEach(({ productId, capacityUsed }, j) => { + expect(productId).to.be.equal(usage[i].products[j].productId); + capacityUsed.forEach(({ amount }, k) => { + expect(amount.toString()).to.be.equal(usage[i].products[j].capacityUsed[k].amount); + }); + }); + }); + }); + + it('should return capacity for one product', () => { + sinon.stub(store, 'getState').callsFake(() => mockStore); + + const [pool] = usageEngine(store, ['1']); + const [expectedUsage] = usage; + + expect(pool.poolId).to.be.equal(expectedUsage.poolId); + pool.products.forEach(({ productId, capacityUsed }, i) => { + expect(productId).to.be.equal(expectedUsage.products[i].productId); + capacityUsed.forEach(({ amount }, j) => { + expect(amount.toString()).to.be.equal(expectedUsage.products[j].capacityUsed[j].amount); + }); + }); + }); + + it('should throw non existing product', () => { + sinon.stub(store, 'getState').callsFake(() => mockStore); + + const [pool] = usageEngine(store, ['10']); + + expect(pool.products.length).to.be.equal(0); + }); +});