Commit 4551f0f6 authored by Andreas Cord-Landwehr's avatar Andreas Cord-Landwehr
Browse files

Fix and test correct setting of unit parents

parent 40357fc0
...@@ -26,6 +26,8 @@ ...@@ -26,6 +26,8 @@
#include "core/resourcerepository.h" #include "core/resourcerepository.h"
#include "core/contributorrepository.h" #include "core/contributorrepository.h"
#include "core/language.h" #include "core/language.h"
#include "core/icourse.h"
#include "core/unit.h"
#include "../src/settings.h" #include "../src/settings.h"
void TestIResourceRepository::init() void TestIResourceRepository::init()
...@@ -84,6 +86,7 @@ void TestIResourceRepository::performInterfaceTests(IResourceRepository *interfa ...@@ -84,6 +86,7 @@ void TestIResourceRepository::performInterfaceTests(IResourceRepository *interfa
QVERIFY(german != nullptr); // ensure that German language was found QVERIFY(german != nullptr); // ensure that German language was found
QCOMPARE(interface->courses(german->id()).count(), 1); // there is exactly one German course QCOMPARE(interface->courses(german->id()).count(), 1); // there is exactly one German course
QCOMPARE(interface->courses(nullptr).count(), 2); // all courses in total are 2 QCOMPARE(interface->courses(nullptr).count(), 2); // all courses in total are 2
QVERIFY(interface->courses().first()->units().size() > 0);
} }
QTEST_GUILESS_MAIN(TestIResourceRepository) QTEST_GUILESS_MAIN(TestIResourceRepository)
...@@ -72,6 +72,7 @@ void TestCourseResource::loadCourseResource() ...@@ -72,6 +72,7 @@ void TestCourseResource::loadCourseResource()
QVERIFY(course.language() != nullptr); QVERIFY(course.language() != nullptr);
QCOMPARE(course.language()->id(), "de"); QCOMPARE(course.language()->id(), "de");
QCOMPARE(course.units().count(), 1); QCOMPARE(course.units().count(), 1);
QCOMPARE(course.units().first()->course(), &course);
const auto unit = course.units().first(); const auto unit = course.units().first();
QVERIFY(unit != nullptr); QVERIFY(unit != nullptr);
...@@ -114,10 +115,11 @@ void TestCourseResource::unitAddAndRemoveHandling() ...@@ -114,10 +115,11 @@ void TestCourseResource::unitAddAndRemoveHandling()
QSignalSpy spyAdded(&course, SIGNAL(unitAdded())); QSignalSpy spyAdded(&course, SIGNAL(unitAdded()));
QCOMPARE(spyAboutToBeAdded.count(), 0); QCOMPARE(spyAboutToBeAdded.count(), 0);
QCOMPARE(spyAdded.count(), 0); QCOMPARE(spyAdded.count(), 0);
course.addUnit(std::move(unit)); auto sharedUnit = course.addUnit(std::move(unit));
QCOMPARE(course.units().count(), initialUnitNumber + 1); QCOMPARE(course.units().count(), initialUnitNumber + 1);
QCOMPARE(spyAboutToBeAdded.count(), 1); QCOMPARE(spyAboutToBeAdded.count(), 1);
QCOMPARE(spyAdded.count(), 1); QCOMPARE(spyAdded.count(), 1);
QCOMPARE(sharedUnit->course(), &course);
} }
void TestCourseResource::coursePropertyChanges() void TestCourseResource::coursePropertyChanges()
......
...@@ -81,6 +81,7 @@ void TestEditableCourseResource::loadCourseResource() ...@@ -81,6 +81,7 @@ void TestEditableCourseResource::loadCourseResource()
QVERIFY(course.language() != nullptr); QVERIFY(course.language() != nullptr);
QCOMPARE(course.language()->id(), "de"); QCOMPARE(course.language()->id(), "de");
QCOMPARE(course.units().count(), 1); QCOMPARE(course.units().count(), 1);
QCOMPARE(course.units().first()->course(), &course);
const auto unit = course.units().first(); const auto unit = course.units().first();
QVERIFY(unit != nullptr); QVERIFY(unit != nullptr);
...@@ -120,10 +121,11 @@ void TestEditableCourseResource::unitAddAndRemoveHandling() ...@@ -120,10 +121,11 @@ void TestEditableCourseResource::unitAddAndRemoveHandling()
QSignalSpy spyAdded(&course, SIGNAL(unitAdded())); QSignalSpy spyAdded(&course, SIGNAL(unitAdded()));
QCOMPARE(spyAboutToBeAdded.count(), 0); QCOMPARE(spyAboutToBeAdded.count(), 0);
QCOMPARE(spyAdded.count(), 0); QCOMPARE(spyAdded.count(), 0);
course.addUnit(std::move(unit)); auto sharedUnit = course.addUnit(std::move(unit));
QCOMPARE(course.units().count(), initialUnitNumber + 1); QCOMPARE(course.units().count(), initialUnitNumber + 1);
QCOMPARE(spyAboutToBeAdded.count(), 1); QCOMPARE(spyAboutToBeAdded.count(), 1);
QCOMPARE(spyAdded.count(), 1); QCOMPARE(spyAdded.count(), 1);
QCOMPARE(sharedUnit->course(), &course);
} }
void TestEditableCourseResource::coursePropertyChanges() void TestEditableCourseResource::coursePropertyChanges()
......
...@@ -247,6 +247,7 @@ void CourseResource::setLanguage(std::shared_ptr<Language> language) ...@@ -247,6 +247,7 @@ void CourseResource::setLanguage(std::shared_ptr<Language> language)
std::shared_ptr<Unit> CourseResource::addUnit(std::unique_ptr<Unit> unit) std::shared_ptr<Unit> CourseResource::addUnit(std::unique_ptr<Unit> unit)
{ {
std::shared_ptr<Unit> storedUnit(std::move(unit)); std::shared_ptr<Unit> storedUnit(std::move(unit));
storedUnit->setCourse(this);
emit unitAboutToBeAdded(storedUnit, d->m_units.count() - 1); emit unitAboutToBeAdded(storedUnit, d->m_units.count() - 1);
d->m_units.append(storedUnit); d->m_units.append(storedUnit);
emit unitAdded(); emit unitAdded();
......
...@@ -40,6 +40,11 @@ EditableCourseResource::EditableCourseResource(const QUrl &path, IResourceReposi ...@@ -40,6 +40,11 @@ EditableCourseResource::EditableCourseResource(const QUrl &path, IResourceReposi
, m_course(new CourseResource(path, repository)) , m_course(new CourseResource(path, repository))
{ {
QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
for (auto unit : m_course->units()) {
unit->setCourse(this);
}
connect(m_course.get(), &ICourse::unitAboutToBeAdded, this, &ICourse::unitAboutToBeAdded); connect(m_course.get(), &ICourse::unitAboutToBeAdded, this, &ICourse::unitAboutToBeAdded);
connect(m_course.get(), &ICourse::unitAdded, this, &ICourse::unitAdded); connect(m_course.get(), &ICourse::unitAdded, this, &ICourse::unitAdded);
connect(m_course.get(), &CourseResource::idChanged, this, &EditableCourseResource::idChanged); connect(m_course.get(), &CourseResource::idChanged, this, &EditableCourseResource::idChanged);
...@@ -153,7 +158,9 @@ bool EditableCourseResource::exportCourse(const QUrl &filePath) ...@@ -153,7 +158,9 @@ bool EditableCourseResource::exportCourse(const QUrl &filePath)
std::shared_ptr<Unit> EditableCourseResource::addUnit(std::unique_ptr<Unit> unit) std::shared_ptr<Unit> EditableCourseResource::addUnit(std::unique_ptr<Unit> unit)
{ {
setModified(true); setModified(true);
return m_course->addUnit(std::move(unit)); auto sharedUnit = m_course->addUnit(std::move(unit));
sharedUnit->setCourse(this);
return sharedUnit;
} }
QVector<std::shared_ptr<Unit>> EditableCourseResource::units() QVector<std::shared_ptr<Unit>> EditableCourseResource::units()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment