I'm writing a C++ wrapper for libpq. However I can't figure out how I should write my unit tests. My initial reaction was to just wrap the libpq interface into something that's mockable, but that kind of defeats the purpose. Has anyone done this before? If so, how?
Asked
Active
Viewed 1,079 times
2 Answers
3
Don't do that. There's already a C++ wrapper for libpq, called libpqxx
.
I'd test using cppunit, with pre- and post- code to initdb
and start a new new PostgreSQL cluster given initdb
and pg_ctl
on the path.

Craig Ringer
- 307,061
- 76
- 688
- 778
-
No binary transfer or array parsing support. – Ilia Choly Nov 23 '12 at 14:04
-
@iliacholy That's pretty woeful. I'm surprised you're not trying to improve libpqxx (perhaps building on top of libpqtypes) rather than replacing it, though. – Craig Ringer Nov 23 '12 at 15:14
-
I looked into improving libpqxx to fit my needs but decided against it because I feel like it tries to do too much already. I am using libpqtypes, but it's not part of my lib. It's basically just sugar over libpq with very simple base classes meant to be extended. – Ilia Choly Nov 23 '12 at 15:32
2
For unit testing, you can mock some of your classes (db connection, query, parameter binder). For larger functional tests you can use a real connection to a test db. Worked well for two libpq C++ wrappers I did a few years back (one for Qt, another for C++/STL).
With regards to libpqxx - it still does not support binary libpq v3 protocol, which in some cases (e.g. import/export large amounts binary data) may be a bottleneck. Other than this, it's pretty straightforward to use and is quite enjoyable.

IggShaman
- 531
- 1
- 4
- 6
-
1A quick followup - I did not even bother with mocks in C++, just used real classes and real connections all along. – IggShaman Nov 23 '12 at 08:03
-
I was unaware of that libpqxx limitation. Thankyou for pointing it out. The v2 protocol won't be around forever and it's already very old, so that's a pretty amazing limitation. I wonder if it's better to enhance libpqxx than to write a whole new wrapper, though. – Craig Ringer Nov 23 '12 at 09:33