From 6b383b8f559f8c379e0fd4d662a87f2f018a318c Mon Sep 17 00:00:00 2001 From: Bent Witthold Date: Sat, 21 Feb 2026 22:53:29 +0100 Subject: [PATCH] Added more model tests and fixed the calculations for nPlacedBiddings, biddingSum and totalSharesWithBiddings if there are conflicting entries (shareType: "erarbeitet" but a bidding is placed). --- libs/BeetRoundCore/model/tablemodel.cpp | 34 ++++++++-- tests/GenericCoreTests/model_test.cpp | 87 ++++++++++++++++++++++++- 2 files changed, 113 insertions(+), 8 deletions(-) diff --git a/libs/BeetRoundCore/model/tablemodel.cpp b/libs/BeetRoundCore/model/tablemodel.cpp index efac9c7..8d0dc5a 100644 --- a/libs/BeetRoundCore/model/tablemodel.cpp +++ b/libs/BeetRoundCore/model/tablemodel.cpp @@ -437,9 +437,9 @@ void TableModel::execEditItemData(const int row, const QMap& chan bool isDataChanged = item->setItemData(changedValues); if (isDataChanged) { - /// FIXME due to the mapping from roles to the DisplayRole of different columns the complete row - /// is getting notified about (potential) data changes; dataChanged should be called only for - /// the affected columns + /// FIXME due to the mapping from roles to the DisplayRole of different columns the complete + /// row is getting notified about (potential) data changes; dataChanged should be called only + /// for the affected columns const QModelIndex firstIndex = this->index(row, 0); const QModelIndex lastIndex = this->index(row, USER_FACING_ROLES.size() - 1); QList roles = changedValues.keys(); @@ -577,6 +577,15 @@ bool TableModel::isItemEqualToItemValues(const QModelIndex& itemIndex, int TableModel::nPlacedBiddings(const UserRoles biddingRole) const { int result = 0; for (auto i = m_items.begin(), end = m_items.end(); i != end; ++i) { + const QString biddingType = (*i)->data(BiddingTypeRole).toString(); + const qreal shareAmount = (*i)->data(ShareAmountRole).toReal(); + const QString shareType = (*i)->data(ShareTypeRole).toString(); + if (biddingType.isEmpty() || shareAmount == 0.0) { + continue; + } + if (shareType == "erarbeitet" || shareType == "") { + continue; + } int localBidding = (*i)->data(biddingRole).toInt(); if (localBidding > 0) { result++; @@ -588,6 +597,15 @@ int TableModel::nPlacedBiddings(const UserRoles biddingRole) const { int TableModel::biddingSum(const UserRoles biddingRole) const { int result = 0; for (auto i = m_items.begin(), end = m_items.end(); i != end; ++i) { + const QString biddingType = (*i)->data(BiddingTypeRole).toString(); + const qreal shareAmount = (*i)->data(ShareAmountRole).toReal(); + const QString shareType = (*i)->data(ShareTypeRole).toString(); + if (biddingType.isEmpty() || shareAmount == 0.0) { + continue; + } + if (shareType == "erarbeitet" || shareType == "") { + continue; + } result += (*i)->data(biddingRole).toInt(); } return result; @@ -612,10 +630,14 @@ qreal TableModel::averageBiddingAmount(const UserRoles biddingRole) const { qreal TableModel::totalSharesWithBiddings(const UserRoles biddingRole) const { qreal result = 0; for (auto i = m_items.begin(), end = m_items.end(); i != end; ++i) { - const qreal bidding = (*i)->data(biddingRole).toReal(); - const qreal shareAmount = (*i)->data(ShareAmountRole).toReal(); + const QString biddingType = (*i)->data(BiddingTypeRole).toString(); + const qreal shareAmount = (*i)->data(ShareAmountRole).toReal(); + if (biddingType.isEmpty() || shareAmount == 0.0) { + continue; + } + const int bidding = (*i)->data(biddingRole).toInt(); const QString shareType = (*i)->data(ShareTypeRole).toString(); - const bool isValid = bidding != 0 && shareAmount != 0; + const bool isValid = bidding != 0 && shareAmount != 0.0; if (isValid) { // qInfo() << "Including entry in bidding average calculation. MailRole:" // << (*i)->data(MailRole); diff --git a/tests/GenericCoreTests/model_test.cpp b/tests/GenericCoreTests/model_test.cpp index 9a727df..1153562 100644 --- a/tests/GenericCoreTests/model_test.cpp +++ b/tests/GenericCoreTests/model_test.cpp @@ -15,10 +15,56 @@ struct ModelTest : testing::Test { ModelTest() { model = make_shared(new QUndoStack()); } - ~ModelTest() {} + virtual ~ModelTest() {} }; -TEST_F(ModelTest, TestRowCount) { +struct ModelTestWithData : ModelTest { + ModelTestWithData() { + QList itemList; + + ModelItemValues itemValues1 = {{Bidding1Role, 100}, + {ShareTypeRole, "bezahlt"}, + {ShareAmountRole, 1}, + {BiddingTypeRole, "digital"}}; + itemList.append(itemValues1); + + ModelItemValues itemValues2 = {{Bidding1Role, 100}, + {ShareTypeRole, "bezahlt"}, + {ShareAmountRole, 1}, + {BiddingTypeRole, ""}}; + itemList.append(itemValues2); + + ModelItemValues itemValues3 = {{Bidding1Role, 50}, + {ShareTypeRole, "teils/teils"}, + {ShareAmountRole, 1}, + {BiddingTypeRole, "paper"}}; + itemList.append(itemValues3); + + ModelItemValues itemValues4 = {{Bidding1Role, 50}, + {ShareTypeRole, "bezahlt"}, + {ShareAmountRole, 0.5}, + {BiddingTypeRole, "paper"}}; + itemList.append(itemValues4); + + ModelItemValues itemValues5 = {{Bidding1Role, 100}, + {ShareTypeRole, "erarbeitet"}, + {ShareAmountRole, 1}, + {BiddingTypeRole, "paper"}}; + itemList.append(itemValues5); + + ModelItemValues itemValues6 = {{Bidding1Role, 0}, + {ShareTypeRole, "bezahlt"}, + {ShareAmountRole, 1}, + {BiddingTypeRole, "online"}}; + itemList.append(itemValues6); + + model->insertItems(0, itemList); + } +}; + +/// empty model + +TEST_F(ModelTest, TestRowCountEmpty) { const int expectedRowCount = 0; const auto actualRowCount = model->rowCount(); @@ -32,3 +78,40 @@ TEST_F(ModelTest, TestBidding1Average) { ASSERT_EQ(expected, actualBidding1Average); } + +TEST_F(ModelTest, ExpectedPlacedBiddings) { + const int expected = 0; + const auto expectedPlacedBiddings = model->nExpectedBiddings(); + + ASSERT_EQ(expected, expectedPlacedBiddings); +} + +/// model with data + +TEST_F(ModelTestWithData, TestRowCount) { + const int expected = 6; + + const auto actual = model->rowCount(); + ASSERT_EQ(expected, actual); +} + +TEST_F(ModelTestWithData, TestBidding1Average) { + const qreal expected = 100; + const auto actual = model->biddingAverage1(); + + ASSERT_EQ(expected, actual); +} + +TEST_F(ModelTestWithData, ExpectedPlacedBiddings) { + const int expected = 4; + const auto actual = model->nExpectedBiddings(); + + ASSERT_EQ(expected, actual); +} + +TEST_F(ModelTestWithData, PlacedBiddings1) { + const int expected = 3; + const auto actual = model->nPlacedBiddings1(); + + ASSERT_EQ(expected, actual); +}