Mocking HappyBase for unit testing HBase code
08 Mar 2013
HappyBase is a friendly interface to interact with HBase from Python. It lets you perform basic HBase operations like get, put and scan. But say you have a bunch of puts littered around your code. How do you unit test that? One method would be to mock out the happy base calls themselves, and just assert that they are called with specific parameters. But what if you want to test the final state of the HBase tables after a series of operations?
For that, you can replace the HappyBase Table class with a version that keeps the data in memory.
To use it, just have your unit test class extend HBaseTestCase.
Note: this version does not force you to use valid column families, it just created families on the fly as you put columns in. It also does not support any of the options on scan, such as filtering by a range of rowkeys. Some of those options could be added easily. Others would be very difficult, such as java based column filters. Hopefully this is enough you get you started testing your HBase code.
I'm currently working at NerdWallet, a startup in San Francisco trying to bring clarity to all of life's financial decisions. We're hiring like crazy. Hit me up on Twitter, I would love to talk.