Changeset View
Changeset View
Standalone View
Standalone View
autotests/integration/querytest.cpp
Show All 28 Lines | |||||
29 | #include <QTemporaryDir> | 29 | #include <QTemporaryDir> | ||
30 | 30 | | |||
31 | using namespace Baloo; | 31 | using namespace Baloo; | ||
32 | 32 | | |||
33 | class QueryTest : public QObject | 33 | class QueryTest : public QObject | ||
34 | { | 34 | { | ||
35 | Q_OBJECT | 35 | Q_OBJECT | ||
36 | private Q_SLOTS: | 36 | private Q_SLOTS: | ||
37 | void initTestCase() { | ||||
38 | dir.reset(new QTemporaryDir()); | ||||
39 | | ||||
40 | auto touchFile = [](const QString& path) { | ||||
41 | QFile file(path); | ||||
42 | file.open(QIODevice::WriteOnly); | ||||
43 | file.write("data"); | ||||
44 | file.close(); | ||||
45 | | ||||
46 | return filePathToId(QFile::encodeName(path)); | ||||
47 | }; | ||||
48 | | ||||
49 | m_id1 = touchFile(dir->path() + "/file1"); | ||||
50 | m_id2 = touchFile(dir->path() + "/file2"); | ||||
51 | m_id3 = touchFile(dir->path() + "/file3"); | ||||
52 | m_id4 = touchFile(dir->path() + "/file4"); | ||||
53 | } | ||||
54 | | ||||
37 | void init() { | 55 | void init() { | ||
38 | dir = new QTemporaryDir(); | 56 | dbDir = new QTemporaryDir(); | ||
39 | db = new Database(dir->path()); | 57 | db = new Database(dbDir->path()); | ||
40 | db->open(Database::CreateDatabase); | 58 | db->open(Database::CreateDatabase); | ||
41 | 59 | | |||
42 | Transaction tr(db, Transaction::ReadWrite); | 60 | insertDocuments(); | ||
43 | insertDocuments(&tr); | | |||
44 | tr.commit(); | | |||
45 | } | 61 | } | ||
46 | 62 | | |||
47 | void cleanup() { | 63 | void cleanup() { | ||
48 | delete db; | 64 | delete db; | ||
49 | delete dir; | 65 | delete dbDir; | ||
50 | } | 66 | } | ||
51 | 67 | | |||
52 | void testTermEqual(); | 68 | void testTermEqual(); | ||
53 | void testTermStartsWith(); | 69 | void testTermStartsWith(); | ||
54 | void testTermAnd(); | 70 | void testTermAnd(); | ||
55 | void testTermOr(); | 71 | void testTermOr(); | ||
56 | void testTermPhrase(); | 72 | void testTermPhrase(); | ||
57 | 73 | | |||
58 | private: | 74 | private: | ||
59 | QTemporaryDir* dir; | 75 | QScopedPointer<QTemporaryDir> dir; | ||
76 | QTemporaryDir* dbDir; | ||||
60 | Database* db; | 77 | Database* db; | ||
61 | 78 | | |||
62 | void insertDocuments(Transaction* tr); | 79 | void insertDocuments(); | ||
63 | void addDocument(Transaction* tr,const QString& text, quint64 id, const QString& url) | 80 | void addDocument(Transaction* tr,const QString& text, quint64 id, const QString& url) | ||
64 | { | 81 | { | ||
65 | Document doc; | 82 | Document doc; | ||
66 | doc.setUrl(QFile::encodeName(url)); | 83 | doc.setUrl(QFile::encodeName(url)); | ||
67 | 84 | | |||
68 | QString fileName = url.mid(url.lastIndexOf('/') + 1); | 85 | QString fileName = url.mid(url.lastIndexOf('/') + 1); | ||
69 | 86 | | |||
70 | TermGenerator tg(doc); | 87 | TermGenerator tg(doc); | ||
71 | tg.indexText(text); | 88 | tg.indexText(text); | ||
72 | tg.indexFileNameText(fileName); | 89 | tg.indexFileNameText(fileName); | ||
73 | doc.setId(id); | 90 | doc.setId(id); | ||
74 | doc.setMTime(1); | 91 | doc.setMTime(1); | ||
75 | doc.setCTime(2); | 92 | doc.setCTime(2); | ||
76 | 93 | | |||
77 | tr->addDocument(doc); | 94 | tr->addDocument(doc); | ||
78 | } | 95 | } | ||
79 | 96 | | |||
80 | quint64 m_id1; | 97 | quint64 m_id1; | ||
81 | quint64 m_id2; | 98 | quint64 m_id2; | ||
82 | quint64 m_id3; | 99 | quint64 m_id3; | ||
83 | quint64 m_id4; | 100 | quint64 m_id4; | ||
84 | }; | 101 | }; | ||
85 | 102 | | |||
86 | static quint64 touchFile(const QString& path) { | | |||
87 | QFile file(path); | | |||
88 | file.open(QIODevice::WriteOnly); | | |||
89 | file.write("data"); | | |||
90 | file.close(); | | |||
91 | | ||||
92 | return filePathToId(QFile::encodeName(path)); | | |||
93 | } | | |||
94 | 103 | | |||
95 | void QueryTest::insertDocuments(Transaction* tr) | 104 | void QueryTest::insertDocuments() | ||
96 | { | 105 | { | ||
97 | m_id1 = touchFile(dir->path() + "/file1"); | 106 | Transaction tr(db, Transaction::ReadWrite); | ||
98 | m_id2 = touchFile(dir->path() + "/file2"); | 107 | addDocument(&tr, QStringLiteral("The quick brown foxed jumped over the crazy dog"), m_id1, dir->path() + "/file1"); | ||
99 | m_id3 = touchFile(dir->path() + "/file3"); | 108 | addDocument(&tr, QStringLiteral("The night is dark and full of terror"), m_id2, dir->path() + "/file2"); | ||
100 | m_id4 = touchFile(dir->path() + "/file4"); | 109 | addDocument(&tr, QStringLiteral("Don't feel sorry for yourself. Only assholes do that"), m_id3, dir->path() + "/file3"); | ||
101 | 110 | addDocument(&tr, QStringLiteral("Only the dead stay 17 forever. crazy"), m_id4, dir->path() + "/file4"); | |||
102 | addDocument(tr, QStringLiteral("The quick brown foxed jumped over the crazy dog"), m_id1, dir->path() + "/file1"); | 111 | tr.commit(); | ||
103 | addDocument(tr, QStringLiteral("The night is dark and full of terror"), m_id2, dir->path() + "/file2"); | | |||
104 | addDocument(tr, QStringLiteral("Don't feel sorry for yourself. Only assholes do that"), m_id3, dir->path() + "/file3"); | | |||
105 | addDocument(tr, QStringLiteral("Only the dead stay 17 forever. crazy"), m_id4, dir->path() + "/file4"); | | |||
106 | } | 112 | } | ||
107 | 113 | | |||
108 | void QueryTest::testTermEqual() | 114 | void QueryTest::testTermEqual() | ||
109 | { | 115 | { | ||
110 | EngineQuery q("the"); | 116 | EngineQuery q("the"); | ||
111 | 117 | | |||
112 | QVector<quint64> result = {m_id1, m_id2, m_id4}; | 118 | QVector<quint64> result = {m_id1, m_id2, m_id4}; | ||
113 | Transaction tr(db, Transaction::ReadOnly); | 119 | Transaction tr(db, Transaction::ReadOnly); | ||
▲ Show 20 Lines • Show All 55 Lines • Show Last 20 Lines |