/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_pack.py

  • Committer: Andrew Bennetts
  • Date: 2007-06-08 06:33:59 UTC
  • mto: (2506.2.2 container-format)
  • mto: This revision was merged to the branch mainline in revision 2572.
  • Revision ID: andrew.bennetts@canonical.com-20070608063359-s5ps81a8i85w7by0
More progress:

 * Rename container.py to pack.py
 * Refactor bytes record reading into a separate class for ease of unit testing.
 * Start handling error conditions such as invalid content lengths in byte
   records.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
"""Tests for bzrlib.container."""
 
17
"""Tests for bzrlib.pack."""
18
18
 
19
19
 
20
20
from cStringIO import StringIO
21
21
 
22
 
from bzrlib import container, errors
 
22
from bzrlib import pack, errors
23
23
from bzrlib.tests import TestCase
24
24
 
25
25
 
31
31
        This uses None as the output stream to show that the constructor doesn't
32
32
        try to use the output stream.
33
33
        """
34
 
        writer = container.ContainerWriter(None)
 
34
        writer = pack.ContainerWriter(None)
35
35
 
36
36
    def test_begin(self):
37
37
        """Test the begin() method."""
38
38
        output = StringIO()
39
 
        writer = container.ContainerWriter(output.write)
 
39
        writer = pack.ContainerWriter(output.write)
40
40
        writer.begin()
41
41
        self.assertEqual('bzr pack format 1\n', output.getvalue())
42
42
 
43
43
    def test_end(self):
44
44
        """Test the end() method."""
45
45
        output = StringIO()
46
 
        writer = container.ContainerWriter(output.write)
 
46
        writer = pack.ContainerWriter(output.write)
47
47
        writer.begin()
48
48
        writer.end()
49
49
        self.assertEqual('bzr pack format 1\nE', output.getvalue())
51
51
    def test_add_bytes_record_no_name(self):
52
52
        """Add a bytes record with no name."""
53
53
        output = StringIO()
54
 
        writer = container.ContainerWriter(output.write)
 
54
        writer = pack.ContainerWriter(output.write)
55
55
        writer.begin()
56
56
        writer.add_bytes_record('abc', names=[])
57
57
        self.assertEqual('bzr pack format 1\nB3\n\nabc', output.getvalue())
59
59
    def test_add_bytes_record_one_name(self):
60
60
        """Add a bytes record with one name."""
61
61
        output = StringIO()
62
 
        writer = container.ContainerWriter(output.write)
 
62
        writer = pack.ContainerWriter(output.write)
63
63
        writer.begin()
64
64
        writer.add_bytes_record('abc', names=['name1'])
65
65
        self.assertEqual('bzr pack format 1\nB3\nname1\n\nabc',
68
68
    def test_add_bytes_record_two_names(self):
69
69
        """Add a bytes record with two names."""
70
70
        output = StringIO()
71
 
        writer = container.ContainerWriter(output.write)
 
71
        writer = pack.ContainerWriter(output.write)
72
72
        writer.begin()
73
73
        writer.add_bytes_record('abc', names=['name1', 'name2'])
74
74
        self.assertEqual('bzr pack format 1\nB3\nname1\nname2\n\nabc',
83
83
        This uses None as the output stream to show that the constructor doesn't
84
84
        try to use the input stream.
85
85
        """
86
 
        reader = container.ContainerReader(None)
 
86
        reader = pack.ContainerReader(None)
87
87
 
88
88
    def test_empty_container(self):
89
89
        """Read an empty container."""
90
90
        input = StringIO("bzr pack format 1\nE")
91
 
        reader = container.ContainerReader(input.read)
 
91
        reader = pack.ContainerReader(input.read)
92
92
        self.assertEqual([], list(reader.iter_records()))
93
93
 
94
94
    def test_unknown_format(self):
95
95
        """Unrecognised container formats raise UnknownContainerFormatError."""
96
96
        input = StringIO("unknown format\n")
97
 
        reader = container.ContainerReader(input.read)
 
97
        reader = pack.ContainerReader(input.read)
98
98
        self.assertRaises(
99
99
            errors.UnknownContainerFormatError, reader.iter_records)
100
100
 
103
103
        UnexpectedEndOfContainerError to be raised.
104
104
        """
105
105
        input = StringIO("bzr pack format 1\n")
106
 
        reader = container.ContainerReader(input.read)
 
106
        reader = pack.ContainerReader(input.read)
107
107
        iterator = reader.iter_records()
108
108
        self.assertRaises(
109
109
            errors.UnexpectedEndOfContainerError, iterator.next)
111
111
    def test_unknown_record_type(self):
112
112
        """Unknown record types cause UnknownRecordTypeError to be raised."""
113
113
        input = StringIO("bzr pack format 1\nX")
114
 
        reader = container.ContainerReader(input.read)
 
114
        reader = pack.ContainerReader(input.read)
115
115
        iterator = reader.iter_records()
116
116
        self.assertRaises(
117
117
            errors.UnknownRecordTypeError, iterator.next)
118
118
 
119
 
    # XXX: refactor Bytes record parsing into a seperate BytesRecordReader for
120
 
    #      better unit testing.
121
 
    def test_one_unnamed_record(self):
122
 
        """Read a container with one Bytes record."""
 
119
    def test_container_with_one_unnamed_record(self):
 
120
        """Read a container with one Bytes record.
 
121
        
 
122
        Parsing Bytes records is more thoroughly exercised by XXX.  This test is
 
123
        here to ensure that ContainerReader's integration with BytesRecordReader
 
124
        is working.
 
125
        """
123
126
        input = StringIO("bzr pack format 1\nB5\n\naaaaaE")
124
 
        reader = container.ContainerReader(input.read)
 
127
        reader = pack.ContainerReader(input.read)
125
128
        expected_records = [([], 'aaaaa')]
126
129
        self.assertEqual(expected_records, list(reader.iter_records()))
127
130
 
128
 
    def test_one_named_record(self):
129
 
        """Read a container with one Bytes record with a single name."""
130
 
        input = StringIO("bzr pack format 1\nB5\nname1\n\naaaaaE")
131
 
        reader = container.ContainerReader(input.read)
132
 
        expected_records = [(['name1'], 'aaaaa')]
133
 
        self.assertEqual(expected_records, list(reader.iter_records()))
134
 
 
 
131
 
 
132
class TestBytesRecordReader(TestCase):
 
133
    """Tests for parsing Bytes records with BytesRecordReader."""
 
134
 
 
135
    def test_record_with_no_name(self):
 
136
        """Reading a Bytes record with no name returns an empty list of
 
137
        names.
 
138
        """
 
139
        input = StringIO("5\n\naaaaa")
 
140
        reader = pack.BytesRecordReader(input.read)
 
141
        names, bytes = reader.read()
 
142
        self.assertEqual([], names)
 
143
        self.assertEqual('aaaaa', bytes)
 
144
 
 
145
    def test_record_with_one_name(self):
 
146
        """Reading a Bytes record with one name returns a list of just that
 
147
        name.
 
148
        """
 
149
        input = StringIO("5\nname1\n\naaaaa")
 
150
        reader = pack.BytesRecordReader(input.read)
 
151
        names, bytes = reader.read()
 
152
        self.assertEqual(['name1'], names)
 
153
        self.assertEqual('aaaaa', bytes)
 
154
 
 
155
    def test_record_with_two_names(self):
 
156
        """Reading a Bytes record with two names returns a list of both names.
 
157
        """
 
158
        input = StringIO("5\nname1\nname2\n\naaaaa")
 
159
        reader = pack.BytesRecordReader(input.read)
 
160
        names, bytes = reader.read()
 
161
        self.assertEqual(['name1', 'name2'], names)
 
162
        self.assertEqual('aaaaa', bytes)
 
163
 
 
164
    def test_invalid_length(self):
 
165
        """If the length-prefix is not a number, parsing raises
 
166
        InvalidRecordError.
 
167
        """
 
168
        input = StringIO("not a number\n")
 
169
        reader = pack.BytesRecordReader(input.read)
 
170
        self.assertRaises(errors.InvalidRecordError, reader.read)
135
171
 
136
172
    # Other Bytes record parsing cases to test:
137
 
    #  - invalid length value
138
173
    #  - incomplete bytes (i.e. stream ends before $length bytes read)
139
174
    #  - _read_line encountering end of stream (at any time; during length,
140
175
    #    names, end of headers...)