Ask HN: How do you test SQL?
61–70 of 322 posts
Re: Ask HN: How do you test SQL?
#62Re: Ask HN: How do you test SQL?
#63We 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.
Re: Ask HN: How do you test SQL?
#64Plenty of constraints, uniques and foreign keys and not nulls. Enum types.
Visuals, dump to csv and plot some graphs. Much easier to find gaps and strange distributions visually.
Asserts in DO blocks, mostly counts being equal.
Build tables in a a _next suffix schema and swap when done.
Never mutating the source data.
Using psqls ON_ERROR_STOP setting.
Avoid all but the most trivial CTEs, preferring intermediate tables that can be inspected. Constraints and assertions on the intermediate tables.
“Wasting” machine resources and always rebuilding from scratch when feasible. CREATE TABLE foo AS SELECT is much simpler than figuring out which row to UPDATE. Also ensures reproducibility, if you’re always reproducing from scratch it’s always easy. State is hard.
Overall i’m quite happy with the workflow and very rarely do we make mistakes that unit tests would have caught. Our source data is complex and not always well understood (10+ years of changing business logic) so writing good tests would be very hard. Because we never touch the raw source data any errors we inevitably make are recoverable.
This talk by Dr Martin Loetzsch helped a lot: https://youtu.be/whwNi21jAm4
Re: Ask HN: How do you test SQL?
#65To be honest. I would love testing to work. Have set up and maintained several unit test suites in Jest. Wrote several large e2e test suites in Cypress. I don't think anyone won time from simply having a manual checklist and testing manually. Maybe me and my former teammates are doing it wrong. Talking 8+ teams, from corporate to startup. But loved de proven wrong. E2e def. catched most issues.
Re: Ask HN: How do you test SQL?
#66A list of best practices: https://docs.getdbt.com/guides/legacy/best-practices
And shameless plug but there's a chapter on modeling in my book: https://theinformedcompany.com
Re: Ask HN: How do you test SQL?
#67Earlier quoted context omitted.
SQL is not a declarative language. It is a functional language, and structured language on the top as extensions. HTML is a declarative language.
The most important requirement for a functional language is that functions are first-class values. i.e. Not SQL. On the other hand, the details of query execution are left to the planner and optimizer. What's the case that it's functional, but not declarative?
Re: Ask HN: How do you test SQL?
#68Try 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…
Not much more to say, just observing, sorry if this is irrelevant commentary.
Re: Ask HN: How do you test SQL?
#69We 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.
Re: Ask HN: How do you test SQL?
#70We 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.
would love to do this, but how does one spin up a redshift cluster inside of a docker container?