Results 1 to 1 of 1

Thread: QTestLib with QDjango

  1. #1
    Join Date
    Jun 2007
    Location
    Netherlands
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QTestLib with QDjango

    Hello all,

    I am trying to write an application that uses QDjango to handle the database communication of my data classes. For my application I try to use QTestlib to make sure everything is working correctly.

    In QDjango to connection to the database is stored in a Static variable so it can be used over the entire application. Now when I test my application I test the setup of the database and if the database connection gets stored correctly.

    The fuction that I call to set the database is:
    Qt Code:
    1. void AudioLibrary::createDatabase()
    2. {
    3.  
    4. if(!QSqlDatabase::contains("qt_sql_default_connection")) {
    5. db = QSqlDatabase::addDatabase("QSQLITE");
    6. db.setDatabaseName("test.db");
    7. db.open();
    8. } else {
    9. db = QSqlDatabase::database();
    10.  
    11. if(!db.isOpen()) db.open();
    12. }
    13.  
    14. Q_ASSERT(db.isValid());
    15. Q_ASSERT(db.isOpen());
    16.  
    17. QDjango::setDatabase(db);
    18.  
    19. QDjango::registerModel<Song>();
    20. QDjango::createTables();
    21. }
    To copy to clipboard, switch view to plain text mode 

    In this code I also tell QDjango about my class Song that has to be stored in the database.

    Now when I run the test program this works. Now when I try to test the song class if it stores the data correctly a problem arises.

    Qt Code:
    1. void tst_Song::initTestCase()
    2. {
    3. AudioLibrary lib;
    4. lib.createDatabase();
    5. }
    6.  
    7. void tst_Song::testCreationAndStorageOfNewSong()
    8. {
    9. Song song;
    10.  
    11. AudioLibrary lib;
    12. lib.createDatabase();
    13.  
    14. song.setTitle("Test Album");
    15. song.setArtist("Marcel");
    16. song.save();
    17. }
    To copy to clipboard, switch view to plain text mode 

    To make sure there is a database connection available I also call the function that assings the database connection in the initialization of the test class for Song. When I debugged this application and stepped through the code there I could see that there was already a database assigned so it worked correctly. To narrow the point of failure in my application I also tried to assign the database again in the actual testcase, here again there was a database present.

    Then were the actual problem is, when I call save on my class, which is derived from QDjangoModel, it tries to get the database connection but it isn't available anymore. The static variable is NULL.

    The song class:
    Qt Code:
    1. class Album;
    2.  
    3. class AUDIOLIBRARYSHARED_EXPORT Song : public QDjangoModel
    4. {
    5. Q_OBJECT
    6. Q_PROPERTY(QString artist READ getArtist WRITE setArtist)
    7. Q_PROPERTY(QString title READ title WRITE setTitle)
    8. Q_PROPERTY(qint64 year READ year WRITE setYear)
    9. Q_PROPERTY(QString codec READ codec WRITE setCodec)
    10. Q_PROPERTY(qint64 bitrate READ bitrate WRITE setBitrate)
    11. Q_PROPERTY(qint64 size READ size WRITE setSize)
    12. Q_PROPERTY(QString fileLocation READ fileLocation WRITE setFileLocation)
    13.  
    14. public:
    15. Song();
    16.  
    17. QString getArtist() const;
    18. void setArtist(QString m_artist);
    19.  
    20. QString title() const;
    21. void setTitle(QString title);
    22.  
    23. qint64 year() const;
    24. void setYear(qint64 year);
    25.  
    26. QString codec() const;
    27. void setCodec(QString codec);
    28.  
    29. qint64 bitrate() const;
    30. void setBitrate(qint64 bitrate);
    31.  
    32. qint64 size() const;
    33. void setSize(qint64 size);
    34.  
    35. QString fileLocation() const;
    36. void setFileLocation(QString fileLocation);
    37.  
    38. private:
    39. QString m_artist;
    40. QList<qint64> m_albumIds;
    41. QString m_title;
    42. qint64 m_year;
    43. QString m_codec;
    44. qint64 m_bitrate;
    45. qint64 m_size;
    46. QString m_fileLocation;
    47. };
    To copy to clipboard, switch view to plain text mode 

    So somewhere in my testcase when calling save I am trying to access the database connection which is static, and how I understand it that should be available, but it is somehow not the same variable that is assigned in createDatabase.

    Hope you guys can clarify this for me.

    Regards
    Eekhoorn12
    Attached Files Attached Files
    Last edited by eekhoorn12; 16th January 2012 at 17:08. Reason: Added project as attachment

Similar Threads

  1. QTestlib problem
    By asinghma in forum Newbie
    Replies: 0
    Last Post: 25th October 2010, 11:56
  2. QTestlib - How to add own types
    By AlGaN in forum Qt Programming
    Replies: 1
    Last Post: 14th October 2010, 20:22
  3. qtestlib
    By Jordan in forum Qt Programming
    Replies: 7
    Last Post: 28th September 2010, 12:47
  4. How to use QTestLib to my project??
    By Klaxuz in forum Newbie
    Replies: 1
    Last Post: 3rd June 2010, 15:57
  5. GUI runner of QTestLib
    By Intaek Lim in forum Qt-based Software
    Replies: 1
    Last Post: 25th April 2007, 09:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.