Skip to content

Commit

Permalink
Update tests for stored DmlHandler Instance
Browse files Browse the repository at this point in the history
  • Loading branch information
darunrs committed Mar 21, 2024
1 parent 0bda419 commit 6bf7b4a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
61 changes: 33 additions & 28 deletions runner/src/indexer/indexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,16 +455,17 @@ CREATE TABLE
});

test('indexer builds context and inserts an objects into existing table', async () => {
const mockDmlHandlerInstance: any = { insert: jest.fn().mockReturnValue([{ colA: 'valA' }, { colA: 'valA' }]) };
const mockDmlHandler: any = {
create: jest.fn().mockImplementation(() => {
return { insert: jest.fn().mockReturnValue([{ colA: 'valA' }, { colA: 'valA' }]) };
return mockDmlHandlerInstance;
})
};

const indexer = new Indexer(defaultIndexerBehavior, {
fetch: genericMockFetch as unknown as typeof fetch,
DmlHandler: mockDmlHandler
}, genericDbCredentials);
}, genericDbCredentials, mockDmlHandlerInstance);
const context = indexer.buildContext(SOCIAL_SCHEMA, 'morgs.near/social_feed1', 1, 'postgres');

const objToInsert = [{
Expand Down Expand Up @@ -493,15 +494,15 @@ CREATE TABLE
query: jest.fn().mockReturnValue({ rows: [] }),
format: jest.fn().mockReturnValue('mock')
} as unknown as PgClient;
const dmlHandlerInstance = DmlHandler.create(genericDbCredentials, mockPgClient);
const dmlHandlerInstance: any = DmlHandler.create(genericDbCredentials, mockPgClient);
const upsertSpy = jest.spyOn(dmlHandlerInstance, 'upsert');
const mockDmlHandler: any = {
create: jest.fn().mockReturnValue(dmlHandlerInstance)
};
const indexer = new Indexer(defaultIndexerBehavior, {
fetch: genericMockFetch as unknown as typeof fetch,
DmlHandler: mockDmlHandler
}, genericDbCredentials);
}, genericDbCredentials, dmlHandlerInstance);
const context = indexer.buildContext(SOCIAL_SCHEMA, 'morgs.near/social_feed1', 1, 'postgres');
const promises = [];

Expand Down Expand Up @@ -531,16 +532,17 @@ CREATE TABLE
// Expects limit to be last parameter
return args[args.length - 1] === null ? [{ colA: 'valA' }, { colA: 'valA' }] : [{ colA: 'valA' }];
});
const mockDmlHandlerInstance: any = { select: selectFn };
const mockDmlHandler: any = {
create: jest.fn().mockImplementation(() => {
return { select: selectFn };
return mockDmlHandlerInstance;
})
};

const indexer = new Indexer(defaultIndexerBehavior, {
fetch: genericMockFetch as unknown as typeof fetch,
DmlHandler: mockDmlHandler
}, genericDbCredentials);
}, genericDbCredentials, mockDmlHandlerInstance);
const context = indexer.buildContext(SOCIAL_SCHEMA, 'morgs.near/social_feed1', 1, 'postgres');

const objToSelect = {
Expand All @@ -554,23 +556,24 @@ CREATE TABLE
});

test('indexer builds context and updates multiple objects from existing table', async () => {
const mockDmlHandlerInstance: any = {
update: jest.fn().mockImplementation((_, __, whereObj, updateObj) => {
if (whereObj.account_id === 'morgs_near' && updateObj.content === 'test_content') {
return [{ colA: 'valA' }, { colA: 'valA' }];
}
return [{}];
})
};
const mockDmlHandler: any = {
create: jest.fn().mockImplementation(() => {
return {
update: jest.fn().mockImplementation((_, __, whereObj, updateObj) => {
if (whereObj.account_id === 'morgs_near' && updateObj.content === 'test_content') {
return [{ colA: 'valA' }, { colA: 'valA' }];
}
return [{}];
})
};
return mockDmlHandlerInstance;
})
};

const indexer = new Indexer(defaultIndexerBehavior, {
fetch: genericMockFetch as unknown as typeof fetch,
DmlHandler: mockDmlHandler
}, genericDbCredentials);
}, genericDbCredentials, mockDmlHandlerInstance);
const context = indexer.buildContext(SOCIAL_SCHEMA, 'morgs.near/social_feed1', 1, 'postgres');

const whereObj = {
Expand All @@ -586,25 +589,26 @@ CREATE TABLE
});

test('indexer builds context and upserts on existing table', async () => {
const mockDmlHandlerInstance: any = {
upsert: jest.fn().mockImplementation((_, __, objects, conflict, update) => {
if (objects.length === 2 && conflict.includes('account_id') && update.includes('content')) {
return [{ colA: 'valA' }, { colA: 'valA' }];
} else if (objects.length === 1 && conflict.includes('account_id') && update.includes('content')) {
return [{ colA: 'valA' }];
}
return [{}];
})
};
const mockDmlHandler: any = {
create: jest.fn().mockImplementation(() => {
return {
upsert: jest.fn().mockImplementation((_, __, objects, conflict, update) => {
if (objects.length === 2 && conflict.includes('account_id') && update.includes('content')) {
return [{ colA: 'valA' }, { colA: 'valA' }];
} else if (objects.length === 1 && conflict.includes('account_id') && update.includes('content')) {
return [{ colA: 'valA' }];
}
return [{}];
})
};
return mockDmlHandlerInstance;
})
};

const indexer = new Indexer(defaultIndexerBehavior, {
fetch: genericMockFetch as unknown as typeof fetch,
DmlHandler: mockDmlHandler
}, genericDbCredentials);
}, genericDbCredentials, mockDmlHandlerInstance);
const context = indexer.buildContext(SOCIAL_SCHEMA, 'morgs.near/social_feed1', 1, 'postgres');

const objToInsert = [{
Expand All @@ -631,16 +635,17 @@ CREATE TABLE
});

test('indexer builds context and deletes objects from existing table', async () => {
const mockDmlHandlerInstance: any = { delete: jest.fn().mockReturnValue([{ colA: 'valA' }, { colA: 'valA' }]) };
const mockDmlHandler: any = {
create: jest.fn().mockImplementation(() => {
return { delete: jest.fn().mockReturnValue([{ colA: 'valA' }, { colA: 'valA' }]) };
return mockDmlHandlerInstance;
})
};

const indexer = new Indexer(defaultIndexerBehavior, {
fetch: genericMockFetch as unknown as typeof fetch,
DmlHandler: mockDmlHandler
}, genericDbCredentials);
}, genericDbCredentials, mockDmlHandlerInstance);
const context = indexer.buildContext(SOCIAL_SCHEMA, 'morgs.near/social_feed1', 1, 'postgres');

const deleteFilter = {
Expand Down
2 changes: 1 addition & 1 deletion runner/src/indexer/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class Indexer {

const simultaneousPromises: Array<Promise<any>> = [];
const allMutations: string[] = [];

for (const functionName in functions) {
try {
const indexerFunction = functions[functionName];
Expand Down

0 comments on commit 6bf7b4a

Please sign in to comment.