Skip to content

Commit

Permalink
test: unify createMockEvent util function
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Nov 19, 2024
1 parent 180333a commit 41a449d
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 502 deletions.
57 changes: 57 additions & 0 deletions packages/processors/test/mocks/event.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
ChainId,
ContractToEventName,
DeepPartial,
EventParams,
Hex,
mergeDeep,
ProcessorEvent,
} from "@grants-stack-indexer/shared";

/**
* Creates a mock event for testing.
*
* @param eventName - The name of the event.
* @param params - The parameters of the event.
* @param strategyId - The ID of the strategy.
* @param overrides - The overrides for the event.
* @returns A mock event.
*
* @default
* srcAddress: "0x1234567890123456789012345678901234567890",
* blockNumber: 118034410,
* blockTimestamp: 1000000000,
* chainId: 10 as ChainId,
* contractName: "Strategy",
* logIndex: 1,
* transactionFields: {
* hash: "0xd2352acdcd59e312370831ea927d51a1917654697a72434cd905a60897a5bb8b",
* transactionIndex: 1,
* from: "0xcBf407C33d68a55CB594Ffc8f4fD1416Bba39DA5",
* },
*/
export const createMockEvent = <T extends ContractToEventName<"Strategy">>(
eventName: T,
params: EventParams<"Strategy", T>,
strategyId: Hex,
overrides: DeepPartial<ProcessorEvent<"Strategy", T>> = {},
): ProcessorEvent<"Strategy", T> => {
const defaultEvent: ProcessorEvent<"Strategy", T> = {
eventName,
params,
srcAddress: "0x1234567890123456789012345678901234567890",
blockNumber: 118034410,
blockTimestamp: 1000000000,
chainId: 10 as ChainId,
contractName: "Strategy",
logIndex: 1,
transactionFields: {
hash: "0xd2352acdcd59e312370831ea927d51a1917654697a72434cd905a60897a5bb8b",
transactionIndex: 1,
from: "0xcBf407C33d68a55CB594Ffc8f4fD1416Bba39DA5",
},
strategyId,
};

return mergeDeep(defaultEvent, overrides);
};
1 change: 1 addition & 0 deletions packages/processors/test/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./event.mock.js";
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,37 @@ import { IRoundReadRepository, Round } from "@grants-stack-indexer/repository";
import { ChainId, ILogger, ProcessorEvent } from "@grants-stack-indexer/shared";

import { BaseDistributedHandler } from "../../../src/processors/strategy/common/baseDistributed.handler.js";

function createMockEvent(
overrides: Partial<ProcessorEvent<"Strategy", "DistributedWithRecipientAddress">> = {},
): ProcessorEvent<"Strategy", "DistributedWithRecipientAddress"> {
const defaultEvent: ProcessorEvent<"Strategy", "DistributedWithRecipientAddress"> = {
params: {
amount: "1000",
recipientAddress: "0x1234567890123456789012345678901234567890",
recipientId: "0x1234567890123456789012345678901234567890",
sender: "0x1234567890123456789012345678901234567890",
},
eventName: "DistributedWithRecipientAddress",
srcAddress: "0x1234567890123456789012345678901234567890",
blockNumber: 12345,
blockTimestamp: 1000000000,
chainId: 10 as ChainId,
contractName: "Strategy",
logIndex: 1,
transactionFields: {
hash: "0xd2352acdcd59e312370831ea927d51a1917654697a72434cd905a60897a5bb8b",
transactionIndex: 6,
from: "0xcBf407C33d68a55CB594Ffc8f4fD1416Bba39DA5",
},
strategyId: "0x9fa6890423649187b1f0e8bf4265f0305ce99523c3d11aa36b35a54617bb0ec0",
};

return { ...defaultEvent, ...overrides };
}
import { createMockEvent } from "../../mocks/index.js";

describe("BaseDistributedHandler", () => {
let handler: BaseDistributedHandler;
let mockRoundRepository: IRoundReadRepository;
let mockEvent: ProcessorEvent<"Strategy", "DistributedWithRecipientAddress">;
const chainId = 10 as ChainId;
const eventName = "DistributedWithRecipientAddress";
const defaultParams = {
amount: "1000",
recipientAddress: "0x1234567890123456789012345678901234567890",
recipientId: "0x1234567890123456789012345678901234567890",
sender: "0x1234567890123456789012345678901234567890",
} as const;
const defaultStrategyId = "0x9fa6890423649187b1f0e8bf4265f0305ce99523c3d11aa36b35a54617bb0ec0";

const logger: ILogger = {
debug: vi.fn(),
error: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
};

beforeEach(() => {
mockRoundRepository = {
getRoundByStrategyAddress: vi.fn(),
} as unknown as IRoundReadRepository;
});

it("increments round total distributed when round is found", async () => {
mockEvent = createMockEvent();
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
const mockRound = { id: "round1" } as Round;

vi.spyOn(mockRoundRepository, "getRoundByStrategyAddress").mockResolvedValue(mockRound);
Expand All @@ -76,7 +59,7 @@ describe("BaseDistributedHandler", () => {
});

it("returns an empty array when round is not found", async () => {
mockEvent = createMockEvent();
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);

vi.spyOn(mockRoundRepository, "getRoundByStrategyAddress").mockResolvedValue(undefined);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,27 @@ import { beforeEach, describe, expect, it, vi } from "vitest";

import { IMetadataProvider } from "@grants-stack-indexer/metadata";
import { PartialRound } from "@grants-stack-indexer/repository";
import {
Bytes32String,
ChainId,
DeepPartial,
Logger,
mergeDeep,
ProcessorEvent,
} from "@grants-stack-indexer/shared";
import { Bytes32String, ChainId, Logger, ProcessorEvent } from "@grants-stack-indexer/shared";

import {
BaseDistributionUpdatedHandler,
MetadataNotFound,
MetadataParsingFailed,
} from "../../../src/internal.js";

function createMockEvent(
overrides: DeepPartial<ProcessorEvent<"Strategy", "DistributionUpdated">> = {},
): ProcessorEvent<"Strategy", "DistributionUpdated"> {
const defaultEvent: ProcessorEvent<"Strategy", "DistributionUpdated"> = {
params: {
metadata: ["1", "ipfs://QmTestHash"],
merkleRoot: "0xroot" as Bytes32String,
},
eventName: "DistributionUpdated",
srcAddress: "0x1234567890123456789012345678901234567890",
blockNumber: 12345,
blockTimestamp: 1000000000,
chainId: 10 as ChainId,
contractName: "Strategy",
logIndex: 1,
transactionFields: {
hash: "0xd2352acdcd59e312370831ea927d51a1917654697a72434cd905a60897a5bb8b",
transactionIndex: 6,
from: "0xcBf407C33d68a55CB594Ffc8f4fD1416Bba39DA5",
},
strategyId: "0x9fa6890423649187b1f0e8bf4265f0305ce99523c3d11aa36b35a54617bb0ec0",
};

return mergeDeep(defaultEvent, overrides);
}
import { createMockEvent } from "../../mocks/index.js";

describe("BaseDistributionUpdatedHandler", () => {
let handler: BaseDistributionUpdatedHandler;
let mockMetadataProvider: IMetadataProvider;
let mockLogger: Logger;
let mockEvent: ProcessorEvent<"Strategy", "DistributionUpdated">;
const chainId = 10 as ChainId;
const eventName = "DistributionUpdated";
const defaultParams = {
metadata: ["1", "ipfs://QmTestHash"] as [string, string],
merkleRoot: "0xroot" as Bytes32String,
};
const defaultStrategyId = "0x9fa6890423649187b1f0e8bf4265f0305ce99523c3d11aa36b35a54617bb0ec0";

beforeEach(() => {
mockMetadataProvider = {
Expand All @@ -64,7 +38,7 @@ describe("BaseDistributionUpdatedHandler", () => {
});

it("handles a valid distribution update event", async () => {
mockEvent = createMockEvent();
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
const mockDistribution = {
matchingDistribution: [
{
Expand Down Expand Up @@ -105,7 +79,7 @@ describe("BaseDistributionUpdatedHandler", () => {
});

it("throws MetadataNotFound if distribution metadata is not found", async () => {
mockEvent = createMockEvent();
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
vi.spyOn(mockMetadataProvider, "getMetadata").mockResolvedValue(undefined);

handler = new BaseDistributionUpdatedHandler(mockEvent, chainId, {
Expand All @@ -120,7 +94,7 @@ describe("BaseDistributionUpdatedHandler", () => {
});

it("throw MatchingDistributionParsingError if distribution format is invalid", async () => {
mockEvent = createMockEvent();
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
const invalidDistribution = {
matchingDistribution: [
{
Expand All @@ -145,7 +119,7 @@ describe("BaseDistributionUpdatedHandler", () => {
});

it("handles empty matching distribution array", async () => {
mockEvent = createMockEvent();
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
const emptyDistribution = {
matchingDistribution: [],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,12 @@ import {
Round,
RoundNotFound,
} from "@grants-stack-indexer/repository";
import {
ChainId,
DeepPartial,
Logger,
mergeDeep,
ProcessorEvent,
} from "@grants-stack-indexer/shared";
import { ChainId, Logger, ProcessorEvent } from "@grants-stack-indexer/shared";

import "../../../src/exceptions/index.js";

import { BaseFundsDistributedHandler } from "../../../src/internal.js";

function createMockEvent(
overrides: DeepPartial<ProcessorEvent<"Strategy", "FundsDistributed">> = {},
): ProcessorEvent<"Strategy", "FundsDistributed"> {
const defaultEvent: ProcessorEvent<"Strategy", "FundsDistributed"> = {
params: {
recipientId: "0x1234567890123456789012345678901234567890",
amount: "1000000000000000000",
grantee: "0x1234567890123456789012345678901234567890",
token: "0x0000000000000000000000000000000000000000",
},
eventName: "FundsDistributed",
srcAddress: "0x1234567890123456789012345678901234567890",
blockNumber: 12345,
blockTimestamp: 1000000000,
chainId: 10 as ChainId,
contractName: "Strategy",
logIndex: 1,
transactionFields: {
hash: "0xd2352acdcd59e312370831ea927d51a1917654697a72434cd905a60897a5bb8b",
transactionIndex: 6,
from: "0xcBf407C33d68a55CB594Ffc8f4fD1416Bba39DA5",
},
strategyId: "0x9fa6890423649187b1f0e8bf4265f0305ce99523c3d11aa36b35a54617bb0ec0",
};

return mergeDeep(defaultEvent, overrides);
}
import { createMockEvent } from "../../mocks/index.js";

describe("BaseFundsDistributedHandler", () => {
let handler: BaseFundsDistributedHandler;
Expand All @@ -55,6 +22,14 @@ describe("BaseFundsDistributedHandler", () => {
let mockLogger: Logger;
let mockEvent: ProcessorEvent<"Strategy", "FundsDistributed">;
const chainId = 10 as ChainId;
const eventName = "FundsDistributed";
const defaultParams = {
recipientId: "0x1234567890123456789012345678901234567890",
amount: "1000000000000000000",
grantee: "0x1234567890123456789012345678901234567890",
token: "0x0000000000000000000000000000000000000000",
} as const;
const defaultStrategyId = "0x9fa6890423649187b1f0e8bf4265f0305ce99523c3d11aa36b35a54617bb0ec0";

beforeEach(() => {
mockRoundRepository = {
Expand All @@ -72,7 +47,7 @@ describe("BaseFundsDistributedHandler", () => {
});

it("handles a valid funds distributed event", async () => {
mockEvent = createMockEvent();
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
const mockRound = { id: "round1" } as unknown as Round;
const mockApplication = { id: "app1" } as unknown as Application;

Expand Down Expand Up @@ -116,7 +91,7 @@ describe("BaseFundsDistributedHandler", () => {
});

it("throws RoundNotFound if round is not found", async () => {
mockEvent = createMockEvent();
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
vi.spyOn(mockRoundRepository, "getRoundByStrategyAddressOrThrow").mockRejectedValue(
new RoundNotFound(chainId, mockEvent.strategyId),
);
Expand All @@ -131,7 +106,7 @@ describe("BaseFundsDistributedHandler", () => {
});

it("throws ApplicationNotFound if application is not found", async () => {
mockEvent = createMockEvent();
mockEvent = createMockEvent(eventName, defaultParams, defaultStrategyId);
const mockRound = { id: "round1" } as unknown as Round;

vi.spyOn(mockRoundRepository, "getRoundByStrategyAddressOrThrow").mockResolvedValue(
Expand Down
Loading

0 comments on commit 41a449d

Please sign in to comment.