def test_takes_a_filelike_with_shorter_length(self): filelike = JustLikeAFile() self.seeker = utils.SeekableFileLikeIter(filelike, length=2**16 + 48) self.assertEqual('ABCD' * ((2**16 + 48) / 4), ''.join(self.seeker)) self.assertEqual(2**16 + 48, self.seeker.tell()) self.assertEqual('', self.seeker.read()) self.assertEqual('', self.seeker.read(1)) self.assertRaises(StopIteration, self.seeker.next) with self.assertRaises(StopIteration): next(self.seeker)
def test_takes_a_filelike(self): filelike = JustLikeAFile() self.seeker = utils.SeekableFileLikeIter(filelike) self.assertEqual('ABCD' * 2**16, ''.join(self.seeker)) self.assertEqual(2**16 * 4, self.seeker.tell()) self.assertEqual('', self.seeker.read()) self.assertEqual('', self.seeker.read(1)) self.assertRaises(StopIteration, self.seeker.next) with self.assertRaises(StopIteration): next(self.seeker)
def test_seekable_read_all(self): self.assertEqual(0, self.seeker.tell()) self.assertEqual('abcdefijk', ''.join(c for c in self.seeker)) self.assertEqual(9, self.seeker.tell()) self.assertEqual('', ''.join(c for c in self.seeker)) self.assertEqual(9, self.seeker.tell()) self.seeker = utils.SeekableFileLikeIter(['abc', 'def', 'ijk']) self.assertEqual(0, self.seeker.tell()) self.assertEqual('abcdefijk', self.seeker.read()) self.assertEqual(9, self.seeker.tell()) self.assertEqual('', self.seeker.read()) self.assertEqual(9, self.seeker.tell())
def test_bounded_reading(self): # Pete and Repete were in a boat; Pete fell out. Who was left?? self.seeker = utils.SeekableFileLikeIter(repeat('abcd'), length=10) self.assertEqual('abcdabcdab', ''.join(self.seeker)) self.assertEqual(10, self.seeker.tell()) self.assertEqual('', self.seeker.read()) self.assertEqual('', self.seeker.read(1)) self.assertRaises(StopIteration, self.seeker.next) with self.assertRaises(StopIteration): next(self.seeker) # Seeking after reading data, without a callback, is most likely a # coding bug and is explicitly disallowed. self.assertRaises(RuntimeError, self.seeker.reset)
def test_seek_zero_with_cb(self): cb = mock.Mock() cb.side_effect = lambda: iter(['1', '23', '456']) self.seeker = utils.SeekableFileLikeIter(['abc', 'def', 'ijk'], seek_zero_cb=cb) self.assertEqual(0, self.seeker.tell()) self.seeker.seek(0) # flag is optional self.assertEqual(0, self.seeker.tell()) self.seeker.seek(0, os.SEEK_CUR) # flag is actually ignored # A seek-zero with no bytes read is a NOOP self.assertEqual([], cb.mock_calls) self.assertEqual('ab', self.seeker.read(2)) self.assertEqual(2, self.seeker.tell()) self.seeker.seek(0, os.SEEK_END) # LOL self.assertEqual(0, self.seeker.tell()) self.assertEqual('123456', ''.join(self.seeker)) self.assertEqual(6, self.seeker.tell()) self.assertEqual('', self.seeker.read()) self.assertEqual('', self.seeker.read(1)) self.assertRaises(StopIteration, self.seeker.next)
def test_body_not_iter_str_unicode_or_filelike(self): with self.assertRaises(TypeError) as cm: self.seeker = utils.SeekableFileLikeIter(None) self.assertEqual("'NoneType' object is not iterable", cm.exception.message)
def setUp(self): self.mock_swift = FakeSwiftClient() self.seeker = utils.SeekableFileLikeIter(['abc', 'def', 'ijk'])