add yearly tests
This commit is contained in:
parent
66ebb1b4c5
commit
ae51660e79
@ -95,7 +95,7 @@ bool CalendarItem::isValid() const
|
||||
{
|
||||
// If dateTime AND a repeating option definend...
|
||||
if (m_dateTime.isValid() && !repeatingOption().isEmtpy())
|
||||
// ...only yearly repeating mode is allowed
|
||||
// ...only repeating mode yearly is allowed for dateTime
|
||||
if (repeatingOption().mode() != RepeatingOption::RepeatingModeYearly)
|
||||
return false;
|
||||
|
||||
@ -123,10 +123,11 @@ bool CalendarItem::evaluate(const QDateTime &dateTime) const
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
} else if (m_repeatingOption.mode() == RepeatingOption::RepeatingModeYearly) {
|
||||
return evaluateYearly(dateTime);
|
||||
} else {
|
||||
return dateTime >= m_dateTime && dateTime < m_dateTime.addSecs(duration() * 60);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CalendarItem::evaluateHourly(const QDateTime &dateTime) const
|
||||
@ -192,10 +193,10 @@ bool CalendarItem::evaluateWeekly(const QDateTime &dateTime) const
|
||||
// Check if this calendar item overlaps a week...
|
||||
if (startDateTime.date().weekNumber() != endDateTime.date().weekNumber()) {
|
||||
// ...jump one week back in to the past
|
||||
QDateTime startDateTimePreviouseWeek = startDateTime.addDays(-7);
|
||||
QDateTime endDateTimePreviouseWeek = startDateTimePreviouseWeek.addSecs(duration() * 60);
|
||||
QDateTime startDateTimePreviousWeek = startDateTime.addDays(-7);
|
||||
QDateTime endDateTimePreviousWeek = startDateTimePreviousWeek.addSecs(duration() * 60);
|
||||
|
||||
if (dateTime >= startDateTimePreviouseWeek && dateTime < endDateTimePreviouseWeek)
|
||||
if (dateTime >= startDateTimePreviousWeek && dateTime < endDateTimePreviousWeek)
|
||||
// Return true if the current time is between start
|
||||
// and end of this calendar item from the previouse week
|
||||
return true;
|
||||
@ -229,17 +230,17 @@ bool CalendarItem::evaluateMonthly(const QDateTime &dateTime) const
|
||||
// and end of this calendar item
|
||||
return true;
|
||||
|
||||
// Check if this calendar item overlaps a month...
|
||||
if (startDateTime.date().month() != endDateTime.date().month()) {
|
||||
// ...jump one month back in to the past
|
||||
QDateTime startDateTimePreviouseMonth = startDateTime.addMonths(-1);
|
||||
QDateTime endDateTimePreviouseMonth = startDateTimePreviouseMonth.addSecs(duration() * 60);
|
||||
// // Check if this calendar item overlaps a month...
|
||||
// if (startDateTime.date().month() != endDateTime.date().month()) {
|
||||
// // ...jump one month back in to the past
|
||||
// QDateTime startDateTimePreviousMonth = startDateTime.addMonths(-1);
|
||||
// QDateTime endDateTimePreviousMonth = startDateTimePreviousMonth.addSecs(duration() * 60);
|
||||
|
||||
if (dateTime >= startDateTimePreviouseMonth && dateTime < endDateTimePreviouseMonth)
|
||||
// Return true if the current time is between start
|
||||
// and end of this calendar item from the previouse month
|
||||
return true;
|
||||
}
|
||||
// if (dateTime >= startDateTimePreviousMonth && dateTime < endDateTimePreviousMonth)
|
||||
// // Return true if the current time is between start
|
||||
// // and end of this calendar item from the previouse month
|
||||
// return true;
|
||||
// }
|
||||
}
|
||||
|
||||
return false;
|
||||
@ -247,7 +248,25 @@ bool CalendarItem::evaluateMonthly(const QDateTime &dateTime) const
|
||||
|
||||
bool CalendarItem::evaluateYearly(const QDateTime &dateTime) const
|
||||
{
|
||||
Q_UNUSED(dateTime)
|
||||
// check for this year
|
||||
QDateTime startDateTimeThisYear = QDateTime(QDate(dateTime.date().year(), m_dateTime.date().month(), m_dateTime.date().day()), m_dateTime.time());
|
||||
QDateTime endDateTimeThisYear = startDateTimeThisYear.addSecs(duration() * 60);
|
||||
|
||||
// check if we are in the interval of this year
|
||||
if (dateTime >= startDateTimeThisYear && dateTime < endDateTimeThisYear)
|
||||
return true;
|
||||
|
||||
// check if this calendaritem overlaps a year
|
||||
if (startDateTimeThisYear.date().year() != endDateTimeThisYear.date().year()) {
|
||||
// go one year in to the past
|
||||
QDateTime startDateTimePreviousYear = startDateTimeThisYear.addYears(-1);
|
||||
QDateTime endDateTimePreviousYear = startDateTimePreviousYear.addSecs(duration() * 60);
|
||||
|
||||
// check if we are in the interval of the previous year
|
||||
if (dateTime >= startDateTimePreviousYear && dateTime < endDateTimePreviousYear)
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -49,6 +49,9 @@ private slots:
|
||||
void testCalendarDateTime_data();
|
||||
void testCalendarDateTime();
|
||||
|
||||
void testCalendarYearlyDateTime_data();
|
||||
void testCalendarYearlyDateTime();
|
||||
|
||||
void testCalendarItemHourly_data();
|
||||
void testCalendarItemHourly();
|
||||
|
||||
@ -258,14 +261,13 @@ void TestTimeManager::testCalendarDateTime_data()
|
||||
QTest::addColumn<QDateTime>("dateTime");
|
||||
QTest::addColumn<int>("duration");
|
||||
|
||||
QTest::newRow("dateTime - chrismas") << QDateTime::fromString("24.12.2017 20:00", "dd.MM.yyyy hh:mm") << 60;
|
||||
QTest::newRow("dateTime - christmas") << QDateTime::fromString("24.12.2017 20:00", "dd.MM.yyyy hh:mm") << 60;
|
||||
QTest::newRow("dateTime - new year") << QDateTime::fromString("31.12.2017 23:00", "dd.MM.yyyy hh:mm") << 120;
|
||||
QTest::newRow("dateTime - valentines day") << QDateTime::fromString("14.02.2017 08:00", "dd.MM.yyyy hh:mm") << 120;
|
||||
}
|
||||
|
||||
void TestTimeManager::testCalendarDateTime()
|
||||
{
|
||||
|
||||
QFETCH(QDateTime, dateTime);
|
||||
QFETCH(int, duration);
|
||||
|
||||
@ -307,9 +309,91 @@ void TestTimeManager::testCalendarDateTime()
|
||||
verifyRuleError(response);
|
||||
RuleId ruleId = RuleId(response.toMap().value("params").toMap().value("ruleId").toString());
|
||||
|
||||
QVariantMap params;
|
||||
params.insert("ruleId", ruleId);
|
||||
response = injectAndWait("Rules.GetRuleDetails", params);
|
||||
QDateTime oneMinuteBeforeEvent = dateTime.addSecs(-60);
|
||||
|
||||
GuhCore::instance()->timeManager()->setTime(oneMinuteBeforeEvent);
|
||||
verifyRuleNotExecuted();
|
||||
// active
|
||||
GuhCore::instance()->timeManager()->setTime(dateTime);
|
||||
verifyRuleExecuted(mockActionIdNoParams);
|
||||
cleanupMockHistory();
|
||||
// active unchanged
|
||||
GuhCore::instance()->timeManager()->setTime(dateTime.addSecs(duration * 30));
|
||||
verifyRuleNotExecuted();
|
||||
// inactive
|
||||
GuhCore::instance()->timeManager()->setTime(dateTime.addSecs(duration * 60));
|
||||
verifyRuleExecuted(mockActionIdWithParams);
|
||||
cleanupMockHistory();
|
||||
// inactive unchanged
|
||||
GuhCore::instance()->timeManager()->setTime(dateTime.addSecs((duration + 1) * 60));
|
||||
verifyRuleNotExecuted();
|
||||
|
||||
cleanupMockHistory();
|
||||
|
||||
// REMOVE rule
|
||||
QVariantMap removeParams;
|
||||
removeParams.insert("ruleId", ruleId);
|
||||
response = injectAndWait("Rules.RemoveRule", removeParams);
|
||||
verifyRuleError(response);
|
||||
}
|
||||
|
||||
void TestTimeManager::testCalendarYearlyDateTime_data()
|
||||
{
|
||||
QTest::addColumn<QDateTime>("dateTime");
|
||||
QTest::addColumn<int>("duration");
|
||||
|
||||
QTest::newRow("dateTime - yearly - christmas") << QDateTime::fromString(QString("24.12.%1 20:00").arg(QDateTime::currentDateTime().date().year() + 1), "dd.MM.yyyy hh:mm") << 60;
|
||||
QTest::newRow("dateTime - yearly - new year") << QDateTime::fromString(QString("31.12.%1 23:00").arg(QDateTime::currentDateTime().date().year() + 1), "dd.MM.yyyy hh:mm") << 120;
|
||||
QTest::newRow("dateTime - yearly - valentines day") << QDateTime::fromString("14.02.2017 08:00", "dd.MM.yyyy hh:mm") << 120;
|
||||
}
|
||||
|
||||
void TestTimeManager::testCalendarYearlyDateTime()
|
||||
{
|
||||
QFETCH(QDateTime, dateTime);
|
||||
QFETCH(int, duration);
|
||||
|
||||
initTimeManager();
|
||||
|
||||
// Action (without params)
|
||||
QVariantMap ruleMap; QVariantMap action; QVariantMap exitAction;
|
||||
action.insert("actionTypeId", mockActionIdNoParams);
|
||||
action.insert("deviceId", m_mockDeviceId);
|
||||
action.insert("ruleActionParams", QVariantList());
|
||||
|
||||
// Exit action (with params)
|
||||
QVariantList actionParams;
|
||||
QVariantMap param1;
|
||||
param1.insert("name", "mockActionParam1");
|
||||
param1.insert("value", 12);
|
||||
actionParams.append(param1);
|
||||
QVariantMap param2;
|
||||
param2.insert("name", "mockActionParam2");
|
||||
param2.insert("value", true);
|
||||
actionParams.append(param2);
|
||||
exitAction.insert("actionTypeId", mockActionIdWithParams);
|
||||
exitAction.insert("deviceId", m_mockDeviceId);
|
||||
exitAction.insert("ruleActionParams", actionParams);
|
||||
|
||||
// RepeatingOption
|
||||
QVariantMap repeatingOption;
|
||||
repeatingOption.insert("mode", "RepeatingModeYearly");
|
||||
|
||||
// CalendarItem
|
||||
QVariantMap calendarItem;
|
||||
calendarItem.insert("datetime", QVariant(dateTime.toTime_t()));
|
||||
calendarItem.insert("duration", QVariant(duration));
|
||||
calendarItem.insert("repeating", repeatingOption);
|
||||
|
||||
// Create the rule map
|
||||
ruleMap.insert("name", "Time based yearly calendar rule");
|
||||
ruleMap.insert("timeDescriptor", createTimeDescriptorCalendar(calendarItem));
|
||||
ruleMap.insert("actions", QVariantList() << action);
|
||||
ruleMap.insert("exitActions", QVariantList() << exitAction);
|
||||
|
||||
// Add the rule
|
||||
QVariant response = injectAndWait("Rules.AddRule", ruleMap);
|
||||
verifyRuleError(response);
|
||||
RuleId ruleId = RuleId(response.toMap().value("params").toMap().value("ruleId").toString());
|
||||
|
||||
QDateTime oneMinuteBeforeEvent = dateTime.addSecs(-60);
|
||||
|
||||
@ -330,6 +414,28 @@ void TestTimeManager::testCalendarDateTime()
|
||||
GuhCore::instance()->timeManager()->setTime(dateTime.addSecs((duration + 1) * 60));
|
||||
verifyRuleNotExecuted();
|
||||
|
||||
|
||||
// One year "Back to the future"
|
||||
oneMinuteBeforeEvent = oneMinuteBeforeEvent.addYears(1);
|
||||
dateTime = dateTime.addYears(1);
|
||||
|
||||
GuhCore::instance()->timeManager()->setTime(oneMinuteBeforeEvent);
|
||||
verifyRuleNotExecuted();
|
||||
// active
|
||||
GuhCore::instance()->timeManager()->setTime(dateTime);
|
||||
verifyRuleExecuted(mockActionIdNoParams);
|
||||
cleanupMockHistory();
|
||||
// active unchanged
|
||||
GuhCore::instance()->timeManager()->setTime(dateTime.addSecs(duration * 30));
|
||||
verifyRuleNotExecuted();
|
||||
// inactive
|
||||
GuhCore::instance()->timeManager()->setTime(dateTime.addSecs(duration * 60));
|
||||
verifyRuleExecuted(mockActionIdWithParams);
|
||||
cleanupMockHistory();
|
||||
// inactive unchanged
|
||||
GuhCore::instance()->timeManager()->setTime(dateTime.addSecs((duration + 1) * 60));
|
||||
verifyRuleNotExecuted();
|
||||
|
||||
cleanupMockHistory();
|
||||
|
||||
// REMOVE rule
|
||||
|
||||
Reference in New Issue
Block a user