[odb-users] Unit Testing, Mocking of classes that contain ODB code

Iris-Marie Köster imk at qosmotec.com
Fri Oct 17 10:14:11 EDT 2014


Hello!

To make code testable without real database access, I'm trying to get rid of all direct uses of odb functions.
For example, I have a class foo in which something is loaded. Right now, it is done like this:

std::shared_ptr<odb::database> databaseInstance;
(databaseInstance.reset(new odb:: .. and so on - this is done somewhere else)
databaseInstance->load(someId, pointerToSomeObject);

What I'm trying to do now is to write a base class (called DatabaseInstance) and derive two classes from it (OdbInstance and MockInstance).
These classes shall contain all necessary methods to perform (or mock performing) database actions.

In trying to write these methods I have encountered the following problem (to describe it, I need to go a little further afield):
I have a class "DatabaseEntity", which is the common base class of all entities within the project that are stored in the database. It is there to keep track of the entity's database ID and version.
The class foo (which could be any of the entities that are stored in the database) is of course derived from "DatabaseEntity".

In DatabaseInstance, I have declared a pure virtual method "void load(DatabaseEntity * pEntity) = 0;". The implementation of this method in the derived class OdbInstance looks like this:

void OdbInstance::load(DatabaseEntity * pEntity)
{
       m_database->load(pEntity->getId(), *pEntity);
}

(m_database is a private member of OdbInstance of type std::shared_ptr<odb::database>)

When I try to compile this, I get funny error messages:
'find' is not an element of  'odb::object_traits_impl<T,id_common>'
 'odb::access::object_traits_impl<T,id_common>': base class undefined
'bool odb::database::find(const object_traits<T>::id_type &, T&)': 2 arguments expected - 3 supported
'object_traits<T>::pointer_type odb::database::find(const object_traits<T>::id_type &)': 1 argument expected - 3 supported

My questions are:

1)      Can you make sense of this? (I went through the database.ixx file and did not even find the position where the "find"-method is called after the "load"-method)

2)      Does anyone have experience with Unit Testing classes that use odb-code? Did you mock the database access or not?

3)      Do my ideas described above even make sense or did I miss something that make my plans inexecutable?

Thanks a lot for your attention and help or ideas!

Best regards
Iris

P.S.: I have appended a little uml-diagram to illustrate what I want to do


Qosmotec Software Solutions GmbH
Principal Address:                    Tel: +49 241 87975 26
Schloss-Rahe-Straße 3               Fax: +49 241 87975 15
52072 Aachen
Germany                                    Web: http://www.qosmotec.com<http://www.qosmotec.com/>
Managing Directors : Mark Hakim - Dr. Dieter Kreuer - Axel C. Voigt
Commercial Register: Amtsgericht Aachen HRB 12272, VAT-ID DE234919984

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Mocking Database.pdf
Type: application/pdf
Size: 2073 bytes
Desc: Mocking Database.pdf
Url : http://codesynthesis.com/pipermail/odb-users/attachments/20141017/918d7c6e/MockingDatabase.pdf


More information about the odb-users mailing list