Skip to content

Commit

Permalink
tests refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Dec 27, 2020
1 parent a0812d9 commit d994799
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 174 deletions.
4 changes: 4 additions & 0 deletions test/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const utils = {
return web3.utils.sha3(Math.random().toString())
},

generateAccount: () => {
return web3.eth.accounts.create()
},

assertEmitted: (result, n, name) => {
let gotEvents = 0
for (let i = 0; i < result.logs.length; i++) {
Expand Down
36 changes: 16 additions & 20 deletions test/unit/HashLists/add.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@ chai.use(chaiAsPromised)
const HashListLibrary = artifacts.require('HashListLibrary')
const HashLists = artifacts.require('HashLists')

const testUtils = require('../../helpers/utils.js')

contract('HashLists', (accounts) => {
let hashListLibrary
let hashList
const owner = accounts[0]

beforeEach(async () => {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(accounts[0], { from: owner })
if (!hashList) {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(accounts[0], { from: owner })
}
})

describe('add', () => {
it('should add a new value to list', async () => {
const newAccountHash = await hashList.hash(accounts[1])
const accountAddress = testUtils.generateAccount().address
const newAccountHash = await hashList.hash(accountAddress)
await hashList.methods['add(bytes32)'](
newAccountHash,
{
Expand All @@ -37,30 +42,21 @@ contract('HashLists', (accounts) => {
})

it('should fail if value already exists', async () => {
const newAccountHash = await hashList.hash(accounts[1])
await hashList.methods['add(bytes32)'](
newAccountHash,
{
from: owner
}
)
const accountAddress = testUtils.generateAccount().address
const newAccountHash = await hashList.hash(accountAddress)
await hashList.methods['add(bytes32)'](newAccountHash, { from: owner })

// assert
await assert.isRejected(
hashList.methods['add(bytes32)'](
newAccountHash,
{
from: owner
}
),
hashList.methods['add(bytes32)'](newAccountHash, { from: owner }),
'Value already exists'
)
})

it('should add multiple values at a time', async () => {
const values = [
await hashList.hash(accounts[1]),
await hashList.hash(accounts[2])
await hashList.hash(accounts[3]),
await hashList.hash(accounts[4])
]

await hashList.methods['add(bytes32[])'](
Expand Down
18 changes: 12 additions & 6 deletions test/unit/HashLists/has.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@ chai.use(chaiAsPromised)
const HashListLibrary = artifacts.require('HashListLibrary')
const HashLists = artifacts.require('HashLists')

const testUtils = require('../../helpers/utils.js')

contract('HashLists', (accounts) => {
let hashListLibrary
let hashList
const owner = accounts[0]

beforeEach(async () => {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(accounts[0], { from: owner })
if (!hashList) {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(accounts[0], { from: owner })
}
})

describe('has', () => {
it('should return true if value exists', async () => {
const newValue = await hashList.hash(accounts[1])
const accountAddress = testUtils.generateAccount().address
const newValue = await hashList.hash(accountAddress)
await hashList.methods['add(bytes32)'](
newValue,
{
Expand All @@ -41,7 +46,8 @@ contract('HashLists', (accounts) => {
})

it('should return false if value does not exist', async () => {
const value = await hashList.hash(accounts[1])
const accountAddress = testUtils.generateAccount().address
const value = await hashList.hash(accountAddress)
// assert
assert.strictEqual(
await hashList.has(value),
Expand Down
10 changes: 6 additions & 4 deletions test/unit/HashLists/index.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ contract('HashList', (accounts) => {
const owner = accounts[0]

beforeEach(async () => {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(accounts[0], { from: owner })
if (!hashList) {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(accounts[0], { from: owner })
}
})

describe('index', () => {
Expand Down
19 changes: 8 additions & 11 deletions test/unit/HashLists/ownership.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ contract('HashList', (accounts) => {
const owner = accounts[0]

beforeEach(async () => {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(owner, { from: owner })
const newAccountHash = await hashList.hash(accounts[1])
await hashList.methods['add(bytes32)'](
newAccountHash,
{
from: owner
}
)
if (!hashList) {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(owner, { from: owner })
const newAccountHash = await hashList.hash(accounts[1])
await hashList.methods['add(bytes32)'](newAccountHash, { from: owner })
}
})

describe('ownedBy', () => {
Expand Down
18 changes: 12 additions & 6 deletions test/unit/HashLists/remove.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@ chai.use(chaiAsPromised)
const HashListLibrary = artifacts.require('HashListLibrary')
const HashLists = artifacts.require('HashLists')

const testUtils = require('../../helpers/utils.js')

contract('HashList', (accounts) => {
let hashListLibrary
let hashList
const owner = accounts[0]

beforeEach(async () => {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(accounts[0], { from: owner })
if (!hashList) {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(accounts[0], { from: owner })
}
})

describe('remove', () => {
it('should remove value from list', async () => {
const newAccountHash = await hashList.hash(accounts[1])
const accountAddress = testUtils.generateAccount().address
const newAccountHash = await hashList.hash(accountAddress)
const listId = await hashList.hash(owner)
await hashList.methods['add(bytes32)'](
newAccountHash,
Expand Down Expand Up @@ -52,7 +57,8 @@ contract('HashList', (accounts) => {
})

it('should fail to remove if value does not exist', async () => {
const newAccountHash = await hashList.hash(accounts[1])
const accountAddress = testUtils.generateAccount().address
const newAccountHash = await hashList.hash(accountAddress)
await hashList.methods['add(bytes32)'](
newAccountHash,
{
Expand Down
28 changes: 17 additions & 11 deletions test/unit/HashLists/update.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,33 @@ chai.use(chaiAsPromised)
const HashListLibrary = artifacts.require('HashListLibrary')
const HashLists = artifacts.require('HashLists')

const testUtils = require('../../helpers/utils.js')

contract('HashLists', (accounts) => {
let hashListLibrary
let hashList
const owner = accounts[0]

beforeEach(async () => {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(owner, { from: owner })
if (!hashList) {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(owner, { from: owner })
}
})

describe('update', () => {
it('should fail if value does not exist', async () => {
const newValue = await hashList.hash(accounts[1])
const accountAddress = testUtils.generateAccount().address
const newValue = await hashList.hash(accountAddress)
await hashList.methods['add(bytes32)'](
newValue,
{
from: owner
}
)
const invalidValue = await hashList.hash(accounts[3])
const invalidValue = await hashList.hash(testUtils.generateAccount().address)
await assert.isRejected(
hashList.update(
invalidValue,
Expand All @@ -44,7 +49,8 @@ contract('HashLists', (accounts) => {
})

it('should fail if old value equals new value', async () => {
const oldValue = await hashList.hash(accounts[1])
const accountAddress = testUtils.generateAccount().address
const oldValue = await hashList.hash(accountAddress)
await hashList.methods['add(bytes32)'](
oldValue,
{
Expand All @@ -64,8 +70,8 @@ contract('HashLists', (accounts) => {
})

it('should update if old value is exists', async () => {
const oldValue = await hashList.hash(accounts[1])
const newValue = await hashList.hash(accounts[2])
const oldValue = await hashList.hash(testUtils.generateAccount().address)
const newValue = await hashList.hash(testUtils.generateAccount().address)
const listId = await hashList.hash(owner)

await hashList.methods['add(bytes32)'](
Expand Down Expand Up @@ -97,8 +103,8 @@ contract('HashLists', (accounts) => {
})

it('should fail in case of invalid list owner', async () => {
const oldValue = await hashList.hash(accounts[1])
const invalidOwner = accounts[5]
const oldValue = await hashList.hash(testUtils.generateAccount().address)
const invalidOwner = testUtils.generateAccount().address
await hashList.methods['add(bytes32)'](
oldValue,
{
Expand Down
39 changes: 24 additions & 15 deletions test/unit/HashLists/utils.Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ chai.use(chaiAsPromised)
const HashListLibrary = artifacts.require('HashListLibrary')
const HashLists = artifacts.require('HashLists')

const testUtils = require('../../helpers/utils.js')

contract('HashLists', (accounts) => {
let hashListLibrary
let hashList
let listId
const owner = accounts[0]

beforeEach(async () => {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(accounts[0], { from: owner })
listId = await hashList.hash(owner)
if (!hashList) {
hashListLibrary = await HashListLibrary.new()
HashLists.link('HashListLibrary', hashListLibrary.address)
hashList = await HashLists.new()
await hashList.initialize(accounts[0], { from: owner })
listId = await hashList.hash(owner)
}
})

describe('get', () => {
Expand All @@ -45,7 +49,9 @@ contract('HashLists', (accounts) => {

describe('all', () => {
it('should return all list values', async () => {
const newValue = await hashList.hash(accounts[1])
let listId = await hashList.hash(owner)
const newValue = await hashList.hash(testUtils.generateAccount().address)
const lengthBefore = (await hashList.all(listId)).length
await hashList.methods['add(bytes32)'](
newValue,
{
Expand All @@ -55,14 +61,15 @@ contract('HashLists', (accounts) => {

assert.strictEqual(
(await hashList.all(listId)).length,
1
lengthBefore + 1
)
})
})

describe('indexOf', () => {
it('should return index of value in a list', async () => {
const newValue = await hashList.hash(accounts[1])
const newValue = await hashList.hash(testUtils.generateAccount().address)
const lengthBefore = (await hashList.all(listId)).length
await hashList.methods['add(bytes32)'](
newValue,
{
Expand All @@ -72,12 +79,12 @@ contract('HashLists', (accounts) => {
// assert
assert.strictEqual(
(await hashList.indexOf(listId, newValue)).toNumber(),
1
lengthBefore + 1
)
})

it('should fail if value does not exists', async () => {
const newValue = await hashList.hash(accounts[1])
const newValue = await hashList.hash(testUtils.generateAccount().address)
await assert.isRejected(
hashList.indexOf(listId, newValue),
'Value does not exist'
Expand All @@ -87,13 +94,14 @@ contract('HashLists', (accounts) => {

describe('isIndexed', () => {
it('should return false if not indexed list', async () => {
const listId = await hashList.hash(testUtils.generateAccount().address)
await assert.isRejected(
hashList.isIndexed(listId)
)
})

it('should return true if indexed in case of add single element', async () => {
const newValue = await hashList.hash(accounts[1])
const newValue = await hashList.hash(testUtils.generateAccount().address)
await hashList.methods['add(bytes32)'](
newValue,
{
Expand All @@ -109,8 +117,8 @@ contract('HashLists', (accounts) => {

it('should return true if indexed using add multiple elements', async () => {
const values = [
await hashList.hash(accounts[1]),
await hashList.hash(accounts[2])
await hashList.hash(testUtils.generateAccount().address),
await hashList.hash(testUtils.generateAccount().address)
]

await hashList.methods['add(bytes32[])'](
Expand Down Expand Up @@ -158,7 +166,8 @@ contract('HashLists', (accounts) => {

describe('size', () => {
it('should return size', async () => {
const newValue = await hashList.hash(accounts[1])
const newValue = await hashList.hash(testUtils.generateAccount().address)
const lengthBefore = (await hashList.all(listId)).length
await hashList.methods['add(bytes32)'](
newValue,
{
Expand All @@ -168,7 +177,7 @@ contract('HashLists', (accounts) => {
// assert
assert.strictEqual(
(await hashList.size(listId)).toNumber(),
1
lengthBefore + 1
)
})
})
Expand Down

0 comments on commit d994799

Please sign in to comment.