<commit_before>#include <QtTest/QtTest>

// file configured by cmake
// with required paths
#include "medSqlTest.config.h"

#include <dtkCore/dtkPluginManager.h>

#include <medSql/medDatabaseController.h>
#include <medSql/medDatabaseImporter.h>
#include <medCore/medStorage.h>
#include <medCore/medDataIndex.h>

void checkDirectoryHierarchyAndThumbnails(QString dbPath, QString patientName, QString studyName, QString sessionName, unsigned int thumbnailsCount);
int countRowsInTable(QSqlDatabase db, QString tableName);
bool checkIfRowExists(QSqlDatabase db, QString tableName, QString columnName, QString value);
bool checkIfRowExists(QSqlDatabase db, QString tableName, QHash<QString, QString> columnValues);

class medDatabaseImporterTest: public QObject
{
    Q_OBJECT

private:
    QString dataPath;
    QString dbPath;

private slots:

    /*
     * initTestCase() is called before the
     * execution of all the test cases.
     * If it fails, no test is executed.
     */
    void initTestCase();

    /*
     * init() is called before each test.
     * If it fails, the following
     * test is not executed.
     */
    void init();

    /*
     * cleanup() is called after each test.
     */
    void cleanup();

    /*
     * cleanupTestCase() is called
     * after all test have been executed.
     */
    void cleanupTestCase();


private slots:

    // every function here is a test, unless they end with "_data"
    // in this case "importSingleVolume_data()" will prepare some data
    // to run "importSingleVolume()" different times
    // with different input values
    void importSingleVolume_data();
    void importSingleVolume();
};

void medDatabaseImporterTest::initTestCase()
{
    QString pluginsPath = MEDINRIA_PLUGINS_PATH;
    QDir pluginsDir(pluginsPath);
    if (!pluginsDir.exists())
        QFAIL("Medinria plugins are required to run this test.");

    this->dataPath = MEDINRIA_TEST_DATA_ROOT;
    QDir dataDir(this->dataPath);
    if (!dataDir.exists())
        QFAIL("Medinria data directory is required to run this test.");

     // load medinria plugins, we will need data types, readers and writers
     dtkPluginManager::instance()->setPath(pluginsPath);
     dtkPluginManager::instance()->initialize();

     // create temporary directory for new database
     this->dbPath = QDir::tempPath() + "/medinria-test-db";

     medStorage::setDataLocation(this->dbPath);
}

void medDatabaseImporterTest::init()
{
    // create one new clean database for each test

    QPointer<medDatabaseControllerImpl> dbc = medDatabaseController::instance();

    // create connection and create database schema
    if(!dbc->createConnection())
        QFAIL("Unable to create a connection to the database");
}

void medDatabaseImporterTest::cleanup()
{
    // clean objects and destroy database after each test
    // a new database is going to be used
    medDatabaseController::instance()->closeConnection();
    medDatabaseController::destroy();
    medStorage::removeDir(dbPath, NULL);
}

void medDatabaseImporterTest::cleanupTestCase()
{
    //qDebug() << "cleanupTestCase()";
}

void medDatabaseImporterTest::importSingleVolume_data()
{
    // here we will prepare data for running "importSingleVolume" test
    // many times with different input

    QTest::addColumn<QString>("pathToFileOrDirectory"); // what we want to import
    QTest::addColumn<QString>("patientName");
    QTest::addColumn<QString>("studyName");
    QTest::addColumn<QString>("sessionName");
    QTest::addColumn<int>("imageCount"); // number of images inside the single volume

    // if we are importing a directory this helps
    // building the filenames inside (e.g. prefix+023+suffix = cta_022.dcm)
    QTest::addColumn<QString>("filesPrefix");
    QTest::addColumn<QString>("filesSuffix");

    QString fileOrDirToImport;

    // test with NRRD
    fileOrDirToImport = QDir::separator() + QString("Clatz") + QDir::separator() + QString("I1.nhdr");
    QTest::newRow("I1.nhdr") << fileOrDirToImport << "<NAME>" << "EmptyStudy" << "I11" << 64 << "" << "";

    // test with NIfTI
    fileOrDirToImport = QDir::separator() + QString("mask") + QDir::separator()+ QString("1-session01.nii");
    QTest::newRow("1-session01.nii") << fileOrDirToImport << "<NAME>" << "EmptyStudy" << "1-session011" << 26 << "" << "";

    // test with a directory full of DICOMs
    fileOrDirToImport = QDir::separator() + QString("Dicoms") + QDir::separator()+ QString("CTA");
    QTest::newRow("Dicoms-CTA") << fileOrDirToImport << "Anonymized-JA." << "Anonymized with DicomWorks" << "Anonymized with DicomWorks1" << 202 << "CT_" << ".dcm";
}

void medDatabaseImporterTest::importSingleVolume()
{
    QFETCH(QString, pathToFileOrDirectory);
    QString fullPathToFileOrDirectory = dataPath + pathToFileOrDirectory;
    QFileInfo info(fullPathToFileOrDirectory);

    bool indexWithoutCopying = false;
    // import
    medDatabaseImporter *importer = new medDatabaseImporter(info.absoluteFilePath(), indexWithoutCopying);
    importer->run();
<commit_msg>Added check for test data.<commit_after>#include <QtTest/QtTest>

// file configured by cmake
// with required paths
#include "medSqlTest.config.h"

#include <dtkCore/dtkPluginManager.h>

#include <medSql/medDatabaseController.h>
#include <medSql/medDatabaseImporter.h>
#include <medCore/medStorage.h>
#include <medCore/medDataIndex.h>

void checkDirectoryHierarchyAndThumbnails(QString dbPath, QString patientName, QString studyName, QString sessionName, unsigned int thumbnailsCount);
int countRowsInTable(QSqlDatabase db, QString tableName);
bool checkIfRowExists(QSqlDatabase db, QString tableName, QString columnName, QString value);
bool checkIfRowExists(QSqlDatabase db, QString tableName, QHash<QString, QString> columnValues);

class medDatabaseImporterTest: public QObject
{
    Q_OBJECT

private:
    QString dataPath;
    QString dbPath;

private slots:

    /*
     * initTestCase() is called before the
     * execution of all the test cases.
     * If it fails, no test is executed.
     */
    void initTestCase();

    /*
     * init() is called before each test.
     * If it fails, the following
     * test is not executed.
     */
    void init();

    /*
     * cleanup() is called after each test.
     */
    void cleanup();

    /*
     * cleanupTestCase() is called
     * after all test have been executed.
     */
    void cleanupTestCase();


private slots:

    // every function here is a test, unless they end with "_data"
    // in this case "importSingleVolume_data()" will prepare some data
    // to run "importSingleVolume()" different times
    // with different input values
    void importSingleVolume_data();
    void importSingleVolume();
};

void medDatabaseImporterTest::initTestCase()
{
    QString pluginsPath = MEDINRIA_PLUGINS_PATH;
    QDir pluginsDir(pluginsPath);
    if (!pluginsDir.exists())
        QFAIL("Medinria plugins are required to run this test.");

    this->dataPath = MEDINRIA_TEST_DATA_ROOT;
    QDir dataDir(this->dataPath);
    if (!dataDir.exists())
        QFAIL("Medinria data directory is required to run this test.");

     // load medinria plugins, we will need data types, readers and writers
     dtkPluginManager::instance()->setPath(pluginsPath);
     dtkPluginManager::instance()->initialize();

     // create temporary directory for new database
     this->dbPath = QDir::tempPath() + "/medinria-test-db";

     medStorage::setDataLocation(this->dbPath);
}

void medDatabaseImporterTest::init()
{
    // create one new clean database for each test

    QPointer<medDatabaseControllerImpl> dbc = medDatabaseController::instance();

    // create connection and create database schema
    if(!dbc->createConnection())
        QFAIL("Unable to create a connection to the database");
}

void medDatabaseImporterTest::cleanup()
{
    // clean objects and destroy database after each test
    // a new database is going to be used
    medDatabaseController::instance()->closeConnection();
    medDatabaseController::destroy();
    medStorage::removeDir(dbPath, NULL);
}

void medDatabaseImporterTest::cleanupTestCase()
{
    //qDebug() << "cleanupTestCase()";
}

void medDatabaseImporterTest::importSingleVolume_data()
{
    // here we will prepare data for running "importSingleVolume" test
    // many times with different input

    QTest::addColumn<QString>("pathToFileOrDirectory"); // what we want to import
    QTest::addColumn<QString>("patientName");
    QTest::addColumn<QString>("studyName");
    QTest::addColumn<QString>("sessionName");
    QTest::addColumn<int>("imageCount"); // number of images inside the single volume

    // if we are importing a directory this helps
    // building the filenames inside (e.g. prefix+023+suffix = cta_022.dcm)
    QTest::addColumn<QString>("filesPrefix");
    QTest::addColumn<QString>("filesSuffix");

    QString fileOrDirToImport;

    // test with NRRD
    fileOrDirToImport = QDir::separator() + QString("Clatz") + QDir::separator() + QString("I1.nhdr");
    QTest::newRow("I1.nhdr") << fileOrDirToImport << "<NAME>" << "EmptyStudy" << "I11" << 64 << "" << "";

    // test with NIfTI
    fileOrDirToImport = QDir::separator() + QString("mask") + QDir::separator()+ QString("1-session01.nii");
    QTest::newRow("1-session01.nii") << fileOrDirToImport << "<NAME>" << "EmptyStudy" << "1-session011" << 26 << "" << "";

    // test with a directory full of DICOMs
    fileOrDirToImport = QDir::separator() + QString("Dicoms") + QDir::separator()+ QString("CTA");
    QTest::newRow("Dicoms-CTA") << fileOrDirToImport << "Anonymized-JA." << "Anonymized with DicomWorks" << "Anonymized with DicomWorks1" << 202 << "CT_" << ".dcm";
}

void medDatabaseImporterTest::importSingleVolume()
{
    QFETCH(QString, pathToFileOrDirectory);
    QString fullPathToFileOrDirectory = dataPath + pathToFileOrDirectory;
    QFileInfo info(fullPathToFileOrDirectory);

    if(!info.exists())
        QFAIL("Test data does not exists.");

    bool indexWithoutCopying = false;
    // import
    medDatabaseImporter *importer = new medDatabaseImporter(info.absoluteFilePath(), indexWithoutCopying);
    importer->run();

{'fraction_non_alphanumeric': 0.12330516800943211, 'fraction_numerical': 0.005502063273727648, 'mean_word_length': 4.081877184223664, 'pattern_counts': {'":': 0, '<': 125, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3009689', 'n_tokens_mistral': 3031, 'n_tokens_neox': 2943, 'n_words': 790}