We spin up a docker container running the DB technology we use, run our DB migration scripts on it, and then run integration tests against it. You get coverage of your migration scripts this way too.
I've used this same approach as well. Testcontainers is a nice way to help with this! https://www.testcontainers.org/
Ask HN: How do you test SQL?
141–150 of 322 posts
Re: Ask HN: How do you test SQL?
#142Re: Ask HN: How do you test SQL?
#143Starting with a framework that is programming language first (IE Spark) can help you build your own tooling to help you actually build unit tests. It's frustrating though, that this isn't just common across other ETL tooling.
Re: Ask HN: How do you test SQL?
#144Earlier quoted context omitted.
> if sql was declarative, than the code of the SQL above would be: GIVE ME ALL THE COST OF THE EMPLOYEES --, I DONT CARE ABOUT THE DETAILS This is exactly what SQL is. There are exceptions, but they're exceptions. > an SQL query itself (if no structured language used) is just a big function applied on a set. I don't think you have a correct model of SQL. SQL is a language that expresses definitions, not functions. It…
>This is exactly what SQL is. There are exceptions, but they're exceptions. no, i meant the query is literally this: GIVE ME ALL THE COST OF THE EMPLOYEES or if we want to formalize more: ALL COST OF ALL EMPLOYEES the point here that in a declarative mindset EMPLOYEE is a word that SQL server understands, like HTML understand paragraph or table with rows and cells >I don't think you have a correct model of SQL. SQL i…
I dont think html is anything like sql (only in the sense that all markup languages are also declarative) but a comprable situation would be if you were trying to render some ascii art in html and had to use pre tags in order to tell the layout engine how to show it.
Re: Ask HN: How do you test SQL?
#145Re: Ask HN: How do you test SQL?
#146It's a lot faster and easier than dealing with containers and the like.
Re: Ask HN: How do you test SQL?
#147Try and write any complex SQL as a series of semantically meaningful CTEs. Test each part of the CTE pipeline with an in.parquet and an expected_out.parquet (or in.csv and out.csv if you have simple datatypes, so it works better with git). And similarly test larger parts of the pipeline with 'in' and 'expected_out' files. If you use DuckDB to run the tests, you can reference those files as if they were tables (select…
One caution with PostgreSQL, CTEs under some circumstances (and in all circumstances, prior to PostgreSQL 12) act as optimization barriers. Specify `NOT MATERIALIZED` before the CTE definition to ensure that they are optimized same as a sub-SELECT would be.
Re: Ask HN: How do you test SQL?
#148Earlier quoted context omitted.
> if sql was declarative, than the code of the SQL above would be: GIVE ME ALL THE COST OF THE EMPLOYEES --, I DONT CARE ABOUT THE DETAILS This is exactly what SQL is. There are exceptions, but they're exceptions. > an SQL query itself (if no structured language used) is just a big function applied on a set. I don't think you have a correct model of SQL. SQL is a language that expresses definitions, not functions. It…
>This is exactly what SQL is. There are exceptions, but they're exceptions. no, i meant the query is literally this: GIVE ME ALL THE COST OF THE EMPLOYEES or if we want to formalize more: ALL COST OF ALL EMPLOYEES the point here that in a declarative mindset EMPLOYEE is a word that SQL server understands, like HTML understand paragraph or table with rows and cells >I don't think you have a correct model of SQL. SQL i…
Re: Ask HN: How do you test SQL?
#149for real though I love tools like SequelPro or TablePlus that let me work out a query before I bake logic or stuff into my apps. Also sometimes I use it to work out the data needed for reports. I am working with salesforce for the first time in my life and apparently there are tools that let me treat it like I'm used to SequelPro.
Re: Ask HN: How do you test SQL?
#150Earlier quoted context omitted.
This is what we did at my last job. You can catch DB specific issues that a false implementation wouldn’t show and make sure all your code paths work as expected. Every time new issues cropped up we would put new data in the test data designed to reproduce it. Every edge case we would run into. It provided so much confidence because it would catch and trigger so many edge cases that testing with mocks or by hand woul…
thats a huge amount of work and money. at my company they just told us to stop reporting edge cases. much easier, much cheaper.