Supplying test data for LocalStorage in unit tests



  • I want to write an unit test for a pure QtQuick + javascript application which uses LocalStorage 2.0 as its backend. The feature I'm testing is mostly a database migration, so I'd like to supply a test database file with the old schema to the unit test.
    Sadly, LocalStorage.openDatabaseSync doesn't allow specifying a database file directly, just a database name.
    What is the best solution to supply test data for a QtQuick test (QtTest 1.0)? Or do I need to mock away LocalStorage? If yes, how?

    regards,
    a QtQuick novice


  • Moderators

    @schmittlauch

    Sadly, LocalStorage.openDatabaseSync doesn't allow specifying a database file directly, just a database name.

    There is a sort of hack. The SQLite database is stored in OfflineStorage path with its name md5 encrypted. You can copy your database to this path with its name md5 hashed. Then as per this example open this database by passing its original non-hashed name.



  • Sorry, it's been a while, but now I'm working on my project again :]

    @p3c0

    There is a sort of hack. The SQLite database is stored in OfflineStorage path with its name md5 encrypted. You can copy your database to this path with its name md5 hashed. Then as per this example open this database by passing its original non-hashed name.

    Uuhh, that sound's messy. But well, if this is the way to go... It'd be useful to at least read out the OfflineStorage dir at runtime and copy the files there instead of trying to do that statically beforehand. This is possible with QQmlEngine::offlineStoragePath, even changing that path is possible thanks to QQmlEngine::setOfflineStoragePath.

    But here the problems start: Exposing the path to QtQuick or changing it before running the test involves some C++ magic. Currently my test runner file looks exactly like this:

    #include <QtQuickTest/quicktest.h>
    // TasklistCalcTestSet is just a convenient name for reports - not linked to any of the main project entities
    QUICK_TEST_MAIN(TasklistTestSet)
    

    I'm afraid there's not much room here for adding additional set-up code, is it? And the code of this macro also doesn't look like I want to customize it and replace the one shipped with Qt with my own setup-code, does it?

    Any ideas what I can do there?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.