|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { PgMetastore } from './pg' |
| 3 | + |
| 4 | +function createMetastore(query: ReturnType<typeof vi.fn>) { |
| 5 | + return new PgMetastore({ query } as never, { |
| 6 | + multiTenant: false, |
| 7 | + schema: 'storage', |
| 8 | + }) |
| 9 | +} |
| 10 | + |
| 11 | +function getLastStatement(query: ReturnType<typeof vi.fn>): string { |
| 12 | + const [statement] = query.mock.calls.at(-1) || [] |
| 13 | + |
| 14 | + if (!statement) { |
| 15 | + throw new Error('No query calls found') |
| 16 | + } |
| 17 | + |
| 18 | + if (typeof statement === 'string') { |
| 19 | + return statement |
| 20 | + } |
| 21 | + |
| 22 | + if (typeof statement === 'object' && 'text' in statement) { |
| 23 | + return String(statement.text) |
| 24 | + } |
| 25 | + |
| 26 | + throw new Error('Expected a PgStatement query with text property') |
| 27 | +} |
| 28 | + |
| 29 | +describe('PgMetastore.countCatalogs', () => { |
| 30 | + it('excludes soft-deleted catalogs by default', async () => { |
| 31 | + const query = vi.fn().mockResolvedValue({ rows: [{ count: 5 }] }) |
| 32 | + const metastore = createMetastore(query) |
| 33 | + |
| 34 | + await metastore.countCatalogs({ |
| 35 | + tenantId: 'tenant-id', |
| 36 | + limit: 100, |
| 37 | + }) |
| 38 | + |
| 39 | + const statement = getLastStatement(query) |
| 40 | + expect(statement).toContain('deleted_at IS NULL') |
| 41 | + }) |
| 42 | + |
| 43 | + it('includes soft-deleted catalogs when deleted parameter is true', async () => { |
| 44 | + const query = vi.fn().mockResolvedValue({ rows: [{ count: 10 }] }) |
| 45 | + const metastore = createMetastore(query) |
| 46 | + |
| 47 | + await metastore.countCatalogs({ |
| 48 | + tenantId: 'tenant-id', |
| 49 | + limit: 100, |
| 50 | + deleted: true, |
| 51 | + }) |
| 52 | + |
| 53 | + const statement = getLastStatement(query) |
| 54 | + expect(statement).not.toContain('deleted_at IS NULL') |
| 55 | + }) |
| 56 | +}) |
0 commit comments