3

I am close to finishing an ORM for RethinkDB in Python and I got stuck at writing tests. Particularly at those involving save(), get() and delete() operations. What's the recommended way to test whether my ORM does what it is supposed to do when saving or deleting or getting a document?

Right now, for each test in my suite I create a database, populate it with all tables needed by the test models (this takes a lot of time, almost 5 seconds/test!), run the operation on my model (e.g.: save()) and then manually run a query against the database (using RethinkDB's Python driver) to see whether everything has been updated in the database.

Now, I feel this isn't just right; maybe there is another way to write these tests or maybe I can design the tests without even running that many queries against the database. Any idea on how can I improve this or a suggestion on how this has to be really done?

linkyndy
  • 17,038
  • 20
  • 114
  • 194

1 Answers1

3

You can create all your databases/tables just once for all your test.

You can also use the raw data directory: - Start RethinkDB - Create all your databases/tables - Commit it.

Before each test, copy the data directory, start RethinkDB on the copy, then when your test is done, delete the copied data directory.

neumino
  • 4,342
  • 1
  • 18
  • 17
  • Each test suite will have its own models hence their own tables, so I cannot and should not have the same database/tables for all my tests. Also, I will start RethinkDB and then run all tests, I don't think it's a good bet to start and stop RethinkDB for each test - the performance kill will probably be the same in my current case. – linkyndy Sep 18 '14 at 21:35
  • One way to gain a bit of time would be to just clean the tables instead of dropping/recreating them. I'm maintaining an ORM too, and I just drop/create tables too, and I have to admit, it's a bit slow. – neumino Sep 18 '14 at 21:46
  • I know, and you've done a great job with it! :) Ok then, it's good to know that fellow coders have the same approach...And what's your opinion on my other issue, regarding the method of testing? How do you test whether your model operations do what they are expected to do? – linkyndy Sep 18 '14 at 22:05
  • I do the same to test save/delete/update/etc. I basically run the command, and then query the database to make sure that the document was properly saved/deleted/updated. You can't really do better here I think since you may not send the appropriate query (when doing an update) but still get the expected result (like `replaced: 1`) – neumino Sep 18 '14 at 22:59
  • I understand. Thought there would be a less time-consuming approach. As I would have expected, unit tests should be fast and should not depend on external parties, such as databases. – linkyndy Sep 18 '14 at 23:08
  • 1
    The only way would be to mock up a database Python, but that's quite a lot of work :/ – neumino Sep 18 '14 at 23:42