Created
December 22, 2019 01:13
-
-
Save WindAzure/e267f4a40c95a95d4e533edfc1b56068 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "gtest/gtest.h" | |
#include "MockBudgetRepo.h" | |
#include "Accounting.h" | |
using namespace std; | |
using namespace ::testing; | |
class AccoutingTestCase : public testing::Test | |
{ | |
protected: | |
void SetUp() override { m_spMockBudgetRepo = make_shared<MockBudgetRepo>(); } | |
void TearDown() override {} | |
void setBudget(const vector<Budget> &budgetList) | |
{ | |
m_spMockBudgetRepo->setBudget(budgetList); | |
m_accounting.setBudgetRepo(m_spMockBudgetRepo); | |
} | |
Accounting m_accounting; | |
shared_ptr<MockBudgetRepo> m_spMockBudgetRepo = nullptr; | |
}; | |
TEST_F(AccoutingTestCase, queryOneMonthBudget) | |
{ | |
setBudget(vector<Budget> { | |
Budget {"201901", 31}, | |
Budget {"201902", 280}, | |
Budget {"201903", 3100}, | |
}); | |
auto budget = m_accounting.queryBudget(QDate(2019, 01, 01), QDate(2019, 01, 31)); | |
ASSERT_EQ(budget, 31); | |
} | |
TEST_F(AccoutingTestCase, queryTwoMonthBudget) | |
{ | |
setBudget(vector<Budget> { | |
Budget {"201901", 31}, | |
Budget {"201902", 280}, | |
Budget {"201903", 3100}, | |
}); | |
auto budget = m_accounting.queryBudget(QDate(2019, 01, 01), QDate(2019, 02, 28)); | |
ASSERT_EQ(budget, 311); | |
} | |
TEST_F(AccoutingTestCase, queryPartialMonthBudget) | |
{ | |
setBudget(vector<Budget> { | |
Budget {"201901", 31}, | |
Budget {"201902", 280}, | |
Budget {"201903", 3100}, | |
}); | |
auto budget = m_accounting.queryBudget(QDate(2019, 01, 01), QDate(2019, 01, 15)); | |
ASSERT_EQ(budget, 15); | |
} | |
TEST_F(AccoutingTestCase, queryCrossMonthBudget) | |
{ | |
setBudget(vector<Budget> { | |
Budget {"201901", 31}, | |
Budget {"201902", 280}, | |
Budget {"201903", 3100}, | |
}); | |
auto budget = m_accounting.queryBudget(QDate(2019, 01, 31), QDate(2019, 03, 01)); | |
ASSERT_EQ(budget, 381); | |
} | |
TEST_F(AccoutingTestCase, queryBudgetWithInvaildDate) | |
{ | |
setBudget(vector<Budget> { | |
Budget {"201901", 31}, | |
Budget {"201902", 280}, | |
Budget {"201903", 3100}, | |
}); | |
auto budget = m_accounting.queryBudget(QDate(2019, 03, 01), QDate(2019, 01, 31)); | |
ASSERT_EQ(budget, 0); | |
} | |
TEST_F(AccoutingTestCase, queryNotExistBudget) | |
{ | |
setBudget(vector<Budget> { | |
Budget {"201901", 31}, | |
Budget {"201902", 280}, | |
Budget {"201903", 3100}, | |
}); | |
auto budget = m_accounting.queryBudget(QDate(2018, 01, 01), QDate(2018, 01, 31)); | |
ASSERT_EQ(budget, 0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Accounting.h" | |
#include "IBudgetRepo.h" | |
double Accounting::queryBudget(const QDate &startDate, const QDate &endDate) | |
{ | |
auto sum = 0; | |
for (auto currentDate = startDate; currentDate <= endDate; currentDate = currentDate.addDays(1)) | |
{ | |
auto yearMonthString = currentDate.toString("yyyyMM"); | |
if (budgetMap.find(yearMonthString) != budgetMap.end()) | |
{ | |
sum += budgetMap[yearMonthString]; | |
} | |
} | |
return sum; | |
} | |
void Accounting::setBudgetRepo(std::shared_ptr<IBudgetRepo> budgetRepo) | |
{ | |
for (const auto &budget: budgetRepo->getAll()) | |
{ | |
auto currentDate = QDate::fromString(budget.YearMonth, "yyyyMM"); | |
budgetMap.insert(std::make_pair(budget.YearMonth, budget.Amount / currentDate.daysInMonth())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment