Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: gslib/tests/test_file_part.py

Issue 698893003: Update checked in version of gsutil to version 4.6 (Closed) Base URL: http://dart.googlecode.com/svn/third_party/gsutil/
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gslib/tests/test_du.py ('k') | gslib/tests/test_gsutil.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # -*- coding: utf-8 -*-
1 # Copyright 2013 Google Inc. All Rights Reserved. 2 # Copyright 2013 Google Inc. All Rights Reserved.
2 # 3 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License. 5 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at 6 # You may obtain a copy of the License at
6 # 7 #
7 # http://www.apache.org/licenses/LICENSE-2.0 8 # http://www.apache.org/licenses/LICENSE-2.0
8 # 9 #
9 # Unless required by applicable law or agreed to in writing, software 10 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and 13 # See the License for the specific language governing permissions and
13 # limitations under the License. 14 # limitations under the License.
15 """Unit tests for FilePart class."""
14 16
15 import gslib.tests.testcase as testcase 17 from __future__ import absolute_import
18
16 import os 19 import os
17 20
18 from gslib.file_part import FilePart 21 from gslib.file_part import FilePart
22 import gslib.tests.testcase as testcase
19 23
24
25 # pylint: disable=protected-access
20 class TestFilePart(testcase.GsUtilUnitTestCase): 26 class TestFilePart(testcase.GsUtilUnitTestCase):
21 """Unit tests for file_part.py""" 27 """Unit tests for FilePart class."""
22 28
23 def test_tell(self): 29 def test_tell(self):
24 filename = 'test_tell' 30 filename = 'test_tell'
25 contents = 100 * 'x' 31 contents = 100 * 'x'
26 fpath = self.CreateTempFile(file_name=filename, contents=contents) 32 fpath = self.CreateTempFile(file_name=filename, contents=contents)
27 part_length = 23 33 part_length = 23
28 start_pos = 50 34 start_pos = 50
29 fp = FilePart(fpath, start_pos, part_length) 35 fp = FilePart(fpath, start_pos, part_length)
30 self.assertEqual(start_pos, fp._fp.tell()) 36 self.assertEqual(start_pos, fp._fp.tell())
31 self.assertEqual(0, fp.tell()) 37 self.assertEqual(0, fp.tell())
32 38
33 def test_seek(self): 39 def test_seek(self):
40 """Tests seeking in a FilePart."""
34 filename = 'test_seek' 41 filename = 'test_seek'
35 contents = 100 * 'x' 42 contents = 100 * 'x'
36 part_length = 23 43 part_length = 23
37 start_pos = 50 44 start_pos = 50
38 fpath = self.CreateTempFile(file_name=filename, contents=contents) 45 fpath = self.CreateTempFile(file_name=filename, contents=contents)
39 fp = FilePart(fpath, start_pos, part_length) 46 fp = FilePart(fpath, start_pos, part_length)
40 offset = 10 47 offset = 10
41 48
42 # Absolute positioning. 49 # Absolute positioning.
43 fp.seek(offset) 50 fp.seek(offset)
44 self.assertEqual(start_pos + offset, fp._fp.tell()) 51 self.assertEqual(start_pos + offset, fp._fp.tell())
45 self.assertEqual(offset, fp.tell()) 52 self.assertEqual(offset, fp.tell())
46 53
47 # Relative positioning. 54 # Relative positioning.
48 fp.seek(offset, whence=os.SEEK_CUR) 55 fp.seek(offset, whence=os.SEEK_CUR)
49 self.assertEqual(start_pos + 2 * offset, fp._fp.tell()) 56 self.assertEqual(start_pos + 2 * offset, fp._fp.tell())
50 self.assertEqual(2 * offset, fp.tell()) 57 self.assertEqual(2 * offset, fp.tell())
51 58
52 # Absolute positioning from EOF. 59 # Absolute positioning from EOF.
53 fp.seek(-offset, whence=os.SEEK_END) 60 fp.seek(-offset, whence=os.SEEK_END)
54 self.assertEqual(start_pos + part_length - offset, fp._fp.tell()) 61 self.assertEqual(start_pos + part_length - offset, fp._fp.tell())
55 self.assertEqual(part_length - offset, fp.tell()) 62 self.assertEqual(part_length - offset, fp.tell())
56 63
57 # Seek past EOF. 64 # Seek past EOF.
58 fp.seek(1, whence=os.SEEK_END) 65 fp.seek(1, whence=os.SEEK_END)
59 self.assertEqual(start_pos + part_length + 1, fp._fp.tell()) 66 self.assertEqual(start_pos + part_length + 1, fp._fp.tell())
60 self.assertEqual(part_length + 1, fp.tell()) 67 self.assertEqual(part_length + 1, fp.tell())
61 68
62 def test_read(self): 69 def test_read(self):
70 """Tests various reaad operations with FilePart."""
63 filename = 'test_read' 71 filename = 'test_read'
64 contents = "" 72 contents = ''
65 for i in range(1, 256): 73 for i in range(1, 256):
66 contents += str(i) 74 contents += str(i)
67 part_length = 23 75 part_length = 23
68 start_pos = 50 76 start_pos = 50
69 fpath = self.CreateTempFile(file_name=filename, contents=contents) 77 fpath = self.CreateTempFile(file_name=filename, contents=contents)
70 78
71 # Read in the whole file. 79 # Read in the whole file.
72 fp = FilePart(fpath, start_pos, part_length) 80 fp = FilePart(fpath, start_pos, part_length)
73 whole_file = fp.read() 81 whole_file = fp.read()
74 self.assertEqual(contents[start_pos:(start_pos + part_length)], whole_file) 82 self.assertEqual(contents[start_pos:(start_pos + part_length)], whole_file)
(...skipping 14 matching lines...) Expand all
89 self.assertEqual( 97 self.assertEqual(
90 contents[start_pos:(start_pos + part_length)], 98 contents[start_pos:(start_pos + part_length)],
91 partial_file + remaining_file) 99 partial_file + remaining_file)
92 100
93 # Try to read after reaching EOF. 101 # Try to read after reaching EOF.
94 empty_file = fp.read(100) 102 empty_file = fp.read(100)
95 self.assertEqual('', empty_file) 103 self.assertEqual('', empty_file)
96 104
97 empty_file = fp.read() 105 empty_file = fp.read()
98 self.assertEqual('', empty_file) 106 self.assertEqual('', empty_file)
OLDNEW
« no previous file with comments | « gslib/tests/test_du.py ('k') | gslib/tests/test_gsutil.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698