1 | """ |
---|
2 | Ported to Python 3. |
---|
3 | """ |
---|
4 | |
---|
5 | from ..common import SyncTestCase |
---|
6 | from allmydata.mutable.publish import MutableData |
---|
7 | from testtools.matchers import Equals, HasLength |
---|
8 | |
---|
9 | |
---|
10 | class DataHandle(SyncTestCase): |
---|
11 | def setUp(self): |
---|
12 | super(DataHandle, self).setUp() |
---|
13 | self.test_data = b"Test Data" * 50000 |
---|
14 | self.uploadable = MutableData(self.test_data) |
---|
15 | |
---|
16 | |
---|
17 | def test_datahandle_read(self): |
---|
18 | chunk_size = 10 |
---|
19 | for i in range(0, len(self.test_data), chunk_size): |
---|
20 | data = self.uploadable.read(chunk_size) |
---|
21 | data = b"".join(data) |
---|
22 | start = i |
---|
23 | end = i + chunk_size |
---|
24 | self.assertThat(data, Equals(self.test_data[start:end])) |
---|
25 | |
---|
26 | |
---|
27 | def test_datahandle_get_size(self): |
---|
28 | actual_size = len(self.test_data) |
---|
29 | size = self.uploadable.get_size() |
---|
30 | self.assertThat(size, Equals(actual_size)) |
---|
31 | |
---|
32 | |
---|
33 | def test_datahandle_get_size_out_of_order(self): |
---|
34 | # We should be able to call get_size whenever we want without |
---|
35 | # disturbing the location of the seek pointer. |
---|
36 | chunk_size = 100 |
---|
37 | data = self.uploadable.read(chunk_size) |
---|
38 | self.assertThat(b"".join(data), Equals(self.test_data[:chunk_size])) |
---|
39 | |
---|
40 | # Now get the size. |
---|
41 | size = self.uploadable.get_size() |
---|
42 | self.assertThat(self.test_data, HasLength(size)) |
---|
43 | |
---|
44 | # Now get more data. We should be right where we left off. |
---|
45 | more_data = self.uploadable.read(chunk_size) |
---|
46 | start = chunk_size |
---|
47 | end = chunk_size * 2 |
---|
48 | self.assertThat(b"".join(more_data), Equals(self.test_data[start:end])) |
---|