Skip to content

Commit

Permalink
Merge pull request #5 from JithinKS97/statistics-testcases
Browse files Browse the repository at this point in the history
Added gap and testcases
  • Loading branch information
CeliktepeMurat authored Nov 14, 2022
2 parents d536c77 + 17e6cbe commit e0bf71b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
8 changes: 8 additions & 0 deletions contracts/contracts/metrics/Statistics.sol
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,12 @@ contract Statistics is Initializable {
function doesPlayerExist(address player) public view returns (bool) {
return playerExists[player];
}


/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[45] private __gap;
}
10 changes: 1 addition & 9 deletions contracts/test/EthernautTests.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/*eslint no-undef: "off"*/
const Ethernaut = artifacts.require('./Ethernaut.sol');
const DummyFactory = artifacts.require('./levels/DummyFactory.sol');
const Dummy = artifacts.require('./levels/Dummy.sol');
const FallbackFactory = artifacts.require('./levels/FallbackFactory.sol');
const Manufactured = artifacts.require('./levels/Manufactured.sol');
const { expectRevert } = require('openzeppelin-test-helpers');
const utils = require('./utils/TestUtils');
const { ethers, upgrades } = require('hardhat');

contract('Ethernaut', function (accounts) {
// ----------------------------------
Expand All @@ -16,15 +14,9 @@ contract('Ethernaut', function (accounts) {
let owner = accounts[0];
let player = accounts[1];
let ethernaut;
let statproxy;

before(async function () {
ethernaut = await Ethernaut.new();

const ProxyStat = await ethers.getContractFactory('Statistics');
statproxy = await upgrades.deployProxy(ProxyStat, [ethernaut.address]);

await ethernaut.setStatistics(statproxy.address);
ethernaut = await utils.getEthernautWithStatsProxy()
});

it(`should not allow a player to manufacture a solution instance`, async function () {
Expand Down
27 changes: 19 additions & 8 deletions contracts/test/metrics/level.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ contract('Level metrics', (accounts) => {
PLAYER_ADDRESS_2,
LEVEL_FACTORY_ADDRESS_1,
LEVEL_FACTORY_ADDRESS_2,
LEVEL_FACTORY_ADDRESS_3,
LEVEL_INSTANCE_ADDRESS_1,
LEVEL_INSTANCE_ADDRESS_2,
LEVEL_INSTANCE_ADDRESS_3
LEVEL_INSTANCE_ADDRESS_3,
LEVEL_INSTANCE_ADDRESS_4,
LEVEL_INSTANCE_ADDRESS_5
] = accounts;

before(async () => {
Expand All @@ -29,18 +32,21 @@ contract('Level metrics', (accounts) => {
it('adds multiple levels', async () => {
await statistics.saveNewLevel(LEVEL_FACTORY_ADDRESS_1);
await statistics.saveNewLevel(LEVEL_FACTORY_ADDRESS_2);
await statistics.saveNewLevel(LEVEL_FACTORY_ADDRESS_3);
});
});

describe("Creation of multiple level instances", () => {
before(async () => {
await statistics.createNewInstance(LEVEL_INSTANCE_ADDRESS_1, LEVEL_FACTORY_ADDRESS_1, PLAYER_ADDRESS_1);
await statistics.createNewInstance(LEVEL_INSTANCE_ADDRESS_2, LEVEL_FACTORY_ADDRESS_2, PLAYER_ADDRESS_1);
await statistics.createNewInstance(LEVEL_INSTANCE_ADDRESS_3, LEVEL_FACTORY_ADDRESS_1, PLAYER_ADDRESS_2);
await statistics.createNewInstance(LEVEL_INSTANCE_ADDRESS_3, LEVEL_FACTORY_ADDRESS_3, PLAYER_ADDRESS_1);
await statistics.createNewInstance(LEVEL_INSTANCE_ADDRESS_4, LEVEL_FACTORY_ADDRESS_1, PLAYER_ADDRESS_2);
await statistics.createNewInstance(LEVEL_INSTANCE_ADDRESS_5, LEVEL_FACTORY_ADDRESS_2, PLAYER_ADDRESS_2);
})

it('checks no of level instances created', async () => {
expect((await statistics.getTotalNoOfLevelsCreated()).toNumber()).to.equal(3)
expect((await statistics.getTotalNoOfLevelsCreated()).toNumber()).to.equal(5)
})

it('checks total no of players', async () => {
Expand All @@ -51,31 +57,36 @@ contract('Level metrics', (accounts) => {
describe("Getting global level stats", () => {
before(async () => {
await statistics.submitSuccess(LEVEL_INSTANCE_ADDRESS_1, LEVEL_FACTORY_ADDRESS_1, PLAYER_ADDRESS_1);
await statistics.submitFailure(LEVEL_INSTANCE_ADDRESS_2, LEVEL_FACTORY_ADDRESS_2, PLAYER_ADDRESS_1);
await statistics.submitSuccess(LEVEL_INSTANCE_ADDRESS_3, LEVEL_FACTORY_ADDRESS_1, PLAYER_ADDRESS_2);
await statistics.submitSuccess(LEVEL_INSTANCE_ADDRESS_2, LEVEL_FACTORY_ADDRESS_2, PLAYER_ADDRESS_1);
await statistics.submitFailure(LEVEL_INSTANCE_ADDRESS_3, LEVEL_FACTORY_ADDRESS_3, PLAYER_ADDRESS_1);
await statistics.submitSuccess(LEVEL_INSTANCE_ADDRESS_4, LEVEL_FACTORY_ADDRESS_1, PLAYER_ADDRESS_2);
await statistics.submitFailure(LEVEL_INSTANCE_ADDRESS_5, LEVEL_FACTORY_ADDRESS_2, PLAYER_ADDRESS_2);
})

it('checks no of completed submissions', async () => {
expect((await statistics.getTotalNoOfLevelsCompleted()).toNumber()).to.equal(2)
expect((await statistics.getTotalNoOfLevelsCompleted()).toNumber()).to.equal(3)
})

it('checks no of failed submissions', async () => {
expect((await statistics.getTotalNoOfLevelsFailures()).toNumber()).to.equal(1)
expect((await statistics.getTotalNoOfLevelsFailures()).toNumber()).to.equal(2)
})
})

describe("Getting the level specific stats", () => {
it('checks the no of instances created for a level', async () => {
expect((await statistics.getNoOfInstancesForLevel(LEVEL_FACTORY_ADDRESS_1)).toNumber()).to.equal(2)
expect((await statistics.getNoOfInstancesForLevel(LEVEL_FACTORY_ADDRESS_2)).toNumber()).to.equal(1)
expect((await statistics.getNoOfInstancesForLevel(LEVEL_FACTORY_ADDRESS_2)).toNumber()).to.equal(2)
expect((await statistics.getNoOfInstancesForLevel(LEVEL_FACTORY_ADDRESS_3)).toNumber()).to.equal(1)
})

it('checks the no of completed instances of a level', async () => {
expect((await statistics.getNoOfCompletedSubmissionForLevel(LEVEL_FACTORY_ADDRESS_1)).toNumber()).to.equal(2)
expect((await statistics.getNoOfCompletedSubmissionForLevel(LEVEL_FACTORY_ADDRESS_2)).toNumber()).to.equal(1)
})

it('checks the no of failed instances of a level', async () => {
expect((await statistics.getNoOfFailedSubmissionForLevel(LEVEL_FACTORY_ADDRESS_2)).toNumber()).to.equal(1)
expect((await statistics.getNoOfFailedSubmissionForLevel(LEVEL_FACTORY_ADDRESS_3)).toNumber()).to.equal(1)
})
})
});
Expand Down

0 comments on commit e0bf71b

Please sign in to comment.