Changeset View
Changeset View
Standalone View
Standalone View
autotests/unit/file/unindexedfileiteratortest.cpp
Show All 13 Lines | 1 | /* | |||
---|---|---|---|---|---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | Lesser General Public License for more details. | 16 | Lesser General Public License for more details. | ||
17 | 17 | | |||
18 | You should have received a copy of the GNU Lesser General Public | 18 | You should have received a copy of the GNU Lesser General Public | ||
19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | 19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "fileindexerconfigutils.h" | | |||
23 | #include "filtereddiriterator.h" | | |||
24 | #include "fileindexerconfig.h" | 22 | #include "fileindexerconfig.h" | ||
23 | #include "unindexedfileiterator.h" | ||||
24 | #include "basicindexingjob.h" | ||||
25 | #include "database.h" | ||||
26 | #include "transaction.h" | ||||
27 | #include "fileindexerconfigutils.h" | ||||
25 | 28 | | |||
26 | #include <QTemporaryDir> | 29 | #include <QTemporaryDir> | ||
27 | #include <QTest> | 30 | #include <QTest> | ||
28 | 31 | | |||
29 | class UnIndexedFileIterator : public QObject | 32 | using namespace Baloo; | ||
33 | | ||||
34 | /** | ||||
35 | * Folder structure for the test is following: | ||||
36 | * - home/ | ||||
37 | * - home/included/ | ||||
38 | * - home/included/file1 | ||||
39 | * (this file will be indexed inside testIndexedFile) | ||||
40 | * - home/included/file2 | ||||
41 | * - home/included/.hidden | ||||
42 | * (this file is hidden, so it shouldn't pop up in UnindexedFileIterator) | ||||
43 | * - home/excluded/ | ||||
44 | * (this whole folder will be excluded) | ||||
45 | * - home/excluded/file | ||||
46 | */ | ||||
47 | | ||||
48 | static const QString rootFolder = QStringLiteral("home"); | ||||
49 | static const QString includedSubfolder = rootFolder + QStringLiteral("/included"); | ||||
50 | static const QString excludedFolder = rootFolder + QStringLiteral("/excluded"); | ||||
51 | | ||||
52 | static const QString includedSubfolderFile = includedSubfolder + QStringLiteral("/file"); | ||||
53 | static const QString hiddenFile = includedSubfolder + QStringLiteral("/.hidden"); | ||||
54 | static const QString indexedFile = rootFolder + QStringLiteral("/file1"); | ||||
55 | static const QString unindexedFile = rootFolder + QStringLiteral("/file2"); | ||||
56 | static const QString excludedFile = excludedFolder + QStringLiteral("/file"); | ||||
57 | | ||||
58 | class UnIndexedFileIteratorTest : public QObject | ||||
30 | { | 59 | { | ||
31 | Q_OBJECT | 60 | Q_OBJECT | ||
32 | private Q_SLOTS: | 61 | private Q_SLOTS: | ||
33 | void test(); | 62 | void init() { | ||
63 | // Creating DB | ||||
64 | m_dbdir = new QTemporaryDir(); | ||||
65 | m_db = new Database(m_dbdir->path()); | ||||
66 | m_db->open(Database::CreateDatabase); | ||||
67 | | ||||
68 | // Creating files & folders (folders path should end with "/") | ||||
69 | QStringList dirsAndFiles; | ||||
70 | dirsAndFiles << rootFolder + QStringLiteral("/"); | ||||
71 | dirsAndFiles << includedSubfolder + QStringLiteral("/"); | ||||
72 | dirsAndFiles << excludedFolder + QStringLiteral("/"); | ||||
73 | dirsAndFiles << includedSubfolderFile; | ||||
74 | dirsAndFiles << indexedFile; | ||||
75 | dirsAndFiles << unindexedFile; | ||||
76 | dirsAndFiles << excludedFile; | ||||
77 | dirsAndFiles << hiddenFile; | ||||
78 | m_testDir = Test::createTmpFilesAndFolders(dirsAndFiles); | ||||
79 | m_dirPrefix = m_testDir->path() + QStringLiteral("/"); | ||||
80 | | ||||
81 | // Initializing config | ||||
82 | Test::writeIndexerConfig({ m_dirPrefix + rootFolder }, | ||||
83 | { m_dirPrefix + excludedFolder } ); | ||||
84 | m_config = new FileIndexerConfig(); | ||||
85 | } | ||||
86 | | ||||
87 | void cleanup() { | ||||
88 | delete m_db; | ||||
89 | delete m_config; | ||||
90 | delete m_dbdir; | ||||
91 | delete m_testDir; | ||||
92 | } | ||||
93 | | ||||
94 | void testNoIndexedFiles(); | ||||
95 | void testIndexedFiles(); | ||||
96 | void testDirRenamed(); | ||||
97 | private: | ||||
98 | Database* m_db; | ||||
99 | QTemporaryDir* m_dbdir; | ||||
bruns: Make this plain members, not pointers.
Also, one temporary dir is enough, you can put the db… | |||||
I wanted both DB and directory tree to be recreated from scratch for each test, since tests modify DB or rename folders, and thus can affect each other. That's why I made them to be pointers. poboiko: I wanted both DB and directory tree to be recreated from scratch for each test, since tests… | |||||
100 | QTemporaryDir* m_testDir; | ||||
101 | QString m_dirPrefix; | ||||
102 | FileIndexerConfig *m_config; | ||||
34 | }; | 103 | }; | ||
35 | 104 | | |||
36 | using namespace Baloo; | 105 | void UnIndexedFileIteratorTest::testNoIndexedFiles() | ||
106 | { | ||||
107 | Transaction tr(m_db, Transaction::ReadOnly); | ||||
108 | UnIndexedFileIterator it(m_config, &tr, m_dirPrefix + rootFolder); | ||||
109 | QSet<QString> unindexedFiles; | ||||
110 | while (!it.next().isEmpty()) { | ||||
111 | unindexedFiles << it.filePath(); | ||||
112 | } | ||||
113 | | ||||
114 | QSet<QString> expectedFiles; | ||||
115 | expectedFiles << m_dirPrefix + rootFolder; | ||||
116 | expectedFiles << m_dirPrefix + indexedFile; | ||||
117 | expectedFiles << m_dirPrefix + unindexedFile; | ||||
118 | expectedFiles << m_dirPrefix + includedSubfolder; | ||||
119 | expectedFiles << m_dirPrefix + includedSubfolderFile; | ||||
120 | QCOMPARE(unindexedFiles, expectedFiles); | ||||
121 | } | ||||
37 | 122 | | |||
38 | void UnIndexedFileIterator::test() | 123 | void UnIndexedFileIteratorTest::testIndexedFiles() | ||
39 | { | 124 | { | ||
40 | // Bah!! | 125 | // Indexing single file | ||
41 | // Testing this is complex! | 126 | { | ||
42 | // FIXME: How in the world should I test this? | 127 | Transaction tr(m_db, Transaction::ReadWrite); | ||
128 | BasicIndexingJob job(m_dirPrefix + indexedFile, QStringLiteral("text/plain"), BasicIndexingJob::NoLevel); | ||||
129 | job.index(); | ||||
bruns: bad name, use something like excluded. | |||||
130 | tr.addDocument(job.document()); | ||||
131 | tr.commit(); | ||||
132 | } | ||||
133 | | ||||
134 | Transaction tr(m_db, Transaction::ReadOnly); | ||||
135 | UnIndexedFileIterator it(m_config, &tr, m_dirPrefix + rootFolder); | ||||
136 | QSet<QString> unindexedFiles; | ||||
137 | while (!it.next().isEmpty()) { | ||||
138 | unindexedFiles << it.filePath(); | ||||
139 | } | ||||
140 | // We now also know not only about the file itself, | ||||
141 | // but also about all directories above | ||||
142 | QSet<QString> expectedFiles; | ||||
143 | expectedFiles << m_dirPrefix + unindexedFile; | ||||
144 | expectedFiles << m_dirPrefix + includedSubfolder; | ||||
145 | expectedFiles << m_dirPrefix + includedSubfolderFile; | ||||
146 | QCOMPARE(unindexedFiles, expectedFiles); | ||||
bruns: Bad variable name, these are files **and** dirs. | |||||
43 | } | 147 | } | ||
44 | 148 | | |||
149 | void UnIndexedFileIteratorTest::testDirRenamed() | ||||
150 | { | ||||
151 | QString renamedSubfolder = rootFolder + QStringLiteral("/renamed"); | ||||
152 | // First we index file inside subfolder | ||||
153 | { | ||||
154 | Transaction tr(m_db, Transaction::ReadWrite); | ||||
155 | BasicIndexingJob job(m_dirPrefix + includedSubfolderFile, QStringLiteral("text/plain"), BasicIndexingJob::NoLevel); | ||||
156 | job.index(); | ||||
157 | tr.addDocument(job.document()); | ||||
158 | tr.commit(); | ||||
159 | } | ||||
160 | // Then we rename this subfolder | ||||
161 | QDir dir; | ||||
162 | dir.rename(m_dirPrefix + includedSubfolder, m_dirPrefix + renamedSubfolder); | ||||
163 | // And then we perform checks | ||||
164 | Transaction tr(m_db, Transaction::ReadOnly); | ||||
165 | UnIndexedFileIterator it(m_config, &tr, m_dirPrefix + rootFolder); | ||||
bruns: you don't actually have to determine the mimetype here, just use "text/plain" | |||||
166 | QSet<QString> unindexedFiles; | ||||
167 | while (!it.next().isEmpty()) { | ||||
168 | unindexedFiles << it.filePath(); | ||||
169 | } | ||||
170 | // We now also should know not only about the file itself, | ||||
171 | // but also about all directories above | ||||
172 | QSet<QString> expectedFiles; | ||||
173 | expectedFiles << m_dirPrefix + indexedFile; | ||||
174 | expectedFiles << m_dirPrefix + unindexedFile; | ||||
175 | expectedFiles << m_dirPrefix + renamedSubfolder; | ||||
176 | QCOMPARE(unindexedFiles, expectedFiles); | ||||
177 | } | ||||
45 | 178 | | |||
46 | QTEST_GUILESS_MAIN(UnIndexedFileIterator) | 179 | QTEST_GUILESS_MAIN(UnIndexedFileIteratorTest) | ||
47 | 180 | | |||
48 | #include "unindexedfileiteratortest.moc" | 181 | #include "unindexedfileiteratortest.moc" |
Make this plain members, not pointers.
Also, one temporary dir is enough, you can put the db and the test tree side-by-side.