by yujzgzc · 1y ago · 0 comments
The problem is most of the SQL-first devs hate it and would rather write untested code, or rely on alternative test automation techniques like bringing up a local DB instance with fake data, than have to refit their business logic into pure functions. The truth is that it's just not a way of working that comes naturally to people who write a lot of SQL for a living.
There is a trade off between unit tests and having control and overview of what you are writing.
Also very often it is so much more about verifying that the input data is clean and if this becomes more cumbersome, you may have lost the ability to debug the biggest source of errors.
And of course sometimes it hits a nerve when the "real engineers" come and dictate exactly how you should do things. Reference also to Joel Spolsky's architecture astronauts.
I haven't tried it yet but I look forward to doing so. Already dbt macro expansions with tests have provided me way more test coverage than I ever had in the pre-dbt days.
It's a footnote in the article, but it seemed like a natural half-step forward in the direction indicated by the article.
So imho the DB people are totally right, to test any SQL, you need database tables with data in them.
https://github.com/svilendobrev/svd_util/blob/master/expr.py
the problem lies with lax/weak table design that allows too lax data (all string nullable fields for example), instead of strict types like int, float, varchar, foreign key constraints etc.
you dont need unit test if you table design is clean, because SQL operators are deterministic and are tested by the RDBMS vendor
just need integration test, dry run test, periodic data integrity checks at the system boundaries when data is ingested/egress
SQL represents operations on the Sets of data and their relations, so you need not unit test on a single row, but a test suite on your entire DataSet (backup copy or synthetic copy)
Imagine well structured tables in 3rd normal form.
And then a view which joins 20 tables and creates a report with myriad of case/then expressions.
Many things can go wrong. (Wrong join conditions, duplicate rows, omitted rows, wrong formulas)
You might say that there should be no such logic in database and I understand the sentiment, but circumstances (e.g. performance) might say it’s needed.
It is only testable on real production database, so you can do test of complex queries on backup of your prod to get a sense of performance.
But still query can degrade unexpectedly under consistent load in prod from other queries
I don't know... Refactoring Databases is pretty old now. Plenty of DBAs can handle this style and even prefer it. I think this is more of a cultural problem in the small than in the large.
You know that SQL is completely pure up to the point where you specify a data alteration command, right?
Your framework is ditching the queries, that are completely pure as enforced by the engine, and insists on using maybe-pure functions that all the engines have problems with.