Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cleanup: Split tests into unit, integ, e2e + additional mocking #369

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ UniswapX Service is an API to propagate signed, executable UniswapX orders. Swap

3. (optional) To run dynamo-db integration tests, you need to have Java Runtime installed (https://www.java.com/en/download/manual.jsp).

## Integration Tests
## End-to-end Tests

1. Deploy your API using the intructions above.

Expand All @@ -36,7 +36,7 @@ UniswapX Service is an API to propagate signed, executable UniswapX orders. Swap

1. Run the tests with:
```
yarn integ-test
yarn test:e2e
```

## Development Cycle
Expand All @@ -57,7 +57,7 @@ PERMIT_TENDERLY=<>

1. `yarn build && cdk deploy GoudaServiceStack`

1. `yarn integ-test`
1. `yarn test:e2e`

1. If failures, look at logs in Cloudwatch Insights

Expand Down
15 changes: 15 additions & 0 deletions __mocks__/aws-embedded-metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const original = jest.requireActual('aws-embedded-metrics')
andysmith415 marked this conversation as resolved.
Show resolved Hide resolved

class MockMetricsLogger {
public setNamespace = jest.fn()
public putMetric = jest.fn()
public flush = jest.fn()
}

const metricScope = jest.fn((fn) => fn(new MockMetricsLogger()))

module.exports = {
...original,
metricScope,
MockMetricsLogger,
}
2 changes: 1 addition & 1 deletion bin/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export class APIPipeline extends Stack {
'echo "TEST_FILLER_PK=${TEST_FILLER_PK}" > .env',
'yarn install --network-concurrency 1 --skip-integrity-check',
'yarn build',
'yarn run integ-test',
'yarn run test:e2e',
],
partialBuildSpec: BuildSpec.fromObject({
phases: {
Expand Down
18 changes: 0 additions & 18 deletions lib/util/network-requests.ts

This file was deleted.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
"build": "tsc",
"clean": "rm -rf dist cdk.out",
"watch": "tsc -w",
"test": "jest --detectOpenHandles --forceExit --testPathPattern=test/unit/",
"integ-test": "jest --detectOpenHandles --runInBand --testPathPattern=integ/",
"test": "jest --testPathPattern=test/unit/",
"test:watch": "yarn test --watch",
"test:integ": "jest --detectOpenHandles --forceExit --testPathPattern=test/integ/",
"test:e2e": "jest --detectOpenHandles --runInBand --testPathPattern=test/e2e/",
"cdk": "cdk",
"fix": "run-s fix:*",
"fix:prettier": "prettier \"lib/**/*.ts\" --write",
"fix:lint": "eslint lib --ext .ts --fix",
"lint": "eslint lib --ext .ts",
"coverage": "yarn test --coverage"
"coverage": "jest --detectOpenHandles --runInBand --forceExit --coverage --testPathPattern=\"test/(unit/|integ/)\""
},
"devDependencies": {
"@shelf/jest-dynamodb": "^3.3.1",
Expand Down
13 changes: 0 additions & 13 deletions test/unit/utils.ts → test/HeaderExpectation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { BaseOrdersRepository } from '../../lib/repositories/base'
import { DYNAMO_BATCH_WRITE_MAX } from '../../lib/util/constants'

export class HeaderExpectation {
private headers: { [header: string]: string | number | boolean } | undefined

Expand All @@ -23,13 +20,3 @@ export class HeaderExpectation {
return this
}
}

export async function deleteAllRepoEntries(ordersRepository: BaseOrdersRepository) {
let orders = await ordersRepository.getOrders(DYNAMO_BATCH_WRITE_MAX, { chainId: 1 })
if (!orders.orders.length) {
return
}
do {
await ordersRepository.deleteOrders(orders.orders.map((o) => o.orderHash))
} while (orders.cursor && (orders = await ordersRepository.getOrders(DYNAMO_BATCH_WRITE_MAX, { chainId: 1 })))
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { deleteStaleOrders } from '../../../lib/crons/gs-reaper'
import { ORDER_STATUS } from '../../../lib/entities'
import { DutchOrdersRepository } from '../../../lib/repositories/dutch-orders-repository'
import { DYNAMO_BATCH_WRITE_MAX } from '../../../lib/util/constants'
import { deleteAllRepoEntries } from '../utils'
import { deleteAllRepoEntries } from '../repositories/deleteAllRepoEntries'

const dynamoConfig = {
convertEmptyValues: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ORDER_STATUS } from '../../../lib/entities'
import { CheckOrderStatusRequest, CheckOrderStatusService } from '../../../lib/handlers/check-order-status/service'
import { getProvider, getValidator, getWatcher } from '../../../lib/handlers/check-order-status/util'
import { log } from '../../../lib/Logging'
import { MOCK_ORDER_ENTITY, MOCK_ORDER_HASH } from './test-data'
import { MOCK_ORDER_ENTITY, MOCK_ORDER_HASH } from '../../test-data'

jest.mock('../../../lib/handlers/check-order-status/util', () => {
const original = jest.requireActual('../../../lib/handlers/check-order-status/util')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { BigNumber } from 'ethers'
import { ORDER_STATUS } from '../../../lib/entities'
import { CheckOrderStatusHandler } from '../../../lib/handlers/check-order-status/handler'
import { FILL_EVENT_LOOKBACK_BLOCKS_ON, getSettledAmounts } from '../../../lib/handlers/check-order-status/util'
import { log } from '../../../lib/Logging'
import { NATIVE_ADDRESS } from '../../../lib/util/constants'
import { ORDER_INFO } from '../fixtures'
import { MOCK_ORDER_ENTITY, MOCK_ORDER_HASH } from './test-data'
import { MOCK_ORDER_ENTITY, MOCK_ORDER_HASH } from '../../test-data'
import { ORDER_INFO } from '../../unit/fixtures'

describe('Testing check order status handler', () => {
const mockedBlockNumber = 123
Expand All @@ -19,6 +20,10 @@ describe('Testing check order status handler', () => {
const providerMock = jest.fn().mockReturnValue(mockedBlockNumber)
const getTransactionMock = jest.fn()

beforeAll(() => {
log.setLogLevel('SILENT')
})

const buildInjectorPromiseMock = (retryCount: number, orderStatus: string) => {
return {
getContainerInjected: () => {
Expand All @@ -35,7 +40,7 @@ describe('Testing check order status handler', () => {
orderHash: MOCK_ORDER_HASH,
retryCount: retryCount,
orderStatus: orderStatus,
log: { info: () => jest.fn(), error: () => jest.fn() },
log,
orderQuoter: {
validate: validateMock,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { getProvider, getValidator, getWatcher } from '../../../lib/handlers/che
import { log } from '../../../lib/Logging'
import { OnChainStatusCheckerMetricNames, powertoolsMetric } from '../../../lib/Metrics'
import { LimitOrdersRepository } from '../../../lib/repositories/limit-orders-repository'
import { deleteAllRepoEntries } from '../utils'
import { dynamoConfig, MOCK_ORDER_ENTITY, MOCK_ORDER_HASH, MOCK_SIGNATURE } from './test-data'
import { dynamoConfig, MOCK_ORDER_ENTITY, MOCK_ORDER_HASH, MOCK_SIGNATURE } from '../../test-data'
import { deleteAllRepoEntries } from '../repositories/deleteAllRepoEntries'

jest.mock('../../../lib/handlers/check-order-status/util', () => {
const original = jest.requireActual('../../../lib/handlers/check-order-status/util')
Expand Down
12 changes: 12 additions & 0 deletions test/integ/repositories/deleteAllRepoEntries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BaseOrdersRepository } from '../../../lib/repositories/base'
import { DYNAMO_BATCH_WRITE_MAX } from '../../../lib/util/constants'

export async function deleteAllRepoEntries(ordersRepository: BaseOrdersRepository) {
let orders = await ordersRepository.getOrders(DYNAMO_BATCH_WRITE_MAX, { chainId: 1 })
if (!orders.orders.length) {
return
}
do {
await ordersRepository.deleteOrders(orders.orders.map((o) => o.orderHash))
} while (orders.cursor && (orders = await ordersRepository.getOrders(DYNAMO_BATCH_WRITE_MAX, { chainId: 1 })))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ORDER_STATUS, SORT_FIELDS } from '../../../lib/entities/Order'
import { DutchOrdersRepository } from '../../../lib/repositories/dutch-orders-repository'
import { generateRandomNonce } from '../../../lib/util/nonce'
import { currentTimestampInSeconds } from '../../../lib/util/time'
import { deleteAllRepoEntries } from '../utils'
import { deleteAllRepoEntries } from './deleteAllRepoEntries'

jest.mock('../../../lib/util/time')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ORDER_STATUS, SORT_FIELDS } from '../../../lib/entities/Order'
import { LimitOrdersRepository } from '../../../lib/repositories/limit-orders-repository'
import { generateRandomNonce } from '../../../lib/util/nonce'
import { currentTimestampInSeconds } from '../../../lib/util/time'
import { deleteAllRepoEntries } from '../utils'
import { deleteAllRepoEntries } from './deleteAllRepoEntries'

jest.mock('../../../lib/util/time')

Expand Down
3 changes: 1 addition & 2 deletions test/unit/handlers/test-data.ts → test/test-data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable */
import { OrderType, REACTOR_ADDRESS_MAPPING } from '@uniswap/uniswapx-sdk'
import { OrderEntity, ORDER_STATUS } from '../../../lib/entities'
import { OrderEntity, ORDER_STATUS } from '../lib/entities'

export const MOCK_ORDER_HASH = '0xc57af022b96e1cb0da0267c15f1d45cdfccf57cfeb8b33869bb50d7f478ab203'
export const MOCK_ENCODED_ORDER =
Expand Down
4 changes: 2 additions & 2 deletions test/unit/handlers/fill-event-logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BigNumber } from 'ethers'
import { FillEventLogger } from '../../../lib/handlers/check-order-status/fill-event-logger'
import { FILL_EVENT_LOOKBACK_BLOCKS_ON } from '../../../lib/handlers/check-order-status/util'
import { log } from '../../../lib/Logging'
import { MOCK_ORDER_ENTITY, MOCK_ORDER_HASH } from './test-data'
import { MOCK_ORDER_ENTITY, MOCK_ORDER_HASH } from '../../test-data'

jest.mock('../../../lib/handlers/check-order-status/util')

Expand Down Expand Up @@ -36,7 +36,7 @@ describe('processFillEvent', () => {
amountIn: '2',
}

let fillEventProcessor = new FillEventLogger(FILL_EVENT_LOOKBACK_BLOCKS_ON)
const fillEventProcessor = new FillEventLogger(FILL_EVENT_LOOKBACK_BLOCKS_ON)

const response = await fillEventProcessor.processFillEvent({
fillEvent: MOCK_FILL_EVENT,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/handlers/get-docs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GetDocsHandler } from '../../../lib/handlers/get-docs/GetDocsHandler'
import schema from '../../../swagger.json'
import { HeaderExpectation } from '../utils'
import { HeaderExpectation } from '../../HeaderExpectation'

describe('Testing get api docs json handler.', () => {
// Creating mocks for all the handler dependencies.
Expand Down
2 changes: 1 addition & 1 deletion test/unit/handlers/get-nonce.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GetNonceHandler } from '../../../lib/handlers/get-nonce/handler'
import { HeaderExpectation } from '../utils'
import { HeaderExpectation } from '../../HeaderExpectation'

describe('Testing get nonce handler.', () => {
const MOCK_ADDRESS = '0x11E4857Bb9993a50c685A79AFad4E6F65D518DDa'
Expand Down
4 changes: 1 addition & 3 deletions test/unit/handlers/get-orders.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OrderType } from '@uniswap/uniswapx-sdk'
import { ORDER_STATUS, SORT_FIELDS } from '../../../lib/entities'
import { GetOrdersHandler } from '../../../lib/handlers/get-orders/handler'
import { HeaderExpectation } from '../utils'
import { HeaderExpectation } from '../../HeaderExpectation'

describe('Testing get orders handler.', () => {
const MOCK_ORDER = {
Expand Down Expand Up @@ -221,7 +221,6 @@ describe('Testing get orders handler.', () => {
requestInjectedMock.cursor
)
expect(getOrdersResponse.statusCode).toEqual(200)
console.log(getOrdersResponse.body)

expect(JSON.parse(getOrdersResponse.body).orders[0].quoteId).toEqual('4385e89a-0553-46fa-9b7e-464c1fa7822f')
})
Expand All @@ -235,7 +234,6 @@ describe('Testing get orders handler.', () => {
requestInjectedMock.cursor
)
expect(getOrdersResponse.statusCode).toEqual(200)
console.log(getOrdersResponse.body)
expect(JSON.parse(getOrdersResponse.body).orders[0].quoteId).not.toBeDefined()
})
})
Expand Down
2 changes: 2 additions & 0 deletions test/unit/handlers/post-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DEFAULT_MAX_OPEN_ORDERS } from '../../../lib/handlers/constants'
import { PostOrderHandler } from '../../../lib/handlers/post-order/handler'
import { getMaxOpenOrders } from '../../../lib/handlers/post-order/injector'
import { kickoffOrderTrackingSfn } from '../../../lib/handlers/shared/sfn'
import { log } from '../../../lib/Logging'
import { ORDER_INFO } from '../fixtures'

const MOCK_ARN_1 = 'MOCK_ARN_1'
Expand Down Expand Up @@ -145,6 +146,7 @@ describe('Testing post order handler.', () => {
process.env['STATE_MACHINE_ARN_5'] = MOCK_ARN_5
process.env['REGION'] = 'region'
DutchOrder.parse.mockImplementation((_order: any, chainId: number) => ({ ...DECODED_ORDER, chainId }))
log.setLogLevel('SILENT')
})

afterEach(() => {
Expand Down
18 changes: 0 additions & 18 deletions test/unit/util/network-requests.test.ts

This file was deleted.

Loading