AaronCrane.co.uk

Testing code that uses databases

I’m hardly the first person to observe that it’s hard to test code that needs a database. Production usage almost certainly needs a database server, but then the tests need some way of getting a suitable database handle.

I think this reveals a potential advantage of ORMs, in at least some circumstances. If your code contains no actual SQL, just instructions to the ORM, then you can have tests which use SQLite as the database, and have a high degree of confidence that things will continue to work if you’re using MySQL or PostgreSQL in production. That’s not really good enough in situations where you’re writing, say, an application that makes use of your DBMS’s unique features, but for relatively simple library code, it might well suffice.

The cross-DBMS code still needs to be written and tested, but you can punt on that problem and just leave it up to the ORM.