def test_basic_recordwriter_write_synced(self): test_string = "hello world" with self.EphemeralFile('r+') as fp: RecordWriter.do_write(fp, test_string, StringCodec(), sync=True) fp.seek(0) rr = RecordReader(fp) assert rr.read() == test_string
def test_basic_recordwriter_write(self): test_string = "hello world" with self.EphemeralFile('r+') as fp: rw = RecordWriter(fp) rw.write(test_string) fp.seek(0) rr = RecordReader(fp) assert rr.read() == test_string
def test_basic_recordreader_iterator(self): test_strings = ["hello", "world", "etc"] with self.EphemeralFile('r+') as fp: for string in test_strings: RecordWriter.do_write(fp, string, StringCodec(), sync=True) fp.seek(0) rr = RecordReader(fp) assert list(rr) == test_strings
def test_basic_recordwriter_write_synced(): test_string = "hello world" with EphemeralFile('w') as fp: fn = fp.name RecordWriter.do_write(fp, test_string, RecordIO.StringCodec(), sync=True) with open(fn) as fpr: rr = RecordReader(fpr) assert rr.read() == test_string
def test_basic_recordwriter_write(): test_string = "hello world" with EphemeralFile('w') as fp: fn = fp.name rw = RecordWriter(fp) rw.write(test_string) rw.close() with open(fn) as fpr: rr = RecordReader(fpr) assert rr.read() == test_string
def test_basic_recordwriter_write_fail(self): test_string = "hello" header = struct.pack('>L', len(test_string)) fp = self.mox.CreateMock(file) fp.write(header).AndRaise(IOError) fp.write(header).AndRaise(OSError) self.mox.ReplayAll() assert RecordWriter.do_write(fp, test_string, StringCodec()) == False assert RecordWriter.do_write(fp, test_string, StringCodec()) == False
def test_basic_recordwriter_write_synced_raises(self): test_string = "hello world" self.mox.StubOutWithMock(os, 'fsync') with RecordioTestBase.EphemeralFile('r+') as fp: os.fsync(fp.fileno()).AndRaise(OSError) self.mox.ReplayAll() rw = RecordWriter(FileLike(fp)) rw.set_sync(True) rw.write(test_string) fp.seek(0) rr = RecordReader(fp) assert rr.read() == test_string
def test_paranoid_append_framing(self): with self.DurableFile('w') as fp: fn = fp.name test_string_1 = "hello world" test_string_2 = "ahoy ahoy, bonjour" RecordWriter.append(fn, test_string_1) RecordWriter.append(fn, test_string_2) with open(fn) as fpr: rr = RecordReader(fpr) assert rr.read() == test_string_1 assert rr.read() == test_string_2 os.remove(fn)
def test_basic_recordreader_read(self): test_string = "hello world" with self.EphemeralFile('r') as fp: fn = fp.name rr = RecordReader(fp) assert rr.read() is None rr.close() with open(fn, 'w') as fpw: rw = RecordWriter(fpw) rw.write(test_string) with open(fn) as fpr: rr = RecordReader(fpr) assert rr.read() == test_string
def test_recordwriter_initializing(self): for mode in ('a', 'r+', 'w'): with self.EphemeralFile(mode) as fp: try: RecordWriter(fp) except Exception as e: assert False, ( "Failed to initialize RecordWriter in '%s' mode (exception: %s)" % (mode, e))
def test_recordwriter_framing(self): test_string_1 = "hello world" test_string_2 = "ahoy ahoy, bonjour" with self.EphemeralFile('w') as fp: fn = fp.name rw = RecordWriter(fp) rw.write(test_string_1) rw.close() with open(fn, 'a') as fpa: rw = RecordWriter(fpa) rw.write(test_string_2) with open(fn) as fpr: rr = RecordReader(fpr) assert rr.read() == test_string_1 assert rr.read() == test_string_2
def test_recordwriter_works_with_write(): with EphemeralFile('w') as fp: try: RecordWriter(fp) except: assert False, 'Failed to initialize RecordWriter in r+ mode'
def test_raises_on_nonfile(): with pytest.raises(RecordIO.InvalidFileHandle): RecordWriter('/tmp/poop') with pytest.raises(RecordIO.InvalidFileHandle): RecordReader('/tmp/poop')
def test_paranoid_append_returns_false_on_nonexistent_file(): fn = tempfile.mktemp() assert RecordWriter.append(fn, 'hello world!') == False
def test_append_fails_on_inaccessible_file(self): with RecordioTestBase.EphemeralFile('w') as fp: os.fchmod(fp.fileno(), 000) with pytest.raises(IOError): RecordWriter.append(fp.name, 'hello world!')
def test_append_fails_on_nonexistent_file(self): fn = tempfile.mktemp() assert RecordWriter.append(fn, 'hello world!') == False
def test_recordwriter_raises_on_readonly_file(self): with self.EphemeralFile('r') as fp: with pytest.raises(RecordIO.InvalidFileHandle): RecordWriter(fp)
def test_raises_if_initialized_with_nil_filehandle(self): with pytest.raises(RecordIO.InvalidFileHandle): RecordWriter(None) with pytest.raises(RecordIO.InvalidFileHandle): RecordReader(None)