From 0d80a71a5d8272dcd9ff5c50d31c890725201b87 Mon Sep 17 00:00:00 2001 From: Tomek Sobkowiak Date: Sun, 6 Dec 2020 20:42:24 +0100 Subject: [PATCH] [EDG-4494] Exclude contacts from temporary group (#1109) * [EDG-4494] Exclude contacts from temporary group --- module-db/Tables/ContactsTable.cpp | 29 +++++++------------- module-db/tests/ContactsTable_tests.cpp | 17 ++++++++---- test/pytest/service-desktop/test_contacts.py | 11 ++++++++ 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/module-db/Tables/ContactsTable.cpp b/module-db/Tables/ContactsTable.cpp index 6f10b2cc7c6d00ef40a3be73f3f333406da62fa8..1b49182cb38abcadb94c2fd2169ab363052c13b1 100644 --- a/module-db/Tables/ContactsTable.cpp +++ b/module-db/Tables/ContactsTable.cpp @@ -277,16 +277,17 @@ std::vector ContactsTable::GetIDsSortedByField( "contact_match_groups.group_id = " + std::to_string(groupId); + constexpr auto exclude_temporary = " WHERE contacts._id not in ( " + " SELECT cmg.contact_id " + " FROM contact_match_groups cmg, contact_groups cg " + " WHERE cmg.group_id = cg._id " + " AND cg.name = 'Temporary' " + " ) "; + switch (matchType) { case MatchType::Name: { + query += exclude_temporary; if (!name.empty()) { - query += " WHERE contacts._id not in ( " - " SELECT cmg.contact_id " - " FROM contact_match_groups cmg, contact_groups cg " - " WHERE cmg.group_id = cg._id " - " AND cg.name = 'Temporary' " - " ) "; - query += " AND ( contact_name.name_primary || ' ' || contact_name.name_alternative LIKE '" + name + "%%'"; query += " OR contact_name.name_alternative || ' ' || contact_name.name_primary LIKE '" + name + "%%')"; } @@ -297,13 +298,8 @@ std::vector ContactsTable::GetIDsSortedByField( query += " INNER JOIN contact_number ON contact_number.contact_id == contacts._id AND " "contact_number.number_user LIKE '%%" + name + "%%'"; - query += " WHERE contacts._id not in ( " - " SELECT cmg.contact_id " - " FROM contact_match_groups cmg, contact_groups cg " - " WHERE cmg.group_id = cg._id " - " AND cg.name = 'Temporary' " - " ) "; } + query += exclude_temporary; } break; case MatchType::Group: @@ -311,12 +307,7 @@ std::vector ContactsTable::GetIDsSortedByField( break; case MatchType::None: { - query += " WHERE contacts._id not in ( " - " SELECT cmg.contact_id " - " FROM contact_match_groups cmg, contact_groups cg " - " WHERE cmg.group_id = cg._id " - " AND cg.name = 'Temporary' " - " ) "; + query += exclude_temporary; } break; } diff --git a/module-db/tests/ContactsTable_tests.cpp b/module-db/tests/ContactsTable_tests.cpp index d5fd37c908a1546b458959b6ff7cb99570fe6652..bedc5eca4d8c2a52d8e3a71218c280878c7ecffc 100644 --- a/module-db/tests/ContactsTable_tests.cpp +++ b/module-db/tests/ContactsTable_tests.cpp @@ -21,11 +21,11 @@ TEST_CASE("Contacts Table tests") REQUIRE(contactsdb.isInitialized()); ContactsTableRow testRow1 = {{.ID = DB_ID_NONE}, - .nameID = DB_ID_NONE, - .numbersID = "0 1 2 3 4", - .ringID = DB_ID_NONE, - .addressID = DB_ID_NONE, - .speedDial = "666" + .nameID = DB_ID_NONE, + .numbersID = "0 1 2 3 4", + .ringID = DB_ID_NONE, + .addressID = DB_ID_NONE, + .speedDial = "666" }; @@ -62,6 +62,13 @@ TEST_CASE("Contacts Table tests") auto retOffsetLimitBigger = contactsdb.contacts.getLimitOffset(0, 100); REQUIRE(retOffsetLimitBigger.size() == 4); + auto sortedRetOffsetLimitBigger = + contactsdb.contacts.GetIDsSortedByField(ContactsTable::MatchType::Name, "", 1, 100, 0); + REQUIRE(sortedRetOffsetLimitBigger.size() == 4); + + sortedRetOffsetLimitBigger = contactsdb.contacts.GetIDsSortedByName(1, 100); + REQUIRE(sortedRetOffsetLimitBigger.size() == 4); + // Get table rows using invalid offset/limit parameters(should return empty object) auto retOffsetLimitFailed = contactsdb.contacts.getLimitOffset(5, 4); REQUIRE(retOffsetLimitFailed.size() == 0); diff --git a/test/pytest/service-desktop/test_contacts.py b/test/pytest/service-desktop/test_contacts.py index 7a75ac26c356fe0e8b148157d4555a5d7d5efced..13a8382c22ef143f3405da8d965553e07e3cf93c 100644 --- a/test/pytest/service-desktop/test_contacts.py +++ b/test/pytest/service-desktop/test_contacts.py @@ -25,6 +25,17 @@ def test_contacts(harness): assert contacts_length assert contacts_length == count + # try to get more than available + body = {"count": 10 + count} + ret = harness.endpoint_request("contacts", "get", body) + assert ret["status"] == status["OK"] + + contacts = ret["body"] + contacts_length = len(contacts) + assert contacts_length + assert contacts_length == count + + # adding new contact body = {"address": "6 Czeczota St.\n02600 Warsaw", "altName": "Testowy",