Add per-module unit-test dirs for the Release 2 scope

One unit-test/ directory per module/target, mirroring the existing
TetraIF/unit-test/ layout (one QtTest executable per module, guarded by
BUILD_TESTING and the module target, each registered with ctest):

  Lts/Common/CodeGen      codegen_unit_test
  Lts/Common/LtsCoordinate ltscoordinate_unit_test
  Lts/Common/LtsSession   ltssession_unit_test
  Lts/DDS/Core            ddscore_unit_test
  Lts/DDS/QosProfile      ddsqos_unit_test
  Lts/Tools/LogViewer     logviewer_unit_test
  Lts/Tools/SqlBrowser    sqlbrowser_unit_test
  Z/src/NGLLogger         ngllogger_unit_test
  Z/src/WMS_Interface     wms_json_ds_unit_test
  Z/src/nglapplication    nglcoreapplication_unit_test
  Z/src/nglcompression    nglcompression_unit_test
  Z/src/nglsettings       nglsettings_unit_test
  Z/src/ngltranslator     ngltranslator_unit_test

Where a module is built with hidden visibility and has no export macro
(LogViewer, SqlBrowser, ngltranslator, NGLLogger, nglcompression,
nglsettings), its sources are compiled directly into the test
executable, the same pattern as prtcnv_unit_test.

ctest baseline in the container (QT_QPA_PLATFORM=offscreen): 20/23
passed (13 new + 7 pre-existing in-scope tests), 3 Not Run as before
(nglcomp_test, GrantleeParserSmoke, utils_test).

README: ctest baseline table extended to the 23 registered tests and
section 2.4 now documents the per-module test layout. ddsview has no
unit test (GUI tool, XmlHelper needs the DDS dynamic-xtypes runtime).
This commit is contained in:
berrux-dev committed 2026-11-15 01:08:17 +01:00
1 parent a62b729577
commit 09b6df8bc3
40 files changed
+4966 -17

No files matched your search

@@ -170,3 +170,8 @@ endif()
ELSE()
message(STATUS "BUILD_GRANTLEE disabled or Grantlee not found; skipping codegen_grantlee, ngl_codegen, and codegen_gui targets")
ENDIF()
#
# Unit tests
#
ADD_SUBDIRECTORY(unit-test)
@@ -0,0 +1,20 @@
#
# Unit tests for the CodeGen module.
# One test executable per module, each registered with ctest.
#
if (NOT BUILD_TESTING)
return()
endif()
# --- CodeGen: string helpers, data classes and Grantlee template-context
# --- wrappers (pure logic; the KTextTemplate engine is covered by
# --- GrantleeParserSmoke)
if (TARGET codegen_grantlee)
ADD_EXECUTABLE(codegen_unit_test codegen_unit_test.cpp)
TARGET_LINK_LIBRARIES(codegen_unit_test
codegen_grantlee
Qt6::Core
Qt6::Test
)
ADD_TEST(NAME codegen_unit_test COMMAND $<TARGET_FILE:codegen_unit_test>)
endif()
@@ -0,0 +1,701 @@
//
// Unit tests for the ngl_codegen module (Lts/Common/CodeGen): the pure-logic
// string helpers (StructBuilder statics), the codegen_base data classes and
// the KTextTemplate context wrappers (GrantleeWrappers).
// No template execution is done here; the Grantlee/KTextTemplate engine
// itself is covered by the GrantleeParserSmoke smoke test.
//
#include <CodeGen/GrantleeWrappers.h>
#include <CodeGen/SqlGenerator.h>
#include <CodeGen/StructBuilder.h>
#include <QtCore/QObject>
#include <QtCore/QSharedPointer>
#include <QtCore/QString>
#include <QtCore/QVariant>
#include <QtTest/QTest>
using namespace CodeGen;
// Minimal concrete DataTypeInfo for the IdlDataWrapper tests (the module's
// own concrete data types are private to StructBuilder.cpp).
class TestDataTypeInfo : public DataTypeInfo
{
public:
TestDataTypeInfo(const QString& idlType, const QString& cppType, bool isPod)
: DataTypeInfo(QStringLiteral("test"), isPod), m_idlType(idlType), m_cppType(cppType)
{
}
QString idlType() const override final
{
return m_idlType;
}
QString ddsType() const override final
{
return m_cppType;
}
QString sqlType() const override final
{
return m_cppType;
}
QString cppType() const override final
{
return m_cppType;
}
private:
QString m_idlType;
QString m_cppType;
};
static MemberInformation makeMember(const QString& table, const QString& column)
{
MemberInformation mi;
mi.ea.table = table;
mi.ea.column = column;
mi.db.table = table;
mi.db.columnName = column;
mi.cpp.memberName = column;
return mi;
}
class codegen_unit_test : public QObject
{
Q_OBJECT
private slots:
void firstCharLowercase();
void camelCaseString();
void camelCaseToUnderline();
void nameWrapperTransforms();
void memberWrapperGetters();
void memberWrapperVariantConversion();
void memberWrapperDdsToJsonElement();
void memberWrapperJsonElementToDds();
void memberWrapperDbToJsonElement();
void memberWrapperJsonElementToDb();
void memberWrapperJsonPrePostFallback();
void memberListWrapperFilters();
void dataTopicWrapper();
void functionTopicWrapper();
void idlWrappers();
void dbStructWrapper();
void codegenBaseDataClasses();
};
void codegen_unit_test::firstCharLowercase()
{
QCOMPARE(StructBuilder::firstCharLowercase(QString()), QString());
QCOMPARE(StructBuilder::firstCharLowercase(QStringLiteral("FooBar")), QStringLiteral("fooBar"));
QCOMPARE(StructBuilder::firstCharLowercase(QStringLiteral("abc")), QStringLiteral("abc"));
}
void codegen_unit_test::camelCaseString()
{
// underscore-separated -> camelCase (default keeps the first character lower case)
QCOMPARE(StructBuilder::camelCaseString(QStringLiteral("foo_bar")), QStringLiteral("fooBar"));
QCOMPARE(StructBuilder::camelCaseString(QStringLiteral("foo_bar"), true), QStringLiteral("FooBar"));
QCOMPARE(StructBuilder::camelCaseString(QStringLiteral("foo")), QStringLiteral("foo"));
QCOMPARE(StructBuilder::camelCaseString(QStringLiteral("_foo")), QStringLiteral("Foo"));
// trailing underscore sets the "next is upper" flag but appends nothing
QCOMPARE(StructBuilder::camelCaseString(QStringLiteral("foo_")), QStringLiteral("foo"));
QCOMPARE(StructBuilder::camelCaseString(QString()), QString());
}
void codegen_unit_test::camelCaseToUnderline()
{
QCOMPARE(StructBuilder::camelCaseToUnderline(QStringLiteral("FooBarBaz")), QStringLiteral("foo_bar_baz"));
QCOMPARE(StructBuilder::camelCaseToUnderline(QStringLiteral("foo")), QStringLiteral("foo"));
QCOMPARE(StructBuilder::camelCaseToUnderline(QStringLiteral("A")), QStringLiteral("a"));
QCOMPARE(StructBuilder::camelCaseToUnderline(QStringLiteral("aB")), QStringLiteral("a_b"));
// digits are kept, an upper case after a digit still splits
QCOMPARE(StructBuilder::camelCaseToUnderline(QStringLiteral("Foo1Bar")), QStringLiteral("foo1_bar"));
QCOMPARE(StructBuilder::camelCaseToUnderline(QString()), QString());
}
void codegen_unit_test::nameWrapperTransforms()
{
QObject parent;
NameWrapper w(QStringLiteral("foo_bar"), &parent);
QCOMPARE(w.name(), QStringLiteral("foo_bar"));
QCOMPARE(w.nameCc(), QStringLiteral("FooBar"));
QCOMPARE(w.nameFlc(), QStringLiteral("fooBar"));
NameWrapper w2(QStringLiteral("FooBar"), &parent);
QCOMPARE(w2.nameCc(), QStringLiteral("FooBar"));
QCOMPARE(w2.nameFlc(), QStringLiteral("fooBar"));
}
void codegen_unit_test::memberWrapperGetters()
{
QObject parent;
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("a_name"));
mi.ea.type = QStringLiteral("varchar(255)");
mi.db.datatype = QStringLiteral("varchar(255)");
mi.db.columnNameView = QStringLiteral("a_name_v");
mi.db.hasSequence = true;
mi.cpp.structName = QStringLiteral("AStruct");
mi.cpp.defaultValue = QStringLiteral("''");
mi.cpp.type = QStringLiteral("QString");
mi.cpp.ddsType = QStringLiteral("dds::core::string255");
mi.cpp.isOptional = true;
mi.cpp.usedForCppStruct = true;
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.cppMemberName(), QStringLiteral("a_name"));
QCOMPARE(w.cppMemberNameCc(), QStringLiteral("AName"));
QCOMPARE(w.cppStructName(), QStringLiteral("AStruct"));
QCOMPARE(w.cppType(), QStringLiteral("QString"));
QCOMPARE(w.cppDefault(), QStringLiteral("''"));
QCOMPARE(w.dbTableName(), QStringLiteral("tbl_a"));
QCOMPARE(w.dbColumnName(), QStringLiteral("a_name"));
QCOMPARE(w.dbColumnNameView(), QStringLiteral("a_name_v"));
QCOMPARE(w.cppDdsType(), QStringLiteral("dds::core::string255"));
QCOMPARE(w.dbHasSequence(), true);
QCOMPARE(w.isOptional(), true);
QCOMPARE(w.isCppStruct(), true);
QCOMPARE(w.info().db.table, QStringLiteral("tbl_a"));
// view column name and dds type fall back when empty
MemberInformation mi2 = makeMember(QStringLiteral("tbl_b"), QStringLiteral("b_id"));
mi2.cpp.type = QStringLiteral("int");
MemberInformationWrapper w2(mi2, &parent);
QCOMPARE(w2.dbColumnNameView(), QStringLiteral("b_id"));
QCOMPARE(w2.cppDdsType(), QStringLiteral("int"));
}
void codegen_unit_test::memberWrapperVariantConversion()
{
QObject parent;
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.type = QStringLiteral("int");
mi.cpp.isOptional = false;
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.ddsToQVariant(), QStringLiteral("toQVariant(data.foo)"));
QCOMPARE(w.qVariantToDds(), QStringLiteral("data.foo = fromQVariant<int>(q.value(i++))"));
// optional members are wrapped in dds::core::optional<>
mi.cpp.isOptional = true;
mi.cpp.ddsType = QStringLiteral("MyStruct");
MemberInformationWrapper w2(mi, &parent);
QCOMPARE(w2.qVariantToDds(),
QStringLiteral("data.foo = fromQVariant<dds::core::optional<MyStruct>>(q.value(i++))"));
}
void codegen_unit_test::memberWrapperDdsToJsonElement()
{
QObject parent;
// plain member
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.type = QStringLiteral("int");
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.ddsToJsonElement(), QStringLiteral("obj[QStringLiteral(\"foo\")] = element.foo"));
// optional member goes through has_value()
mi.cpp.isOptional = true;
MemberInformationWrapper w2(mi, &parent);
QCOMPARE(w2.ddsToJsonElement(),
QStringLiteral("if (element.foo.has_value()) obj[QStringLiteral(\"foo\")] = *element.foo"));
// empty member name falls back to the member type
MemberInformation mi3;
mi3.cpp.type = QStringLiteral("int");
MemberInformationWrapper w3(mi3, &parent);
QCOMPARE(w3.ddsToJsonElement(), QStringLiteral("obj[QStringLiteral(\"int\")] = element;"));
}
void codegen_unit_test::memberWrapperJsonElementToDds()
{
QObject parent;
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.type = QStringLiteral("int");
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.jsonElementToDds(),
QStringLiteral("const QJsonValue val(objElement.value(QStringLiteral(\"foo\"))); element.foo = val"));
mi.cpp.isOptional = true;
MemberInformationWrapper w2(mi, &parent);
QCOMPARE(w2.jsonElementToDds(), QStringLiteral(
"const QJsonValue val(objElement.value(QStringLiteral(\"foo\"))); "
"if (!val.isNull()) element.foo = val"));
// empty member name falls back to the member type
MemberInformation mi3;
mi3.cpp.type = QStringLiteral("int");
MemberInformationWrapper w3(mi3, &parent);
QCOMPARE(w3.jsonElementToDds(), QStringLiteral("element = objElement.value(QStringLiteral(\"int\"))"));
}
void codegen_unit_test::memberWrapperDbToJsonElement()
{
QObject parent;
// plain member
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.type = QStringLiteral("int");
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.dbToJsonElement(), QStringLiteral("obj[QStringLiteral(\"foo\")] = element.foo"));
// optional (non-QDateTime) member is only written when it has a value
mi.cpp.type = QStringLiteral("QString");
mi.cpp.isOptional = true;
MemberInformationWrapper w2(mi, &parent);
QCOMPARE(w2.dbToJsonElement(),
QStringLiteral("if (!element.foo.isNull()) obj[QStringLiteral(\"foo\")] = element.foo"));
// optional QDateTime member uses isValid()
mi.cpp.type = QStringLiteral("QDateTime");
MemberInformationWrapper w3(mi, &parent);
QCOMPARE(w3.dbToJsonElement(),
QStringLiteral("if (element.foo.isValid()) obj[QStringLiteral(\"foo\")] = element.foo"));
// empty member name falls back to the member type
MemberInformation mi4;
mi4.cpp.type = QStringLiteral("int");
MemberInformationWrapper w4(mi4, &parent);
QCOMPARE(w4.dbToJsonElement(), QStringLiteral("obj[QStringLiteral(\"int\")] = element;"));
}
void codegen_unit_test::memberWrapperJsonElementToDb()
{
QObject parent;
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.type = QStringLiteral("int");
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.jsonElementToDb(),
QStringLiteral("const QJsonValue val(objElement.value(QStringLiteral(\"foo\"))); element.foo = val"));
mi.cpp.isOptional = true;
MemberInformationWrapper w2(mi, &parent);
QCOMPARE(w2.jsonElementToDb(), QStringLiteral(
"const QJsonValue val(objElement.value(QStringLiteral(\"foo\"))); "
"if (!val.isUndefined()) element.foo = val"));
// empty member name falls back to the member type
MemberInformation mi3;
mi3.cpp.type = QStringLiteral("int");
MemberInformationWrapper w3(mi3, &parent);
QCOMPARE(w3.jsonElementToDb(), QStringLiteral("element = objElement.value(QStringLiteral(\"int\"))"));
}
void codegen_unit_test::memberWrapperJsonPrePostFallback()
{
QObject parent;
// json pre/post strings win over the variant pre/post strings when set
MemberInformation mi = makeMember(QStringLiteral("tbl_a"), QStringLiteral("foo"));
mi.cpp.toVariantPre = QStringLiteral("T(");
mi.cpp.toVariantPost = QStringLiteral(")");
mi.cpp.fromVariantPre2 = QStringLiteral("V(");
mi.cpp.fromVariantPost = QStringLiteral(")");
mi.cpp.toJsonPre = QStringLiteral("J(");
mi.cpp.toJsonPost = QStringLiteral(")");
mi.cpp.fromJsonPre2 = QStringLiteral("F(");
mi.cpp.fromJsonPost = QStringLiteral(")");
MemberInformationWrapper w(mi, &parent);
QCOMPARE(w.cppToQvPre(), QStringLiteral("T("));
QCOMPARE(w.cppToQvPost(), QStringLiteral(")"));
QCOMPARE(w.cppFromQvPre1(), QString());
QCOMPARE(w.cppFromQvPre2(), QStringLiteral("V("));
QCOMPARE(w.cppFromQvPost(), QStringLiteral(")"));
QCOMPARE(w.cppToJsonPre(), QStringLiteral("J("));
QCOMPARE(w.cppToJsonPost(), QStringLiteral(")"));
QCOMPARE(w.cppFromJsonPre2(), QStringLiteral("F("));
QCOMPARE(w.cppFromJsonPost(), QStringLiteral(")"));
// without json strings the variant strings are used
MemberInformation mi2 = makeMember(QStringLiteral("tbl_a"), QStringLiteral("bar"));
mi2.cpp.toVariantPre = QStringLiteral("T(");
mi2.cpp.fromVariantPre2 = QStringLiteral("V(");
MemberInformationWrapper w2(mi2, &parent);
QCOMPARE(w2.cppToJsonPre(), QStringLiteral("T("));
QCOMPARE(w2.cppToJsonPost(), QString());
QCOMPARE(w2.cppFromJsonPre2(), QStringLiteral("V("));
QCOMPARE(w2.cppFromJsonPost(), QString());
}
void codegen_unit_test::memberListWrapperFilters()
{
QObject parent;
MemberInformation cppMi = makeMember(QStringLiteral("t"), QStringLiteral("m1"));
cppMi.cpp.usedForCppStruct = true;
MemberInformation jsonMi = makeMember(QStringLiteral("t"), QStringLiteral("m2"));
jsonMi.cpp.usedForJson = true;
MemberInformation fkMi = makeMember(QStringLiteral("t"), QStringLiteral("m3"));
fkMi.db.isForeignKey = true;
MemberInformation selMi = makeMember(QStringLiteral("t"), QStringLiteral("m4"));
selMi.db.generateSelectRemove = true;
MemberInformation viewMi = makeMember(QStringLiteral("t"), QStringLiteral("m5"));
viewMi.db.usedForView = true;
MemberInformation keyMi = makeMember(QStringLiteral("t"), QStringLiteral("m6"));
keyMi.isDdsKey = true;
MemberInformation pkMi = makeMember(QStringLiteral("t"), QStringLiteral("m7"));
pkMi.db.isPrimaryKey = true;
QVector<MemberInformation> members;
members = {cppMi, jsonMi, fkMi, selMi, viewMi, keyMi, pkMi};
MemberInformationListWrapper w(QStringLiteral("members"), members, &parent);
QCOMPARE(w.member().size(), 7);
QCOMPARE(w.cppMember().size(), 1);
QCOMPARE(w.jsonMember().size(), 1);
QCOMPARE(w.fkMember().size(), 1);
QCOMPARE(w.selectRemoveMember().size(), 1);
QCOMPARE(w.viewMember().size(), 1);
MemberInformationWrapper* key = w.ddsKey();
QVERIFY(key != nullptr);
QCOMPARE(key->cppMemberName(), QStringLiteral("m6"));
MemberInformationWrapper* pk = w.primaryKey();
QVERIFY(pk != nullptr);
QCOMPARE(pk->cppMemberName(), QStringLiteral("m7"));
// an empty list has no key and no primary key
MemberInformationListWrapper w2(QStringLiteral("empty"), QVector<MemberInformation>(), &parent);
QCOMPARE(w2.member().size(), 0);
QVERIFY(w2.ddsKey() == nullptr);
QVERIFY(w2.primaryKey() == nullptr);
}
void codegen_unit_test::dataTopicWrapper()
{
QObject parent;
CompleteTopicTypeInformation ctti;
ctti.name = QStringLiteral("Topic");
ctti.serviceName = QStringLiteral("Svc");
DataTopicWrapper w(ctti, &parent);
QCOMPARE(w.name(), QStringLiteral("Topic"));
QCOMPARE(w.serviceName(), QStringLiteral("Svc"));
QCOMPARE(w.type(), QStringLiteral("Topic"));
QCOMPARE(w.typeFq(), QStringLiteral("Svc::Topic"));
QCOMPARE(w.dtsImpl(), true);
QCOMPARE(w.dtcImpl(), true);
QCOMPARE(w.routeToClient(), true);
QCOMPARE(w.routeToServer(), false);
QCOMPARE(w.routeInReplay(), true);
QCOMPARE(w.isAutoSubscribe(), true);
QCOMPARE(w.recordTopic(), false);
QVERIFY(w.recTable().isNull());
QVERIFY(w.key().isNull());
QCOMPARE(w.hasTrColumns(), false);
// an already service-qualified name is not qualified again
ctti.name = QStringLiteral("Svc::Topic");
DataTopicWrapper w2(ctti, &parent);
QCOMPARE(w2.typeFq(), QStringLiteral("Svc::Topic"));
// without a service name the type is returned as-is
CompleteTopicTypeInformation ctti3;
ctti3.name = QStringLiteral("Topic3");
DataTopicWrapper w3(ctti3, &parent);
QCOMPARE(w3.typeFq(), QStringLiteral("Topic3"));
// key column and translatable columns
QSharedPointer<DataTypeInfo> intType(new TestDataTypeInfo(QStringLiteral("int"), QStringLiteral("int"), true));
IdlData keyData;
keyData.name = QStringLiteral("id");
keyData.dataTypeInfo = intType;
w.setKeyColumnData(keyData);
QVERIFY(!w.key().isNull());
QCOMPARE(w.key().value<IdlDataWrapper*>()->name(), QStringLiteral("id"));
IdlData trData;
trData.name = QStringLiteral("tr1");
trData.dataTypeInfo = intType;
QVector<IdlData> trColumns;
trColumns = {trData};
w.setTrColumns(trColumns);
QCOMPARE(w.hasTrColumns(), true);
QCOMPARE(w.trColumns().size(), 1);
}
void codegen_unit_test::functionTopicWrapper()
{
QObject parent;
FunctionTopic ft;
ft.name = QStringLiteral("MyFunc");
ft.requestTopicName = QStringLiteral("ReqT");
ft.replyTopicName = QStringLiteral("RepT");
ft.replyTopicType = QStringLiteral("Core::FunctionReply");
ft.routeInReplay = true;
CompleteTopicTypeInformation req;
req.name = QStringLiteral("ReqType");
CompleteTopicTypeInformation rep;
rep.name = QStringLiteral("RepType");
FunctionTopicWrapper w(ft, req, rep, &parent);
QCOMPARE(w.name(), QStringLiteral("MyFunc"));
QCOMPARE(w.onReply(), QStringLiteral("onRepT"));
QCOMPARE(w.onRequest(), QStringLiteral("onReqT"));
QCOMPARE(w.onError(), QStringLiteral("onMyFuncError"));
QCOMPARE(w.isCoreFt(), true);
QCOMPARE(w.routeInReplay(), true);
QCOMPARE(w.reply(), QStringLiteral("RepT"));
QCOMPARE(w.replyFlc(), QStringLiteral("repT"));
QCOMPARE(w.replyHandler(), QStringLiteral("RepTHandler"));
QCOMPARE(w.replyHandlerFlc(), QStringLiteral("repTHandler"));
QCOMPARE(w.replyType(), QStringLiteral("RepType"));
QCOMPARE(w.replyTypeFro(), QStringLiteral("Core::FunctionReplyObject"));
QCOMPARE(w.request(), QStringLiteral("ReqT"));
QCOMPARE(w.requestFlc(), QStringLiteral("reqT"));
QCOMPARE(w.requestHandler(), QStringLiteral("ReqTHandler"));
QCOMPARE(w.requestHandlerFlc(), QStringLiteral("reqTHandler"));
QCOMPARE(w.requestType(), QStringLiteral("ReqType"));
QCOMPARE(w.replyHandlerHash(), QString());
// a non-core reply type keeps its own type name
ft.replyTopicType = QStringLiteral("SomeReply");
FunctionTopicWrapper w2(ft, req, rep, &parent);
QCOMPARE(w2.isCoreFt(), false);
QCOMPARE(w2.replyTypeFro(), QStringLiteral("RepType"));
}
void codegen_unit_test::idlWrappers()
{
QObject parent;
QSharedPointer<DataTypeInfo> podType(new TestDataTypeInfo(QStringLiteral("int"), QStringLiteral("int"), true));
QSharedPointer<DataTypeInfo> structType(new TestDataTypeInfo(QStringLiteral("Svc::MyStruct"),
QStringLiteral("Svc::MyStruct"), false));
IdlData idl;
idl.name = QStringLiteral("foo");
idl.isKey = true;
idl.isOptional = false;
idl.dataTypeInfo = podType;
IdlDataWrapper w(idl, &parent);
QCOMPARE(w.name(), QStringLiteral("foo"));
QCOMPARE(w.type(), QStringLiteral("int"));
QCOMPARE(w.cppType(), QStringLiteral("int"));
// POD types are passed by value (no const reference)
QCOMPARE(w.cppTypeCr(), QStringLiteral("int"));
QCOMPARE(w.isKey(), QStringLiteral(" \t //@key"));
QCOMPARE(w.isOptional(), QString());
QCOMPARE(w.nameGetter(), QStringLiteral("foo"));
IdlData opt;
opt.name = QStringLiteral("bar");
opt.isOptional = true;
opt.dataTypeInfo = structType;
IdlDataWrapper w2(opt, &parent);
QCOMPARE(w2.cppTypeCr(), QStringLiteral("const Svc::MyStruct &"));
QCOMPARE(w2.isKey(), QString());
QCOMPARE(w2.isOptional(), QStringLiteral(" \t //@Optional"));
// optional members are unwrapped via .get()
QCOMPARE(w2.nameGetter(), QStringLiteral("bar.get()"));
// IdlStructWrapper
CompleteTopicTypeInformation ctti;
ctti.name = QStringLiteral("MyStruct");
ctti.comment = QStringLiteral("struct comment");
ctti.baseValueType = QStringLiteral("BaseType");
IdlData m1;
m1.name = QStringLiteral("a");
m1.dataTypeInfo = podType;
IdlData m2;
m2.name = QStringLiteral("b");
m2.dataTypeInfo = structType;
ctti.members = {m1, m2};
IdlStructWrapper sw(ctti, &parent);
QCOMPARE(sw.name(), QStringLiteral("MyStruct"));
QCOMPARE(sw.comment(), QStringLiteral("struct comment"));
QCOMPARE(sw.baseValueType(), QStringLiteral("BaseType"));
// the default extensibility maps to the same string with and without RTI
QCOMPARE(sw.extensibility(), QStringLiteral("EXTENSIBLE_EXTENSIBILITY"));
QCOMPARE(sw.member().size(), 2);
QCOMPARE(sw.member().value(0).value<IdlDataWrapper*>()->name(), QStringLiteral("a"));
}
void codegen_unit_test::dbStructWrapper()
{
QObject parent;
QSharedPointer<TableInformation> tableA(new TableInformation());
tableA->name = QStringLiteral("tbl_a");
QSharedPointer<TableInformation> tableB(new TableInformation());
tableB->name = QStringLiteral("tbl_b");
CompleteStructInformation csi;
csi.structName = QStringLiteral("MyStruct");
csi.viewName = QStringLiteral("my_view");
csi.tables = {tableA, tableB};
csi.primaryTable = tableA;
MemberInformation pkA = makeMember(QStringLiteral("tbl_a"), QStringLiteral("a_id"));
pkA.db.isPrimaryKey = true;
pkA.db.usedForInsert = true;
MemberInformation nameA = makeMember(QStringLiteral("tbl_a"), QStringLiteral("a_name"));
nameA.db.usedForInsert = true;
MemberInformation fkB = makeMember(QStringLiteral("tbl_b"), QStringLiteral("b_a_id"));
fkB.db.usedForInsert = true;
fkB.fk = QSharedPointer<ForeignKeyInformation>(new ForeignKeyInformation());
fkB.fk->destination.tableName = QStringLiteral("tbl_a");
csi.member = {pkA, nameA, fkB};
DbNotifcationInformation notif1;
notif1.methodName = QStringLiteral("onInsertA");
DbNotifcationInformation notif2;
notif2.methodName = QStringLiteral("onDeleteA");
csi.dbNotifications = {notif1, notif2};
DbStructWrapper w(csi, &parent);
QCOMPARE(w.name(), QStringLiteral("MyStruct"));
QCOMPARE(w.member().size(), 3);
QCOMPARE(w.deleteWithoutPkAllowed(), false); // two tables
QCOMPARE(w.viewTableName(), QStringLiteral("my_view")); // multi-table -> view name
MemberInformationWrapper* pk = w.primaryKey();
QVERIFY(pk != nullptr);
QCOMPARE(pk->dbColumnName(), QStringLiteral("a_id"));
QCOMPARE(w.dbNotifications().size(), 2);
QCOMPARE(w.dbNotifications().value(0).toString(), QStringLiteral("onInsertA"));
QCOMPARE(w.dbNotifications().value(1).toString(), QStringLiteral("onDeleteA"));
// a single-table struct uses the primary table name as view name
CompleteStructInformation csiSingle;
csiSingle.structName = QStringLiteral("Single");
csiSingle.viewName = QStringLiteral("unused");
csiSingle.tables = {tableA};
csiSingle.primaryTable = tableA;
MemberInformation soloPk = makeMember(QStringLiteral("tbl_a"), QStringLiteral("a_id"));
soloPk.db.isPrimaryKey = true;
soloPk.db.hasSequence = true;
soloPk.db.usedForInsert = true;
csiSingle.member = {soloPk};
DbStructWrapper wSingle(csiSingle, &parent);
QCOMPARE(wSingle.deleteWithoutPkAllowed(), true);
QCOMPARE(wSingle.viewTableName(), QStringLiteral("tbl_a"));
// insertStructures groups the members per table (two tables)
QVariantList structures = w.insertStructures();
QCOMPARE(structures.size(), 2);
TableWrapper* twA = structures.value(0).value<TableWrapper*>();
TableWrapper* twB = structures.value(1).value<TableWrapper*>();
QVERIFY(twA != nullptr);
QVERIFY(twB != nullptr);
QCOMPARE(twA->name(), QStringLiteral("tbl_a"));
QCOMPARE(twA->member().size(), 2);
QVERIFY(twA->primaryKey() != nullptr);
QCOMPARE(twA->primaryKey()->dbColumnName(), QStringLiteral("a_id"));
// first of several tables carries the base id
QCOMPARE(twA->baseId()->dbColumnName(), QStringLiteral("a_id"));
// the FK member pointing to tbl_a becomes its serial member
QCOMPARE(twA->serialMember()->dbColumnName(), QStringLiteral("b_a_id"));
QCOMPARE(twB->name(), QStringLiteral("tbl_b"));
QCOMPARE(twB->member().size(), 1);
QVERIFY(twB->primaryKey() == nullptr);
QVERIFY(twB->baseId() == nullptr);
QVERIFY(twB->serialMember() == nullptr);
// insertStructures for a single table with a sequence primary key
QVariantList single = wSingle.insertStructures();
QCOMPARE(single.size(), 1);
TableWrapper* twSolo = single.value(0).value<TableWrapper*>();
QVERIFY(twSolo != nullptr);
QCOMPARE(twSolo->name(), QStringLiteral("tbl_a"));
QVERIFY(twSolo->primaryKey() != nullptr);
// the sequence PK is used as the serial member
QCOMPARE(twSolo->serialMember()->dbColumnName(), QStringLiteral("a_id"));
QVERIFY(twSolo->baseId() == nullptr);
}
void codegen_unit_test::codegenBaseDataClasses()
{
// StructBuilder::ServiceTypeStruct
StructBuilder::ServiceTypeStruct sts;
sts.service = QStringLiteral("Svc");
sts.type = QStringLiteral("Type");
sts.bIsSequence = true;
QCOMPARE(sts.isExternalType(), true);
QCOMPARE(sts.isSequence(), true);
QCOMPARE(sts.fqType(), QStringLiteral("Svc::Type"));
StructBuilder::ServiceTypeStruct local;
local.type = QStringLiteral("Plain");
QCOMPARE(local.isExternalType(), false);
// SqlGenerator::SqlStatements::isEmpty only considers the DROP statements
SqlGenerator::SqlStatements stmts;
QCOMPARE(stmts.isEmpty(), true);
stmts.createTables.append(QStringLiteral("CREATE TABLE t()"));
QCOMPARE(stmts.isEmpty(), true);
stmts.dropTables.append(QStringLiteral("DROP TABLE t"));
QCOMPARE(stmts.isEmpty(), false);
// EnumInformation
EnumInformation ei;
QCOMPARE(ei.isValid(), false);
ei.enumName = QStringLiteral("E");
QCOMPARE(ei.isValid(), true);
// MemberInformation isEmpty / toString (CSV format)
MemberInformation mi;
QCOMPARE(mi.isEmpty(), true);
mi.ea.table = QStringLiteral("t");
mi.ea.column = QStringLiteral("c");
mi.ea.type = QStringLiteral("int");
mi.db.table = QStringLiteral("t");
mi.db.columnName = QStringLiteral("c");
mi.cpp.memberName = QStringLiteral("c");
QCOMPARE(mi.isEmpty(), false);
QCOMPARE(mi.toString(),
QStringLiteral("t;c;int;t;c;;false;false;false;false;;c;false;false;;;;;;"));
// CompleteTopicTypeInformation
CompleteTopicTypeInformation ctti;
QCOMPARE(ctti.isValid(), false); // no name
ctti.name = QStringLiteral("S");
QCOMPARE(ctti.isValid(), false); // still no members
ctti.baseValueType = QStringLiteral("B");
QCOMPARE(ctti.hasBaseValue(), true);
IdlData m;
m.name = QStringLiteral("x");
m.dataTypeInfo = QSharedPointer<DataTypeInfo>(new TestDataTypeInfo(QStringLiteral("int"),
QStringLiteral("int"), true));
ctti.members = {m};
QCOMPARE(ctti.isValid(), true);
// CompleteTableInformation / CompleteStructInformation
CompleteTableInformation cti;
QCOMPARE(cti.isEmpty(), true);
MemberInformation col;
col.ea.column = QStringLiteral("c1");
cti.columns = {col};
QCOMPARE(cti.isEmpty(), false);
CompleteStructInformation csi;
QCOMPARE(csi.isEmpty(), true);
QSharedPointer<TableInformation> ti(new TableInformation());
ti->name = QStringLiteral("t");
csi.primaryTable = ti;
QCOMPARE(csi.isEmpty(), false);
}
QTEST_MAIN(codegen_unit_test)
#include "codegen_unit_test.moc"
@@ -28,3 +28,8 @@ NGL_TARGET_LINK_LIBRARIES(LtsCoordinate
)
ADD_SUBDIRECTORY(UnitTest_Coordinate)
#
# Unit tests
#
ADD_SUBDIRECTORY(unit-test)
@@ -0,0 +1,20 @@
#
# Unit tests for the LtsCoordinate module.
# One test executable per module, each registered with ctest.
#
if (NOT BUILD_TESTING)
return()
endif()
# --- LtsCoordinate: geo<->UTM conversion, MGRS formatting, distances, angles,
# --- elevation callback and batch LtsCoordVector conversion
if (TARGET LtsCoordinate)
ADD_EXECUTABLE(ltscoordinate_unit_test ltscoordinate_unit_test.cpp)
TARGET_LINK_LIBRARIES(ltscoordinate_unit_test
LtsCoordinate
LtsCoordTrans
Qt6::Core
Qt6::Test
)
ADD_TEST(NAME ltscoordinate_unit_test COMMAND $<TARGET_FILE:ltscoordinate_unit_test>)
endif()
@@ -0,0 +1,314 @@
//
// Unit tests for the LtsCoordinate module (Lts/Common/LtsCoordinate):
// LtsCoordinate (geo<->UTM conversion, MGRS formatting, distances, angles,
// elevation callback, conversion counter) and LtsCoordVector (batch
// geo->UTM conversion with zone caching).
//
// Note: geographic coordinates are radians everywhere (Core::GeoPosition
// and LtsCoordTrans::deg2rad/rad2deg), mirroring the LtsCoordTrans tests.
//
#include <LtsCoordinate/lts_coordinate.h>
#include <LtsCoordinate/lts_coord_vector.h>
#include <LtsCoordTrans/lts_coord_trans.h>
#include <Core/core.hpp>
#include <QtTest/QTest>
#include <QtCore/QPointF>
#include <cmath>
#include <vector>
using namespace LtsCoordTrans;
class ltscoordinate_unit_test : public QObject
{
Q_OBJECT
private slots:
void cleanupTestCase();
void constructorsAndGeoAccessors();
void validityChecks();
void valueIsNullOrNan();
void mgrsFormatting();
void geoUtmRoundtrip();
void fromUtm();
void utmCoordinateDiffZone();
void distanceUtm2D();
void distanceUtm3D();
void angleDegreeUtm2D();
void elevationCallback();
void conversionCounter();
void ltsCoordVector();
};
// The elevation callback is a static (process-wide) of LtsCoordinate —
// reset it so other tests in the suite are not affected.
void ltscoordinate_unit_test::cleanupTestCase()
{
LtsCoordinate::setElevationCallback(nullptr);
}
void ltscoordinate_unit_test::constructorsAndGeoAccessors()
{
const double lon = 8.6264954126 * deg2rad();
const double lat = 53.1138437183 * deg2rad();
LtsCoordinate coord(lon, lat, 42.5);
QCOMPARE(coord.geoPosition().lon, lon);
QCOMPARE(coord.geoPosition().lat, lat);
QCOMPARE(coord.z(), 42.5);
coord.setZ(1.0);
QCOMPARE(coord.z(), 1.0);
const QPointF pnt = coord.toGeoPointF();
QCOMPARE(pnt.x(), lon);
QCOMPARE(pnt.y(), lat);
const LtsCoordinate fromPnt(QPointF(lon, lat), 7.0);
QCOMPARE(fromPnt.z(), 7.0);
const LtsCoordinate def;
QVERIFY(std::isnan(def.z()));
const LtsCoordinate fromGeo = LtsCoordinate::fromGeoPosition(lon, lat, 3.0);
QCOMPARE(fromGeo.z(), 3.0);
const Core::GeoPosition2D geo2D(lon, lat);
const LtsCoordinate from2D(geo2D);
QCOMPARE(from2D.geoPosition().lon, lon);
QCOMPARE(from2D.geoPosition2D().lat, lat);
}
void ltscoordinate_unit_test::validityChecks()
{
// (0,0) is "null" for validity purposes (qFuzzyIsNull), not a real position
LtsCoordinate def;
QVERIFY(!def.hasGeo());
QVERIFY(!def.hasUtm());
QVERIFY(!def.isValid());
const double lon = 8.6264954126 * deg2rad();
const double lat = 53.1138437183 * deg2rad();
LtsCoordinate valid(lon, lat);
QVERIFY(valid.hasGeo());
QVERIFY(valid.isValid());
QVERIFY(!valid.hasUtm()); // not converted yet
(void)valid.utmCoordinate();
QVERIFY(valid.hasUtm());
QVERIFY(valid.utmCoordinate().zone > 0);
}
void ltscoordinate_unit_test::valueIsNullOrNan()
{
QVERIFY(LtsCoordinate::valueIsNullOrNan(0.0));
QVERIFY(LtsCoordinate::valueIsNullOrNan(std::nan("")));
QVERIFY(!LtsCoordinate::valueIsNullOrNan(1.0));
QVERIFY(!LtsCoordinate::valueIsNullOrNan(-1e-6));
}
// Known MGRS values, mirrored from the LtsCoordTrans test suite through the
// LtsCoordinate facade.
void ltscoordinate_unit_test::mgrsFormatting()
{
const LtsCoordinate coord(8.6264954126 * deg2rad(), 53.1138437183 * deg2rad());
QCOMPARE(coord.mgrs(0), QStringLiteral("32U MD"));
QCOMPARE(coord.mgrs(8), QStringLiteral("32U MD 74999999 84999999"));
// no-space form is the concatenation of the space form for this coordinate
QCOMPARE(coord.mgrs(8, false), QStringLiteral("32UMD7499999984999999"));
const LtsCoordinate coord2(11.3003696521 * deg2rad(), 52.8224535191 * deg2rad());
QCOMPARE(coord2.mgrs(0), QStringLiteral("32U PD"));
QCOMPARE(coord2.mgrs(5), QStringLiteral("32U PD 55000 55000"));
const LtsCoordinate coord3(54.8821607021 * deg2rad(), 24.2631973752 * deg2rad());
QCOMPARE(coord3.mgrs(0), QStringLiteral("40R BM"));
QCOMPARE(coord3.mgrs(5), QStringLiteral("40R BM 85000 85000"));
// Without an elevation callback the z part is NaN
const QString withZ = coord.mgrsWithZ(0);
QVERIFY(withZ.startsWith(QStringLiteral("32U MD z=")));
}
void ltscoordinate_unit_test::geoUtmRoundtrip()
{
const double lonDeg = 8.6264954126;
const double latDeg = 53.1138437183;
LtsCoordinate coord(lonDeg * deg2rad(), latDeg * deg2rad(), 5.0);
const UtmCoordinate utm = coord.utmCoordinate();
QVERIFY(utm.zone > 0);
const QPointF geoBack = utm.geographic();
QVERIFY(std::abs(geoBack.x() * rad2deg() - lonDeg) < 1e-6);
QVERIFY(std::abs(geoBack.y() * rad2deg() - latDeg) < 1e-6);
// toUtmPointF / toUtmPoint3D agree with the cached UTM coordinate
const QPointF utmPnt = coord.toUtmPointF();
QVERIFY(std::abs(utmPnt.x() - utm.x) < 1e-6);
QVERIFY(std::abs(utmPnt.y() - utm.y) < 1e-6);
const LtsUtils::QPoint3D utm3D = coord.toUtmPoint3D();
QCOMPARE(utm3D.z(), 5.0);
}
void ltscoordinate_unit_test::fromUtm()
{
const LtsCoordinate c1 = LtsCoordinate::fromUtm(QPointF(500000., 5000000.), 32, 10.0);
QCOMPARE(c1.utmCoordinate().zone, 32);
QVERIFY(std::abs(c1.utmCoordinate().x - 500000.) < 0.01);
QVERIFY(std::abs(c1.utmCoordinate().y - 5000000.) < 0.01);
QCOMPARE(c1.z(), 10.0);
const UtmCoordinate utm(32, 500123., 5000456., true);
const LtsCoordinate c2 = LtsCoordinate::fromUtmCoordinate(utm, 20.0);
QCOMPARE(c2.z(), 20.0);
QVERIFY(std::abs(c2.utmCoordinate().x - 500123.) < 0.01);
QVERIFY(std::abs(c2.utmCoordinate().y - 5000456.) < 0.01);
const UtmCoordinate3D c3 = c1.toUtmCoordinate3D();
QCOMPARE(c3.z(), 10.0);
}
void ltscoordinate_unit_test::utmCoordinateDiffZone()
{
const double lon = 8.6264954126 * deg2rad();
const double lat = 53.1138437183 * deg2rad();
LtsCoordinate coord(lon, lat);
const UtmCoordinate in31 = coord.utmCoordinateDiffZone(31);
const UtmCoordinate in32 = coord.utmCoordinateDiffZone(32);
QCOMPARE(in31.zone, 31);
QCOMPARE(in32.zone, 32);
QVERIFY(in31.x != in32.x);
// Same zone: diff-zone result matches the default conversion
const UtmCoordinate defaultZone = coord.utmCoordinate();
const UtmCoordinate sameZone = coord.utmCoordinateDiffZone(defaultZone.zone);
QCOMPARE(sameZone.x, defaultZone.x);
QCOMPARE(sameZone.y, defaultZone.y);
}
void ltscoordinate_unit_test::distanceUtm2D()
{
const LtsCoordinate a = LtsCoordinate::fromUtm(QPointF(500000., 5000000.), 32, 0.);
const LtsCoordinate b = LtsCoordinate::fromUtm(QPointF(500100., 5000000.), 32, 0.);
QVERIFY(std::abs(a.distanceUtm2D(b) - 100.0) < 1e-3);
QVERIFY(std::abs(a.distanceUtm2D(a)) < 1e-6);
QVERIFY(std::abs(a.distanceUtm2D(b) - b.distanceUtm2D(a)) < 1e-9);
}
void ltscoordinate_unit_test::distanceUtm3D()
{
const LtsCoordinate a = LtsCoordinate::fromUtm(QPointF(500000., 5000000.), 32, 0.);
const LtsCoordinate b = LtsCoordinate::fromUtm(QPointF(500100., 5000000.), 32, 40.);
const double expected = std::hypot(100., 40.);
QVERIFY(std::abs(a.distanceUtm3D(b) - expected) < 1e-3);
}
void ltscoordinate_unit_test::angleDegreeUtm2D()
{
const LtsCoordinate a = LtsCoordinate::fromUtm(QPointF(500000., 5000000.), 32, 0.);
const LtsCoordinate east = LtsCoordinate::fromUtm(QPointF(500100., 5000000.), 32, 0.);
const LtsCoordinate north = LtsCoordinate::fromUtm(QPointF(500000., 5000100.), 32, 0.);
// Conventions: QLineF angle (0 = +x) shifted by +90 degrees
QVERIFY(std::abs(LtsCoordinate::angleDegreeUtm2D(a, east) - 90.0) < 0.1);
QVERIFY(std::abs(LtsCoordinate::angleDegreeUtm2D(a, north) - 0.0) < 0.1);
}
void ltscoordinate_unit_test::elevationCallback()
{
LtsCoordinate::setElevationCallback([](double, double, double, bool* covered) {
if (covered)
*covered = true;
return 123.45;
});
QVERIFY(LtsCoordinate::hasElevationCallback());
const double lon = 8.6264954126 * deg2rad();
const double lat = 53.1138437183 * deg2rad();
LtsCoordinate coord(lon, lat);
bool covered = false;
QCOMPARE(coord.elevationAt(0.0, &covered), 123.45);
QVERIFY(covered);
QCOMPARE(LtsCoordinate::elevationAt(coord), 123.45);
coord.setZValueAccordingElevation();
QCOMPARE(coord.z(), 123.45);
QVERIFY(std::abs(coord.heightOverElevation()) < 1e-9);
const LtsCoordinate above(lon, lat, 200.0);
QVERIFY(std::abs(above.heightOverElevation() - (200.0 - 123.45)) < 1e-9);
// replaceZValueByElevationIfValue: NaN z is replaced
LtsCoordinate nanZ(lon, lat);
QVERIFY(std::isnan(nanZ.z()));
nanZ.replaceZValueByElevationIfValue();
QCOMPARE(nanZ.z(), 123.45);
// matching z is replaced, non-matching z is left alone
LtsCoordinate z77(lon, lat, 77.0);
z77.replaceZValueByElevationIfValue(77.0);
QCOMPARE(z77.z(), 123.45);
z77.replaceZValueByElevationIfValue(99.0);
QCOMPARE(z77.z(), 123.45);
// mgrsWithZ now carries the elevation
const QString withZ = coord.mgrsWithZ(0);
QVERIFY(withZ.startsWith(QStringLiteral("32U MD z=123.45")));
}
void ltscoordinate_unit_test::conversionCounter()
{
LtsCoordinate::resetConversionCounter();
QCOMPARE(LtsCoordinate::getConversionCounter(), 0L);
const double lon = 8.6264954126 * deg2rad();
const double lat = 53.1138437183 * deg2rad();
LtsCoordinate coord(lon, lat);
(void)coord.utmCoordinate();
// The counter is only incremented in LTS_DEBUG builds (the pipeline
// driver defines it: BUILD_RPM is not set)
QVERIFY(LtsCoordinate::getConversionCounter() >= 1L);
}
void ltscoordinate_unit_test::ltsCoordVector()
{
std::vector<Core::GeoPosition> geos;
geos.emplace_back(8.6264954126 * deg2rad(), 53.1138437183 * deg2rad(), 1.0);
geos.emplace_back(11.3003696521 * deg2rad(), 52.8224535191 * deg2rad(), 2.0);
// (14.0 E, 52.0 N) - in UTM zone 32 like the others (toStdUtmCoord(32))
geos.emplace_back(14.0 * deg2rad(), 52.0 * deg2rad(), 3.0);
const LtsCoordVector vec(geos);
QCOMPARE(vec.size(), 3);
QCOMPARE(vec.geoPosition().size(), 3u);
QCOMPARE(vec.geoPosition()[1].lat, geos[1].lat);
const std::vector<UtmCoordinate> utmStd = vec.toStdUtmCoord(32);
QCOMPARE(utmStd.size(), 3u);
for (const auto& u : utmStd)
QCOMPARE(u.zone, 32);
const QVector<UtmCoordinate> utmQv = vec.toQVectorUtmCoord(32);
QCOMPARE(utmQv.size(), 3);
QCOMPARE(utmQv[0].x, utmStd[0].x);
const QVector<QPointF> pts = vec.toQVectorQPointF(32);
QCOMPARE(pts.size(), 3);
for (const QPointF& p : pts)
QVERIFY(std::isfinite(p.x()) && std::isfinite(p.y()));
// Per-zone cache: repeated calls return the same values
const QVector<QPointF> again = vec.toQVectorQPointF(32);
QCOMPARE(again, pts);
}
QTEST_MAIN(ltscoordinate_unit_test)
#include "ltscoordinate_unit_test.moc"
@@ -156,3 +156,8 @@ IF(BUILD_RTI)
)
TARGET_COMPILE_DEFINITIONS(LtsSessionData PUBLIC BUILD_RTI)
ENDIF()
#
# Unit tests
#
ADD_SUBDIRECTORY(unit-test)
@@ -0,0 +1,19 @@
#
# Unit tests for the LtsSession module.
# One test executable per module, each registered with ctest.
#
if (NOT BUILD_TESTING)
return()
endif()
# --- LtsSession: LtsSessionData (session state singleton - pure data logic)
if (TARGET LtsSession)
ADD_EXECUTABLE(ltssession_unit_test ltssession_unit_test.cpp)
TARGET_LINK_LIBRARIES(ltssession_unit_test
LtsSessionData
Qt6::Core
Qt6::Gui
Qt6::Test
)
ADD_TEST(NAME ltssession_unit_test COMMAND $<TARGET_FILE:ltssession_unit_test>)
endif()
@@ -0,0 +1,321 @@
//
// Unit tests for the LtsSession module: LtsSessionData (the module's pure
// data class - session state singleton, presets, operator rights, visible
// parties and debug flags). Compiled into the LtsSessionData library target.
//
#include <LtsSession/lts_session_data.h>
#include <QtTest/QTest>
#include <QtGui/QPixmap>
#include <QtCore/QSet>
#include <QtCore/QUrl>
using LtsSessionData::PartiesByExercise;
using SessionData = LtsSessionData::LtsSessionData;
class ltssession_unit_test : public QObject
{
Q_OBJECT
private slots:
void singletonIdentity();
void constructorDefaults();
void operatorRightChecks();
void presetsRoundtrip();
void removePresetsKeepsDomainId();
void sessionFieldsRoundtrip();
void loggedOut();
void visibleParties();
void debugFlags();
void clearScope();
private:
// LtsSessionData is a process-wide singleton and clear() only resets the
// preset + operator state. Bring it back to a known baseline before each
// slot so the slots do not depend on execution order.
void resetSessionData();
};
void ltssession_unit_test::resetSessionData()
{
auto* data = SessionData::getInstance();
data->clear();
// fields that clear() intentionally does not reset:
data->setDomainIdPreset(-1);
data->setQosFile(QStringList());
data->setScriptId(0);
data->setWorkstationId(-1);
data->setRequestedAppId(LtsAppDescriptor::App::Login);
data->setTcpConnectionUrl(QUrl());
data->setAsTablet(false);
data->setPlaceScriptScreens(false);
for (const QString& key : data->debugFlags().keys())
data->removeDebugFlag(key);
}
void ltssession_unit_test::singletonIdentity()
{
resetSessionData();
auto* data = SessionData::getInstance();
QVERIFY(data != nullptr);
// singleton: same instance on every call
QCOMPARE(SessionData::getInstance(), data);
}
void ltssession_unit_test::constructorDefaults()
{
resetSessionData();
auto* data = SessionData::getInstance();
// presets
QCOMPARE(data->getDomainIdPreset(), -1);
QCOMPARE(data->getPartitionIdPreset(), -1);
QCOMPARE(data->getPartitionTypePreset(), IDL::PartitionType::Any);
QVERIFY(data->getOperatorNamePreset().isEmpty());
QVERIFY(data->getPasswordPreset().isEmpty());
QVERIFY(data->getQosFile().isEmpty());
// session state
QCOMPARE(data->getOperatorId(), -1);
QCOMPARE(data->getScriptId(), 0);
QVERIFY(data->getOperatorName().isEmpty());
QVERIFY(data->getOperatorRights().isEmpty());
QVERIFY(!data->getShowTerminalWindow());
QVERIFY(data->getProfilePicture().isNull());
QVERIFY(data->getVisibleParties().isEmpty());
QVERIFY(!data->getTcpConnectionUrl().isValid());
QCOMPARE(data->getRequestedAppId(), LtsAppDescriptor::App::Login);
QCOMPARE(data->getWorkstationId(), -1);
QVERIFY(!data->isTablet());
QVERIFY(!data->getPlaceScriptScreens());
QVERIFY(data->debugFlags().isEmpty());
}
void ltssession_unit_test::operatorRightChecks()
{
resetSessionData();
auto* data = SessionData::getInstance();
// fresh state: only right 0 is granted
QVERIFY(data->hasOperatorRight(0));
QVERIFY(!data->hasOperatorRight(1));
QVERIFY(!data->hasOperatorRight(-1));
// explicit rights
data->setOperatorRights(QSet<int>{1, 2});
QVERIFY(data->hasOperatorRight(1));
QVERIFY(data->hasOperatorRight(2));
QVERIFY(!data->hasOperatorRight(3));
QVERIFY(data->hasOperatorRight(0)); // 0 is always granted
// -1 means "all rights"
data->setOperatorRights(QSet<int>{-1});
QVERIFY(data->hasOperatorRight(42));
QVERIFY(data->hasOperatorRight(12345));
QVERIFY(data->hasOperatorRight(0));
}
void ltssession_unit_test::presetsRoundtrip()
{
resetSessionData();
auto* data = SessionData::getInstance();
data->setOperatorPreset(QStringLiteral("operator-name"), QStringLiteral("secret"));
QCOMPARE(data->getOperatorNamePreset(), QStringLiteral("operator-name"));
QCOMPARE(data->getPasswordPreset(), QStringLiteral("secret"));
data->setDomainIdPreset(7);
QCOMPARE(data->getDomainIdPreset(), 7);
data->setPartitionPreset(3, IDL::PartitionType::Exercise);
QCOMPARE(data->getPartitionIdPreset(), 3);
QCOMPARE(data->getPartitionTypePreset(), IDL::PartitionType::Exercise);
const QStringList expectedQos{QStringLiteral("a.qos"), QStringLiteral("b.qos")};
data->setQosFile(expectedQos);
QCOMPARE(data->getQosFile(), expectedQos);
}
void ltssession_unit_test::removePresetsKeepsDomainId()
{
resetSessionData();
auto* data = SessionData::getInstance();
data->setOperatorPreset(QStringLiteral("op"), QStringLiteral("pw"));
data->setDomainIdPreset(7);
data->setPartitionPreset(3, IDL::PartitionType::Global);
data->removePresets();
QCOMPARE(data->getPartitionIdPreset(), -1);
QCOMPARE(data->getPartitionTypePreset(), IDL::PartitionType::Any);
QVERIFY(data->getOperatorNamePreset().isEmpty());
QVERIFY(data->getPasswordPreset().isEmpty());
// the domain id preset is kept on purpose
QCOMPARE(data->getDomainIdPreset(), 7);
}
void ltssession_unit_test::sessionFieldsRoundtrip()
{
resetSessionData();
auto* data = SessionData::getInstance();
data->setScriptId(5);
QCOMPARE(data->getScriptId(), 5);
data->setOperatorId(42);
QCOMPARE(data->getOperatorId(), 42);
data->setOperatorName(QStringLiteral("Max Mustermann"));
QCOMPARE(data->getOperatorName(), QStringLiteral("Max Mustermann"));
data->setShowTerminalWindow(true);
QVERIFY(data->getShowTerminalWindow());
data->setTcpConnectionUrl(QUrl(QStringLiteral("tcp://127.0.0.1:51234")));
QCOMPARE(data->getTcpConnectionUrl(), QUrl(QStringLiteral("tcp://127.0.0.1:51234")));
data->setRequestedAppId(LtsAppDescriptor::App::Situation);
QCOMPARE(data->getRequestedAppId(), LtsAppDescriptor::App::Situation);
data->setWorkstationId(11);
QCOMPARE(data->getWorkstationId(), 11);
data->setPlaceScriptScreens(true);
QVERIFY(data->getPlaceScriptScreens());
// a null pixmap is a valid (unfilled) profile picture in console mode
data->setProfilePicture(QPixmap());
QVERIFY(data->getProfilePicture().isNull());
}
void ltssession_unit_test::loggedOut()
{
resetSessionData();
auto* data = SessionData::getInstance();
data->setScriptId(9); // not part of the logged-out state
data->setOperatorId(5);
data->setOperatorName(QStringLiteral("op"));
data->setOperatorRights(QSet<int>{1});
data->setShowTerminalWindow(true);
PartiesByExercise parties;
parties.exerciseId = 1;
parties.visibleParties = QVector<int>{10};
data->setVisibleParties(QVector<PartiesByExercise>{parties});
data->loggedOut();
QCOMPARE(data->getOperatorId(), -1);
QVERIFY(data->getOperatorName().isEmpty());
QVERIFY(data->getOperatorRights().isEmpty());
QVERIFY(!data->getShowTerminalWindow());
QVERIFY(data->getVisibleParties().isEmpty());
QVERIFY(data->getProfilePicture().isNull());
// unrelated fields are left untouched
QCOMPARE(data->getScriptId(), 9);
}
void ltssession_unit_test::visibleParties()
{
resetSessionData();
auto* data = SessionData::getInstance();
PartiesByExercise exercise1;
exercise1.exerciseId = 1;
exercise1.visibleParties = QVector<int>{10, 11};
PartiesByExercise exercise2;
exercise2.exerciseId = 2;
exercise2.visibleParties = QVector<int>{20};
data->setVisibleParties(QVector<PartiesByExercise>{exercise1, exercise2});
QVERIFY(data->isVisibleParty(1, 10));
QVERIFY(data->isVisibleParty(1, 11));
QVERIFY(!data->isVisibleParty(1, 12));
QVERIFY(data->isVisibleParty(2, 20));
QVERIFY(!data->isVisibleParty(2, 10));
QVERIFY(!data->isVisibleParty(99, 10)); // unknown exercise
const QVector<int> expectedParties1{10, 11};
QCOMPARE(data->getVisibleParties(1), expectedParties1);
QCOMPARE(data->getVisibleParties(2), QVector<int>{20});
QVERIFY(data->getVisibleParties(99).isEmpty());
const auto& all = data->getVisibleParties();
QCOMPARE(static_cast<int>(all.size()), 2);
QCOMPARE(all[0].exerciseId, 1);
QCOMPARE(all[1].exerciseId, 2);
}
void ltssession_unit_test::debugFlags()
{
resetSessionData();
auto* data = SessionData::getInstance();
QVERIFY(!data->hasDebugFlag(QStringLiteral("flag")));
QVERIFY(data->debugFlagValue(QStringLiteral("flag")).isNull());
data->setDebugFlagValue(QStringLiteral("flag"), 42);
QVERIFY(data->hasDebugFlag(QStringLiteral("flag")));
QCOMPARE(data->debugFlagValue(QStringLiteral("flag")).toInt(), 42);
// values are overwritten, not accumulated
data->setDebugFlagValue(QStringLiteral("flag"), QStringLiteral("value"));
QCOMPARE(data->debugFlagValue(QStringLiteral("flag")).toString(), QStringLiteral("value"));
QCOMPARE(static_cast<int>(data->debugFlags().size()), 1);
data->setDebugFlagValue(QStringLiteral("other"), 7);
QCOMPARE(static_cast<int>(data->debugFlags().size()), 2);
data->removeDebugFlag(QStringLiteral("flag"));
QVERIFY(!data->hasDebugFlag(QStringLiteral("flag")));
QVERIFY(data->debugFlagValue(QStringLiteral("flag")).isNull());
QCOMPARE(static_cast<int>(data->debugFlags().size()), 1);
data->removeDebugFlag(QStringLiteral("other"));
QVERIFY(data->debugFlags().isEmpty());
// removing an unknown flag is a no-op
data->removeDebugFlag(QStringLiteral("unknown"));
QVERIFY(data->debugFlags().isEmpty());
}
void ltssession_unit_test::clearScope()
{
resetSessionData();
auto* data = SessionData::getInstance();
data->setScriptId(9);
data->setWorkstationId(3);
data->setTcpConnectionUrl(QUrl(QStringLiteral("tcp://127.0.0.1:51234")));
data->setPlaceScriptScreens(true);
data->setDebugFlagValue(QStringLiteral("flag"), 1);
data->setOperatorId(5);
data->setOperatorName(QStringLiteral("op"));
data->setOperatorRights(QSet<int>{1});
data->setDomainIdPreset(7);
data->setPartitionPreset(3, IDL::PartitionType::Exercise);
data->clear();
// operator state and presets are gone
QCOMPARE(data->getOperatorId(), -1);
QVERIFY(data->getOperatorName().isEmpty());
QVERIFY(data->getOperatorRights().isEmpty());
QCOMPARE(data->getPartitionIdPreset(), -1);
QCOMPARE(data->getPartitionTypePreset(), IDL::PartitionType::Any);
QVERIFY(data->getOperatorNamePreset().isEmpty());
QVERIFY(data->getPasswordPreset().isEmpty());
// everything else is preserved by clear()
QCOMPARE(data->getDomainIdPreset(), 7);
QCOMPARE(data->getScriptId(), 9);
QCOMPARE(data->getWorkstationId(), 3);
QCOMPARE(data->getTcpConnectionUrl(), QUrl(QStringLiteral("tcp://127.0.0.1:51234")));
QVERIFY(data->getPlaceScriptScreens());
QVERIFY(data->hasDebugFlag(QStringLiteral("flag")));
}
QTEST_MAIN(ltssession_unit_test)
#include "ltssession_unit_test.moc"
@@ -152,3 +152,8 @@ NGL_INSTALL_TARGET(svcCoreClient)
CREATE_TOPIC_TYPE_PLUGIN(${xml_FILE})
ENDIF(BUILD_RTI)
#
# Unit tests
#
ADD_SUBDIRECTORY(unit-test)
@@ -0,0 +1,19 @@
#
# Unit tests for the DDS Core module.
# One test executable per module, each registered with ctest.
#
if (NOT BUILD_TESTING)
return()
endif()
# --- DDS Core: generated function-reply objects and the CORE logger facade
if (TARGET ddscore)
ADD_EXECUTABLE(ddscore_unit_test ddscore_unit_test.cpp)
TARGET_LINK_LIBRARIES(ddscore_unit_test
ddscore
NGLLogger
Qt6::Core
Qt6::Test
)
ADD_TEST(NAME ddscore_unit_test COMMAND $<TARGET_FILE:ddscore_unit_test>)
endif()
@@ -0,0 +1,169 @@
//
// Unit tests for the ddscore module (Lts/DDS/Core): FunctionReplyObject
// (the module's pure data/logic class) and the CORE.* logger helper.
//
#include <QtCore/QCoreApplication>
#include <QtCore/QString>
#include <Core/function_reply_object.h>
#include <Core/logger.h>
#include <NGLLogger/NGLLoggerBootstrap.h>
#include <QtTest/QTest>
#include <string>
#include <vector>
class ddscore_unit_test : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void functionReplyObjectDefault();
void functionReplyObjectFromIdAndString();
void functionReplyObjectFromStringOnly();
void functionReplyObjectNullString();
void functionReplyObjectWithArgs();
void functionReplyObjectWithArgsAndAdditionalInfo();
void functionReplyObjectRvalueArgs();
void argOverloads();
void setters();
void formattedWithoutPlaceholders();
void formattedExtraArgsIgnored();
void translatedMatchesFormatted();
void uniqueIdRoundtrip();
void coreGetLoggerName();
};
void ddscore_unit_test::initTestCase()
{
NGL::NGLLoggerBootstrap::createNGLRootLogger("log4cxx.props", false);
}
void ddscore_unit_test::functionReplyObjectDefault()
{
Core::FunctionReplyObject reply;
QCOMPARE(reply.id(), -1);
QVERIFY(!reply.error());
QCOMPARE(reply.str(), std::string());
QCOMPARE(reply.additionalInfo(), std::string());
QCOMPARE(reply.args().size(), 0u);
QCOMPARE(reply.uniqueId(), int64_t(0));
}
void ddscore_unit_test::functionReplyObjectFromIdAndString()
{
Core::FunctionReplyObject reply(42, "error %1");
QCOMPARE(reply.id(), 42);
QVERIFY(reply.error());
QCOMPARE(reply.str(), std::string("error %1"));
}
void ddscore_unit_test::functionReplyObjectFromStringOnly()
{
Core::FunctionReplyObject reply("plain error");
QCOMPARE(reply.id(), -1);
QVERIFY(reply.error());
QCOMPARE(reply.str(), std::string("plain error"));
}
void ddscore_unit_test::functionReplyObjectNullString()
{
Core::FunctionReplyObject reply(7, static_cast<const char *>(nullptr));
QCOMPARE(reply.id(), 7);
QVERIFY(!reply.error());
QCOMPARE(reply.str(), std::string());
}
void ddscore_unit_test::functionReplyObjectWithArgs()
{
Core::FunctionReplyObject reply(5, "%1/%2", std::vector<std::string>{"a", "b"});
QCOMPARE(reply.id(), 5);
QCOMPARE(reply.str(), std::string("%1/%2"));
const std::vector<std::string> expectedArgs = {"a", "b"};
QCOMPARE(reply.args(), expectedArgs);
QCOMPARE(reply(), std::string("a/b"));
}
void ddscore_unit_test::functionReplyObjectWithArgsAndAdditionalInfo()
{
Core::FunctionReplyObject reply(5, "failed: %1", std::vector<std::string>{"x"}, "context");
QCOMPARE(reply.id(), 5);
QCOMPARE(reply.str(), std::string("failed: %1"));
QCOMPARE(reply.additionalInfo(), std::string("context"));
QCOMPARE(reply(), std::string("failed: x"));
}
void ddscore_unit_test::functionReplyObjectRvalueArgs()
{
Core::FunctionReplyObject reply(
9, std::string("moved %1"), std::vector<std::string>{"v"}, std::string("info"));
QCOMPARE(reply.id(), 9);
QCOMPARE(reply.str(), std::string("moved %1"));
QCOMPARE(reply.additionalInfo(), std::string("info"));
QCOMPARE(reply(), std::string("moved v"));
}
void ddscore_unit_test::argOverloads()
{
Core::FunctionReplyObject reply(1, "v=%1 s=%2 q=%3");
reply.arg(7).arg(std::string("hello")).arg(QStringLiteral("qt"));
const std::vector<std::string> expectedArgs = {"7", "hello", "qt"};
QCOMPARE(reply.args(), expectedArgs);
QCOMPARE(reply(), std::string("v=7 s=hello q=qt"));
}
void ddscore_unit_test::setters()
{
Core::FunctionReplyObject reply;
reply.setId(11);
reply.setStr("updated %1");
reply.setAdditionalInfo("note");
QCOMPARE(reply.id(), 11);
QCOMPARE(reply.str(), std::string("updated %1"));
QVERIFY(reply.error());
QCOMPARE(reply.additionalInfo(), std::string("note"));
}
void ddscore_unit_test::formattedWithoutPlaceholders()
{
Core::FunctionReplyObject reply(1, "no placeholder");
reply.arg(QStringLiteral("ignored"));
QCOMPARE(reply(), std::string("no placeholder"));
}
void ddscore_unit_test::formattedExtraArgsIgnored()
{
Core::FunctionReplyObject reply(1, "only %1");
reply.arg(QStringLiteral("a")).arg(QStringLiteral("b"));
QCOMPARE(reply(), std::string("only a"));
}
void ddscore_unit_test::translatedMatchesFormatted()
{
// No translator is installed, so QCoreApplication::translate returns the
// source string and translated() must equal the plain formatted result.
Core::FunctionReplyObject reply(1, "translated %1", std::vector<std::string>{"t"});
QCOMPARE(reply.translated().toStdString(), std::string("translated t"));
QCOMPARE(reply.translated().toStdString(), reply());
}
void ddscore_unit_test::uniqueIdRoundtrip()
{
Core::FunctionReplyObject reply;
reply.setUniqueId(123456);
QCOMPARE(reply.uniqueId(), int64_t(123456));
}
void ddscore_unit_test::coreGetLoggerName()
{
// Core::getLogger prefixes the requested name with the "CORE." namespace.
NGL::INGLLogger &logger = Core::getLogger("UnitTest");
QCOMPARE(logger.getLoggerName(), std::string("CORE.UnitTest"));
}
QTEST_MAIN(ddscore_unit_test)
#include "ddscore_unit_test.moc"
@@ -37,3 +37,8 @@ NGL_TARGET_LINK_LIBRARIES(ddsQoS
)
ENDIF(BUILD_RTI)
#
# Unit tests
#
ADD_SUBDIRECTORY(unit-test)
@@ -0,0 +1,30 @@
#
# Unit tests for the DDS QosProfile module.
# One test executable per module, each registered with ctest.
#
if (NOT BUILD_TESTING)
return()
endif()
# --- DDS QosProfile: default QoS profile helpers
if (TARGET ddsQoS)
ADD_EXECUTABLE(ddsqos_unit_test ddsqos_unit_test.cpp)
# Path of the module's default_profile.xml fixture (see ddsqos_unit_test.cpp)
TARGET_COMPILE_DEFINITIONS(ddsqos_unit_test PRIVATE
DDSQOS_UNIT_TEST_PROFILE_XML="${CMAKE_CURRENT_SOURCE_DIR}/../default_profile.xml")
TARGET_LINK_LIBRARIES(ddsqos_unit_test
ddsQoS
NGLLogger
Qt6::Core
Qt6::Test
)
# ddsQoS links the RTI DDS libraries PRIVATE - the test needs them itself
# for the RTI policy inline functions used by the QoS helpers
if (TARGET RTI::nddsc)
TARGET_LINK_LIBRARIES(ddsqos_unit_test
RTI::nddsc
RTI::nddscpp2
)
endif()
ADD_TEST(NAME ddsqos_unit_test COMMAND $<TARGET_FILE:ddsqos_unit_test>)
endif()
@@ -0,0 +1,190 @@
//
// Unit tests for the ddsQoS module (Lts/DDS/QosProfile): DDS_DefaultQos.
// The functions under test only read QoS profiles through the RTI QosProvider
// (policy structs) - no DomainParticipant, Publication or Subscription is
// created and no DDS runtime/network access happens.
//
// The location of the default_profile.xml shipped with the module is passed
// via the DDSQOS_UNIT_TEST_PROFILE_XML compile definition
// (see Lts/DDS/QosProfile/unit-test/CMakeLists.txt).
//
#include <QtCore/QFile>
#include <QtCore/QString>
#include <QtCore/QUrl>
#include <QosProfile/DDS_DefaultQos.h>
#include <NGLLogger/INGLLogger.h>
#include <NGLLogger/NGLLoggerBootstrap.h>
#include <QtTest/QTest>
#include <string>
#include <vector>
namespace
{
using namespace DDS::QosProfile;
const QString &profileXml()
{
static const QString path = QStringLiteral(DDSQOS_UNIT_TEST_PROFILE_XML);
return path;
}
bool containsPeer(const dds::core::StringSeq &seq, const std::string &needle)
{
for (const std::string &s : seq)
if (s == needle)
return true;
return false;
}
}
class ddsqos_unit_test : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void setDefaultLoadsProfile();
void shmUdpParticipantQos();
void shmParticipantQos();
void shmUdpLocalhostParticipantQos();
void domainParticipantTcpBindPort();
void domainParticipantTcpV4LanPeers();
void dataWriterDefaults();
void dataWriterFunctionTopic();
void dataReaderDefaults();
void dataReaderFunctionTopic();
void dataReaderDefaultByConnectionType();
void printSmokeTest();
};
void ddsqos_unit_test::initTestCase()
{
NGL::NGLLoggerBootstrap::createNGLRootLogger("log4cxx.props", false);
if (!QFile::exists(profileXml()))
QSKIP("default_profile.xml not available");
QVERIFY(setDefault(QStringList{profileXml()}));
}
void ddsqos_unit_test::setDefaultLoadsProfile()
{
// Re-loading the known-good profile into the default provider must succeed.
QVERIFY(setDefault(QStringList{profileXml()}));
}
void ddsqos_unit_test::shmUdpParticipantQos()
{
const dds::domain::qos::DomainParticipantQos qos = domainParticipantShmUdp();
const uint64_t expected = (rti::core::policy::TransportBuiltinMask::shmem()
| rti::core::policy::TransportBuiltinMask::udpv4()).to_ulong();
QCOMPARE(qos->transport_builtin.mask().to_ulong(), expected);
}
void ddsqos_unit_test::shmParticipantQos()
{
const dds::domain::qos::DomainParticipantQos qos = domainParticipantShm();
QCOMPARE(qos->transport_builtin.mask().to_ulong(),
rti::core::policy::TransportBuiltinMask::shmem().to_ulong());
const dds::core::StringSeq peers = qos->discovery.initial_peers();
QCOMPARE(peers.size(), 1u);
QCOMPARE(peers.at(0), std::string("30@builtin.shmem://"));
}
void ddsqos_unit_test::shmUdpLocalhostParticipantQos()
{
const dds::domain::qos::DomainParticipantQos qos = domainParticipantShmUdpLocalhost();
const dds::core::StringSeq peers = qos->discovery.initial_peers();
QVERIFY(containsPeer(peers, "builtin.shmem://"));
QVERIFY(containsPeer(peers, "builtin.udpv4://127.0.0.1"));
QVERIFY(qos->property.exists("dds.transport.UDPv4.builtin.multicast_enabled"));
QCOMPARE(qos->property.get("dds.transport.UDPv4.builtin.multicast_enabled"),
std::string("0"));
}
void ddsqos_unit_test::domainParticipantTcpBindPort()
{
// Non-tcpv4lan scheme: the port is written into the TCPv4 transport property.
const dds::domain::qos::DomainParticipantQos qos =
domainParticipantShmTcp(QUrl(QStringLiteral("tcp://10.0.0.5:9999")));
QVERIFY(qos->property.exists("dds.transport.TCPv4.tcp1.server_bind_port"));
QCOMPARE(qos->property.get("dds.transport.TCPv4.tcp1.server_bind_port"),
std::string("9999"));
}
void ddsqos_unit_test::domainParticipantTcpV4LanPeers()
{
// tcpv4lan scheme: the fixed-up URL becomes the single initial peer with
// the "0@" domain prefix (fixupSchemeName in DDS_DefaultQos).
const dds::domain::qos::DomainParticipantQos qos =
domainParticipantShmTcp(QUrl(QStringLiteral("tcpv4lan://10.0.0.5:9999")));
const dds::core::StringSeq peers = qos->discovery.initial_peers();
QCOMPARE(peers.size(), 1u);
QCOMPARE(peers.at(0), std::string("0@tcpv4_lan://10.0.0.5:9999"));
}
void ddsqos_unit_test::dataWriterDefaults()
{
// Default (DataTopic) writer QoS comes from the is_default_qos "Base" profile.
const dds::pub::qos::DataWriterQos qos = dataWriterDefault(TopicType::DataTopic);
QCOMPARE(qos->reliability.kind(), dds::core::policy::ReliabilityKind::RELIABLE);
QCOMPARE(qos->durability.kind(), dds::core::policy::DurabilityKind::TRANSIENT_LOCAL);
QCOMPARE(qos->history.kind(), dds::core::policy::HistoryKind::KEEP_LAST);
QCOMPARE(qos->history.depth(), 1);
QCOMPARE(qos->publish_mode.kind(), rti::core::policy::PublishModeKind::ASYNCHRONOUS);
QCOMPARE(qos->resource_limits.max_samples(), 8000);
}
void ddsqos_unit_test::dataWriterFunctionTopic()
{
const dds::pub::qos::DataWriterQos qos = dataWriterDefault(TopicType::FunctionTopic);
QCOMPARE(qos->durability.kind(), dds::core::policy::DurabilityKind::TRANSIENT_LOCAL);
QCOMPARE(qos->history.kind(), dds::core::policy::HistoryKind::KEEP_LAST);
QCOMPARE(qos->history.depth(), 1);
}
void ddsqos_unit_test::dataReaderDefaults()
{
const dds::sub::qos::DataReaderQos qos = dataReaderDefault(TopicType::DataTopic);
QCOMPARE(qos->reliability.kind(), dds::core::policy::ReliabilityKind::RELIABLE);
QCOMPARE(qos->durability.kind(), dds::core::policy::DurabilityKind::TRANSIENT_LOCAL);
QCOMPARE(qos->history.kind(), dds::core::policy::HistoryKind::KEEP_LAST);
QCOMPARE(qos->history.depth(), 1);
QCOMPARE(qos->resource_limits.max_samples(), 8000);
}
void ddsqos_unit_test::dataReaderFunctionTopic()
{
// The FT reader profile overrides the Base reader history with KEEP_ALL.
const dds::sub::qos::DataReaderQos ftQos = dataReaderDefault(TopicType::FunctionTopic);
QCOMPARE(ftQos->history.kind(), dds::core::policy::HistoryKind::KEEP_ALL);
const dds::sub::qos::DataReaderQos dataQos = dataReaderDefault(TopicType::DataTopic);
QCOMPARE(dataQos->history.kind(), dds::core::policy::HistoryKind::KEEP_LAST);
}
void ddsqos_unit_test::dataReaderDefaultByConnectionType()
{
// The ShmUdp participant profile inherits the Base reader QoS.
const dds::sub::qos::DataReaderQos qos = dataReaderDefault(ConnectionType::ShmUdp);
QCOMPARE(qos->reliability.kind(), dds::core::policy::ReliabilityKind::RELIABLE);
QCOMPARE(qos->history.kind(), dds::core::policy::HistoryKind::KEEP_LAST);
QCOMPARE(qos->resource_limits.max_samples(), 8000);
}
void ddsqos_unit_test::printSmokeTest()
{
// print() walks the profile policies - must not throw.
NGL::INGLLogger &logger =
NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("DDS.DefaultQosTest");
print(domainParticipantShmUdp(), logger);
print(dataWriterDefault(TopicType::DataTopic), logger);
print(dataReaderDefault(TopicType::DataTopic), logger);
}
QTEST_MAIN(ddsqos_unit_test)
#include "ddsqos_unit_test.moc"
@@ -64,3 +64,8 @@ if(QT6_GUI_AVAILABLE)
endif()
NGL_INSTALL_TARGET(LogViewer)
#
# Unit tests
#
ADD_SUBDIRECTORY(unit-test)
@@ -0,0 +1,24 @@
#
# Unit tests for the LogViewer module.
# One test executable per module, each registered with ctest.
#
if (NOT BUILD_TESTING)
return()
endif()
# --- LogViewer: log table model + sort/filter proxy.
# --- The module is an executable, so the model sources are compiled directly
# --- into the test (same pattern as the LogView test in TetraIF/unit-test).
if (TARGET LogViewer)
ADD_EXECUTABLE(logviewer_unit_test
logviewer_unit_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../logviewtablemodel.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../sortfiltermodel.cpp)
TARGET_LINK_LIBRARIES(logviewer_unit_test
NGLLogger
Qt6::Core
Qt6::Gui
Qt6::Test
)
ADD_TEST(NAME logviewer_unit_test COMMAND $<TARGET_FILE:logviewer_unit_test>)
endif()
@@ -0,0 +1,553 @@
//
// Unit tests for the LogViewer module: LogViewTableModel (log line storage,
// column/role data access, standard level colors) and SortFilterModel
// (string/date filtering and date-aware sorting).
//
// Both classes are the module's non-GUI logic (QAbstractTableModel /
// QSortFilterProxyModel, no widgets needed); they are compiled from the
// LogViewer executable sources. The other LogViewer classes are filter
// widgets and the main window.
//
#include "../logviewtablemodel.h"
#include "../sortfiltermodel.h"
#include <NGLLogger/NGLLoggerBootstrap.h>
#include <QtCore/QDateTime>
#include <QtTest/QTest>
using LogInformation = LogViewTableModel::logInformation;
namespace
{
LogInformation makeLine(const QDateTime &date, const QString &level, const QString &logger,
const QString &data, const QString &location)
{
LogInformation line;
line.date = date;
line.level = level;
line.logger = logger;
line.data = data;
line.location = location;
return line;
}
QDateTime d(const QString &iso)
{
return QDateTime::fromString(iso, Qt::ISODate);
}
} // namespace
class logviewer_unit_test : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
// LogViewTableModel
void tableModelEmptyDimensions();
void tableModelSetDataAndReadRoles();
void tableModelInvalidIndex();
void tableModelHeaderData();
void tableModelStandardLevelColors();
void tableModelAdjustColorsAndReset();
void tableModelAddDataAndLength();
void tableModelSetDataByIndex();
void tableModelStartEndDateTimeSignals();
// SortFilterModel
void proxyStringFilter();
void proxyDateStringFilterEdgeCases();
void proxyDateFilter();
void proxyResetAllFilter();
void proxySortByDate();
void proxySortByLevel();
void proxyFigureFilterOutSignal();
};
void logviewer_unit_test::initTestCase()
{
// LogViewTableModel's constructor resolves its logger from the NGL root
// logger, so it must exist before the model is instantiated.
NGL::NGLLoggerBootstrap::createNGLRootLogger("log4cxx.props", false);
}
// ---------------------------------------------------------------------------
// LogViewTableModel
// ---------------------------------------------------------------------------
void logviewer_unit_test::tableModelEmptyDimensions()
{
LogViewTableModel model;
// note: the overrides in LogViewTableModel redeclare the parameter without
// the base class' default value
QCOMPARE(model.rowCount(QModelIndex()), 0);
QCOMPARE(model.columnCount(QModelIndex()), LogViewTableModel::colLast + 1);
QCOMPARE(model.length(), 0);
}
void logviewer_unit_test::tableModelSetDataAndReadRoles()
{
LogViewTableModel model;
const QDateTime date = d(QStringLiteral("2024-05-06T12:34:56"));
QVector<LogInformation> lines;
lines << makeLine(date, QStringLiteral("INFO"), QStringLiteral("main"), QStringLiteral("hello"),
QStringLiteral("a.cpp:42"))
<< makeLine(d(QStringLiteral("2024-05-07T08:00:00")), QStringLiteral("WARN"),
QStringLiteral("net"), QStringLiteral("world"), QStringLiteral("b.cpp:7"));
model.setData(lines);
QCOMPARE(model.rowCount(QModelIndex()), 2);
QCOMPARE(model.length(), 2);
// DisplayRole per column
QCOMPARE(model.data(model.index(0, LogViewTableModel::colDate), Qt::DisplayRole).toString().
startsWith(QStringLiteral("2024-05-06 12:34:56,")), true);
QCOMPARE(model.data(model.index(0, LogViewTableModel::colLevel), Qt::DisplayRole).toString(),
QStringLiteral("INFO"));
QCOMPARE(model.data(model.index(0, LogViewTableModel::colLogger), Qt::DisplayRole).toString(),
QStringLiteral("main"));
QCOMPARE(model.data(model.index(0, LogViewTableModel::colData), Qt::DisplayRole).toString(),
QStringLiteral("hello"));
QCOMPARE(model.data(model.index(0, LogViewTableModel::colLocation), Qt::DisplayRole).toString(),
QStringLiteral("a.cpp:42"));
// UserRole carries the raw QDateTime of the date column
QCOMPARE(model.data(model.index(0, LogViewTableModel::colDate), Qt::UserRole).toDateTime(), date);
// itemAt: note the date format uses dashes between the time parts
QCOMPARE(model.itemAt(1, LogViewTableModel::colDate).startsWith(QStringLiteral("2024-05-07 08-00-00,")),
true);
QCOMPARE(model.itemAt(0, LogViewTableModel::colLevel), QStringLiteral("INFO"));
QCOMPARE(model.itemAt(0, LogViewTableModel::colData), QStringLiteral("hello"));
// unknown column index -> empty string
QCOMPARE(model.itemAt(0, 99), QString());
// replacing all data
model.setData(QVector<LogInformation>());
QCOMPARE(model.rowCount(QModelIndex()), 0);
}
void logviewer_unit_test::tableModelInvalidIndex()
{
LogViewTableModel model;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("INFO"), QStringLiteral("l"),
QStringLiteral("d"), QStringLiteral("x.cpp:1"));
model.setData(lines);
// invalid index -> invalid QVariant for any role
const QModelIndex invalid = QModelIndex();
QVERIFY(!model.data(invalid, Qt::DisplayRole).isValid());
QVERIFY(!model.data(invalid, Qt::UserRole).isValid());
QVERIFY(!model.data(invalid, Qt::BackgroundRole).isValid());
}
void logviewer_unit_test::tableModelHeaderData()
{
LogViewTableModel model;
QCOMPARE(model.headerData(LogViewTableModel::colDate, Qt::Horizontal, Qt::DisplayRole).toString(),
QStringLiteral("Date"));
QCOMPARE(model.headerData(LogViewTableModel::colLevel, Qt::Horizontal, Qt::DisplayRole).toString(),
QStringLiteral("Level"));
QCOMPARE(model.headerData(LogViewTableModel::colLogger, Qt::Horizontal, Qt::DisplayRole).toString(),
QStringLiteral("Logger"));
QCOMPARE(model.headerData(LogViewTableModel::colData, Qt::Horizontal, Qt::DisplayRole).toString(),
QStringLiteral("Data"));
QCOMPARE(model.headerData(LogViewTableModel::colLocation, Qt::Horizontal, Qt::DisplayRole).toString(),
QStringLiteral("Location"));
// vertical orientation and out-of-range section fall through to
// QAbstractTableModel::headerData (1-based: section+1 horizontal, 1 vertical)
QCOMPARE(model.headerData(0, Qt::Vertical, Qt::DisplayRole).toString(), QStringLiteral("1"));
QCOMPARE(model.headerData(99, Qt::Horizontal, Qt::DisplayRole).toString(), QStringLiteral("100"));
}
void logviewer_unit_test::tableModelStandardLevelColors()
{
LogViewTableModel model;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("INFO"), QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("WARN"), QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("ERROR"), QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("FATAL"), QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("DEBUG"), QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("TRACE"), QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("CUSTOM"), QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"));
model.setData(lines);
const int col = LogViewTableModel::colLevel;
QCOMPARE(model.data(model.index(0, col), Qt::BackgroundRole).value<QColor>(), Qt::blue);
QCOMPARE(model.data(model.index(0, col), Qt::ForegroundRole).value<QColor>(), Qt::white);
QCOMPARE(model.data(model.index(1, col), Qt::BackgroundRole).value<QColor>(), Qt::yellow);
QCOMPARE(model.data(model.index(1, col), Qt::ForegroundRole).value<QColor>(), Qt::black);
QCOMPARE(model.data(model.index(2, col), Qt::BackgroundRole).value<QColor>(), Qt::darkRed);
QCOMPARE(model.data(model.index(2, col), Qt::ForegroundRole).value<QColor>(), Qt::white);
QCOMPARE(model.data(model.index(3, col), Qt::BackgroundRole).value<QColor>(), Qt::red);
QCOMPARE(model.data(model.index(3, col), Qt::ForegroundRole).value<QColor>(), Qt::black);
QCOMPARE(model.data(model.index(4, col), Qt::BackgroundRole).value<QColor>(), Qt::green);
QCOMPARE(model.data(model.index(4, col), Qt::ForegroundRole).value<QColor>(), Qt::black);
QCOMPARE(model.data(model.index(5, col), Qt::BackgroundRole).value<QColor>(), Qt::lightGray);
QCOMPARE(model.data(model.index(5, col), Qt::ForegroundRole).value<QColor>(), Qt::black);
// unknown level: no color registered -> invalid variants
QVERIFY(!model.data(model.index(6, col), Qt::BackgroundRole).isValid());
QVERIFY(!model.data(model.index(6, col), Qt::ForegroundRole).isValid());
// other columns have no background/foreground color
QVERIFY(!model.data(model.index(0, LogViewTableModel::colData), Qt::BackgroundRole).isValid());
}
void logviewer_unit_test::tableModelAdjustColorsAndReset()
{
LogViewTableModel model;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("INFO"), QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("WARN"), QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"));
model.setData(lines);
const int col = LogViewTableModel::colLevel;
// adjustCellColor only affects levels that are already registered
model.adjustCellColor(Qt::red, QStringLiteral("INFO"));
QCOMPARE(model.data(model.index(0, col), Qt::BackgroundRole).value<QColor>(), Qt::red);
// unknown level: no effect
model.adjustCellColor(Qt::green, QStringLiteral("NOPE"));
QCOMPARE(model.data(model.index(1, col), Qt::BackgroundRole).value<QColor>(), Qt::yellow);
// adjustFontColor
model.adjustFontColor(Qt::black, QStringLiteral("INFO"));
QCOMPARE(model.data(model.index(0, col), Qt::ForegroundRole).value<QColor>(), Qt::black);
// reset() clears the data and restores the standard colors
QVERIFY(model.reset());
QCOMPARE(model.length(), 0);
QCOMPARE(model.rowCount(QModelIndex()), 0);
}
void logviewer_unit_test::tableModelAddDataAndLength()
{
LogViewTableModel model;
QCOMPARE(model.length(), 0);
model.addData(makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("INFO"), QStringLiteral("l"),
QStringLiteral("d"), QStringLiteral("l.cpp:1")));
QCOMPARE(model.length(), 1);
model.addData(makeLine(d(QStringLiteral("2024-01-02T00:00:00")), QStringLiteral("WARN"), QStringLiteral("l"),
QStringLiteral("d"), QStringLiteral("l.cpp:1")));
QCOMPARE(model.length(), 2);
QCOMPARE(model.data(model.index(1, LogViewTableModel::colLevel), Qt::DisplayRole).toString(),
QStringLiteral("WARN"));
}
void logviewer_unit_test::tableModelSetDataByIndex()
{
LogViewTableModel model;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("INFO"), QStringLiteral("l"),
QStringLiteral("d"), QStringLiteral("l.cpp:1"));
model.setData(lines);
// only Qt::EditRole is accepted
QVERIFY(!model.setData(model.index(0, LogViewTableModel::colLevel), QStringLiteral("X"),
Qt::DisplayRole));
QVERIFY(model.setData(model.index(0, LogViewTableModel::colLevel), QStringLiteral("ERROR"),
Qt::EditRole));
QCOMPARE(model.data(model.index(0, LogViewTableModel::colLevel), Qt::DisplayRole).toString(),
QStringLiteral("ERROR"));
QVERIFY(model.setData(model.index(0, LogViewTableModel::colLogger), QStringLiteral("other"),
Qt::EditRole));
QCOMPARE(model.data(model.index(0, LogViewTableModel::colLogger), Qt::DisplayRole).toString(),
QStringLiteral("other"));
QVERIFY(model.setData(model.index(0, LogViewTableModel::colData), QStringLiteral("new data"),
Qt::EditRole));
QCOMPARE(model.data(model.index(0, LogViewTableModel::colData), Qt::DisplayRole).toString(),
QStringLiteral("new data"));
QVERIFY(model.setData(model.index(0, LogViewTableModel::colLocation), QStringLiteral("m.cpp:9"),
Qt::EditRole));
QCOMPARE(model.data(model.index(0, LogViewTableModel::colLocation), Qt::DisplayRole).toString(),
QStringLiteral("m.cpp:9"));
const QDateTime newDate = d(QStringLiteral("2024-02-02T02:02:02"));
QVERIFY(model.setData(model.index(0, LogViewTableModel::colDate), newDate, Qt::EditRole));
QCOMPARE(model.data(model.index(0, LogViewTableModel::colDate), Qt::UserRole).toDateTime(), newDate);
}
void logviewer_unit_test::tableModelStartEndDateTimeSignals()
{
// empty model: no signals
LogViewTableModel empty;
bool started = false;
bool ended = false;
QObject::connect(&empty, &LogViewTableModel::startTimeSetted, [&started](const QDateTime &) { started = true; });
QObject::connect(&empty, &LogViewTableModel::endTimeSetted, [&ended](const QDateTime &) { ended = true; });
empty.startDateTime();
empty.endDateTime();
QVERIFY(!started);
QVERIFY(!ended);
// populated model: first/last line date
LogViewTableModel model;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-03-01T00:00:00")), QStringLiteral("INFO"), QStringLiteral("l"),
QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-03-02T00:00:00")), QStringLiteral("WARN"), QStringLiteral("l"),
QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-03-03T00:00:00")), QStringLiteral("ERROR"), QStringLiteral("l"),
QStringLiteral("d"), QStringLiteral("l.cpp:1"));
model.setData(lines);
QDateTime start;
QDateTime end;
QObject::connect(&model, &LogViewTableModel::startTimeSetted, [&start](const QDateTime &t) { start = t; });
QObject::connect(&model, &LogViewTableModel::endTimeSetted, [&end](const QDateTime &t) { end = t; });
model.startDateTime();
model.endDateTime();
QCOMPARE(start, d(QStringLiteral("2024-03-01T00:00:00")));
QCOMPARE(end, d(QStringLiteral("2024-03-03T00:00:00")));
}
// ---------------------------------------------------------------------------
// SortFilterModel
// ---------------------------------------------------------------------------
void logviewer_unit_test::proxyStringFilter()
{
LogViewTableModel source;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("INFO"),
QStringLiteral("main"), QStringLiteral("alpha"), QStringLiteral("a.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-02T00:00:00")), QStringLiteral("WARN"),
QStringLiteral("net"), QStringLiteral("beta"), QStringLiteral("b.cpp:2"))
<< makeLine(d(QStringLiteral("2024-01-03T00:00:00")), QStringLiteral("INFO"),
QStringLiteral("net"), QStringLiteral("gamma"), QStringLiteral("c.cpp:3"));
source.setData(lines);
SortFilterModel proxy;
proxy.setSourceModel(&source);
QCOMPARE(proxy.rowCount(), 3);
QVERIFY(proxy.stringFilterIsEmpty());
// setStringFilter itself does not re-run the proxy filter (only the date
// setters and resetAllFilter do); mainwindow.cpp calls
// m_filterProxyModel->invalidate() after every setStringFilter, so the
// tests do the same.
// filter on the level column (col 1)
proxy.setStringFilter(LogViewTableModel::colLevel, QStringLiteral("WARN"));
proxy.invalidate();
QCOMPARE(proxy.rowCount(), 1);
QCOMPARE(proxy.data(proxy.index(0, LogViewTableModel::colLevel), Qt::DisplayRole).toString(),
QStringLiteral("WARN"));
// combined with the logger column (col 2)
proxy.setStringFilter(LogViewTableModel::colLogger, QStringLiteral("net"));
proxy.invalidate();
QCOMPARE(proxy.rowCount(), 1);
QCOMPARE(proxy.data(proxy.index(0, LogViewTableModel::colLevel), Qt::DisplayRole).toString(),
QStringLiteral("WARN"));
// replacing an existing filter: "net" does not match the new text "main"
proxy.setStringFilter(LogViewTableModel::colLogger, QStringLiteral("main"));
proxy.invalidate();
// WARN rows have logger "net" -> nothing left
QCOMPARE(proxy.rowCount(), 0);
// case sensitivity: a fresh column filter is stored as-is and the row data
// "beta" does not match "BETA" (contains() is case sensitive)
proxy.resetAllFilter();
QCOMPARE(proxy.rowCount(), 3);
proxy.setStringFilter(LogViewTableModel::colData, QStringLiteral("BETA"));
proxy.invalidate();
QCOMPARE(proxy.rowCount(), 0);
// setStringFilter keeps the old filter when the new text matches the old
// one case-insensitively, so the stored filter is still "BETA"
proxy.setStringFilter(LogViewTableModel::colData, QStringLiteral("beta"));
proxy.invalidate();
QCOMPARE(proxy.rowCount(), 0);
}
void logviewer_unit_test::proxyDateStringFilterEdgeCases()
{
LogViewTableModel source;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-01-01T00:00:00")), QStringLiteral("INFO"),
QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"));
source.setData(lines);
SortFilterModel proxy;
proxy.setSourceModel(&source);
// column range is validated: out-of-range columns are ignored
proxy.setStringFilter(-1, QStringLiteral("x"));
proxy.setStringFilter(5, QStringLiteral("x"));
proxy.setStringFilter(99, QStringLiteral("x"));
QVERIFY(proxy.stringFilterIsEmpty());
QCOMPARE(proxy.rowCount(), 1);
// an empty filter string still counts as "contained" for the activation
// report (columns 3 and 4 only check presence, columns 1 and 2 check for
// non-emptiness)
proxy.setStringFilter(LogViewTableModel::colData, QString());
QVERIFY(!proxy.stringFilterIsEmpty());
QVector<bool> activated = proxy.activatedStringFilter();
QCOMPARE(activated.size(), 5);
QVERIFY(!activated.at(0));
QVERIFY(!activated.at(1));
QVERIFY(!activated.at(2));
QVERIFY(activated.at(3));
QVERIFY(!activated.at(4));
// empty filter does not filter any row (contains("") is always true)
QCOMPARE(proxy.rowCount(), 1);
}
void logviewer_unit_test::proxyDateFilter()
{
LogViewTableModel source;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-01-01T10:00:00")), QStringLiteral("INFO"),
QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-02T10:00:00")), QStringLiteral("WARN"),
QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-03T10:00:00")), QStringLiteral("ERROR"),
QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"));
source.setData(lines);
SortFilterModel proxy;
proxy.setSourceModel(&source);
// only start date: filter not active (both ends required)
proxy.setStartDateFilter(d(QStringLiteral("2024-01-01T10:00:00")));
QCOMPARE(proxy.rowCount(), 3);
QVector<bool> activated = proxy.activatedStringFilter();
QVERIFY(!activated.at(0));
// start and end: window is exclusive on both ends
proxy.setEndDateFilter(d(QStringLiteral("2024-01-03T10:00:00")));
QCOMPARE(proxy.rowCount(), 1);
QCOMPARE(proxy.data(proxy.index(0, LogViewTableModel::colDate), Qt::UserRole).toDateTime(),
d(QStringLiteral("2024-01-02T10:00:00")));
activated = proxy.activatedStringFilter();
QVERIFY(activated.at(0));
// setting the same start date again is a no-op (and must not break the filter)
proxy.setStartDateFilter(d(QStringLiteral("2024-01-01T10:00:00")));
QCOMPARE(proxy.rowCount(), 1);
}
void logviewer_unit_test::proxyResetAllFilter()
{
LogViewTableModel source;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-01-01T10:00:00")), QStringLiteral("INFO"),
QStringLiteral("main"), QStringLiteral("alpha"), QStringLiteral("a.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-02T10:00:00")), QStringLiteral("WARN"),
QStringLiteral("net"), QStringLiteral("beta"), QStringLiteral("b.cpp:2"));
source.setData(lines);
SortFilterModel proxy;
proxy.setSourceModel(&source);
proxy.setStringFilter(LogViewTableModel::colLevel, QStringLiteral("WARN"));
proxy.setStringFilter(LogViewTableModel::colLogger, QStringLiteral("net"));
proxy.setStartDateFilter(d(QStringLiteral("2024-01-01T10:00:00")));
proxy.setEndDateFilter(d(QStringLiteral("2024-01-02T10:00:00")));
QCOMPARE(proxy.rowCount(), 0);
proxy.resetAllFilter();
QVERIFY(proxy.stringFilterIsEmpty());
QCOMPARE(proxy.rowCount(), 2);
QVector<bool> activated = proxy.activatedStringFilter();
for (const bool active : activated)
QVERIFY(!active);
}
void logviewer_unit_test::proxySortByDate()
{
LogViewTableModel source;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-01-03T10:00:00")), QStringLiteral("INFO"),
QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-01T10:00:00")), QStringLiteral("WARN"),
QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-02T10:00:00")), QStringLiteral("ERROR"),
QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"));
source.setData(lines);
SortFilterModel proxy;
proxy.setSourceModel(&source);
proxy.sort(LogViewTableModel::colDate, Qt::AscendingOrder);
QCOMPARE(proxy.rowCount(), 3);
QCOMPARE(proxy.data(proxy.index(0, LogViewTableModel::colDate), Qt::UserRole).toDateTime(),
d(QStringLiteral("2024-01-01T10:00:00")));
QCOMPARE(proxy.data(proxy.index(1, LogViewTableModel::colDate), Qt::UserRole).toDateTime(),
d(QStringLiteral("2024-01-02T10:00:00")));
QCOMPARE(proxy.data(proxy.index(2, LogViewTableModel::colDate), Qt::UserRole).toDateTime(),
d(QStringLiteral("2024-01-03T10:00:00")));
}
void logviewer_unit_test::proxySortByLevel()
{
// non-date columns fall back to the default QVariant comparison
LogViewTableModel source;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-01-01T10:00:00")), QStringLiteral("WARN"),
QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-02T10:00:00")), QStringLiteral("INFO"),
QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"))
<< makeLine(d(QStringLiteral("2024-01-03T10:00:00")), QStringLiteral("DEBUG"),
QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"));
source.setData(lines);
SortFilterModel proxy;
proxy.setSourceModel(&source);
proxy.sort(LogViewTableModel::colLevel, Qt::AscendingOrder);
QCOMPARE(proxy.data(proxy.index(0, LogViewTableModel::colLevel), Qt::DisplayRole).toString(),
QStringLiteral("DEBUG"));
QCOMPARE(proxy.data(proxy.index(1, LogViewTableModel::colLevel), Qt::DisplayRole).toString(),
QStringLiteral("INFO"));
QCOMPARE(proxy.data(proxy.index(2, LogViewTableModel::colLevel), Qt::DisplayRole).toString(),
QStringLiteral("WARN"));
}
void logviewer_unit_test::proxyFigureFilterOutSignal()
{
LogViewTableModel source;
QVector<LogInformation> lines;
lines << makeLine(d(QStringLiteral("2024-01-01T10:00:00")), QStringLiteral("INFO"),
QStringLiteral("l"), QStringLiteral("d"), QStringLiteral("l.cpp:1"));
source.setData(lines);
SortFilterModel proxy;
proxy.setSourceModel(&source);
QVector<bool> lastFilter;
int calls = 0;
QObject::connect(&proxy, &SortFilterModel::theseFilterAreActivated,
[&lastFilter, &calls](const QVector<bool> &filter) {
lastFilter = filter;
++calls;
});
proxy.figureFilterOut();
QCOMPARE(calls, 1);
QCOMPARE(lastFilter, QVector<bool>({false, false, false, false, false}));
proxy.setStringFilter(LogViewTableModel::colLevel, QStringLiteral("INFO"));
proxy.figureFilterOut();
QCOMPARE(calls, 2);
QCOMPARE(lastFilter, QVector<bool>({false, true, false, false, false}));
}
QTEST_MAIN(logviewer_unit_test)
#include "logviewer_unit_test.moc"
@@ -61,3 +61,8 @@ if(QT6_GUI_AVAILABLE)
)
endif()
NGL_INSTALL_TARGET(sqlbrowser)
#
# Unit tests
#
ADD_SUBDIRECTORY(unit-test)
@@ -0,0 +1,23 @@
#
# Unit tests for the SqlBrowser module.
# One test executable per module, each registered with ctest.
#
if (NOT BUILD_TESTING)
return()
endif()
# --- SqlBrowser: data model + add-table model.
# --- The module is an executable, so the model sources are compiled directly
# --- into the test (same pattern as the LogView test in TetraIF/unit-test).
if (TARGET sqlbrowser)
ADD_EXECUTABLE(sqlbrowser_unit_test
sqlbrowser_unit_test.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../DataModel.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../AddModel.cpp)
TARGET_LINK_LIBRARIES(sqlbrowser_unit_test
Qt6::Core
Qt6::Gui
Qt6::Test
)
ADD_TEST(NAME sqlbrowser_unit_test COMMAND $<TARGET_FILE:sqlbrowser_unit_test>)
endif()
@@ -0,0 +1,410 @@
//
// Unit tests for the SqlBrowser module: DataModel (double-buffered table
// model with role handling) and AddModel (add-row table model).
//
// These are the module's pure non-GUI logic classes. No database access is
// involved: the DB work lives in QueryWorker/QueryThread (QSqlDatabase on a
// worker thread). The remaining SqlBrowser classes are either widgets
// (Query, BrowserWnd, LoginWnd, WaitWnd, AddLines) or DB/thread driven.
// PresetManager is skipped as well: its constructor calls
// QApplication::arguments() and its save path uses QInputDialog, so it
// requires a running GUI application.
//
#include "../DataModel.h"
#include "../AddModel.h"
#include <QtCore/QMetaType>
#include <QtCore/QPair>
#include <QtCore/QRegularExpression>
#include <QtGui/QColor>
#include <QtTest/QTest>
class sqlbrowser_unit_test : public QObject
{
Q_OBJECT
private slots:
// DataModel
void dataModelEmptyState();
void dataModelSwapLists();
void dataModelDataRoleHandling();
void dataModelToolTipTimestamp();
void dataModelHeaderData();
void dataModelTableEditability();
void dataModelSlotSetData();
void dataModelSetDataAsyncSemantics();
void dataModelDeleteLines();
// AddModel
void addModelBasics();
void addModelSetDataCreatesRow();
void addModelStringifiesValues();
void addModelRemoveRow();
void addModelClear();
void addModelTableName();
};
// --- DataModel ----------------------------------------------------------------
void sqlbrowser_unit_test::dataModelEmptyState()
{
DataModel model;
QCOMPARE(model.rowCount(), 0);
QCOMPARE(model.columnCount(), 0);
QVERIFY(model.getTableName().isEmpty());
Qt::ItemFlags flags = model.flags(QModelIndex());
QVERIFY(flags & Qt::ItemIsSelectable);
QVERIFY(flags & Qt::ItemIsEnabled);
QVERIFY(!(flags & Qt::ItemIsEditable));
}
void sqlbrowser_unit_test::dataModelSwapLists()
{
DataModel model;
// columns/rows are staged in the write buffer first
model.addColumn(QStringLiteral("id"), QMetaType::Int);
model.addColumn(QStringLiteral("name"), QMetaType::QString);
model.addColumn(QStringLiteral("ts"), QMetaType::LongLong);
Data row0;
row0 << 1 << QStringLiteral("alpha") << static_cast<qint64>(1700000000000LL);
model.addLine(row0);
// nothing is visible in the live model before the swap
QCOMPARE(model.rowCount(), 0);
QCOMPARE(model.columnCount(), 0);
model.slotSwapLists();
QCOMPARE(model.rowCount(), 1);
QCOMPARE(model.columnCount(), 3);
const HeaderList &live = model.getHeaderList();
QCOMPARE(live.size(), 3);
QCOMPARE(live.at(0).first, QStringLiteral("id"));
QVERIFY(live.at(0).second == QMetaType::Int);
QCOMPARE(live.at(1).first, QStringLiteral("name"));
QVERIFY(live.at(1).second == QMetaType::QString);
QCOMPARE(live.at(2).first, QStringLiteral("ts"));
QVERIFY(live.at(2).second == QMetaType::LongLong);
// the write buffer is cleared by the swap: new staging does not leak
// into the live model
Data row1;
row1 << 999 << QStringLiteral("leak");
model.addLine(row1);
QCOMPARE(model.rowCount(), 1);
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 1);
}
void sqlbrowser_unit_test::dataModelDataRoleHandling()
{
DataModel model;
model.addColumn(QStringLiteral("id"), QMetaType::Int);
model.addColumn(QStringLiteral("name"), QMetaType::QString);
model.addColumn(QStringLiteral("ts"), QMetaType::LongLong);
Data row0;
row0 << 1 << QStringLiteral("alpha") << static_cast<qint64>(1700000000000LL);
Data row1;
row1 << 2 << QVariant() << static_cast<qint64>(5);
Data row2;
row2 << 3 << QStringLiteral("gamma") << QVariant();
model.addLine(row0);
model.addLine(row1);
model.addLine(row2);
model.slotSwapLists();
QCOMPARE(model.rowCount(), 3);
QCOMPARE(model.columnCount(), 3);
// DisplayRole and EditRole both return the raw value
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 1);
QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QStringLiteral("alpha"));
QCOMPARE(model.data(model.index(0, 2), Qt::EditRole).toLongLong(), qint64(1700000000000));
// null cells are highlighted via BackgroundRole; filled cells are not
QCOMPARE(model.data(model.index(1, 1), Qt::BackgroundRole).value<QColor>(), QColor(255, 255, 215));
QVERIFY(!model.data(model.index(0, 0), Qt::BackgroundRole).isValid());
// other roles return nothing
QVERIFY(!model.data(model.index(0, 1), Qt::DecorationRole).isValid());
// a valid parent index means "no children"
QCOMPARE(model.rowCount(model.index(0, 0)), 0);
QCOMPARE(model.columnCount(model.index(0, 0)), 0);
}
void sqlbrowser_unit_test::dataModelToolTipTimestamp()
{
DataModel model;
model.addColumn(QStringLiteral("id"), QMetaType::Int);
model.addColumn(QStringLiteral("name"), QMetaType::QString);
model.addColumn(QStringLiteral("ts"), QMetaType::LongLong);
Data row0;
row0 << 1 << QStringLiteral("alpha") << static_cast<qint64>(1700000000000);
Data row1;
row1 << 2 << QStringLiteral("beta") << static_cast<qint64>(5);
model.addLine(row0);
model.addLine(row1);
model.slotSwapLists();
// 64 bit integer values above 1000000000 are rendered as timestamps
const QVariant tooltip = model.data(model.index(0, 2), Qt::ToolTipRole);
QVERIFY(tooltip.isValid());
const QRegularExpression pattern(QStringLiteral("^\\d{2}\\.\\d{2}\\.\\d{4} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}$"));
QVERIFY(pattern.match(tooltip.toString()).hasMatch());
// below the threshold -> no tooltip
QVERIFY(!model.data(model.index(1, 2), Qt::ToolTipRole).isValid());
// non-integer types -> no tooltip
QVERIFY(!model.data(model.index(0, 1), Qt::ToolTipRole).isValid());
QVERIFY(!model.data(model.index(0, 0), Qt::ToolTipRole).isValid());
}
void sqlbrowser_unit_test::dataModelHeaderData()
{
DataModel model;
model.addColumn(QStringLiteral("id"), QMetaType::Int);
model.addColumn(QStringLiteral("name"), QMetaType::QString);
model.addColumn(QStringLiteral("ts"), QMetaType::LongLong);
model.slotSwapLists();
QCOMPARE(model.headerData(0, Qt::Horizontal, Qt::DisplayRole).toString(), QStringLiteral("id"));
QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QStringLiteral("name"));
QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QStringLiteral("ts"));
// ToolTipRole carries the QMetaType name of the column
QCOMPARE(model.headerData(0, Qt::Horizontal, Qt::ToolTipRole).toString(), QStringLiteral("int"));
QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::ToolTipRole).toString(), QStringLiteral("QString"));
QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::ToolTipRole).toString(), QStringLiteral("qlonglong"));
// out-of-range section and vertical orientation fall through to
// QAbstractTableModel::headerData (1-based: section+1 horizontal, 1 vertical)
QCOMPARE(model.headerData(9, Qt::Horizontal, Qt::DisplayRole).toString(), QStringLiteral("10"));
QCOMPARE(model.headerData(0, Qt::Vertical, Qt::DisplayRole).toString(), QStringLiteral("1"));
}
void sqlbrowser_unit_test::dataModelTableEditability()
{
DataModel model;
// custom queries (empty table name) are read-only
QVERIFY(!(model.flags(QModelIndex()) & Qt::ItemIsEditable));
// table queries are editable
model.setTableName(QStringLiteral("users"));
QCOMPARE(model.getTableName(), QStringLiteral("users"));
QVERIFY(model.flags(QModelIndex()) & Qt::ItemIsEditable);
model.setTableName(QString());
QVERIFY(!(model.flags(QModelIndex()) & Qt::ItemIsEditable));
}
void sqlbrowser_unit_test::dataModelSlotSetData()
{
DataModel model;
model.addColumn(QStringLiteral("id"), QMetaType::Int);
model.addColumn(QStringLiteral("name"), QMetaType::QString);
Data row0;
row0 << 1 << QStringLiteral("alpha");
Data row1;
row1 << 2 << QStringLiteral("beta");
model.addLine(row0);
model.addLine(row1);
model.slotSwapLists();
// slotSetData applies the write to the live list (the DB answered)
model.slotSetData(model.index(1, 1), QStringLiteral("delta"));
QCOMPARE(model.data(model.index(1, 1), Qt::DisplayRole).toString(), QStringLiteral("delta"));
QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QStringLiteral("alpha"));
QCOMPARE(model.rowCount(), 2);
}
void sqlbrowser_unit_test::dataModelSetDataAsyncSemantics()
{
DataModel model;
model.addColumn(QStringLiteral("id"), QMetaType::Int);
Data row0;
row0 << 1;
model.addLine(row0);
model.slotSwapLists();
bool captured = false;
QVariant capturedValue;
DataModel *capturedModel = nullptr;
QObject::connect(&model, &DataModel::signalSetData,
[&captured, &capturedValue, &capturedModel](DataModel *m, const QModelIndex &,
const QVariant &v) {
captured = true;
capturedModel = m;
capturedValue = v;
});
// EditRole: value is NOT applied locally; it is forwarded to the worker
// and the call reports "not done yet"
QVERIFY(!model.setData(model.index(0, 0), QStringLiteral("changed"), Qt::EditRole));
QVERIFY(captured);
QCOMPARE(capturedModel, static_cast<DataModel *>(&model));
QCOMPARE(capturedValue.toString(), QStringLiteral("changed"));
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 1); // untouched
// non-EditRole values are accepted (true) and ignored
QVERIFY(model.setData(model.index(0, 0), QStringLiteral("ignored"), Qt::DisplayRole));
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 1);
}
void sqlbrowser_unit_test::dataModelDeleteLines()
{
DataModel model;
model.addColumn(QStringLiteral("id"), QMetaType::Int);
Data row0;
row0 << 1;
Data row1;
row1 << 2;
Data row2;
row2 << 3;
model.addLine(row0);
model.addLine(row1);
model.addLine(row2);
model.slotSwapLists();
QCOMPARE(model.rowCount(), 3);
// delete in descending row order so the indices stay valid
QList<QModelIndex> toDelete;
toDelete << model.index(2, 0) << model.index(0, 0);
model.deleteLines(toDelete);
QCOMPARE(model.rowCount(), 1);
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 2);
// an empty index list is a no-op
model.deleteLines(QList<QModelIndex>());
QCOMPARE(model.rowCount(), 1);
}
// --- AddModel --------------------------------------------------------------------
void sqlbrowser_unit_test::addModelBasics()
{
AddModel model;
// rowCount is always dataList + 1 (the trailing empty row for new data)
QCOMPARE(model.rowCount(), 1);
QCOMPARE(model.columnCount(), 0);
HeaderList headers;
headers.append(QPair<QString, QMetaType::Type>(QStringLiteral("id"), QMetaType::Int));
headers.append(QPair<QString, QMetaType::Type>(QStringLiteral("name"), QMetaType::QString));
model.setHeaderList(headers);
QCOMPARE(model.columnCount(), 2);
QCOMPARE(model.rowCount(), 1);
QVERIFY(model.getColumnType(0) == QMetaType::Int);
QVERIFY(model.getColumnType(1) == QMetaType::QString);
QVERIFY(model.getColumnType(-1) == QMetaType::UnknownType);
QVERIFY(model.getColumnType(2) == QMetaType::UnknownType);
QCOMPARE(model.headerData(0, Qt::Horizontal, Qt::DisplayRole).toString(), QStringLiteral("id"));
QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QStringLiteral("name"));
// only horizontal + displayRole
QVERIFY(!model.headerData(0, Qt::Vertical, Qt::DisplayRole).isValid());
QVERIFY(!model.headerData(0, Qt::Horizontal, Qt::EditRole).isValid());
const Qt::ItemFlags flags = model.flags(QModelIndex());
QVERIFY(flags & Qt::ItemIsSelectable);
QVERIFY(flags & Qt::ItemIsEnabled);
QVERIFY(flags & Qt::ItemIsEditable);
}
void sqlbrowser_unit_test::addModelSetDataCreatesRow()
{
AddModel model;
HeaderList headers;
headers.append(QPair<QString, QMetaType::Type>(QStringLiteral("id"), QMetaType::Int));
headers.append(QPair<QString, QMetaType::Type>(QStringLiteral("name"), QMetaType::QString));
model.setHeaderList(headers);
QCOMPARE(model.rowCount(), 1);
// writing into the empty row materializes it
QVERIFY(model.setData(model.index(0, 0), QStringLiteral("42"), Qt::EditRole));
QCOMPARE(model.rowCount(), 2);
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("42"));
// other columns of the new row are pre-filled with empty strings
QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QString());
// the trailing empty row still has no data
QVERIFY(!model.data(model.index(1, 0), Qt::DisplayRole).isValid());
// a second write materializes row 1
QVERIFY(model.setData(model.index(1, 1), QStringLiteral("bob"), Qt::EditRole));
QCOMPARE(model.rowCount(), 3);
QCOMPARE(model.data(model.index(1, 1), Qt::DisplayRole).toString(), QStringLiteral("bob"));
QCOMPARE(model.data(model.index(1, 0), Qt::DisplayRole).toString(), QString());
// non-EditRole values are rejected
QVERIFY(!model.setData(model.index(0, 0), QStringLiteral("x"), Qt::DisplayRole));
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("42"));
}
void sqlbrowser_unit_test::addModelStringifiesValues()
{
AddModel model;
HeaderList headers;
headers.append(QPair<QString, QMetaType::Type>(QStringLiteral("id"), QMetaType::Int));
model.setHeaderList(headers);
// stored values are stringified on write
QVERIFY(model.setData(model.index(0, 0), 42, Qt::EditRole));
const QVariant value = model.data(model.index(0, 0), Qt::DisplayRole);
QCOMPARE(value.toString(), QStringLiteral("42"));
QVERIFY(value.typeId() == QMetaType::QString);
}
void sqlbrowser_unit_test::addModelRemoveRow()
{
AddModel model;
HeaderList headers;
headers.append(QPair<QString, QMetaType::Type>(QStringLiteral("id"), QMetaType::Int));
model.setHeaderList(headers);
QVERIFY(model.setData(model.index(0, 0), QStringLiteral("1"), Qt::EditRole));
QVERIFY(model.setData(model.index(1, 0), QStringLiteral("2"), Qt::EditRole));
QCOMPARE(model.rowCount(), 3);
// out-of-range row
QVERIFY(!model.removeRow(10));
QCOMPARE(model.rowCount(), 3);
QVERIFY(model.removeRow(0));
QCOMPARE(model.rowCount(), 2);
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("2"));
}
void sqlbrowser_unit_test::addModelClear()
{
AddModel model;
HeaderList headers;
headers.append(QPair<QString, QMetaType::Type>(QStringLiteral("id"), QMetaType::Int));
model.setHeaderList(headers);
QVERIFY(model.setData(model.index(0, 0), QStringLiteral("1"), Qt::EditRole));
QCOMPARE(model.rowCount(), 2);
model.clear();
QCOMPARE(model.rowCount(), 1);
QCOMPARE(model.columnCount(), 1); // header list survives clear()
}
void sqlbrowser_unit_test::addModelTableName()
{
AddModel model;
QCOMPARE(model.getTableName(), QString());
model.setTablename(QStringLiteral("t_users"));
QCOMPARE(model.getTableName(), QStringLiteral("t_users"));
}
QTEST_MAIN(sqlbrowser_unit_test)
#include "sqlbrowser_unit_test.moc"