~ubuntu-branches/ubuntu/vivid/moin/vivid

« back to all changes in this revision

Viewing changes to MoinMoin/logfile/_tests/test_logfile.py

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-11-23 09:18:37 UTC
  • mfrom: (0.1.30 sid)
  • Revision ID: package-import@ubuntu.com-20121123091837-4u12h753y8jpm6s5
Tags: 1.9.5-1ubuntu1
* Merge from Debian unstable (LP: #1046616). Remaining changes:
 - Remove python-xml from Suggests field, the package isn't anymore in
   sys.path.
 - Demote fckeditor from Recommends to Suggests; the code was previously
   embedded in moin, but it was also disabled, so there's no reason 
   for us to pull this in by default currently. Note: fckeditor has a
   number of security problems and so this change probably needs to be
   carried indefinitely.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""
 
3
    MoinMoin - MoinMoin.logfile Tests
 
4
 
 
5
    @copyright: 2011 MoinMoin:ThomasWaldmann
 
6
    @license: GNU GPL, see COPYING for details.
 
7
"""
 
8
import os
 
9
import tempfile
 
10
import shutil
 
11
from StringIO import StringIO
 
12
 
 
13
from MoinMoin.logfile import LogFile
 
14
 
 
15
 
 
16
class TestLogFile(object):
 
17
    """ testing logfile reading/writing """
 
18
    # mtime rev action pagename host hostname user_id extra comment
 
19
    LOG = [
 
20
           [u'1292630945000000', u'00000001', u'SAVENEW', u'foo', u'0.0.0.0', u'example.org', u'111.111.111', u'', u''],
 
21
           [u'1292630957849084', u'99999999', u'ATTNEW', u'foo', u'0.0.0.0', u'example.org', u'222.222.222', u'file.txt', u''],
 
22
           [u'1292680177309091', u'99999999', u'ATTDEL', u'foo', u'0.0.0.0', u'example.org', u'333.333.333', u'file.txt', u''],
 
23
           [u'1292680233866579', u'99999999', u'ATTNEW', u'foo', u'0.0.0.0', u'example.org', u'444.444.444', u'new.tgz', u''],
 
24
           [u'1303073723000000', u'00000002', u'SAVE', u'foo', u'0.0.0.0', u'example.org', u'555.555.555', u'', u''],
 
25
          ]
 
26
 
 
27
    def make_line(self, linedata):
 
28
        line = u'\t'.join(linedata) + u'\n'
 
29
        return line.encode('utf-8')
 
30
 
 
31
    def write_log(self, fname, data):
 
32
        f = open(fname, "wb")
 
33
        for linedata in data:
 
34
            f.write(self.make_line(linedata))
 
35
        f.close()
 
36
 
 
37
    def setup_method(self, method):
 
38
        self.fname = tempfile.mktemp()
 
39
        self.write_log(self.fname, self.LOG)
 
40
 
 
41
    def teardown_method(self, method):
 
42
        os.remove(self.fname)
 
43
 
 
44
    def test_add(self):
 
45
        fname_log = tempfile.mktemp()
 
46
        lf = LogFile(fname_log)
 
47
        for linedata in self.LOG:
 
48
            lf.add(*linedata)
 
49
        expected_contents = open(self.fname, 'rb').read()
 
50
        real_contents = open(fname_log, 'rb').read()
 
51
        print repr(expected_contents)
 
52
        print repr(real_contents)
 
53
        assert real_contents == expected_contents
 
54
 
 
55
    def test_iter_forward(self):
 
56
        lf = LogFile(self.fname)
 
57
        for result, expected in zip(lf, self.LOG):
 
58
            print expected
 
59
            print result
 
60
            assert result == expected
 
61
 
 
62
    def test_iter_reverse(self):
 
63
        lf = LogFile(self.fname)
 
64
        for result, expected in zip(lf.reverse(), self.LOG[::-1]):
 
65
            print expected
 
66
            print result
 
67
            assert result == expected
 
68
 
 
69
    def test_position(self):
 
70
        lf = LogFile(self.fname)
 
71
        expected_pos = 0
 
72
        for data in lf:
 
73
            expected_pos += len(self.make_line(data))
 
74
            real_pos = lf.position()
 
75
            print expected_pos, real_pos
 
76
            assert real_pos == expected_pos
 
77
 
 
78
    def test_seek(self):
 
79
        lf = LogFile(self.fname)
 
80
        for data in lf:
 
81
            print repr(data)
 
82
        # now we are at the current end, remember position
 
83
        pos = lf.position()
 
84
        # add new data
 
85
        newdata = [u'1303333333000000', u'00000003', u'SAVE', u'foo', u'0.0.0.0', u'example.org', u'666.666.666', u'', u'comment']
 
86
        lf.add(*newdata)
 
87
        # go to position before new data
 
88
        lf.seek(pos)
 
89
        assert lf.position() == pos
 
90
        for data in lf:
 
91
            # reads the one new line we added
 
92
            print 'new:', repr(data)
 
93
            assert data == newdata
 
94
        lf.seek(0)
 
95
        assert lf.position() == 0
 
96
        assert list(lf) == self.LOG + [newdata]
 
97
 
 
98
coverage_modules = ['MoinMoin.logfile']
 
99