Package: rethinkdb-mock
RethinkDB MockFormerly known asMockThinkby Scivey, this is a continuation of that work released under a new name.Rethinkdb-mock is an in-process Python clone of RethinkDB's API. For testing.MockThink provides a stub connection object which can be passed to normal ReQL queries. Instead of being serialized and sent to the server, the ReQL AST is run through an interpreter in the same process. "Tables" and "databases" are based on data given to the MockThink constructor.Avoiding network calls (for tests themselves as well as setup/teardown) makes testing queries with MockThink orders of magnitude faster.Installpipinstallrethinkdb-mock# orpipenvinstall--devrethinkdb-mockUsageBasicfrompprintimportpprintfromrethinkdb_mockimportMockThinkimportrethinkdbasrdb=MockThink({'dbs':{'tara':{'tables':{'people':[{'id':'john-id','name':'John'},{'id':'sam-id','name':'Sam'}]}}}})withdb.connect()asconn:result=r.db('tara').table('people').map(lambdadoc:doc.merge({'also_name':doc['name']})).run(conn)pprint(list(result))# [#    {'also_name': 'John', 'id': 'john-id', 'name': 'John'},#    {'also_name': 'Sam', 'id': 'sam-id', 'name': 'Sam'}# ]r.db('tara').table('people').update({'likes_fonz':True}).run(conn)result=r.db('tara').table('people').run(conn)pprint(list(result))# [#    {'id': 'john-id', 'likes_fonz': True, 'name': 'John'},#    {'id': 'sam-id', 'likes_fonz': True, 'name': 'Sam'}# ]# data is reset at exit of context manager abovewithdb.connect()asconn:result=r.db('tara').table('people').run(conn)pprint(list(result))# [#    {'id': 'john-id', 'name': 'John'},#    {'id': 'sam-id', 'name': 'Sam'}# ]Full support for secondary indexesfrompprintimportpprintfromrethinkdb_mockimportMockThinkimportrethinkdbasrdb=MockThink({'dbs':{'tara':{'tables':{'people':[{'id':'john-id','first_name':'John','last_name':'Generic'},{'id':'sam-id','first_name':'Sam','last_name':'Dull'},{'id':'adam-id','first_name':'Adam','last_name':'Average'}]}}}})withdb.connect()asconn:r.db('tara').table('people').index_create('full_name',lambdadoc:doc['last_name']+doc['first_name']).run(conn)r.db('tara').table('people').index_wait().run(conn)result=r.db('tara').table('people').get_all('GenericJohn','AverageAdam',index='full_name').run(conn)pprint(list(result))# {'id': 'john-id', 'first_name': 'John', 'last_name': 'Generic'},# {'id': 'adam-id', 'first_name': 'Adam', 'last_name': 'Average'}TestingThe most confusing test failures are those caused by errors in test frameworks and harnesses themselves.
This means they need to be tested very thoroughly.The main testing is asuite of functional testswhich are targeted at the individual query level,
e.g.testing all the ways in whichr.mergemight be called.These are all complete ReQL queries, but avoid complexity beyond the target query to make failures easier to diagnose.Theintegration testscover more complicated queries, e.g.eq_join->map->eq_join->map.Both the functional and integration tests have two modes of execution,rethinkdb_mockandrethink. The second mode runs the same tests against a running RethinkDB instance, and is much slower due to the network calls.rethinkdb_mockmode is for testing MockThink's behavior against our expectations;rethinkmode is for testing our expectations against reality.Run tests# Install dependenciespipinstallpipenv
pipenvsync--dev# Run the unit testspipenvruntestRun tests against a live serverThe docker folder contains a dockerfile that grabs rethinkdb and runs the python testsSimply running the below command outputs the resultsdocker-composeup--buildCode formattingThetox.inifile contains the configuration for code formatting# Install dependenciespipinstallpipenv
pipenvsync--dev# Format importspipenvrunisort# Check styling with Flake8pipenvrunlintLicenseThe MIT License (MIT)