by shanemhansen · 11y ago · 0 comments
Vagrant has a mode called rsync-auto which will synchronize directories into a local vm. Or you could just always have the code local to the node process. It makes more sense to have your editor work with remote files.
Next biggest problem was relational database. I think that tests requiring a SQL database are usually a bad sign, and the statement "we'd like to use transactions but our orm doesn't support them" makes me sad. To paraphrase Ben Franklin, "those who would give up transactions for a framework deserve neither". Here's some ideas for speeding up your tests, in order from best to worst.
- Make most business code rely on a service or interface. Swap that our during tests for a trivial in-memory implementation.
- Use sqlite :memory: for tests
- TRUNCATE TABLE
- Set your database to running with scissors mode (disable fsync)
[edit] I just took a look at the ORM used, waterline https://github.com/balderdashy/waterline . Why wasn't step one moving to sails-memory for running tests?Service layer tests should have mocks of the dao and web service calls. You should do most of your testing here and with unit tests for tricky algorithms.
Integration tests should hit the full stack, but those should run on a continuous integration server asynchronously and also take place in their own empty database.
The key to dao and integration testing is the empty database. Don't let it fill up with test crud between tests. PostgreSQL is pretty fast when your test db only has a couple of rows.
Some major bugs are caused by people not testing their code with enough data.
How can I really ensure my CHECK constraints / DOMAINs, unique indexes, RAISE EXCEPTION in insert/update triggers, etc. are correctly implemented if I'm not testing against a real Postgres database?
The unit tests that test out your business logic should use mock data - it's just much more reliable, reproducible, and faster.
It's about creating appropriately sized discrete modules.
Then you run into issues where the functionality works on SQLite, but not on your real database.
Strictly speaking it's also possible to get subtle differences in behavior across OSes. Especially if you have a race condition you don't know about yet. I always tend to encourage more diversity (carefully spent).
The ORM can't abstract away all differences between databases. For example, SQLite doesn't have Numeric type, so all of the Numeric(10, 2) fields on (e.g.) my SQLAlchemy (a Python ORM) models will trigger warnings when saving to a SQLite database.
This strategy is, in essence, mocking the FS layer in DB tests.
In general we're trying to write and run more tests that only create and manipulate objects in memory, especially for testing business logic. Unfortunately we still have to have some level of integration with the database to test things at a higher system level.
Re: transactions, most of our database code uses the ORM, so we thought other test speedups would give a bigger benefit. We recently added an interface for getting/querying with a `pg` connection directly and we're using that to implement transactions in a few critical places. I'm planning to release the code soon - hopefully in a blog post in a few weeks.
There should be some way to combine things into a single module/file or otherwise glob it all together, while removing pointless test, docs, and Adventure Time animated gifs from your deps folder.
Although I'm sure there are module maintainers shipping AT gifs, we can't really blame npm for that.
- Run all your tests in a transaction, roll it back in the end (also avoids writes to disk in most scenarios)
- Put the DB file system on a RAM disk