|
| 1 | +# Test Policy |
| 2 | + |
| 3 | +This document outlines the testing strategy for the rawsql-ts library. |
| 4 | + |
| 5 | +## Types of Tests |
| 6 | + |
| 7 | +We use two main types of tests: |
| 8 | + |
| 9 | +1. **Unit Tests**: These tests verify individual components in isolation, focusing on a specific functionality of a single class or function. |
| 10 | +2. **Integration Tests**: These tests verify how multiple components work together, often including parsing from SQL strings, transforming, and formatting back to SQL. |
| 11 | + |
| 12 | +## Test File Structure |
| 13 | + |
| 14 | +- Tests should be located in the `tests` directory, mirroring the structure of the `src` directory. |
| 15 | +- Test files should be named with the pattern `[component-name].test.ts`. |
| 16 | +- For utilities and transformers, use the pattern `tests/[directory]/[utility-name].test.ts`. |
| 17 | + |
| 18 | +## Test Frameworks and Tools |
| 19 | + |
| 20 | +- We use [Vitest](https://vitest.dev/) as the test runner. |
| 21 | +- Tests should use standard assertion methods provided by Vitest. |
| 22 | + |
| 23 | +## Test Organization |
| 24 | + |
| 25 | +Tests should be organized using `describe` blocks to group related tests: |
| 26 | + |
| 27 | +```typescript |
| 28 | +describe('Component', () => { |
| 29 | + describe('method or functionality', () => { |
| 30 | + test('specific behavior or case', () => { |
| 31 | + // Test code |
| 32 | + }); |
| 33 | + }); |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +## Test Approach for SQL Components |
| 38 | + |
| 39 | +For utilities and transformers that work with SQL AST components, we recommend a two-level testing approach: |
| 40 | + |
| 41 | +1. **SqlComponent-level unit tests**: These test the utility using manually constructed AST nodes. These tests are precise and clearly show the expected behavior. |
| 42 | + |
| 43 | +2. **Query-level integration tests**: These test the utility by: |
| 44 | + - Parsing SQL from a string |
| 45 | + - Applying the transformation |
| 46 | + - Formatting back to SQL |
| 47 | + - Asserting on the resulting SQL string |
| 48 | + |
| 49 | +This approach ensures both that the internal logic works correctly and that the utility integrates properly with the SQL parser and formatter. |
| 50 | + |
| 51 | +## Example |
| 52 | + |
| 53 | +```typescript |
| 54 | +describe('MyUtility', () => { |
| 55 | + describe('SqlComponent-level unit tests', () => { |
| 56 | + test('handles specific case', () => { |
| 57 | + // Create AST manually |
| 58 | + const node = new SomeComponent(/* ... */); |
| 59 | + |
| 60 | + // Apply transformation |
| 61 | + const result = MyUtility.transform(node); |
| 62 | + |
| 63 | + // Assert on the result |
| 64 | + expect(result).toBeInstanceOf(SomeComponent); |
| 65 | + expect(result.property).toBe(expectedValue); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('query-level integration tests', () => { |
| 70 | + test('transforms SQL correctly', () => { |
| 71 | + // Parse SQL |
| 72 | + const sql = `SELECT * FROM users WHERE id = 1`; |
| 73 | + const query = SelectQueryParser.parse(sql); |
| 74 | + |
| 75 | + // Apply transformation |
| 76 | + const result = MyUtility.transform(query); |
| 77 | + |
| 78 | + // Format back to SQL |
| 79 | + const formatter = new Formatter(); |
| 80 | + const resultSql = formatter.format(result); |
| 81 | + |
| 82 | + // Assert on the resulting SQL |
| 83 | + expect(resultSql).toBe(expectedSql); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
| 87 | +``` |
| 88 | + |
| 89 | +## Test Coverage |
| 90 | + |
| 91 | +- Aim for high test coverage, particularly for complex transformations and utilities. |
| 92 | +- Test both normal cases and edge cases. |
| 93 | +- For complex operations, test compound cases with multiple features interacting. |
| 94 | + |
| 95 | +## Testing New Features |
| 96 | + |
| 97 | +When adding a new feature: |
| 98 | + |
| 99 | +1. Start by writing tests that define the expected behavior. |
| 100 | +2. Implement the feature to make the tests pass. |
| 101 | +3. Include both SqlComponent-level unit tests and query-level integration tests. |
| 102 | +4. Consider edge cases and add tests for them. |
| 103 | + |
| 104 | +This test-driven approach helps ensure that features are well-defined and correctly implemented. |
0 commit comments