Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix TypeScript errors: reduced from 308 to 66 errors with systematic …
…fixes
  • Loading branch information
cursoragent committed Aug 3, 2025
commit 301f74a6922df438445172af3f3dd34d4aa4a678
136 changes: 72 additions & 64 deletions packages/db/tests/property-testing/comprehensive-sql-coverage.test.ts

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions packages/db/tests/property-testing/framework-unit-tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ describe(`Property-Based Testing Framework`, () => {
const schema = await fc.sample(schemaArb, 1)[0]

expect(schema).toBeDefined()
expect(schema.tables).toBeInstanceOf(Array)
expect(schema.tables.length).toBeGreaterThan(0)
expect(schema.tables.length).toBeLessThanOrEqual(2)
expect(schema!.tables).toBeInstanceOf(Array)
expect(schema!.tables.length).toBeGreaterThan(0)
expect(schema!.tables.length).toBeLessThanOrEqual(2)

for (const table of schema.tables) {
for (const table of schema!.tables) {
expect(table.name).toBeDefined()
expect(table.columns).toBeInstanceOf(Array)
expect(table.columns.length).toBeGreaterThan(0)
Expand All @@ -60,7 +60,7 @@ describe(`Property-Based Testing Framework`, () => {
const schemaArb = generateSchema({ maxTables: 2, maxColumns: 4 })
const schema = await fc.sample(schemaArb, 1)[0]

if (schema.tables.length >= 2) {
if (schema!.tables.length >= 2) {
// Should have some join hints if there are multiple tables
expect(schema.joinHints).toBeInstanceOf(Array)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,16 @@ function generateJoinHints(tables: Array<TableDef>): TestSchema[`joinHints`] {
const table2 = tables[j]

// Find joinable columns with matching types
const joinableColumns1 = table1.columns.filter((col) => col.isJoinable)
const joinableColumns2 = table2.columns.filter((col) => col.isJoinable)
const joinableColumns1 = table1!.columns.filter((col) => col.isJoinable)
const joinableColumns2 = table2!.columns.filter((col) => col.isJoinable)

for (const col1 of joinableColumns1) {
for (const col2 of joinableColumns2) {
if (col1.type === col2.type) {
hints.push({
table1: table1.name,
table1: table1!.name,
column1: col1.name,
table2: table2.name,
table2: table2!.name,
column2: col2.name,
})
}
Expand Down
24 changes: 12 additions & 12 deletions packages/db/tests/property-testing/harness/property-test-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export class PropertyTestHarness {
const schema = await fc.sample(schemaArb, 1)[0]

// Initialize test state
const state = await this.initializeTestState(schema, seed)
const state = await this.initializeTestState(schema!, seed)

// Generate test commands
const commands = await this.generateTestCommands(schema)
const commands = await this.generateTestCommands(schema!)

// Execute commands and collect results
const result = await this.executeTestSequence(state, commands, seed)
Expand Down Expand Up @@ -92,7 +92,7 @@ export class PropertyTestHarness {

// Execute commands
for (let i = 0; i < commands.length; i++) {
const command = commands[i]
const command = commands[i]!
state.commandCount++
results.commandCount = state.commandCount

Expand Down Expand Up @@ -310,21 +310,21 @@ export class PropertyTestHarness {
) {
// Coverage is always initialized, so this check is unnecessary

if (ast.select) coverage.select++
if (ast.where && ast.where.length > 0) coverage.where++
if (ast.join && ast.join.length > 0) coverage.join++
if (ast.orderBy && ast.orderBy.length > 0) coverage.orderBy++
if (ast.groupBy && ast.groupBy.length > 0) coverage.groupBy++
if (ast.select) coverage!.select++
if (ast.where && ast.where.length > 0) coverage!.where++
if (ast.join && ast.join.length > 0) coverage!.join++
if (ast.orderBy && ast.orderBy.length > 0) coverage!.orderBy++
if (ast.groupBy && ast.groupBy.length > 0) coverage!.groupBy++

// Check for aggregates in select
if (ast.select) {
for (const expr of Object.values(ast.select)) {
if (expr.type === `agg`) coverage.aggregate++
if (expr.type === `agg`) coverage!.aggregate++
}
}

// Check for subqueries in from
if (ast.from.type === `queryRef`) coverage.subquery++
if (ast.from.type === `queryRef`) coverage!.subquery++
}

/**
Expand Down Expand Up @@ -375,7 +375,7 @@ export class PropertyTestHarness {
const checker = new IncrementalChecker(state, this.config)

for (let i = 0; i < commands.length; i++) {
const command = commands[i]
const command = commands[i]!
state.commandCount++

const result = await checker.executeCommand(command)
Expand Down Expand Up @@ -482,7 +482,7 @@ export class PropertyTestHarness {
const checker = new IncrementalChecker(state, this.config)

for (let i = 0; i < commands.length; i++) {
const command = commands[i]
const command = commands[i]!
state.commandCount++

const result = await checker.executeCommand(command)
Expand Down
76 changes: 39 additions & 37 deletions packages/db/tests/property-testing/ir-to-sql-translation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ describe(`IR to SQL Translation`, () => {
// Generate a simple schema
const schemaArb = generateSchema({ maxTables: 1, maxColumns: 3 })
const schema = await fc.sample(schemaArb, 1)[0]
if (!schema) throw new Error(`Failed to generate schema`)

const table = schema.tables[0]
if (!table) throw new Error(`No tables in schema`)
const tableName = table.name

// Create SQLite database
Expand All @@ -44,7 +46,7 @@ describe(`IR to SQL Translation`, () => {
)[0]

// Insert into SQLite
for (const row of testRows) {
for (const row of testRows!) {
sqliteDb.insert(tableName, row)
}

Expand Down Expand Up @@ -74,44 +76,44 @@ describe(`IR to SQL Translation`, () => {
const sqliteResult = sqliteDb.query(sql, params)

// Verify we get the expected number of rows
expect(sqliteResult.length).toBe(testRows.length)
expect(sqliteResult.length).toBe(testRows!.length)
})

it(`should translate WHERE clause queries correctly`, async () => {
// Generate a simple schema
const schemaArb = generateSchema({ maxTables: 1, maxColumns: 3 })
const schema = await fc.sample(schemaArb, 1)[0]

const table = schema.tables[0]
const tableName = table.name
const table = schema!.tables[0]
const tableName = table!.name

// Create SQLite database
const sqliteDb = createTempDatabase()
sqliteDb.initialize(schema)
sqliteDb.initialize(schema!)

// Create TanStack collection
const collection = createCollection(
mockSyncCollectionOptions({
id: tableName,
getKey: (item: any) => item[table.primaryKey],
getKey: (item: any) => item[table!.primaryKey],
initialData: [],
autoIndex: `eager`,
})
)

// Generate and insert test data
const testRows = await fc.sample(
generateRowsForTable(table, { minRows: 10, maxRows: 20 }),
generateRowsForTable(table!, { minRows: 10, maxRows: 20 }),
1
)[0]

// Insert into SQLite
for (const row of testRows) {
for (const row of testRows!) {
sqliteDb.insert(tableName, row)
}

// Find a string column for WHERE clause
const stringColumn = table.columns.find(
const stringColumn = table!.columns.find(
(col) => col.type === `string` && !col.isPrimaryKey
)
if (!stringColumn) {
Expand All @@ -120,15 +122,15 @@ describe(`IR to SQL Translation`, () => {

// Get a sample value for the WHERE clause
const sampleValue =
testRows.find((row) => row[stringColumn.name] !== undefined)?.[
testRows!.find((row) => row[stringColumn.name] !== undefined)?.[
stringColumn.name
] || `test`

// Create IR for WHERE clause
const whereIR = {
from: new CollectionRef(collection as any, tableName),
select: {
[table.primaryKey]: new PropRef([tableName, table.primaryKey]),
[table!.primaryKey]: new PropRef([tableName, table!.primaryKey]),
[stringColumn.name]: new PropRef([tableName, stringColumn.name]),
},
where: [
Expand All @@ -154,44 +156,44 @@ describe(`IR to SQL Translation`, () => {

// Verify we get filtered results
expect(sqliteResult.length).toBeGreaterThanOrEqual(0)
expect(sqliteResult.length).toBeLessThanOrEqual(testRows.length)
expect(sqliteResult.length).toBeLessThanOrEqual(testRows!.length)
})

it(`should translate ORDER BY queries correctly`, async () => {
// Generate a simple schema
const schemaArb = generateSchema({ maxTables: 1, maxColumns: 3 })
const schema = await fc.sample(schemaArb, 1)[0]

const table = schema.tables[0]
const tableName = table.name
const table = schema!.tables[0]
const tableName = table!.name

// Create SQLite database
const sqliteDb = createTempDatabase()
sqliteDb.initialize(schema)
sqliteDb.initialize(schema!)

// Create TanStack collection
const collection = createCollection(
mockSyncCollectionOptions({
id: tableName,
getKey: (item: any) => item[table.primaryKey],
getKey: (item: any) => item[table!.primaryKey],
initialData: [],
autoIndex: `eager`,
})
)

// Generate and insert test data
const testRows = await fc.sample(
generateRowsForTable(table, { minRows: 10, maxRows: 20 }),
generateRowsForTable(table!, { minRows: 10, maxRows: 20 }),
1
)[0]

// Insert into SQLite
for (const row of testRows) {
for (const row of testRows!) {
sqliteDb.insert(tableName, row)
}

// Find a sortable column
const sortColumn = table.columns.find(
const sortColumn = table!.columns.find(
(col) => col.type === `string` || col.type === `number`
)
if (!sortColumn) {
Expand All @@ -202,7 +204,7 @@ describe(`IR to SQL Translation`, () => {
const orderByIR = {
from: new CollectionRef(collection as any, tableName),
select: {
[table.primaryKey]: new PropRef([tableName, table.primaryKey]),
[table!.primaryKey]: new PropRef([tableName, table!.primaryKey]),
[sortColumn.name]: new PropRef([tableName, sortColumn.name]),
},
orderBy: [
Expand All @@ -226,39 +228,39 @@ describe(`IR to SQL Translation`, () => {
const sqliteResult = sqliteDb.query(sql, params)

// Verify we get all rows
expect(sqliteResult.length).toBe(testRows.length)
expect(sqliteResult.length).toBe(testRows!.length)
})

it(`should translate aggregate functions correctly`, async () => {
// Generate a simple schema
const schemaArb = generateSchema({ maxTables: 1, maxColumns: 3 })
const schema = await fc.sample(schemaArb, 1)[0]

const table = schema.tables[0]
const tableName = table.name
const table = schema!.tables[0]
const tableName = table!.name

// Create SQLite database
const sqliteDb = createTempDatabase()
sqliteDb.initialize(schema)
sqliteDb.initialize(schema!)

// Create TanStack collection
const collection = createCollection(
mockSyncCollectionOptions({
id: tableName,
getKey: (item: any) => item[table.primaryKey],
getKey: (item: any) => item[table!.primaryKey],
initialData: [],
autoIndex: `eager`,
})
)

// Generate and insert test data
const testRows = await fc.sample(
generateRowsForTable(table, { minRows: 10, maxRows: 20 }),
generateRowsForTable(table!, { minRows: 10, maxRows: 20 }),
1
)[0]

// Insert into SQLite
for (const row of testRows) {
for (const row of testRows!) {
sqliteDb.insert(tableName, row)
}

Expand All @@ -284,47 +286,47 @@ describe(`IR to SQL Translation`, () => {
// Verify we get a count result
expect(sqliteResult.length).toBe(1)
expect(sqliteResult[0]).toHaveProperty(`count`)
expect(Number(sqliteResult[0].count)).toBe(testRows.length)
expect(Number(sqliteResult[0].count)).toBe(testRows!.length)
})

it(`should translate complex queries with multiple clauses`, async () => {
// Generate a simple schema
const schemaArb = generateSchema({ maxTables: 1, maxColumns: 4 })
const schema = await fc.sample(schemaArb, 1)[0]

const table = schema.tables[0]
const tableName = table.name
const table = schema!.tables[0]
const tableName = table!.name

// Create SQLite database
const sqliteDb = createTempDatabase()
sqliteDb.initialize(schema)
sqliteDb.initialize(schema!)

// Create TanStack collection
const collection = createCollection(
mockSyncCollectionOptions({
id: tableName,
getKey: (item: any) => item[table.primaryKey],
getKey: (item: any) => item[table!.primaryKey],
initialData: [],
autoIndex: `eager`,
})
)

// Generate and insert test data
const testRows = await fc.sample(
generateRowsForTable(table, { minRows: 20, maxRows: 50 }),
generateRowsForTable(table!, { minRows: 20, maxRows: 50 }),
1
)[0]

// Insert into SQLite
for (const row of testRows) {
for (const row of testRows!) {
sqliteDb.insert(tableName, row)
}

// Find columns for complex query
const stringColumn = table.columns.find(
const stringColumn = table!.columns.find(
(col) => col.type === `string` && !col.isPrimaryKey
)
const numericColumn = table.columns.find(
const numericColumn = table!.columns.find(
(col) => col.type === `number` && !col.isPrimaryKey
)

Expand All @@ -336,7 +338,7 @@ describe(`IR to SQL Translation`, () => {
const complexIR = {
from: new CollectionRef(collection as any, tableName),
select: {
[table.primaryKey]: new PropRef([tableName, table.primaryKey]),
[table!.primaryKey]: new PropRef([tableName, table!.primaryKey]),
[stringColumn.name]: new PropRef([tableName, stringColumn.name]),
[numericColumn.name]: new PropRef([tableName, numericColumn.name]),
},
Expand Down
Loading
Loading