Source

moin-2.0 / MoinMoin / storage / middleware / _tests / test_indexing.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# Copyright: 2011 MoinMoin:ThomasWaldmann
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - indexing middleware tests
"""


from __future__ import absolute_import, division

from StringIO import StringIO
import hashlib

import pytest

from flask import g as flaskg

from MoinMoin.constants.keys import (NAME, SIZE, ITEMID, REVID, DATAID, HASH_ALGORITHM, CONTENT, COMMENT,
                                     LATEST_REVS, ALL_REVS, NAMESPACE)
from MoinMoin.constants.namespaces import NAMESPACE_USERPROFILES

from MoinMoin.auth import GivenAuth
from MoinMoin._tests import wikiconfig


def dumper(indexer, idx_name):
    print "*** %s ***" % idx_name
    for kvs in indexer.dump(idx_name=idx_name):
        for k, v in kvs:
            print k, repr(v)[:70]
        print


class TestIndexingMiddleware(object):
    reinit_storage = True  # cleanup after each test method

    def setup_method(self, method):
        self.imw = flaskg.unprotected_storage

    def teardown_method(self, method):
        pass

    def test_nonexisting_item(self):
        item = self.imw[u'foo']
        assert not item  # does not exist

    def test_store_revision(self):
        item_name = u'foo'
        data = 'bar'
        item = self.imw[item_name]
        rev = item.store_revision(dict(name=[item_name, ]), StringIO(data),
                                  return_rev=True)
        revid = rev.revid
        # check if we have the revision now:
        item = self.imw[item_name]
        assert item  # does exist
        rev = item.get_revision(revid)
        assert rev.name == item_name
        assert rev.data.read() == data
        revids = [rev.revid for rev in item.iter_revs()]
        assert revids == [revid]

    def test_overwrite_revision(self):
        item_name = u'foo'
        data = 'bar'
        newdata = 'baz'
        item = self.imw[item_name]
        rev = item.store_revision(dict(name=[item_name, ], comment=u'spam'), StringIO(data),
                                  return_rev=True)
        revid = rev.revid
        # clear revision:
        item.store_revision(dict(name=[item_name, ], revid=revid, comment=u'no spam'), StringIO(newdata), overwrite=True)
        # check if the revision was overwritten:
        item = self.imw[item_name]
        rev = item.get_revision(revid)
        assert rev.name == item_name
        assert rev.meta[COMMENT] == u'no spam'
        assert rev.data.read() == newdata
        revids = [rev.revid for rev in item.iter_revs()]
        assert len(revids) == 1  # we still have the revision, cleared
        assert revid in revids  # it is still same revid

    def test_destroy_revision(self):
        item_name = u'foo'
        item = self.imw[item_name]
        rev = item.store_revision(dict(name=[item_name, ], mtime=1),
                                  StringIO('bar'), trusted=True, return_rev=True)
        revid0 = rev.revid
        rev = item.store_revision(dict(name=[item_name, ], mtime=2),
                                  StringIO('baz'), trusted=True, return_rev=True)
        revid1 = rev.revid
        rev = item.store_revision(dict(name=[item_name, ], mtime=3),
                                  StringIO('...'), trusted=True, return_rev=True)
        revid2 = rev.revid
        print "revids:", revid0, revid1, revid2
        # destroy a non-current revision:
        item.destroy_revision(revid0)
        # check if the revision was destroyed:
        item = self.imw[item_name]
        with pytest.raises(KeyError):
            item.get_revision(revid0)
        revids = [rev.revid for rev in item.iter_revs()]
        print "after destroy revid0", revids
        assert sorted(revids) == sorted([revid1, revid2])
        # destroy a current revision:
        item.destroy_revision(revid2)
        # check if the revision was destroyed:
        item = self.imw[item_name]
        with pytest.raises(KeyError):
            item.get_revision(revid2)
        revids = [rev.revid for rev in item.iter_revs()]
        print "after destroy revid2", revids
        assert sorted(revids) == sorted([revid1])
        # destroy the last revision left:
        item.destroy_revision(revid1)
        # check if the revision was destroyed:
        item = self.imw[item_name]
        with pytest.raises(KeyError):
            item.get_revision(revid1)
        revids = [rev.revid for rev in item.iter_revs()]
        print "after destroy revid1", revids
        assert sorted(revids) == sorted([])

    def test_destroy_item(self):
        revids = []
        item_name = u'foo'
        item = self.imw[item_name]
        rev = item.store_revision(dict(name=[item_name, ], mtime=1),
                                  StringIO('bar'), trusted=True, return_rev=True)
        revids.append(rev.revid)
        rev = item.store_revision(dict(name=[item_name, ], mtime=2),
                                  StringIO('baz'), trusted=True, return_rev=True)
        revids.append(rev.revid)
        # destroy item:
        item.destroy_all_revisions()
        # check if the item was destroyed:
        item = self.imw[item_name]
        assert not item  # does not exist

    def test_all_revisions(self):
        item_name = u'foo'
        item = self.imw[item_name]
        item.store_revision(dict(name=[item_name, ]), StringIO('does not count, different name'))
        item_name = u'bar'
        item = self.imw[item_name]
        item.store_revision(dict(name=[item_name, ]), StringIO('1st'))
        item.store_revision(dict(name=[item_name, ]), StringIO('2nd'))
        item = self.imw[item_name]
        revs = [rev.data.read() for rev in item.iter_revs()]
        assert len(revs) == 2
        assert set(revs) == set(['1st', '2nd'])

    def test_latest_revision(self):
        item_name = u'foo'
        item = self.imw[item_name]
        item.store_revision(dict(name=[item_name, ]), StringIO('does not count, different name'))
        item_name = u'bar'
        item = self.imw[item_name]
        item.store_revision(dict(name=[item_name, ]), StringIO('1st'))
        expected_rev = item.store_revision(dict(name=[item_name, ]), StringIO('2nd'),
                                           return_rev=True)
        revs = list(self.imw.documents(name=item_name))
        assert len(revs) == 1  # there is only 1 latest revision
        assert expected_rev.revid == revs[0].revid  # it is really the latest one

    def test_auto_meta(self):
        item_name = u'foo'
        data = 'bar'
        item = self.imw[item_name]
        rev = item.store_revision(dict(name=[item_name, ]), StringIO(data), return_rev=True)
        print repr(rev.meta)
        assert rev.name == item_name
        assert rev.meta[SIZE] == len(data)
        assert rev.meta[HASH_ALGORITHM] == hashlib.new(HASH_ALGORITHM, data).hexdigest()
        assert ITEMID in rev.meta
        assert REVID in rev.meta
        assert DATAID in rev.meta

    def test_documents(self):
        item_name = u'foo'
        item = self.imw[item_name]
        rev1 = item.store_revision(dict(name=[item_name, ]), StringIO('x'), return_rev=True)
        rev2 = item.store_revision(dict(name=[item_name, ]), StringIO('xx'), return_rev=True)
        rev3 = item.store_revision(dict(name=[item_name, ]), StringIO('xxx'), return_rev=True)
        rev = self.imw.document(idx_name=ALL_REVS, size=2)
        assert rev
        assert rev.revid == rev2.revid
        revs = list(self.imw.documents(idx_name=ALL_REVS, size=2))
        assert len(revs) == 1
        assert revs[0].revid == rev2.revid

    def test_index_rebuild(self):
        # first we index some stuff the slow "on-the-fly" way:
        expected_latest_revids = []
        item_name = u'foo'
        item = self.imw[item_name]
        r = item.store_revision(dict(name=[item_name, ], mtime=1),
                                StringIO('does not count, different name'),
                                trusted=True, return_rev=True)
        expected_latest_revids.append(r.revid)
        item_name = u'bar'
        item = self.imw[item_name]
        item.store_revision(dict(name=[item_name, ], mtime=1),
                            StringIO('1st'), trusted=True)
        r = item.store_revision(dict(name=[item_name, ], mtime=2),
                                StringIO('2nd'), trusted=True, return_rev=True)
        expected_latest_revids.append(r.revid)

        # now we remember the index contents built that way:
        expected_latest_revs = list(self.imw.documents())
        expected_all_revs = list(self.imw.documents(idx_name=ALL_REVS))

        print "*** all on-the-fly:"
        self.imw.dump(idx_name=ALL_REVS)
        print "*** latest on-the-fly:"
        self.imw.dump(idx_name=LATEST_REVS)

        # now kill the index and do a full rebuild
        self.imw.close()
        self.imw.destroy()
        self.imw.create()
        self.imw.rebuild()
        self.imw.open()

        # read the index contents built that way:
        all_revs = list(self.imw.documents(idx_name=ALL_REVS))
        latest_revs = list(self.imw.documents())
        latest_revids = [rev.revid for rev in latest_revs]

        print "*** all rebuilt:"
        self.imw.dump(idx_name=ALL_REVS)
        print "*** latest rebuilt:"
        self.imw.dump(idx_name=LATEST_REVS)

        # should be all the same, order does not matter:
        assert sorted(expected_all_revs) == sorted(all_revs)
        assert sorted(expected_latest_revs) == sorted(latest_revs)
        assert sorted(latest_revids) == sorted(expected_latest_revids)

    def test_index_update(self):
        # first we index some stuff the slow "on-the-fly" way:
        expected_all_revids = []
        expected_latest_revids = []
        missing_revids = []
        item_name = u'updated'
        item = self.imw[item_name]
        r = item.store_revision(dict(name=[item_name, ], mtime=1),
                                StringIO('updated 1st'),
                                trusted=True, return_rev=True)
        expected_all_revids.append(r.revid)
        # we update this item below, so we don't add it to expected_latest_revids
        item_name = u'destroyed'
        item = self.imw[item_name]
        r = item.store_revision(dict(name=[item_name, ], mtime=1),
                                StringIO('destroyed 1st'),
                                trusted=True, return_rev=True)
        destroy_revid = r.revid
        # we destroy this item below, so we don't add it to expected_all_revids
        # we destroy this item below, so we don't add it to expected_latest_revids
        item_name = u'stayssame'
        item = self.imw[item_name]
        r = item.store_revision(dict(name=[item_name, ], mtime=1),
                                StringIO('stayssame 1st'),
                                trusted=True, return_rev=True)
        expected_all_revids.append(r.revid)
        # we update this item below, so we don't add it to expected_latest_revids
        r = item.store_revision(dict(name=[item_name, ], mtime=2),
                                StringIO('stayssame 2nd'),
                                trusted=True, return_rev=True)
        expected_all_revids.append(r.revid)
        expected_latest_revids.append(r.revid)

        dumper(self.imw, ALL_REVS)
        dumper(self.imw, LATEST_REVS)

        # now build a fresh index at tmp location:
        self.imw.create(tmp=True)
        self.imw.rebuild(tmp=True)

        # while the fresh index still sits at the tmp location, we update and add some items.
        # this will not change the fresh index, but the old index we are still using.
        item_name = u'updated'
        item = self.imw[item_name]
        r = item.store_revision(dict(name=[item_name, ], mtime=2),
                                StringIO('updated 2nd'), trusted=True,
                                return_rev=True)
        expected_all_revids.append(r.revid)
        expected_latest_revids.append(r.revid)
        missing_revids.append(r.revid)
        item_name = u'added'
        item = self.imw[item_name]
        r = item.store_revision(dict(name=[item_name, ], mtime=1),
                                StringIO('added 1st'),
                                trusted=True, return_rev=True)
        expected_all_revids.append(r.revid)
        expected_latest_revids.append(r.revid)
        missing_revids.append(r.revid)
        item_name = u'destroyed'
        item = self.imw[item_name]
        item.destroy_revision(destroy_revid)

        # now switch to the not-quite-fresh-any-more index we have built:
        self.imw.close()
        self.imw.move_index()
        self.imw.open()

        dumper(self.imw, ALL_REVS)
        dumper(self.imw, LATEST_REVS)

        # read the index contents we have now:
        all_revids = [doc[REVID] for doc in self.imw._documents(idx_name=ALL_REVS)]
        latest_revids = [doc[REVID] for doc in self.imw._documents()]

        # this index is outdated:
        for missing_revid in missing_revids:
            assert missing_revid not in all_revids
            assert missing_revid not in latest_revids

        # update the index:
        self.imw.close()
        self.imw.update()
        self.imw.open()

        dumper(self.imw, ALL_REVS)
        dumper(self.imw, LATEST_REVS)

        # read the index contents we have now:
        all_revids = [doc[REVID] for doc in self.imw._documents(idx_name=ALL_REVS)]
        latest_revids = [doc[REVID] for doc in self.imw._documents()]

        # now it should have the previously missing rev and all should be as expected:
        for missing_revid in missing_revids:
            assert missing_revid in all_revids
            assert missing_revid in latest_revids
        assert sorted(all_revids) == sorted(expected_all_revids)
        assert sorted(latest_revids) == sorted(expected_latest_revids)

    def test_revision_contextmanager(self):
        # check if rev.data is closed after leaving the with-block
        item_name = u'foo'
        meta = dict(name=[item_name, ])
        data = 'some test content'
        item = self.imw[item_name]
        data_file = StringIO(data)
        with item.store_revision(meta, data_file, return_rev=True) as rev:
            assert rev.data.read() == data
            revid = rev.revid
        with pytest.raises(ValueError):
            rev.data.read()
        with item.get_revision(revid) as rev:
            assert rev.data.read() == data
        with pytest.raises(ValueError):
            rev.data.read()

    def test_indexed_content(self):
        # TODO: this is a very simple check that assumes that data is put 1:1
        # into index' CONTENT field.
        item_name = u'foo'
        meta = dict(name=[item_name, ], contenttype=u'text/plain;charset=utf-8')
        data = 'some test content\n'
        item = self.imw[item_name]
        data_file = StringIO(data)
        with item.store_revision(meta, data_file, return_rev=True) as rev:
            expected_revid = rev.revid
        doc = self.imw._document(content=u'test')
        assert doc is not None
        assert expected_revid == doc[REVID]
        assert unicode(data) == doc[CONTENT]

    def test_namespaces(self):
        item_name_n = u'normal'
        item = self.imw[item_name_n]
        rev_n = item.store_revision(dict(name=[item_name_n, ], contenttype=u'text/plain;charset=utf-8'),
                                    StringIO(str(item_name_n)), return_rev=True)
        item_name_u = u'%s:userprofile' % NAMESPACE_USERPROFILES
        item = self.imw[item_name_u]
        rev_u = item.store_revision(dict(name=[item_name_u, ], contenttype=u'text/plain;charset=utf-8'),
                                    StringIO(str(item_name_u)), return_rev=True)
        item = self.imw[item_name_n]
        rev_n = item.get_revision(rev_n.revid)
        assert rev_n.meta[NAMESPACE] == u''
        assert rev_n.meta[NAME] == [item_name_n, ]
        item = self.imw[item_name_u]
        rev_u = item.get_revision(rev_u.revid)
        assert rev_u.meta[NAMESPACE] == NAMESPACE_USERPROFILES
        assert rev_u.meta[NAME] == [item_name_u.split(':')[1]]

    def test_parentnames(self):
        item_name = u'child'
        item = self.imw[item_name]
        item.store_revision(dict(name=[u'child', u'p1/a', u'p2/b', u'p2/c', u'p3/p4/d', ],
                                 contenttype=u'text/plain;charset=utf-8'),
                            StringIO(''))
        item = self.imw[item_name]
        assert item.parentnames == [u'p1', u'p2', u'p3/p4', ]  # one p2 duplicate removed


class TestProtectedIndexingMiddleware(object):
    reinit_storage = True  # cleanup after each test method

    class Config(wikiconfig.Config):
        auth = [GivenAuth(user_name=u'joe', autocreate=True), ]

    def setup_method(self, method):
        self.imw = flaskg.storage

    def teardown_method(self, method):
        pass

    def test_documents(self):
        item_name = u'public'
        item = self.imw[item_name]
        r = item.store_revision(dict(name=[item_name, ], acl=u'joe:read'),
                                StringIO('public content'), return_rev=True)
        revid_public = r.revid
        revids = [rev.revid for rev in self.imw.documents()
                  if rev.name != u'joe']  # the user profile is a revision in the backend
        assert revids == [revid_public]

    def test_getitem(self):
        item_name = u'public'
        item = self.imw[item_name]
        r = item.store_revision(dict(name=[item_name, ], acl=u'joe:read'),
                                StringIO('public content'), return_rev=True)
        revid_public = r.revid
        # now testing:
        item_name = u'public'
        item = self.imw[item_name]
        r = item[revid_public]
        assert r.data.read() == 'public content'

    def test_perf_create_only(self):
        pytest.skip("usually we do no performance tests")
        # determine create revisions performance
        # for the memory backend we use, this is likely mostly building the indexes
        item_name = u'foo'
        item = self.imw[item_name]
        for i in xrange(100):
            item.store_revision(dict(name=[item_name, ], acl=u'joe:create joe:read'), StringIO('some content'))

    def test_perf_create_read(self):
        pytest.skip("usually we do no performance tests")
        # determine create + read revisions performance
        # for the memory backend we use, this is likely mostly building the indexes and
        # doing index lookups name -> itemid, itemid -> revids list
        item_name = u'foo'
        item = self.imw[item_name]
        for i in xrange(100):
            item.store_revision(dict(name=[item_name, ], acl=u'joe:create joe:read'), StringIO('rev number {0}'.format(i)))
        for r in item.iter_revs():
            #print r.meta
            #print r.data.read()
            pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.