Mirror of https://github.com/roostorg/coop github.com/roostorg/coop
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 76 lines 2.5 kB view raw
1import { Kysely, PostgresDialect, type PostgresQueryResult } from 'kysely'; 2 3import { takeLast } from './sql.js'; 4 5function makeCompileOnlyDb<T extends Record<string, Record<string, unknown>>>() { 6 return new Kysely<T>({ 7 dialect: new PostgresDialect({ 8 pool: { 9 async connect() { 10 return { 11 query: jest.fn().mockResolvedValue({ 12 rows: [], 13 command: 'SELECT', 14 rowCount: 0, 15 } as PostgresQueryResult<unknown>), 16 async release() {}, 17 }; 18 }, 19 async end() {}, 20 }, 21 }), 22 }); 23} 24 25describe('Sql Helpers', () => { 26 describe('takeLast', () => { 27 test('should work for simple queries', () => { 28 type User = { id: string; name: string; email: string }; 29 type TestDb = { users: User }; 30 31 const db = makeCompileOnlyDb<TestDb>(); 32 const users = db.selectFrom('users').select(['id', 'name']); 33 34 const result = takeLast(db, users, [{ column: 'id', order: 'desc' }], 2); 35 36 expect(result.compile().sql).toEqual( 37 'select * from (select "id", "name" from "users" order by "id" asc limit $1) as "dc2d41a9-082e-48b0-a66f-345a22696b02" order by "id" desc', 38 ); 39 expect(result.compile().parameters).toEqual([2]); 40 }); 41 42 test('should work for arbitrarily complex queries', () => { 43 type RuleExecRow = { 44 ORG_ID: string; 45 TS: string; 46 CONTENT: string; 47 CORRELATION_ID: string; 48 }; 49 type TestDb = { RULE_EXECUTIONS: RuleExecRow }; 50 51 const db = makeCompileOnlyDb<TestDb>(); 52 const backtestResults = db 53 .selectFrom('RULE_EXECUTIONS') 54 .select([ 55 'ORG_ID as orgId', 56 'TS as ts', 57 'CONTENT as content', 58 'CORRELATION_ID as correlationId', 59 ]) 60 .where('CORRELATION_ID', '=', '47') 61 .where('TS', '>', '2019-01-01'); 62 63 const result = takeLast( 64 db, 65 backtestResults, 66 [{ column: 'ts', order: 'asc' }], 67 50, 68 ); 69 70 expect(result.compile().sql).toMatchInlineSnapshot( 71 `"select * from (select "ORG_ID" as "orgId", "TS" as "ts", "CONTENT" as "content", "CORRELATION_ID" as "correlationId" from "RULE_EXECUTIONS" where "CORRELATION_ID" = $1 and "TS" > $2 order by "ts" desc limit $3) as "dc2d41a9-082e-48b0-a66f-345a22696b02" order by "ts" asc"`, 72 ); 73 expect(result.compile().parameters).toEqual(['47', '2019-01-01', 50]); 74 }); 75 }); 76});