Change tests to use tempfile

This commit is contained in:
Jason Paryani
2013-12-09 17:16:39 -08:00
parent 3f7c3ae023
commit 0892a63b23
2 changed files with 16 additions and 16 deletions

View File

@@ -3,6 +3,7 @@ import capnp
import os
import platform
import test_regression
import tempfile
this_dir = os.path.dirname(__file__)
@@ -11,22 +12,22 @@ def all_types():
return capnp.load(os.path.join(this_dir, 'all_types.capnp'))
def test_roundtrip_file(all_types):
f = open('example', 'w')
f = tempfile.TemporaryFile()
msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg)
msg.write(f)
f = open('example', 'r')
f.seek(0)
msg = all_types.TestAllTypes.read(f)
test_regression.check_all_types(msg)
def test_roundtrip_file_packed(all_types):
f = open('example', 'w')
f = tempfile.TemporaryFile()
msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg)
msg.write_packed(f)
f = open('example', 'r')
f.seek(0)
msg = all_types.TestAllTypes.read_packed(f)
test_regression.check_all_types(msg)
@@ -47,26 +48,26 @@ def test_roundtrip_bytes_packed(all_types):
test_regression.check_all_types(msg)
def test_roundtrip_file_multiple(all_types):
f = open('example', 'w')
f = tempfile.TemporaryFile()
msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg)
msg.write(f)
msg.write(f)
msg.write(f)
f = open('example', 'r')
f.seek(0)
for msg in all_types.TestAllTypes.read_multiple(f):
test_regression.check_all_types(msg)
def test_roundtrip_file_multiple_packed(all_types):
f = open('example', 'w')
f = tempfile.TemporaryFile()
msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg)
msg.write_packed(f)
msg.write_packed(f)
msg.write_packed(f)
f = open('example', 'r')
f.seek(0)
for msg in all_types.TestAllTypes.read_multiple_packed(f):
test_regression.check_all_types(msg)
@@ -79,23 +80,21 @@ def test_roundtrip_dict(all_types):
test_regression.check_all_types(msg)
def test_file_and_bytes(all_types):
f = open('example', 'w')
f = tempfile.TemporaryFile()
msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg)
msg.write(f)
f.close()
f = open('example', 'r')
f.seek(0)
assert f.read() == msg.to_bytes()
def test_file_and_bytes_packed(all_types):
f = open('example', 'w')
f = tempfile.TemporaryFile()
msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg)
msg.write_packed(f)
f.close()
f = open('example', 'r')
f.seek(0)
assert f.read() == msg.to_bytes_packed()

View File

@@ -1,6 +1,7 @@
import pytest
import capnp
import os
import tempfile
this_dir = os.path.dirname(__file__)
@@ -44,9 +45,9 @@ def test_which_reader(addressbook):
capnp._write_packed_message_to_fd(fd, message)
f = open('example', 'w')
f = tempfile.TemporaryFile()
writeAddressBook(f.fileno())
f = open('example', 'r')
f.seek(0)
addresses = addressbook.AddressBook.read_packed(f)