Skip to content

Implement CTEInjector with fixture support for SQL testing #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented May 20, 2025

This PR implements CTE injection capabilities to enable SQL unit testing without a live database. The implementation allows injecting test data fixtures as Common Table Expressions (CTEs) into SQL queries, making it possible to test the calculation logic of queries with deterministic test data.

Key Features

  • Added two new methods to CTEInjector class:

    • withFixtures(sql, deps, fixtures, options): Injects test fixtures as CTEs with explicit VALUES rows
    • withNullScaffolding(sql, deps, options): Injects NULL-based scaffolding CTEs (for backward compatibility)
  • Support for various data types in fixtures:

    • Strings (with proper escaping for single quotes)
    • Numbers
    • Booleans
    • Dates
    • Objects (serialized to JSON)
    • NULL values
  • Column handling:

    • Uses SchemaCollector's column list for ordering
    • Fills missing columns with NULL values
    • Supports identifier quoting with the quoteIdentifiers option

Usage Example

// SQL query under test
const sql = `
  SELECT u.name, SUM(o.total) AS sum_total
  FROM users u
  JOIN orders o ON o.user_id = u.id
  GROUP BY u.name
`;

// Collect dependencies
const deps = new SchemaCollector().collect(SelectQueryParser.parse(sql));

// Provide fixtures
const fixtures = {
  users: [
    { id: 1, name: 'mike' },
    { id: 2, name: 'ken' },
  ],
  orders: [
    { user_id: 1, total: 100 },
    { user_id: 1, total: 500 },
    { user_id: 2, total: 300 },
  ],
};

// Build final SQL with injected CTEs
const injector = new CTEInjector();
const testSQL = injector.withFixtures(sql, deps, fixtures);

// Now testSQL contains a query with built-in test data in CTEs

Documentation Updates

  • Added CTEInjector documentation to the Main Transformers section in README
  • Added a complete example showing how to use CTEInjector with fixtures
  • Added export for CTEInjector in the main index.ts

Testing

  • Comprehensive test suite ensures all functionality works correctly
  • Tests cover various edge cases like empty fixtures, NULL values, different data types, and multiple tables

Fixes #99.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits May 20, 2025 13:06
Co-authored-by: mk3008 <7686540+mk3008@users.noreply.github.com>
Co-authored-by: mk3008 <7686540+mk3008@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] CTE injection Implement CTEInjector with fixture support for SQL testing May 20, 2025
@Copilot Copilot AI requested a review from mk3008 May 20, 2025 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CTE injection
2 participants