|
| 1 | +# Rosetta API Construction Integration Test Validator |
| 2 | + |
| 3 | +A integration testing tool for Rosetta API construction endpoints. It validates live API responses against both: |
| 4 | +- Snapshot expectations stored in JSON test files, and |
| 5 | +- The OpenAPI schema (when available) for structural correctness. |
| 6 | + |
| 7 | +## What is Snapshot Testing? |
| 8 | + |
| 9 | +This validator implements snapshot testing - it compares actual API responses against previously captured "snapshots" of expected behavior. When the API behavior changes, tests fail, alerting you to potential regressions or the need to update snapshots. |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +### Option 1: Basic Mode (No Environment Setup) |
| 14 | +Run snapshot testing without schema validation - no dependencies needed: |
| 15 | + |
| 16 | +```bash |
| 17 | +# Test all files with default parallel execution (10 workers) |
| 18 | +python3 test_construction_api.py |
| 19 | + |
| 20 | +# Test specific endpoint with more workers for faster execution |
| 21 | +python3 test_construction_api.py parse/ -j 20 |
| 22 | + |
| 23 | +# Note: Schema validation will be skipped in this mode |
| 24 | +``` |
| 25 | + |
| 26 | +### Option 2: Full Mode with uv (Includes Schema Validation) |
| 27 | +For full features including OpenAPI schema validation: |
| 28 | + |
| 29 | +```bash |
| 30 | +# Install uv if you don't have it |
| 31 | +curl -LsSf https://astral.sh/uv/install.sh | sh |
| 32 | + |
| 33 | +# Run the validator with full features (uv handles environment automatically) |
| 34 | +uv run test_construction_api.py |
| 35 | + |
| 36 | +# Run with specific options and increased parallelism |
| 37 | +uv run test_construction_api.py parse/ -v -j 15 |
| 38 | +uv run test_construction_api.py -o results.txt -j 20 |
| 39 | +``` |
| 40 | + |
| 41 | +That's it! The `uv run` command automatically: |
| 42 | +- Creates a virtual environment (if needed) |
| 43 | +- Installs all dependencies (pyyaml, jsonschema) |
| 44 | +- Runs the script with full schema validation enabled |
| 45 | + |
| 46 | +## Features |
| 47 | + |
| 48 | +- **Parallel execution**: Run tests in parallel with configurable workers (10x+ speedup) |
| 49 | +- **Snapshot testing**: Detects API response changes against stored expectations |
| 50 | +- **Network flexibility**: Support for different networks via `{{networkId}}` placeholders and `--network-id` parameter |
| 51 | +- **Schema validation**: Validates responses against OpenAPI spec (when using uv environment) |
| 52 | +- **Two modes**: Basic (Python only) or Full (with uv for schema validation) |
| 53 | +- **CLI interface**: Flexible path/pattern matching |
| 54 | +- **Detailed error reporting**: Shows actual API error details |
| 55 | +- **HTTP status validation**: Robust error detection using status codes |
| 56 | +- **File output**: Save results to file with `-o` option |
| 57 | +- **Dynamic field handling**: Ignores expected volatile fields (TTL, signatures) |
| 58 | +- **Execution timing**: Total test suite execution time tracking |
| 59 | +- **CI/CD integration**: Proper exit codes for continuous integration pipelines |
| 60 | + |
| 61 | +## Detailed Usage Examples |
| 62 | + |
| 63 | +### With uv (Full Features) |
| 64 | +```bash |
| 65 | +# Test all files with schema validation |
| 66 | +uv run test_construction_api.py |
| 67 | + |
| 68 | +# Test specific endpoint |
| 69 | +uv run test_construction_api.py parse/ |
| 70 | + |
| 71 | +# Test with wildcards |
| 72 | +uv run test_construction_api.py "parse/native_assets/*.json" |
| 73 | +uv run test_construction_api.py "*/withdrawals/*.json" |
| 74 | + |
| 75 | +# Test specific file |
| 76 | +uv run test_construction_api.py parse/native_assets/multiple_assets_different_policies.json |
| 77 | + |
| 78 | +# Use specific network (for CI/CD with different networks) |
| 79 | +uv run test_construction_api.py --network-id devkit |
| 80 | +uv run test_construction_api.py -n preprod |
| 81 | + |
| 82 | +# Run with increased parallelism (20 workers instead of default 10) |
| 83 | +uv run test_construction_api.py -j 20 |
| 84 | + |
| 85 | +# Run single-threaded (useful for debugging) |
| 86 | +uv run test_construction_api.py -j 1 |
| 87 | + |
| 88 | +# Output to file |
| 89 | +uv run test_construction_api.py -o results.txt |
| 90 | + |
| 91 | +# Custom API URL |
| 92 | +uv run test_construction_api.py -u http://localhost:8080 |
| 93 | + |
| 94 | +# Verbose output |
| 95 | +uv run test_construction_api.py -v # verbose prints schema details per test |
| 96 | + |
| 97 | +# Combine options for CI/CD environment |
| 98 | +uv run test_construction_api.py --network-id devkit -v -u http://localhost:8080 -o results.txt -j 15 |
| 99 | +``` |
| 100 | + |
| 101 | +### Without uv (Basic Mode) |
| 102 | +```bash |
| 103 | +# Same commands work with python3 directly (no schema validation) |
| 104 | +python3 test_construction_api.py |
| 105 | +python3 test_construction_api.py parse/ -v |
| 106 | +python3 test_construction_api.py --network-id devkit # for CI/CD with devkit network |
| 107 | +python3 test_construction_api.py --no-schema # explicitly skip schema |
| 108 | +python3 test_construction_api.py -j 20 # parallel execution with 20 workers |
| 109 | +``` |
| 110 | + |
| 111 | +## CLI Options |
| 112 | + |
| 113 | +- `[paths/patterns...]` - Test file paths or patterns (positional, optional) |
| 114 | +- `-o <file>` - Output results to file instead of stdout |
| 115 | +- `-u <url>` - Rosetta API URL (default: `http://localhost:8082`) |
| 116 | +- `-n, --network-id <id>` - Network ID to replace {{networkId}} placeholders (e.g., 'devkit', 'preprod', 'mainnet'). Defaults to 'preprod' when placeholders are found. |
| 117 | +- `-v, --verbose` - Show detailed output for each test |
| 118 | +- `-j, --workers <n>` - Number of parallel workers for test execution (default: 10) |
| 119 | +- `--openapi <path>` - OpenAPI spec file (YAML or JSON). Defaults to `api.yaml` next to the script |
| 120 | +- `--no-schema` - Disable schema validation (snapshot-only) |
| 121 | +- `--schema-details` - Force schema summary (redundant with `-v`, which enables it) |
| 122 | + |
| 123 | +## Requirements |
| 124 | + |
| 125 | +- Python 3.6+ |
| 126 | +- Running Rosetta API (configure via `-u` option) |
| 127 | +- Test files in JSON format |
| 128 | +- For schema validation: `uv` (installs pyyaml & jsonschema automatically) |
| 129 | + |
| 130 | +## Test File Structure |
| 131 | + |
| 132 | +Each test file should contain: |
| 133 | +- `request_body`: The API request payload |
| 134 | +- Either `expected_response` (success) or `expected_error` (failure) |
| 135 | +- Test metadata (name, description) |
| 136 | + |
| 137 | +## Exit Codes |
| 138 | + |
| 139 | +- `0`: All tests passed |
| 140 | +- `1`: One or more tests failed or connection error |
| 141 | + |
| 142 | +## What It Validates |
| 143 | + |
| 144 | +The script validates all Rosetta construction flow endpoints: |
| 145 | +- `/construction/derive` |
| 146 | +- `/construction/preprocess` |
| 147 | +- `/construction/metadata` |
| 148 | +- `/construction/payloads` |
| 149 | +- `/construction/parse` |
| 150 | +- `/construction/combine` |
| 151 | +- `/construction/hash` |
| 152 | +- `/construction/submit` |
| 153 | + |
| 154 | +For each test, it compares the actual API response against the expected response, accounting for dynamic fields like TTL and signatures. |
| 155 | + |
| 156 | +If schema validation is enabled and the OpenAPI spec is available, it also validates the structure of the response (and error responses) against the schema. Schema failures are reported alongside snapshot diffs and will mark the test as failed. |
0 commit comments