Ask HN: How do you test SQL?
663 pointsI've been looking for resources for our data team to apply best practices for testing SQL pipelines (we use DBT) but have not found anything. How do you test SQL pipelines? What patterns, tools and best practices would you recommend? Any good reference material you know of?
Comments (315) 98 threads
Earlier commentsExample:
db = Fake().expect_query("SELECT * FROM users", result=[(1, 'Bob'), (2, 'Joe')])
Then you do:
db.query("SELECT * FROM users")
and get back the result.
In Python if you do this in a context manager, you can ensure that all expected queries actually were issued, because the Fake object can track which ones it already saw and throw an exception on exit.
The upside of this is, you don't need any database server running for your tests.
update: This pattern is usually called db-mock or something like this. There are some packages out there. I built it a few times for companies I worked for.
And that can be done by dumping the database (possibly verifying the content of that dump), taking a backup, restoring the backup to a fresh container, then comparing dump of that freshly restored database to the one you took at the start.
I dont know the technical detail of how to set it up, it was already setup when I worked there
But basically, we wrote SQL script that included statements to
1. create the db structure, tables or views
2. insert statement to enter test data (you can insert corner cases etc..)
3. ran the function or procedure
4. ran an assert to confirm if results are to our expectation
test script were ran by the CI/CD process
All comments shown.
Couldn't load more comments. Check your connection and try again.