Skip to content

Commit

Permalink
test: Add test cases for ValidatorNodeProviderCreated events.
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomenezes committed Jan 25, 2024
1 parent 90d8ccc commit 86ca576
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/handlers/ValidatorNodeProviderCreated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default class ValidatorNodeProviderCreated implements Handler {
new ValidatorNodeProvider({
id: providerId,
token: tokenInstance,
paused: false,
authority,
payee: payee.toLowerCase(),
price,
Expand Down
136 changes: 136 additions & 0 deletions tests/handlers/ValidatorNodeProviderCreated.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { EntityClass, FindOneOptions } from '@subsquid/typeorm-store';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import ValidatorNodeProviderCreated from '../../src/handlers/ValidatorNodeProviderCreated';
import TokenHelper from '../../src/handlers/helpers/TokenHelper';
import { Authority, Token, ValidatorNodeProvider } from '../../src/model';
import {
Logs,
TokenAddress,
ValidatorNodeProviderAddress,
authority,
blockData,
ctx,
token,
validatorNodeProvider,
} from '../stubs/validatorNodeProvider';

vi.mock('../../src/model/', async () => {
const ValidatorNodeProvider = vi.fn();
const Authority = vi.fn();
const Token = vi.fn();

return {
ValidatorNodeProvider,
Authority,
Token,
};
});

const ValidatorNodeProviderMock = vi.mocked(ValidatorNodeProvider);
const AuthorityMock = vi.mocked(Authority);

describe('ValidatorNodeProviderCreated', () => {
let handler: ValidatorNodeProviderCreated;
const providerStorage = new Map<string, ValidatorNodeProvider>();
const tokenStorage = new Map<string, Token>();
const authorityStorage = new Map<string, Authority>();
const expectedProvider = {
authority: {
id: '0x83e4283f7eab201b06f749f683f27cfda294ab81',
},
id: '0x4d22c1f970574ae7b8724457d268d41e6459e288',
paused: false,
payee: '0xd8464d1b3592b6c3786b32931e2a2adac501aaad',
price: 400000000000000n,
token: {
decimals: 18,
id: '0xe15e2add14c26b9ae1e735bf5b444ccb11b0bd15',
name: 'SunodoToken',
symbol: 'SUN',
},
};

beforeEach(() => {
ValidatorNodeProviderMock.mockImplementation(
(args) => ({ ...args } as ValidatorNodeProvider),
);
AuthorityMock.mockImplementation((args) => ({ ...args } as Authority));

handler = new ValidatorNodeProviderCreated(
authorityStorage,
providerStorage,
tokenStorage,
);
});

afterEach(() => {
vi.clearAllMocks();
providerStorage.clear();
authorityStorage.clear();
tokenStorage.clear();
});

it('should create the validator node with correct info including cache data', async () => {
authorityStorage.set(authority.id, authority);
tokenStorage.set(token.id, token);

await handler.handle(Logs.created, blockData, ctx);

const provider = providerStorage.get(validatorNodeProvider.id);

expect(provider).toEqual(expectedProvider);
});

it('should create provider with correct information including authority and token info from database', async () => {
vi.spyOn(ctx.store, 'get').mockImplementation(
async (
entityClass: EntityClass<any>,
id: FindOneOptions<any> | string,
): Promise<any | undefined> => {
console.log(`(ClassName): ${entityClass === Authority}`);
if (entityClass === Authority) return { id };
if (entityClass === Token) return token;

return undefined;
},
);

await handler.handle(Logs.created, blockData, ctx);

expect(providerStorage.size).toBe(1);
const provider = providerStorage.get(ValidatorNodeProviderAddress);

expect(provider).toEqual(expectedProvider);
});

it('should create an authority when it is not indexed yet', async () => {
tokenStorage.set(token.id, token);
vi.spyOn(ctx.store, 'get').mockResolvedValue(undefined);

expect(authorityStorage.size).toEqual(0);
expect(providerStorage.size).toEqual(0);

await handler.handle(Logs.created, blockData, ctx);

expect(authorityStorage.size).toEqual(1);
expect(providerStorage.size).toEqual(1);
});

it('should create a Token when it is not indexed yet', async () => {
authorityStorage.set(authority.id, authority);
vi.spyOn(TokenHelper, 'createToken').mockResolvedValue(token);

expect(tokenStorage.size).toEqual(0);

await handler.handle(Logs.created, blockData, ctx);

expect(tokenStorage.size).toEqual(1);
expect(providerStorage.size).toEqual(1);
const expectedToken = tokenStorage.get(TokenAddress);
const expectedProvider = providerStorage.get(
ValidatorNodeProviderAddress,
);

expect(expectedToken).toEqual(expectedProvider?.token);
});
});
19 changes: 11 additions & 8 deletions tests/stubs/validatorNodeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { vi } from 'vitest';
import { events as MarketplaceEvents } from '../../src/abi/Marketplace';
import { events as ValidatorNodeProviderEvents } from '../../src/abi/ValidatorNodeProvider';
import { MarketplaceAddress } from '../../src/config';
import { ValidatorNodeProvider } from '../../src/model';
import { Authority, Token, ValidatorNodeProvider } from '../../src/model';

export const TokenAddress =
'0xE15E2ADD14c26b9ae1E735bF5B444CCB11B0bd15'.toLowerCase();
Expand All @@ -16,16 +16,19 @@ export const AuthorityAddress =
export const ValidatorNodeProviderAddress =
'0x4d22c1F970574ae7B8724457D268D41E6459E288'.toLowerCase();

export const authority = { id: AuthorityAddress } satisfies Authority;
export const token = {
id: TokenAddress,
decimals: 18,
name: 'SunodoToken',
symbol: 'SUN',
} satisfies Token;

export const validatorNodeProvider = {
id: ValidatorNodeProviderAddress,
authority: { id: AuthorityAddress },
authority,
payee: PayeeAddress,
token: {
id: TokenAddress,
decimals: 18,
name: 'SunodoToken',
symbol: 'SUN',
},
token,
price: 400000000000000n,
paused: false,
nodes: [],
Expand Down

0 comments on commit 86ca576

Please sign in to comment.