moin-2.0 / MoinMoin / items / _tests / test_Item.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# Copyright: 2009 MoinMoin:ThomasWaldmann
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
    MoinMoin - MoinMoin.items Tests
"""

# TODO: split the tests into multiple files after the items/__init__.py is split.

import pytest

from flask import g as flaskg

from MoinMoin._tests import become_trusted, update_item
from MoinMoin.items import Item, ApplicationXTar, NonExistent, Binary, Text, Image, TransformableBitmapImage, MarkupItem
from MoinMoin.config import CONTENTTYPE, ADDRESS, COMMENT, HOSTNAME, USERID, ACTION, NAME

class TestItem(object):

    def _testNonExistent(self):
        item = Item.create(u'DoesNotExist')
        assert isinstance(item, NonExistent)
        meta, data = item.meta, item.data
        assert meta == {CONTENTTYPE: u'application/x-nonexistent'}
        assert data == ''

    def _testClassFinder(self):
        for contenttype, ExpectedClass in [
                (u'application/x-foobar', Binary),
                (u'text/plain', Text),
                (u'text/plain;charset=utf-8', Text),
                (u'image/tiff', Image),
                (u'image/png', TransformableBitmapImage),
            ]:
            item = Item.create(u'foo', contenttype=contenttype)
            assert isinstance(item, ExpectedClass)

    def testCRUD(self):
        name = u'NewItem'
        contenttype = u'text/plain;charset=utf-8'
        data = 'foobar'
        meta = {'foo': 'bar', CONTENTTYPE: contenttype}
        comment = u'saved it'
        become_trusted()
        item = Item.create(name)
        # save rev 0
        item._save(meta, data, comment=comment)
        # check save result
        item = Item.create(name)
        saved_meta, saved_data = item.meta, item.data
        assert saved_meta[CONTENTTYPE] == contenttype
        assert saved_meta[COMMENT] == comment
        assert saved_data == data

        data = rev1_data = data * 10000
        comment = comment + u' again'
        # save rev 1
        item._save(meta, data, comment=comment)
        # check save result
        item = Item.create(name)
        saved_meta, saved_data = dict(item.meta), item.data
        assert saved_meta[CONTENTTYPE] == contenttype
        assert saved_meta[COMMENT] == comment
        assert saved_data == data

        data = ''
        comment = 'saved empty data'
        # save rev 2 (auto delete)
        item._save(meta, data, comment=comment)
        # check save result
        item = Item.create(name)
        saved_meta, saved_data = dict(item.meta), item.data
        assert saved_meta[CONTENTTYPE] == contenttype
        assert saved_meta[COMMENT] == comment
        assert saved_data == data

    def testIndex(self):
        # create a toplevel and some sub-items
        basename = u'Foo'
        for name in ['', '/ab', '/cd/ef', '/gh', '/ij', '/ij/kl', ]:
            item = Item.create(basename + name)
            item._save({CONTENTTYPE: u'text/plain;charset=utf-8'}, "foo")
        item = Item.create(basename + '/mn')
        item._save({CONTENTTYPE: u'image/jpeg'}, "JPG")
        # check index
        baseitem = Item.create(basename)
        index = baseitem.get_index()
        assert index == [(u'Foo/ab', u'ab', 'text/plain;charset=utf-8'),
                         (u'Foo/cd/ef', u'cd/ef', 'text/plain;charset=utf-8'),
                         (u'Foo/gh', u'gh', 'text/plain;charset=utf-8'),
                         (u'Foo/ij', u'ij', 'text/plain;charset=utf-8'),
                         (u'Foo/ij/kl', u'ij/kl', 'text/plain;charset=utf-8'),
                         (u'Foo/mn', u'mn', 'image/jpeg'),
                        ]
        flat_index = baseitem.flat_index()
        assert flat_index == [(u'Foo/ab', u'ab', u'text/plain;charset=utf-8'),
                              (u'Foo/cd', u'cd', u'application/x-nonexistent'),
                              (u'Foo/gh', u'gh', u'text/plain;charset=utf-8'),
                              (u'Foo/ij', u'ij', u'text/plain;charset=utf-8'),
                              (u'Foo/mn', u'mn', u'image/jpeg'),
                             ]
        # check index when startswith param is passed
        flat_index = baseitem.flat_index(startswith=u'a')
        assert flat_index == [(u'Foo/ab', u'ab', 'text/plain;charset=utf-8')]

        #check that virtual container item is returned with startswith
        flat_index = baseitem.flat_index(startswith=u'c')
        assert flat_index == [(u'Foo/cd', u'cd', u'application/x-nonexistent')]

        # check index when contenttype_groups is passed
        ctgroups = ["image items"]
        flat_index = baseitem.flat_index(selected_groups=ctgroups)
        assert flat_index == [(u'Foo/mn', u'mn', 'image/jpeg')]

        # If we ask for text/plain type, should Foo/cd be returned?

        # check detailed index
        detailed_index = baseitem.get_detailed_index(baseitem.flat_index())
        assert detailed_index == [(u'Foo/ab', u'ab', 'text/plain;charset=utf-8', False),
                                  (u'Foo/cd', u'cd', 'application/x-nonexistent', True),
                                  (u'Foo/gh', u'gh', 'text/plain;charset=utf-8', False),
                                  (u'Foo/ij', u'ij', 'text/plain;charset=utf-8', True),
                                  (u'Foo/mn', u'mn', 'image/jpeg', False),
                                  ]


    def testIndexOnDisconnectedLevels(self):
        # create a toplevel and some sub-items
        basename = u'Bar'
        for name in ['', '/ab', '/ab/cd/ef/gh', '/ij/kl/mn/op', '/ij/kl/rs']:
            item = Item.create(basename + name)
            item._save({CONTENTTYPE: u'text/plain;charset=utf-8'}, "foo")

        baseitem = Item.create(basename)
        index = baseitem.get_index()
        index = baseitem._connect_levels(index)

        assert index == [(u'Bar/ab', u'ab', u'text/plain;charset=utf-8'),
                         (u'Bar/ab/cd', u'ab/cd', u'application/x-nonexistent'),
                         (u'Bar/ab/cd/ef', u'ab/cd/ef', u'application/x-nonexistent'),
                         (u'Bar/ab/cd/ef/gh', u'ab/cd/ef/gh', u'text/plain;charset=utf-8'),
                         (u'Bar/ij', u'ij', u'application/x-nonexistent'),
                         (u'Bar/ij/kl', u'ij/kl', u'application/x-nonexistent'),
                         (u'Bar/ij/kl/mn', u'ij/kl/mn', u'application/x-nonexistent'),
                         (u'Bar/ij/kl/mn/op', u'ij/kl/mn/op', u'text/plain;charset=utf-8'),
                         (u'Bar/ij/kl/rs', u'ij/kl/rs', u'text/plain;charset=utf-8')]

        flat_index = baseitem.flat_index()
        assert flat_index == [(u'Bar/ab', u'ab', u'text/plain;charset=utf-8'),
                              (u'Bar/ij', u'ij', u'application/x-nonexistent'),
                             ]

    def test_index_on_pages_with_multiple_names(self):
        update_item(u'FooBar',
                    {NAME: [u'FooBar',
                            u'BarFoo',
                            ],
                     CONTENTTYPE: u'text/x.moin.wiki'}, u'')

        update_item(u'One',
                    {NAME: [u'One',
                            u'FooBar/FBChild',
                            ],
                     CONTENTTYPE: u'text/x.moin.wiki'}, u'')
        update_item(u'Two',
                    {NAME: [u'BarFoo/BFChild',
                            u'Two',
                            ],
                     CONTENTTYPE: u'text/x.moin.wiki'}, u'')

        update_item(u'FooBar/FBChild/Grand', {CONTENTTYPE: u'text/x.moin.wiki'}, u'')
        update_item(u'Two/TwoChild', {CONTENTTYPE: u'text/x.moin.wiki'}, u'')
        update_item(u'One/OneChild', {CONTENTTYPE: u'text/x.moin.wiki'}, u'')

        index = Item.create(u'FooBar').get_index()
        assert index == [(u'FooBar/FBChild', u'FBChild', u'text/x.moin.wiki'),
                         (u'FooBar/FBChild/Grand', u'FBChild/Grand', u'text/x.moin.wiki'),
                         ]
        index = Item.create(u'BarFoo').get_index()
        assert index == [(u'BarFoo/BFChild', u'BFChild', u'text/x.moin.wiki')]

        assert Item.create(u'BarFoo/BFChild').get_index() == []

        index = Item.create(u'One').get_index()
        assert index == [(u'One/OneChild', u'OneChild', u'text/x.moin.wiki')]

        index = Item.create(u'Two').get_index()
        assert index == [(u'Two/TwoChild', u'TwoChild', u'text/x.moin.wiki')]

    def test_meta_filter(self):
        name = u'Test_item'
        contenttype = u'text/plain;charset=utf-8'
        meta = {'test_key': 'test_val', CONTENTTYPE: contenttype, 'name': 'test_name'}
        item = Item.create(name)
        result = Item.meta_filter(item, meta)
        # keys like NAME, ITEMID, REVID, DATAID are filtered
        expected = {'test_key': 'test_val', CONTENTTYPE: contenttype}
        assert result == expected

    def test_meta_dict_to_text(self):
        name = u'Test_item'
        contenttype = u'text/plain;charset=utf-8'
        meta = {'test_key': 'test_val', CONTENTTYPE: contenttype, 'name': 'test_name'}
        item = Item.create(name)
        result = Item.meta_dict_to_text(item, meta)
        expected = '{\n  "contenttype": "text/plain;charset=utf-8", \n  "test_key": "test_val"\n}'
        assert result == expected

    def test_meta_text_to_dict(self):
        name = u'Test_item'
        contenttype = u'text/plain;charset=utf-8'
        text = '{\n  "contenttype": "text/plain;charset=utf-8", \n  "test_key": "test_val", \n "name": "test_name" \n}'
        item = Item.create(name)
        result = Item.meta_text_to_dict(item, text)
        expected = {'test_key': 'test_val', CONTENTTYPE: contenttype}
        assert result == expected

    def test_item_can_have_several_names(self):
        content = u"This is page content"

        update_item(u'Page',
                    {NAME: [u'Page',
                            u'Another name',
                            ],
                     CONTENTTYPE: u'text/x.moin.wiki'}, content)

        item1 = Item.create(u'Page')
        assert item1.name == u'Page'
        assert item1.meta[CONTENTTYPE] == 'text/x.moin.wiki'
        assert item1.data == content

        item2 = Item.create(u'Another name')
        assert item2.name == u'Another name'
        assert item2.meta[CONTENTTYPE] == 'text/x.moin.wiki'
        assert item2.data == content

        assert item1.rev.revid == item2.rev.revid

    def test_rename(self):
        name = u'Test_Item'
        contenttype = u'text/plain;charset=utf-8'
        data = 'test_data'
        meta = {'test_key': 'test_value', CONTENTTYPE: contenttype}
        comment = u'saved it'
        item = Item.create(name)
        item._save(meta, data, comment=comment)
        # item and its contents before renaming
        item = Item.create(name)
        assert item.name == u'Test_Item'
        assert item.meta['comment'] == u'saved it'
        new_name = u'Test_new_Item'
        item.rename(new_name, comment=u'renamed')
        # item at original name and its contents after renaming
        item = Item.create(name)
        assert item.name == u'Test_Item'
        # this should be a fresh, new item, NOT the stuff we renamed:
        assert item.meta[CONTENTTYPE] == 'application/x-nonexistent'
        # item at new name and its contents after renaming
        item = Item.create(new_name)
        assert item.name == u'Test_new_Item'
        assert item.meta['name_old'] == [u'Test_Item']
        assert item.meta['comment'] == u'renamed'
        assert item.data == u'test_data'

    def test_rename_acts_only_in_active_name_in_case_there_are_several_names(self):
        content = u"This is page content"

        update_item(u'Page',
                    {NAME: [u'First',
                            u'Second',
                            u'Third',
                            ],
                     CONTENTTYPE: u'text/x.moin.wiki'}, content)

        item = Item.create(u'Second')
        item.rename(u'New name', comment=u'renamed')

        item1 = Item.create(u'First')
        assert item1.name == u'First'
        assert item1.meta[CONTENTTYPE] == 'text/x.moin.wiki'
        assert item1.data == content

        item2 = Item.create(u'New name')
        assert item2.name == u'New name'
        assert item2.meta[CONTENTTYPE] == 'text/x.moin.wiki'
        assert item2.data == content

        item3 = Item.create(u'Third')
        assert item3.name == u'Third'
        assert item3.meta[CONTENTTYPE] == 'text/x.moin.wiki'
        assert item3.data == content

        assert item1.rev.revid == item2.rev.revid == item3.rev.revid

        item4 = Item.create(u'Second')
        assert item4.meta[CONTENTTYPE] == 'application/x-nonexistent'

    def test_rename_recursion(self):
        update_item(u'Page', {CONTENTTYPE: u'text/x.moin.wiki'}, u'Page 1')
        update_item(u'Page/Child', {CONTENTTYPE: u'text/x.moin.wiki'}, u'this is child')
        update_item(u'Page/Child/Another', {CONTENTTYPE: u'text/x.moin.wiki'}, u'another child')

        item = Item.create(u'Page')
        item.rename(u'Renamed_Page', comment=u'renamed')

        # items at original name and its contents after renaming
        item = Item.create(u'Page')
        assert item.name == u'Page'
        assert item.meta[CONTENTTYPE] == 'application/x-nonexistent'
        item = Item.create(u'Page/Child')
        assert item.name == u'Page/Child'
        assert item.meta[CONTENTTYPE] == 'application/x-nonexistent'
        item = Item.create(u'Page/Child/Another')
        assert item.name == u'Page/Child/Another'
        assert item.meta[CONTENTTYPE] == 'application/x-nonexistent'

        # item at new name and its contents after renaming
        item = Item.create(u'Renamed_Page')
        assert item.name == u'Renamed_Page'
        assert item.meta['name_old'] == [u'Page']
        assert item.meta['comment'] == u'renamed'
        assert item.data == u'Page 1'

        item = Item.create(u'Renamed_Page/Child')
        assert item.name == u'Renamed_Page/Child'
        assert item.meta['name_old'] == [u'Page/Child']
        assert item.meta['comment'] == u'renamed'
        assert item.data == u'this is child'

        item = Item.create(u'Renamed_Page/Child/Another')
        assert item.name == u'Renamed_Page/Child/Another'
        assert item.meta['name_old'] == [u'Page/Child/Another']
        assert item.meta['comment'] == u'renamed'
        assert item.data == u'another child'

    def test_rename_recursion_with_multiple_names_and_children(self):
        update_item(u'Foo',
                    {CONTENTTYPE: u'text/x.moin.wiki',
                         NAME: [u'Other', u'Page', u'Foo']},
                    u'Parent')
        update_item(u'Page/Child', {CONTENTTYPE: u'text/x.moin.wiki'}, u'Child of Page')
        update_item(u'Other/Child2', {CONTENTTYPE: u'text/x.moin.wiki'}, u'Child of Other')
        update_item(u'Another',
                    {CONTENTTYPE: u'text/x.moin.wiki',
                     NAME: [u'Another', u'Page/Second']
                         },
                    u'Both')
        update_item(u'Page/Second/Child', {CONTENTTYPE: u'text/x.moin.wiki'}, u'Child of Second')
        update_item(u'Another/Child', {CONTENTTYPE: u'text/x.moin.wiki'}, u'Child of Another')

        item = Item.create(u'Page')

        item.rename(u'Renamed', comment=u'renamed')

        assert Item.create(u'Page/Child').meta[CONTENTTYPE] == 'application/x-nonexistent'
        assert Item.create(u'Renamed/Child').data == u'Child of Page'
        assert Item.create(u'Page/Second').meta[CONTENTTYPE] == 'application/x-nonexistent'
        assert Item.create(u'Renamed/Second').data == u'Both'
        assert Item.create(u'Another').data == u'Both'
        assert Item.create(u'Page/Second/Child').meta[CONTENTTYPE] == 'application/x-nonexistent'
        assert Item.create(u'Renamed/Second/Child').data == u'Child of Second'
        assert Item.create(u'Other/Child2').data == u'Child of Other'
        assert Item.create(u'Another/Child').data == u'Child of Another'

    def test_delete(self):
        name = u'Test_Item2'
        contenttype = u'text/plain;charset=utf-8'
        data = 'test_data'
        meta = {'test_key': 'test_value', CONTENTTYPE: contenttype}
        comment = u'saved it'
        item = Item.create(name)
        item._save(meta, data, comment=comment)
        # item and its contents before deleting
        item = Item.create(name)
        assert item.name == u'Test_Item2'
        assert item.meta['comment'] == u'saved it'
        item.delete(u'deleted')
        # item and its contents after deletion
        item = Item.create(name)
        assert item.name == u'Test_Item2'
        # this should be a fresh, new item, NOT the stuff we deleted:
        assert item.meta[CONTENTTYPE] == 'application/x-nonexistent'

    def test_revert(self):
        name = u'Test_Item'
        contenttype = u'text/plain;charset=utf-8'
        data = 'test_data'
        meta = {'test_key': 'test_value', CONTENTTYPE: contenttype}
        comment = u'saved it'
        item = Item.create(name)
        item._save(meta, data, comment=comment)
        item = Item.create(name)
        item.revert()
        item = Item.create(name)
        assert item.meta['action'] == u'REVERT'

    def test_modify(self):
        name = u'Test_Item'
        contenttype = u'text/plain;charset=utf-8'
        data = 'test_data'
        meta = {'test_key': 'test_value', CONTENTTYPE: contenttype}
        comment = u'saved it'
        item = Item.create(name)
        item._save(meta, data, comment=comment)
        item = Item.create(name)
        assert item.name == u'Test_Item'
        assert item.meta['test_key'] == 'test_value'
        # call item.modify
        item.modify()
        item = Item.create(name)
        assert item.name == u'Test_Item'
        with pytest.raises(KeyError):
            item.meta['test_key']


class TestBinary(object):
    """ Test for arbitrary binary items """

    def test_get_templates(self):
        item_name1 = u'Template_Item1'
        item1 = Binary.create(item_name1)
        contenttype1 = u'text/plain'
        meta = {CONTENTTYPE: contenttype1, 'tags': ['template']}
        item1._save(meta)
        item1 = Binary.create(item_name1)

        item_name2 = u'Template_Item2'
        item2 = Binary.create(item_name2)
        contenttype1 = u'text/plain'
        meta = {CONTENTTYPE: contenttype1, 'tags': ['template']}
        item2._save(meta)
        item2 = Binary.create(item_name2)

        item_name3 = u'Template_Item3'
        item3 = Binary.create(item_name3)
        contenttype2 = u'image/png'
        meta = {CONTENTTYPE: contenttype2, 'tags': ['template']}
        item3._save(meta)
        item3 = Binary.create(item_name3)
        # two items of same content type
        result1 = item1.get_templates(contenttype1)
        assert result1 == [item_name1, item_name2]
        # third of different content type
        result2 = item1.get_templates(contenttype2)
        assert result2 == [item_name3]

class TestTarItems(object):
    """
    tests for the container items
    """

    def testCreateContainerRevision(self):
        """
        creates a container and tests the content saved to the container
        """
        item_name = u'ContainerItem1'
        item = Item.create(item_name, contenttype=u'application/x-tar')
        filecontent = 'abcdefghij'
        content_length = len(filecontent)
        members = set(['example1.txt', 'example2.txt'])
        item.put_member('example1.txt', filecontent, content_length, expected_members=members)
        item.put_member('example2.txt', filecontent, content_length, expected_members=members)

        item = Item.create(item_name, contenttype=u'application/x-tar')
        tf_names = set(item.list_members())
        assert tf_names == members
        assert item.get_member('example1.txt').read() == filecontent

    def testRevisionUpdate(self):
        """
        creates two revisions of a container item
        """
        item_name = u'ContainerItem2'
        item = Item.create(item_name, contenttype=u'application/x-tar')
        filecontent = 'abcdefghij'
        content_length = len(filecontent)
        members = set(['example1.txt'])
        item.put_member('example1.txt', filecontent, content_length, expected_members=members)
        filecontent = 'AAAABBBB'
        content_length = len(filecontent)
        item.put_member('example1.txt', filecontent, content_length, expected_members=members)

        item = Item.create(item_name, contenttype=u'application/x-tar')
        assert item.get_member('example1.txt').read() == filecontent

class TestZipMixin(object):
    """ Test for zip-like items """

    def test_put_member(self):
        item_name = u'Zip_file'
        item = Item.create(item_name, contenttype='application/zip')
        filecontent = 'test_contents'
        content_length = len(filecontent)
        members = set(['example1.txt', 'example2.txt'])
        with pytest.raises(NotImplementedError):
            item.put_member('example1.txt', filecontent, content_length, expected_members=members)

class TestTransformableBitmapImage(object):

    def test__transform(self):
        item_name = u'image_Item'
        item = Binary.create(item_name)
        contenttype = u'image/jpeg'
        meta = {CONTENTTYPE: contenttype}
        item._save(meta)
        item = Binary.create(item_name)
        try:
            from PIL import Image as PILImage
            with pytest.raises(ValueError):
                result = TransformableBitmapImage._transform(item, 'text/plain')
        except ImportError:
            result = TransformableBitmapImage._transform(item, contenttype)
            assert result == (u'image/jpeg', '')

    def test__render_data_diff(self):
        item_name = u'image_Item'
        item = Binary.create(item_name)
        contenttype = u'image/jpeg'
        meta = {CONTENTTYPE: contenttype}
        item._save(meta)
        item1 = Binary.create(item_name)
        try:
            from PIL import Image as PILImage
            result = TransformableBitmapImage._render_data_diff(item1, item.rev, item1.rev)
            expected = '<img src="/+diffraw/image_Item?rev2=0" />'
            assert str(result) == expected
        except ImportError:
            # no PIL
            pass

    def test__render_data_diff_text(self):
        item_name = u'image_Item'
        item = Binary.create(item_name)
        contenttype = u'image/jpeg'
        meta = {CONTENTTYPE: contenttype}
        item._save(meta)
        item1 = Binary.create(item_name)
        data = 'test_data'
        comment = u'next revision'
        item1._save(meta, data, comment=comment)
        item2 = Binary.create(item_name)
        try:
            from PIL import Image as PILImage
            result = TransformableBitmapImage._render_data_diff_text(item1, item1.rev, item2.rev)
            expected = u'The items have different data.'
            assert result == expected
        except ImportError:
            pass

class TestText(object):

    def test_data_conversion(self):
        item_name = u'Text_Item'
        item = Text.create(item_name, u'text/plain')
        test_text = u'This \n is \n a \n Test'
        # test for data_internal_to_form
        result = Text.data_internal_to_form(item, test_text)
        expected = u'This \r\n is \r\n a \r\n Test'
        assert result == expected
        # test for data_form_to_internal
        test_form = u'This \r\n is \r\n a \r\n Test'
        result = Text.data_form_to_internal(item, test_text)
        expected = test_text
        assert result == expected
        # test for data_internal_to_storage
        result = Text.data_internal_to_storage(item, test_text)
        expected = 'This \r\n is \r\n a \r\n Test'
        assert result == expected
        # test for data_storage_to_internal
        data_storage = 'This \r\n is \r\n a \r\n Test'
        result = Text.data_storage_to_internal(item, data_storage)
        expected = test_text
        assert result == expected

    def test__render_data_diff_text(self):
        item_name = u'Text_Item'
        item = Text.create(item_name)
        contenttype = u'text/plain'
        meta = {CONTENTTYPE: contenttype}
        item._save(meta)
        item1 = Text.create(item_name)
        data = 'test_data'
        comment = u'next revision'
        item1._save(meta, data, comment=comment)
        item2 = Text.create(item_name)
        result = Text._render_data_diff_text(item1, item1.rev, item2.rev)
        expected = u'- \n+ test_data'
        assert result == expected
        assert item2.data == ''

    def test__render_data_highlight(self):
        item_name = u'Text_Item'
        item = Text.create(item_name)
        contenttype = u'text/plain'
        meta = {CONTENTTYPE: contenttype}
        item._save(meta)
        item1 = Text.create(item_name)
        data = 'test_data\nnext line'
        comment = u'next revision'
        item1._save(meta, data, comment=comment)
        item2 = Text.create(item_name)
        result = Text._render_data_highlight(item2)
        assert u'<pre class="highlight">test_data\n' in result
        assert item2.data == ''

coverage_modules = ['MoinMoin.items']
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.