85 lines
3.1 KiB
C++
85 lines
3.1 KiB
C++
#include "TestFixtures.h"
|
|
#include "core/JsonIo.h"
|
|
#include "core/NetworkModel.h"
|
|
|
|
#include <QTemporaryDir>
|
|
#include <QTest>
|
|
|
|
using namespace diag;
|
|
|
|
class TestJson : public QObject {
|
|
Q_OBJECT
|
|
private slots:
|
|
void init()
|
|
{
|
|
fixtures::fillRegistry(m_reg);
|
|
delete m_model;
|
|
m_model = new NetworkModel(&m_reg, this);
|
|
}
|
|
|
|
void roundTrip_preservesDocument()
|
|
{
|
|
const QUuid src = m_model->addNode(QStringLiteral("source"), {20, 40});
|
|
const QUuid pump = m_model->addNode(QStringLiteral("pump"), {220, 40});
|
|
const QUuid c = m_model->addNode(QStringLiteral("consumer"), {420, 40});
|
|
const QUuid e1 = m_model->addEdge(src, "out", pump, "in");
|
|
const QUuid e2 = m_model->addEdge(pump, "out", c, "in");
|
|
m_model->setNodeProperty(src, "title", QStringLiteral("Intake"));
|
|
m_model->setNodeProperty(src, "tags", QStringList{"a", "b"});
|
|
m_model->setEdgeProperty(e1, "diameter", 150.0);
|
|
m_model->setEdgeProperty(e1, "headDecoration", QStringLiteral("Diamond"));
|
|
m_model->setEdgeProperty(e2, "series", QStringLiteral("dn100"));
|
|
|
|
const QJsonObject doc = JsonIo::toJson(*m_model);
|
|
|
|
NetworkModel restored(&m_reg);
|
|
QString err;
|
|
QVERIFY2(JsonIo::fromJson(doc, &restored, &err), qPrintable(err));
|
|
|
|
QCOMPARE(restored.nodeIds().size(), 3);
|
|
QCOMPARE(restored.edgeIds().size(), 2);
|
|
QCOMPARE(restored.node(src)->pos, QPointF(20, 40));
|
|
QCOMPARE(restored.nodeProperty(src, "title").toString(), QStringLiteral("Intake"));
|
|
QCOMPARE(restored.nodeProperty(src, "tags").toStringList(), (QStringList{"a", "b"}));
|
|
QCOMPARE(restored.edgeProperty(e1, "diameter").toDouble(), 150.0);
|
|
QCOMPARE(restored.edgeProperty(e1, "headDecoration").toString(),
|
|
QStringLiteral("Diamond"));
|
|
QCOMPARE(restored.edgeProperty(e2, "series").toString(), QStringLiteral("dn100"));
|
|
QCOMPARE(restored.edge(e1)->fromNode, src);
|
|
QCOMPARE(restored.edge(e1)->toNode, pump);
|
|
QCOMPARE(restored.edge(e1)->relation, QStringLiteral("water"));
|
|
}
|
|
|
|
void saveAndLoad_file()
|
|
{
|
|
QTemporaryDir dir;
|
|
const QString path = dir.filePath(QStringLiteral("doc.json"));
|
|
const QUuid src = m_model->addNode(QStringLiteral("source"), {0, 0});
|
|
const QUuid c = m_model->addNode(QStringLiteral("consumer"), {200, 0});
|
|
m_model->addEdge(src, "out", c, "in");
|
|
|
|
QString err;
|
|
QVERIFY2(JsonIo::save(*m_model, path, &err), qPrintable(err));
|
|
|
|
NetworkModel restored(&m_reg);
|
|
QVERIFY2(JsonIo::load(&restored, path, &err), qPrintable(err));
|
|
QCOMPARE(restored.nodeIds().size(), 2);
|
|
QCOMPARE(restored.edgeIds().size(), 1);
|
|
}
|
|
|
|
void invalidDocument_rejected()
|
|
{
|
|
NetworkModel restored(&m_reg);
|
|
QString err;
|
|
QVERIFY(!JsonIo::fromJson(QJsonObject{{"format", "other"}}, &restored, &err));
|
|
QVERIFY(!err.isEmpty());
|
|
}
|
|
|
|
private:
|
|
TypeRegistry m_reg;
|
|
NetworkModel* m_model = nullptr;
|
|
};
|
|
|
|
QTEST_GUILESS_MAIN(TestJson)
|
|
#include "tst_json.moc"
|