From 48ebeff90fa77e8f38d67f2472fa5eee178cd765 Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Sun, 5 Feb 2017 20:41:59 +0000 Subject: [PATCH] tests: Added more tests for CalendarDataStorage --- .../CalendarDataStorage.cpp | 81 +++++++++++++++- .../CalendarDataStorage/CalendarDataStorage.h | 4 +- .../xestiacalendar_calendardatastorage.h | 94 +++++++++++++++++-- 3 files changed, 170 insertions(+), 9 deletions(-) diff --git a/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp b/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp index 4a690b2..63c8a3e 100644 --- a/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp +++ b/source/libraries/CalendarDataStorage/CalendarDataStorage.cpp @@ -157,6 +157,33 @@ CDSAccountResult CalendarDataStorage::AddAccount(string accountName, int account int resultCode; sqlite3_stmt *statementHandle; + sqlite3_stmt *findHandle; + + if (accountName == ""){ + return CDSACCOUNT_NONAME; + } + + // Check if the account name already exsits. + + resultCode = sqlite3_prepare_v2(db, "SELECT name FROM accounts WHERE name=(?1);", -1, &findHandle, nullptr); + + if (resultCode != 0){ + return CDSACCOUNT_FAILED; + } + + resultCode = sqlite3_bind_text(findHandle, 1, accountName.c_str(), -1, SQLITE_STATIC); + + if (resultCode != 0){ + return CDSACCOUNT_FAILED; + } + + resultCode = sqlite3_step(findHandle); + + if (resultCode != SQLITE_DONE){ + return CDSACCOUNT_FAILED; + } else if (resultCode == SQLITE_ROW){ + return CDSACCOUNT_FAILED; + } resultCode = sqlite3_prepare_v2(db, "INSERT INTO accounts (name, preferencesid) VALUES(?1, ?2);", -1, &statementHandle, nullptr); @@ -316,8 +343,13 @@ CDSAccountResult CalendarDataStorage::UpdateAccount(int accountID, string accoun int resultCode; sqlite3_stmt *findHandle; + sqlite3_stmt *existingHandle; sqlite3_stmt *statementHandle; + if (accountName == ""){ + return CDSACCOUNT_NONAME; + } + // Check if account exists first. resultCode = sqlite3_prepare_v2(db, "SELECT id from accounts WHERE id=(?1);", -1, &findHandle, nullptr); @@ -342,6 +374,30 @@ CDSAccountResult CalendarDataStorage::UpdateAccount(int accountID, string accoun return CDSACCOUNT_FAILED; } + // Check if account with the name given already exists before renaming. + + resultCode = sqlite3_prepare_v2(db, "SELECT name from accounts WHERE name=(?1);", -1, &existingHandle, nullptr); + + if (resultCode != 0){ + return CDSACCOUNT_FAILED; + } + + resultCode = sqlite3_bind_text(existingHandle, 1, accountName.c_str(), -1, SQLITE_STATIC); + + if (resultCode != 0){ + return CDSACCOUNT_FAILED; + } + + resultCode = sqlite3_step(existingHandle); + + if (resultCode == SQLITE_ROW){ + return CDSACCOUNT_FAILED; + } else if (resultCode == SQLITE_DONE) { + + } else { + return CDSACCOUNT_FAILED; + } + // Update the account. resultCode = sqlite3_prepare_v2(db, "UPDATE accounts SET name=(?1) WHERE id=(?2);", -1, &statementHandle, nullptr); @@ -409,7 +465,7 @@ CDSAccountResult CalendarDataStorage::DeleteAccount(int accountID) } else if (resultCode == SQLITE_DONE) { return CDSACCOUNT_NOACCOUNT; } else { - return CDSACCOUNT_FAILED; + } // Delete the account. @@ -482,9 +538,30 @@ CDSCalendarResult CalendarDataStorage::AddCalendar(int accountID, string calenda CDSCalendarResult addResult = CDSCALENDAR_UNITTESTFAIL; int resultCode; - + + sqlite3_stmt *findHandle; sqlite3_stmt *statementHandle; + // Check if the account exists first. + + resultCode = sqlite3_prepare_v2(db, "SELECT id FROM accounts WHERE id=(?1);", -1, &findHandle, nullptr); + + if (resultCode != 0){ + return CDSCALENDAR_FAILED; + } + + resultCode = sqlite3_bind_int(findHandle, 1, accountID); + + if (resultCode != 0){ + return CDSCALENDAR_FAILED; + } + + resultCode = sqlite3_step(findHandle); + + if (resultCode != SQLITE_DONE){ + return CDSCALENDAR_NOACCOUNT; + } + resultCode = sqlite3_prepare_v2(db, "INSERT INTO calendars (name, calendarid, accountid, colour, description) VALUES(?1, ?2, ?3, ?4, ?5);", -1, &statementHandle, nullptr); if (resultCode != 0){ diff --git a/source/libraries/CalendarDataStorage/CalendarDataStorage.h b/source/libraries/CalendarDataStorage/CalendarDataStorage.h index 87c5caa..402aee8 100644 --- a/source/libraries/CalendarDataStorage/CalendarDataStorage.h +++ b/source/libraries/CalendarDataStorage/CalendarDataStorage.h @@ -36,7 +36,8 @@ enum CDSAccountResult{ CDSACCOUNT_OK, CDSACCOUNT_FAILED, CDSACCOUNT_NOACTION, - CDSACCOUNT_NOACCOUNT + CDSACCOUNT_NOACCOUNT, + CDSACCOUNT_NONAME }; enum CDSCalendarResult{ @@ -44,6 +45,7 @@ enum CDSCalendarResult{ CDSCALENDAR_OK, CDSCALENDAR_FAILED, CDSCALENDAR_NOACTION, + CDSCALENDAR_NOACCOUNT, CDSCALENDAR_NOCALENDAR }; diff --git a/source/tests/xestiacalendar_calendardatastorage.h b/source/tests/xestiacalendar_calendardatastorage.h index 9b04282..4e4a959 100644 --- a/source/tests/xestiacalendar_calendardatastorage.h +++ b/source/tests/xestiacalendar_calendardatastorage.h @@ -55,6 +55,23 @@ TEST(CalendarDataStorage, Add_Three_Accounts){ } +TEST(CalendarDataStorage, Add_Account_With_No_Name_And_Return_No_Name_Message){ + + CalendarDataStorage failNameTest; + + ASSERT_EQ(CDSACCOUNT_NONAME, failNameTest.AddAccount("", 1)); + +} + +TEST(CalendarDataStorage, Add_Account_With_Same_Name_Twice_And_Return_Failed_Message){ + + CalendarDataStorage duplicateNameTest; + + ASSERT_EQ(CDSACCOUNT_OK, duplicateNameTest.AddAccount("Account 1", 1)); + ASSERT_EQ(CDSACCOUNT_FAILED, duplicateNameTest.AddAccount("Account 1", 1)); + +} + TEST(CalendarDataStorage, Get_Account_Information){ CalendarDataStorage accountInformationTest; @@ -66,6 +83,14 @@ TEST(CalendarDataStorage, Get_Account_Information){ } +TEST(CalendarDataStorage, Get_Account_Information_That_Does_Not_Exist_Return_No_Account_Message){ + + CalendarDataStorage noAccountTest; + + ASSERT_EQ(CDSACCOUNT_NOACCOUNT, noAccountTest.GetAccount("Test Account 1").accountInfoResult); + +} + TEST(CalendarDataStorage, Get_Two_Accounts_Information){ CalendarDataStorage accountInformationTest; @@ -197,6 +222,41 @@ TEST(CalendarDataStorage, Update_One_Account){ } +TEST(CalendarDataStorage, Update_One_Account_Set_Account_Name_To_No_Name_Return_No_Name_Message){ + + CalendarDataStorage updateAccountTest; + + updateAccountTest.AddAccount("Test Account 1", 1); + + int testAccount1ID = updateAccountTest.GetAccount("Test Account 1").accountID; + + EXPECT_EQ(1, testAccount1ID); + ASSERT_EQ(CDSACCOUNT_NONAME, updateAccountTest.UpdateAccount(testAccount1ID, "")); + +} + +TEST(CalendarDataStorage, Update_One_Account_Set_Account_Name_To_Existing_Account_Return_Failed_Message){ + + CalendarDataStorage updateAccountTest; + + updateAccountTest.AddAccount("Test Account 1", 1); + updateAccountTest.AddAccount("Test Account 2", 2); + + int testAccount2ID = updateAccountTest.GetAccount("Test Account 2").accountID; + + EXPECT_EQ(2, testAccount2ID); + ASSERT_EQ(CDSACCOUNT_FAILED, updateAccountTest.UpdateAccount(testAccount2ID, "Test Account 1")); + +} + +TEST(CalendarDataStorage, Update_One_Account_That_Does_Not_Exist_Return_No_Account_Message){ + + CalendarDataStorage updateAccountTest; + + ASSERT_EQ(CDSACCOUNT_NOACCOUNT, updateAccountTest.UpdateAccount(1, "Missing Account")); + +} + TEST(CalendarDataStorage, Update_Two_Accounts){ CalendarDataStorage updateAccountTest; @@ -288,6 +348,14 @@ TEST(CalendarDataStorage, Delete_Three_Accounts){ } +TEST(CalendarDataStorage, Delete_Account_That_Does_Not_Exist_Return_No_Account_Message){ + + CalendarDataStorage deleteAccountTest; + + ASSERT_EQ(CDSACCOUNT_NOACCOUNT, deleteAccountTest.DeleteAccount(1)); + +} + TEST(CalendarDataStorage, Add_One_Calendar_To_One_Account){ CalendarDataStorage addCalendarTest; @@ -321,6 +389,14 @@ TEST(CalendarDataStorage, Add_Three_Calendars_To_One_Account){ } +TEST(CalendarDataStorage, Add_Calendar_To_Account_That_Does_Not_Exist_Return_No_Account_Message){ + + CalendarDataStorage addCalendarTest; + + ASSERT_EQ(CDSCALENDAR_NOACCOUNT, addCalendarTest.AddCalendar(addCalendarTest.GetAccount("Test Account 1").accountID, "Calendar 1", "calendar1-test", defaultColour, "Calendar Description")); + +} + TEST(CalendarDataStorage, Add_One_Calendar_To_Two_Accounts){ CalendarDataStorage addCalendarTest; @@ -1570,7 +1646,8 @@ TEST(CalendarDataStorage, Delete_One_Account_And_Associated_Data){ EXPECT_EQ(CDSCALENDAR_OK, deleteAccountTest.DeleteAccount(1)); - EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 1", "calendar1-test").calendarInfoResult); + EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 1", "calendar1-test").accountInfoResult); + EXPECT_EQ(CDSENTRY_NOCALENDAR, deleteAccountTest.GetCalendar("Test Account 1", "calendar1-test").calendarInfoResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent1Result.calendarEntryID).getEventResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent2Result.calendarEntryID).getEventResult); @@ -1598,7 +1675,8 @@ TEST(CalendarDataStorage, Delete_Two_Accounts_And_Associated_Data){ EXPECT_EQ(CDSCALENDAR_OK, deleteAccountTest.DeleteAccount(1)); - EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 1", "calendar1-test").calendarInfoResult); + EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 1", "calendar1-test").accountInfoResult); + EXPECT_EQ(CDSENTRY_NOCALENDAR, deleteAccountTest.GetCalendar("Test Account 1", "calendar1-test").calendarInfoResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent1Result.calendarEntryID).getEventResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent2Result.calendarEntryID).getEventResult); @@ -1616,7 +1694,8 @@ TEST(CalendarDataStorage, Delete_Two_Accounts_And_Associated_Data){ EXPECT_EQ(CDSCALENDAR_OK, deleteAccountTest.DeleteAccount(2)); - EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 2", "calendar1-test").calendarInfoResult); + EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 2", "calendar1-test").accountInfoResult); + EXPECT_EQ(CDSENTRY_NOCALENDAR, deleteAccountTest.GetCalendar("Test Account 2", "calendar1-test").calendarInfoResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent1Result.calendarEntryID).getEventResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent2Result.calendarEntryID).getEventResult); @@ -1644,7 +1723,8 @@ TEST(CalendarDataStorage, Delete_Three_Accounts_And_Associated_Data){ EXPECT_EQ(CDSCALENDAR_OK, deleteAccountTest.DeleteAccount(1)); - EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 1", "calendar1-test").calendarInfoResult); + EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 1", "calendar1-test").accountInfoResult); + EXPECT_EQ(CDSENTRY_NOCALENDAR, deleteAccountTest.GetCalendar("Test Account 1", "calendar1-test").calendarInfoResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent1Result.calendarEntryID).getEventResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent2Result.calendarEntryID).getEventResult); @@ -1662,7 +1742,8 @@ TEST(CalendarDataStorage, Delete_Three_Accounts_And_Associated_Data){ EXPECT_EQ(CDSCALENDAR_OK, deleteAccountTest.DeleteAccount(2)); - EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 2", "calendar1-test").calendarInfoResult); + EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 2", "calendar1-test").accountInfoResult); + EXPECT_EQ(CDSENTRY_NOCALENDAR, deleteAccountTest.GetCalendar("Test Account 2", "calendar1-test").calendarInfoResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent1Result.calendarEntryID).getEventResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent2Result.calendarEntryID).getEventResult); @@ -1680,7 +1761,8 @@ TEST(CalendarDataStorage, Delete_Three_Accounts_And_Associated_Data){ EXPECT_EQ(CDSCALENDAR_OK, deleteAccountTest.DeleteAccount(3)); - EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 3", "calendar1-test").calendarInfoResult); + EXPECT_EQ(CDSENTRY_NOACCOUNT, deleteAccountTest.GetCalendar("Test Account 3", "calendar1-test").accountInfoResult); + EXPECT_EQ(CDSENTRY_NOCALENDAR, deleteAccountTest.GetCalendar("Test Account 3", "calendar1-test").calendarInfoResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent1Result.calendarEntryID).getEventResult); EXPECT_EQ(CDSENTRY_NOENTRY, deleteAccountTest.GetEvent(addEvent2Result.calendarEntryID).getEventResult); -- 2.39.2