tests extracted common code to library class

Extracted for tst_collection_models/relations tests
to the TestUtils::Common::Collection.
This commit is contained in:
silverqx
2023-05-16 15:12:50 +02:00
parent 58a197a5ca
commit b37147fecf
5 changed files with 697 additions and 746 deletions

View File

@@ -22,6 +22,7 @@ project(${TinyUtils_ns}
# ---
add_library(${TinyUtils_target}
src/common/collection.hpp
src/databases.hpp
src/export.hpp
src/fs.hpp

View File

@@ -0,0 +1,144 @@
#pragma once
#ifndef TINYUTILS_COMMON_COLLECTION_HPP
#define TINYUTILS_COMMON_COLLECTION_HPP
#include "orm/tiny/types/modelscollection.hpp"
namespace Orm::Tiny::Types
{
template<DerivedCollectionModel Model>
class ModelsCollection;
}
namespace TestUtils::Common
{
/*! Common code for tst_collection_xyz auto tests, library class. */
class Collection final
{
Q_DISABLE_COPY_MOVE(Collection)
/*! Alias for the ModelsCollection. */
template<typename T>
using ModelsCollection = Orm::Tiny::Types::ModelsCollection<T>;
public:
/*! Deleted default constructor, this is a pure library class. */
Collection() = delete;
/*! Deleted destructor. */
~Collection() = delete;
/*! Orm::Column constant "album_id" (perf. reason, one time initialization). */
inline static const Orm::Column album_id {std::in_place_index<0>, "album_id"};
/*! QString constant "album_id" (perf. reason, one time initialization). */
inline static const auto album_id_s = QStringLiteral("album_id");
/*! QString constant "albumImages" (perf. reason, one time initialization). */
inline static const auto albumImages = QStringLiteral("albumImages");
/*! QString constant "ext" (perf. reason, one time initialization). */
inline static const auto ext = QStringLiteral("ext");
/*! Verify primary key IDs in the given collection. */
template<typename M>
static bool
verifyIds(const ModelsCollection<M *> &actual,
const std::unordered_set<quint64> &expected);
/*! Verify primary key IDs in the given collection. */
template<typename M>
static bool
verifyIds(const ModelsCollection<M> &actual,
const std::unordered_set<quint64> &expected);
/*! Verify primary key IDs in the given vector. */
inline static bool
verifyIds(const QVector<quint64> &actual,
const std::unordered_set<quint64> &expected);
/*! Verify primary key IDs in the given vector. */
template<typename T>
static bool
verifyIdsQVariant(const QVector<QVariant> &actual,
const std::unordered_set<T> &expected);
/*! Verify attribute values in the given vector. */
template<typename T, typename M>
static bool
verifyAttributeValues(
const QString &attribute, const ModelsCollection<M *> &actual,
const std::unordered_set<T> &expected);
/*! Get the value of the model's primary key casted to the Derived::KeyType. */
template<typename M>
static typename M::KeyType getKeyCasted(M *model);
/*! Get the value of the model's primary key casted to the Derived::KeyType. */
template<typename M>
static typename M::KeyType getKeyCasted(const M &model);
};
/* public */
template<typename M>
bool Collection::verifyIds(const ModelsCollection<M *> &actual,
const std::unordered_set<quint64> &expected)
{
return std::ranges::all_of(actual, [&expected](M *const model)
{
return expected.contains(getKeyCasted(model));
});
}
template<typename M>
bool Collection::verifyIds(const ModelsCollection<M> &actual,
const std::unordered_set<quint64> &expected)
{
return std::ranges::all_of(actual, [&expected](const M &model)
{
return expected.contains(getKeyCasted(model));
});
}
bool Collection::verifyIds(const QVector<quint64> &actual,
const std::unordered_set<quint64> &expected)
{
return std::ranges::all_of(actual, [&expected](const quint64 id)
{
return expected.contains(id);
});
}
template<typename T>
bool Collection::verifyIdsQVariant(const QVector<QVariant> &actual,
const std::unordered_set<T> &expected)
{
return std::ranges::all_of(actual, [&expected](const QVariant &id)
{
return expected.contains(id.template value<T>());
});
}
template<typename T, typename M>
bool Collection::verifyAttributeValues(
const QString &attribute, const ModelsCollection<M *> &actual,
const std::unordered_set<T> &expected)
{
return std::ranges::all_of(actual,
[&attribute, &expected](M *const model)
{
return expected.contains(model->getAttribute(attribute).template value<T>());
});
}
template<typename M>
typename M::KeyType
Collection::getKeyCasted(M *const model)
{
return model->getKey().template value<typename M::KeyType>();
}
template<typename M>
typename M::KeyType
Collection::getKeyCasted(const M &model)
{
return model.getKey().template value<typename M::KeyType>();
}
} // namespace TestUtils::Common
#endif // TINYUTILS_COMMON_COLLECTION_HPP

View File

@@ -1,6 +1,7 @@
INCLUDEPATH += $$PWD
HEADERS += \
$$PWD/common/collection.hpp \
$$PWD/databases.hpp \
$$PWD/export.hpp \
$$PWD/fs.hpp \