|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * Integration test asserting that `table.schema.columns` is forwarded to |
| 5 | + * `buildFilterClause` from each service function that filters rows. This |
| 6 | + * guards the contract that type-aware JSONB casts (numeric for numbers, |
| 7 | + * timestamp for dates) are always available at the SQL builder layer — the |
| 8 | + * latent bug that PR #4657 was originally fixing. |
| 9 | + */ |
| 10 | +import { dbChainMock, dbChainMockFns, resetDbChainMock } from '@sim/testing' |
| 11 | +import { sql } from 'drizzle-orm' |
| 12 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 13 | +import { buildFilterClause, buildSortClause } from '@/lib/table/sql' |
| 14 | +import type { ColumnDefinition, TableDefinition } from '@/lib/table/types' |
| 15 | + |
| 16 | +vi.mock('@sim/db', () => dbChainMock) |
| 17 | + |
| 18 | +vi.mock('@/lib/table/sql', () => ({ |
| 19 | + buildFilterClause: vi.fn(() => sql`true`), |
| 20 | + buildSortClause: vi.fn(() => sql`true`), |
| 21 | +})) |
| 22 | + |
| 23 | +vi.mock('@/lib/table/trigger', () => ({ |
| 24 | + fireTableTrigger: vi.fn(), |
| 25 | +})) |
| 26 | + |
| 27 | +vi.mock('@/lib/table/workflow-columns', () => ({ |
| 28 | + assertValidSchema: vi.fn(), |
| 29 | + scheduleRunsForRows: vi.fn(), |
| 30 | + scheduleRunsForTable: vi.fn(), |
| 31 | + stripGroupDeps: vi.fn(), |
| 32 | +})) |
| 33 | + |
| 34 | +vi.mock('@/lib/table/validation', () => ({ |
| 35 | + validateRowSize: vi.fn(() => ({ valid: true, errors: [] })), |
| 36 | + validateRowAgainstSchema: vi.fn(() => ({ valid: true, errors: [] })), |
| 37 | + validateTableName: vi.fn(() => ({ valid: true, errors: [] })), |
| 38 | + validateTableSchema: vi.fn(() => ({ valid: true, errors: [] })), |
| 39 | + getUniqueColumns: vi.fn(() => []), |
| 40 | + checkUniqueConstraintsDb: vi.fn(async () => ({ valid: true, errors: [] })), |
| 41 | + checkBatchUniqueConstraintsDb: vi.fn(async () => ({ valid: true, errors: [] })), |
| 42 | +})) |
| 43 | + |
| 44 | +import { deleteRowsByFilter, queryRows, updateRowsByFilter } from '@/lib/table/service' |
| 45 | + |
| 46 | +const COLUMNS: ColumnDefinition[] = [ |
| 47 | + { name: 'name', type: 'string' }, |
| 48 | + { name: 'birthDate', type: 'date' }, |
| 49 | + { name: 'score', type: 'number' }, |
| 50 | +] |
| 51 | + |
| 52 | +const TABLE: TableDefinition = { |
| 53 | + id: 'tbl-1', |
| 54 | + name: 'People', |
| 55 | + description: null, |
| 56 | + schema: { columns: COLUMNS }, |
| 57 | + metadata: null, |
| 58 | + rowCount: 0, |
| 59 | + maxRows: 1000, |
| 60 | + workspaceId: 'ws-1', |
| 61 | + createdBy: 'user-1', |
| 62 | + archivedAt: null, |
| 63 | + createdAt: new Date('2024-01-01'), |
| 64 | + updatedAt: new Date('2024-01-01'), |
| 65 | +} |
| 66 | + |
| 67 | +describe('service filter threading', () => { |
| 68 | + beforeEach(() => { |
| 69 | + vi.clearAllMocks() |
| 70 | + resetDbChainMock() |
| 71 | + }) |
| 72 | + |
| 73 | + it('queryRows forwards table.schema.columns to buildFilterClause', async () => { |
| 74 | + await queryRows( |
| 75 | + TABLE, |
| 76 | + { filter: { birthDate: { $gte: '2024-01-01' } }, includeTotal: false }, |
| 77 | + 'req-1' |
| 78 | + ).catch(() => {}) |
| 79 | + |
| 80 | + expect(buildFilterClause).toHaveBeenCalledTimes(1) |
| 81 | + expect(buildFilterClause).toHaveBeenCalledWith( |
| 82 | + { birthDate: { $gte: '2024-01-01' } }, |
| 83 | + expect.any(String), |
| 84 | + COLUMNS |
| 85 | + ) |
| 86 | + }) |
| 87 | + |
| 88 | + it('queryRows forwards columns to buildSortClause as well', async () => { |
| 89 | + await queryRows(TABLE, { sort: { birthDate: 'asc' }, includeTotal: false }, 'req-1').catch( |
| 90 | + () => {} |
| 91 | + ) |
| 92 | + |
| 93 | + expect(buildSortClause).toHaveBeenCalledWith({ birthDate: 'asc' }, expect.any(String), COLUMNS) |
| 94 | + }) |
| 95 | + |
| 96 | + it('updateRowsByFilter forwards table.schema.columns to buildFilterClause', async () => { |
| 97 | + dbChainMockFns.where.mockResolvedValueOnce([]) |
| 98 | + await updateRowsByFilter( |
| 99 | + TABLE, |
| 100 | + { filter: { birthDate: { $lt: '2024-06-01' } }, data: { name: 'x' } }, |
| 101 | + 'req-1' |
| 102 | + ) |
| 103 | + |
| 104 | + expect(buildFilterClause).toHaveBeenCalledTimes(1) |
| 105 | + expect(buildFilterClause).toHaveBeenCalledWith( |
| 106 | + { birthDate: { $lt: '2024-06-01' } }, |
| 107 | + expect.any(String), |
| 108 | + COLUMNS |
| 109 | + ) |
| 110 | + }) |
| 111 | + |
| 112 | + it('deleteRowsByFilter forwards table.schema.columns to buildFilterClause', async () => { |
| 113 | + dbChainMockFns.where.mockResolvedValueOnce([]) |
| 114 | + await deleteRowsByFilter(TABLE, { filter: { score: { $gt: 90 } } }, 'req-1') |
| 115 | + |
| 116 | + expect(buildFilterClause).toHaveBeenCalledTimes(1) |
| 117 | + expect(buildFilterClause).toHaveBeenCalledWith( |
| 118 | + { score: { $gt: 90 } }, |
| 119 | + expect.any(String), |
| 120 | + COLUMNS |
| 121 | + ) |
| 122 | + }) |
| 123 | +}) |
0 commit comments