Skip to content

Conversation

@tobyhede
Copy link
Contributor

@tobyhede tobyhede commented Oct 26, 2025

Summary

This PR introduces a comprehensive Rust/SQLx testing infrastructure to replace the existing pgTAP framework. This is the foundation for migrating EQL tests to a more maintainable, type-safe testing approach.

Part 1 of 3 in the test migration series:

  • PR 1 (this): Infrastructure and tooling
  • PR 2: JSONB operator tests
  • PR 3: Equality operator tests

What's Changed

Core Test Framework

  • SQLx Crate (tests/sqlx/): New Rust test crate with SQLx integration
  • Fluent Assertion API: Ergonomic query assertion builder for readable tests
  • Selector Constants: Type-safe constants replacing magic string literals
  • Test Fixtures: SQL data seeding files for consistent test setup
  • Migrations: SQLx migrations for EQL installation and test helper functions

Tooling & Scripts

  • Assertion Tracking: Count and compare test assertions across migrations
  • Coverage Analysis: Function call tracking to measure test coverage
  • Test Inventory: Auto-generated documentation of test cases
  • Master Coverage Check: Unified script validating all coverage metrics
  • Mise Tasks: Integrated test execution and coverage commands

Configuration Updates

  • Cargo Workspace: Added tests/sqlx to workspace for unified builds
  • Docker Compose: Enabled track_functions for coverage tracking
  • Gitignore: Added generated migration and documentation files

Testing Infrastructure

The new framework provides:

// Fluent assertions for readable tests
query("SELECT some_function($1)")
    .bind(value)
    .assert_equals("expected")
    .await?;

// Type-safe selectors
use selectors::SELECTOR_CT;  // Instead of "$.ct"

Implement pgTAP test framework to provide industry-standard PostgreSQL testing capabilities alongside existing custom assertion helpers.

Changes:
- Add pgTAP to Docker containers (PostgreSQL 14-17)
- Create pgTAP test runner script (tasks/test-pgtap.sh)
- Add structure tests: schema, types, functions, operators
- Add functionality tests: equality operators
- Update docker-compose.yml to build custom images with pgTAP

Structure tests verify EQL schema elements exist with correct signatures.
Functionality tests validate encrypted data equality operations using pgTAP assertions.

This implements Tasks 1-3 from docs/pgtap-implementation-plan.md.
Improvements based on plan review:

- Add Task 0.5: Verify prerequisites (Rust, mise, Docker, psql)
- Add Success Criteria section with clear metrics and rollback strategy
- Improve Task 3, Step 3: Add function signature verification for selectors
- Enhance Task 12: Better error messages in Rust helper with unwrap_or_else
- Document Task 4: Add fixture dependency comments (array_data → encrypted_json)
- Add test coverage metric: 16 tests from 2 source files

These changes improve plan clarity, verification steps, and error handling
while maintaining the POC scope.
@tobyhede tobyhede force-pushed the feature/sqlx-infrastructure branch from 0e53726 to fed5efc Compare October 26, 2025 22:25
Copy link
Contributor

@freshtonic freshtonic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks like a big uplift.

Left some comments/questions on the code.

One thing I found confusing was the tests dir - which in a cargo project is usually for the integration tests of the containing crate, but tests actually defines a new crate called eql_tests. Not a hill I'll die on but it confused me and also Claude.

let rows = sqlx::query(&self.sql)
.fetch_all(self.pool)
.await
.unwrap_or_else(|_| panic!("Query failed: {}", self.sql));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The panic message for query failures could include the error:

  // Current
  .unwrap_or_else(|_| panic!("Query failed: {}", self.sql));
  // Suggestion
  .unwrap_or_else(|e| panic!("Query failed: {}\nError: {}", self.sql, e));

Comment on lines 15 to 18
✅ **Like-for-Like Migration: Complete** (40/40 SQL assertions ported)

- Equality operators: 16/16 (HMAC + Blake3, operators + functions + JSONB)
- JSONB functions: 24/24 (arrays, paths, structure validation, encrypted selectors)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this accurate? I can't find any tests.

Comment on lines 185 to 190
### Test Count

- **Total**: 35 tests (34 functional + 1 helper)
- **JSONB**: 19 tests
- **Equality**: 15 tests
- **Helpers**: 1 test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a hallucination or due to not committing the actual tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep yep. Good catch.
I split the PRS. Docs have been included in this one.
Will fix

@tobyhede tobyhede force-pushed the feature/sqlx-infrastructure branch from fed5efc to f54dce0 Compare October 26, 2025 23:27
Add comprehensive Rust/SQLx testing infrastructure to replace pgTAP:

**Core Framework:**
- Create tests/sqlx crate with fluent assertion API
- Add selector constants to eliminate magic string literals
- Implement query assertion builder for ergonomic test writing

**Test Infrastructure:**
- Add SQLx migrations for EQL installation and test helpers
- Add SQL fixtures for test data seeding
- Configure Cargo workspace integration

**Tooling & Scripts:**
- Add mise tasks for test execution and coverage tracking
- Add assertion counting and comparison tools
- Add function call tracking for coverage analysis
- Add test inventory generator
- Add master coverage check script

**Configuration:**
- Update Docker Compose with track_functions enabled
- Add generated migration files to gitignore
- Update mise.toml with Rust test tasks

**Migration:**
- Remove pgTAP testing infrastructure (Dockerfile, tests, scripts)
- Add SQL-to-SQLx migration guide documentation
- Add assertion count and test inventory docs

This infrastructure enables writing Rust tests with SQLx queries instead
of pgTAP, providing better IDE support, type safety, and debugging.
@tobyhede tobyhede force-pushed the feature/sqlx-infrastructure branch from f54dce0 to 7d036c7 Compare October 26, 2025 23:46
@tobyhede
Copy link
Contributor Author

@freshtonic
I will also remove the workspace setup.
You're right, at the moment this is not actually a cargo project and does not need to be a workspace.

@tobyhede tobyhede merged commit 9bd00cf into main Oct 27, 2025
4 checks passed
@tobyhede tobyhede deleted the feature/sqlx-infrastructure branch October 27, 2025 00:23
@tobyhede tobyhede restored the feature/sqlx-infrastructure branch October 27, 2025 00:37
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.

3 participants