• Mark Tyneway's avatar
    core-utils: add GenesisJsonProvider and fix tests · 7c352b1e
    Mark Tyneway authored
    The `GenesisJsonProvider` implements the `ethers.Provider`
    interface and is constructed with a geth genesis file, either
    as an object or as a file to be read from disk. It implements
    a subset of the RPC methods that use the genesis file
    as the backing storage. It includes tests for its correctness.
    Not all methods are implemented, just the ones for the regenesis
    testing.
    
    This PR also moves the tests around in the `core-utils` package
    as some of the tests were being skipped. The `tests` directory is
    flattened, having so many subdirectories was not needed. The
    `package.json` test script is updated to ensure that all tests
    are run.
    
    Also add some deps that are required for the `GenesisJsonProvider`.
    7c352b1e
test-utils.spec.ts 5.79 KB
import { expect } from './setup'

/* Imports: Internal */
import { expectApprox, awaitCondition } from '../src'
import { assert } from 'chai'

describe('awaitCondition', () => {
  it('should try the condition fn until it returns true', async () => {
    let i = 0
    const condFn = async () => {
      i++
      return Promise.resolve(i === 2)
    }

    await awaitCondition(condFn, 50, 3)
    expect(i).to.equal(2)
  })

  it('should only try the configured number of attempts', async () => {
    let i = 0
    const condFn = async () => {
      i++
      return Promise.resolve(i === 2)
    }

    try {
      await awaitCondition(condFn, 50, 1)
    } catch (e) {
      return
    }

    assert.fail('Condition never failed, but it should have.')
  })
})

describe('expectApprox', () => {
  it('should pass when the actual number is higher, but within the expected range of the target', async () => {
    expectApprox(119, 100, {
      percentUpperDeviation: 20,
      percentLowerDeviation: 20,
      absoluteUpperDeviation: 20,
      absoluteLowerDeviation: 20,
    })
  })
  it('should pass when the actual number is lower, but within the expected range of the target', async () => {
    expectApprox(81, 100, {
      percentUpperDeviation: 20,
      percentLowerDeviation: 20,
      absoluteUpperDeviation: 20,
      absoluteLowerDeviation: 20,
    })
  })
  it('should throw an error when no deviation values are given', async () => {
    try {
      expectApprox(101, 100, {})
      assert.fail('expectApprox did not throw an error')
    } catch (error) {
      expect(error.message).to.equal(
        'Must define at least one parameter to limit the deviation of the actual value.'
      )
    }
  })

  describe('should throw an error if the actual value is higher than expected', () => {
    describe('... when only one upper bound value is defined', () => {
      it('... and percentUpperDeviation sets the upper bound', async () => {
        try {
          expectApprox(121, 100, {
            percentUpperDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true'
          )
        }
      })
      it('... and absoluteUpperDeviation sets the upper bound', async () => {
        try {
          expectApprox(121, 100, {
            absoluteUpperDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true'
          )
        }
      })
    })
    describe('... when both values are defined', () => {
      it('... and percentUpperDeviation sets the upper bound', async () => {
        try {
          expectApprox(121, 100, {
            percentUpperDeviation: 20,
            absoluteUpperDeviation: 30,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true'
          )
        }
      })
      it('... and absoluteUpperDeviation sets the upper bound', async () => {
        try {
          expectApprox(121, 100, {
            percentUpperDeviation: 30,
            absoluteUpperDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true'
          )
        }
      })
    })
  })

  describe('should throw an error if the actual value is lower than expected', () => {
    describe('... when only one lower bound value is defined', () => {
      it('... and percentLowerDeviation sets the lower bound', async () => {
        try {
          expectApprox(79, 100, {
            percentLowerDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (79) is less than the calculated lower bound of (80): expected false to be true'
          )
        }
      })

      it('... and absoluteLowerDeviation sets the lower bound', async () => {
        try {
          expectApprox(79, 100, {
            absoluteLowerDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (79) is less than the calculated lower bound of (80): expected false to be true'
          )
        }
      })
    })

    describe('... when both values are defined', () => {
      it('... and percentLowerDeviation sets the lower bound', async () => {
        try {
          expectApprox(79, 100, {
            percentLowerDeviation: 20,
            absoluteLowerDeviation: 30,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (79) is less than the calculated lower bound of (80): expected false to be true'
          )
        }
      })

      it('... and absoluteLowerDeviation sets the lower bound', async () => {
        try {
          expectApprox(79, 100, {
            percentLowerDeviation: 30,
            absoluteLowerDeviation: 20,
          })
          assert.fail('expectApprox did not throw an error')
        } catch (error) {
          expect(error.message).to.equal(
            'Actual value (79) is less than the calculated lower bound of (80): expected false to be true'
          )
        }
      })
    })
  })
})