Skip to content

Commit 45ed1b4

Browse files
committed
added tests and ci for a test run.
1 parent 7dda6fe commit 45ed1b4

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

.github/workflows/python-ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ jobs:
2828

2929
- name: Build package
3030
run: python -m build
31+
32+
- name: Run test suite
33+
run: |
34+
python tests/test_runner.py

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[![PyPI version](https://img.shields.io/pypi/v/crdb-sql-audit)](https://pypi.org/project/crdb-sql-audit/)
22
[![Python version](https://img.shields.io/pypi/pyversions/crdb-sql-audit)](https://pypi.org/project/crdb-sql-audit/)
33
[![License](https://img.shields.io/pypi/l/crdb-sql-audit)](https://pypi.org/project/crdb-sql-audit/)
4+
[![Build status](https://github.com/viragtripathi/crdb-sql-audit/actions/workflows/python-ci.yml/badge.svg)](https://github.com/viragtripathi/crdb-sql-audit/actions)
45

56
# crdb-sql-audit
67

@@ -169,3 +170,25 @@ You can use basic Unix commands to check for patterns like pg_ functions directl
169170
| Unique function names | `grep -oE '\bpg_[a-zA-Z0-9_]+\(' chunks/* \| sort \| uniq` |
170171
| Count occurrences of each function | `grep -oE '\bpg_[a-zA-Z0-9_]+\(' chunks/* \| sort \| uniq -c \| sort -nr` |
171172
| Full SQL lines containing pg\_\* | `grep -E '\bpg_[a-zA-Z0-9_]+\(' chunks/*` |
173+
174+
175+
---
176+
177+
## 🧪 Running Tests
178+
179+
This project includes a test suite using sample logs and rules to validate behavior.
180+
181+
### 🔧 To run locally:
182+
183+
```bash
184+
python tests/test_runner.py
185+
```
186+
187+
### 🧪 What it does:
188+
189+
* Runs `crdb-sql-audit` on a small sample of PostgreSQL-style logs
190+
* Uses `tests/rules/test_rules.yaml`
191+
* Verifies that a CSV report is created with expected issues
192+
193+
✅ This runs automatically in GitHub Actions on every commit to `main`.
194+

tests/rules/test_rules.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
- id: malformed_dml
2+
match: '^(SELECT|DELETE FROM)\s*$'
3+
message: "Too short"
4+
level: warning
5+
tags: [syntax]
6+
7+
- id: hash_table
8+
match: '"[^"]*#\w*"'
9+
message: "Has # in table name"
10+
level: error
11+
tags: [identifier]
12+
13+
- id: pg_func
14+
match: '^.*\bpg_\w+\s*\(.*$'
15+
message: "PostgreSQL built-in function"
16+
level: error
17+
tags: [function]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2025-05-15 12:00:00 CEST [123456]: [1-1] user=testusr,db=testdb LOG: execute <unnamed>: SELECT * FROM "ACCOUNTS#NAU"
2+
2025-05-15 12:00:01 CEST [123456]: [2-1] user=testusr,db=testdb LOG: execute <unnamed>: DELETE FROM
3+
2025-05-15 12:00:02 CEST [123456]: [3-1] user=testusr,db=testdb LOG: execute <unnamed>: SELECT pg_backend_pid()
4+
2025-05-15 12:00:03 CEST [123456]: [4-1] user=testusr,db=testdb LOG: execute <unnamed>: SELECT

tests/test_runner.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
import subprocess
3+
4+
def run_test():
5+
print("🧪 Running crdb-sql-audit on test logs...")
6+
test_output = "tests/output/test_report"
7+
os.makedirs("tests/output", exist_ok=True)
8+
9+
subprocess.run([
10+
"crdb-sql-audit",
11+
"--dir", "tests/sample_logs",
12+
"--terms", "execute,pg_",
13+
"--rules", "tests/rules/test_rules.yaml",
14+
"--out", test_output
15+
], check=True)
16+
17+
if os.path.exists(test_output + ".csv"):
18+
print("✅ CSV report generated successfully")
19+
else:
20+
print("❌ CSV report not found")
21+
22+
if __name__ == "__main__":
23+
run_test()

0 commit comments

Comments
 (0)