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:
void AudioLibrary::createDatabase()
{
db.setDatabaseName("test.db");
db.open();
} else {
if(!db.isOpen()) db.open();
}
Q_ASSERT(db.isValid());
Q_ASSERT(db.isOpen());
QDjango::setDatabase(db);
QDjango::registerModel<Song>();
QDjango::createTables();
}
void AudioLibrary::createDatabase()
{
QSqlDatabase db;
if(!QSqlDatabase::contains("qt_sql_default_connection")) {
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("test.db");
db.open();
} else {
db = QSqlDatabase::database();
if(!db.isOpen()) db.open();
}
Q_ASSERT(db.isValid());
Q_ASSERT(db.isOpen());
QDjango::setDatabase(db);
QDjango::registerModel<Song>();
QDjango::createTables();
}
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.
void tst_Song::initTestCase()
{
AudioLibrary lib;
lib.createDatabase();
}
void tst_Song::testCreationAndStorageOfNewSong()
{
Song song;
AudioLibrary lib;
lib.createDatabase();
song.setTitle("Test Album");
song.setArtist("Marcel");
song.save();
}
void tst_Song::initTestCase()
{
AudioLibrary lib;
lib.createDatabase();
}
void tst_Song::testCreationAndStorageOfNewSong()
{
Song song;
AudioLibrary lib;
lib.createDatabase();
song.setTitle("Test Album");
song.setArtist("Marcel");
song.save();
}
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:
class Album;
class AUDIOLIBRARYSHARED_EXPORT Song : public QDjangoModel
{
Q_OBJECT
Q_PROPERTY(QString artist READ getArtist WRITE setArtist
) Q_PROPERTY(QString title READ title WRITE setTitle
) Q_PROPERTY(qint64 year READ year WRITE setYear)
Q_PROPERTY(QString codec READ codec WRITE setCodec
) Q_PROPERTY(qint64 bitrate READ bitrate WRITE setBitrate)
Q_PROPERTY(qint64 size READ size WRITE setSize)
Q_PROPERTY(QString fileLocation READ fileLocation WRITE setFileLocation
)
public:
Song();
qint64 year() const;
void setYear(qint64 year);
qint64 bitrate() const;
void setBitrate(qint64 bitrate);
qint64 size() const;
void setSize(qint64 size);
void setFileLocation
(QString fileLocation
);
private:
QList<qint64> m_albumIds;
qint64 m_year;
qint64 m_bitrate;
qint64 m_size;
};
class Album;
class AUDIOLIBRARYSHARED_EXPORT Song : public QDjangoModel
{
Q_OBJECT
Q_PROPERTY(QString artist READ getArtist WRITE setArtist)
Q_PROPERTY(QString title READ title WRITE setTitle)
Q_PROPERTY(qint64 year READ year WRITE setYear)
Q_PROPERTY(QString codec READ codec WRITE setCodec)
Q_PROPERTY(qint64 bitrate READ bitrate WRITE setBitrate)
Q_PROPERTY(qint64 size READ size WRITE setSize)
Q_PROPERTY(QString fileLocation READ fileLocation WRITE setFileLocation)
public:
Song();
QString getArtist() const;
void setArtist(QString m_artist);
QString title() const;
void setTitle(QString title);
qint64 year() const;
void setYear(qint64 year);
QString codec() const;
void setCodec(QString codec);
qint64 bitrate() const;
void setBitrate(qint64 bitrate);
qint64 size() const;
void setSize(qint64 size);
QString fileLocation() const;
void setFileLocation(QString fileLocation);
private:
QString m_artist;
QList<qint64> m_albumIds;
QString m_title;
qint64 m_year;
QString m_codec;
qint64 m_bitrate;
qint64 m_size;
QString m_fileLocation;
};
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
Bookmarks