diff options
Diffstat (limited to 'tests/TestEntrySearcher.cpp')
-rw-r--r-- | tests/TestEntrySearcher.cpp | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/tests/TestEntrySearcher.cpp b/tests/TestEntrySearcher.cpp index 7107cff0a..1b8b4f5b8 100644 --- a/tests/TestEntrySearcher.cpp +++ b/tests/TestEntrySearcher.cpp @@ -23,6 +23,7 @@ QTEST_GUILESS_MAIN(TestEntrySearcher) void TestEntrySearcher::init() { m_rootGroup = new Group(); + m_entrySearcher = EntrySearcher(); } void TestEntrySearcher::cleanup() @@ -259,6 +260,113 @@ void TestEntrySearcher::testCustomAttributesAreSearched() QCOMPARE(m_searchResult.count(), 2); // protected attributes are ignored + m_entrySearcher = EntrySearcher(false, true); m_searchResult = m_entrySearcher.search("_testAttribute:test _testProtected:testP2", m_rootGroup); QCOMPARE(m_searchResult.count(), 2); } + +void TestEntrySearcher::testGroup() +{ + /** + * Root + * - group1 (1 entry) + * - subgroup1 (2 entries) + * - group2 + * - subgroup2 (1 entry) + */ + Group* group1 = new Group(); + Group* group2 = new Group(); + + group1->setParent(m_rootGroup); + group1->setName("group1"); + group2->setParent(m_rootGroup); + group2->setName("group2"); + + Group* subgroup1 = new Group(); + subgroup1->setName("subgroup1"); + subgroup1->setParent(group1); + + Group* subgroup2 = new Group(); + subgroup2->setName("subgroup2"); + subgroup2->setParent(group2); + + Entry* eGroup1 = new Entry(); + eGroup1->setTitle("Entry Group 1"); + eGroup1->setGroup(group1); + + Entry* eSub1 = new Entry(); + eSub1->setTitle("test search term test"); + eSub1->setGroup(subgroup1); + + Entry* eSub2 = new Entry(); + eSub2->setNotes("test test"); + eSub2->setGroup(subgroup1); + + Entry* eSub3 = new Entry(); + eSub3->setNotes("test term test"); + eSub3->setGroup(subgroup2); + + m_searchResult = m_entrySearcher.search("group:subgroup", m_rootGroup); + QCOMPARE(m_searchResult.count(), 3); + + m_searchResult = m_entrySearcher.search("g:subgroup1", m_rootGroup); + QCOMPARE(m_searchResult.count(), 2); + + m_searchResult = m_entrySearcher.search("g:subgroup1 search", m_rootGroup); + QCOMPARE(m_searchResult.count(), 1); + + m_searchResult = m_entrySearcher.search("g:*1/sub*1", m_rootGroup); + QCOMPARE(m_searchResult.count(), 2); + + m_searchResult = m_entrySearcher.search("g:/group1 search", m_rootGroup); + QCOMPARE(m_searchResult.count(), 1); +} + +void TestEntrySearcher::testSkipProtected() +{ + QScopedPointer<Entry> e1(new Entry()); + e1->setGroup(m_rootGroup); + + e1->attributes()->set("testAttribute", "testE1"); + e1->attributes()->set("testProtected", "apple", true); + + QScopedPointer<Entry> e2(new Entry()); + e2->setGroup(m_rootGroup); + e2->attributes()->set("testAttribute", "testE2"); + e2->attributes()->set("testProtected", "banana", true); + + const QList<Entry*> expectE1{e1.data()}; + const QList<Entry*> expectE2{e2.data()}; + const QList<Entry*> expectBoth{e1.data(), e2.data()}; + + // when not skipping protected, empty term matches everything + m_searchResult = m_entrySearcher.search("", m_rootGroup); + QCOMPARE(m_searchResult, expectBoth); + + // now test the searcher with skipProtected = true + m_entrySearcher = EntrySearcher(false, true); + + // when skipping protected, empty term matches nothing + m_searchResult = m_entrySearcher.search("", m_rootGroup); + QCOMPARE(m_searchResult, {}); + + // having a protected entry in terms should not affect the results in anyways + m_searchResult = m_entrySearcher.search("_testProtected:apple", m_rootGroup); + QCOMPARE(m_searchResult, {}); + m_searchResult = m_entrySearcher.search("_testProtected:apple _testAttribute:testE2", m_rootGroup); + QCOMPARE(m_searchResult, expectE2); + m_searchResult = m_entrySearcher.search("_testProtected:apple _testAttribute:testE1", m_rootGroup); + QCOMPARE(m_searchResult, expectE1); + m_searchResult = + m_entrySearcher.search("_testProtected:apple _testAttribute:testE1 _testAttribute:testE2", m_rootGroup); + QCOMPARE(m_searchResult, {}); + + // also move the protected term around to execurise the short-circut logic + m_searchResult = m_entrySearcher.search("_testAttribute:testE2 _testProtected:apple", m_rootGroup); + QCOMPARE(m_searchResult, expectE2); + m_searchResult = m_entrySearcher.search("_testAttribute:testE1 _testProtected:apple", m_rootGroup); + QCOMPARE(m_searchResult, expectE1); + m_searchResult = + m_entrySearcher.search("_testAttribute:testE1 _testProtected:apple _testAttribute:testE2", m_rootGroup); + QCOMPARE(m_searchResult, {}); +} |