2013-09-03 22:12:28 -07:00
import pytest
import capnp
import os
2013-09-03 22:50:58 -07:00
import platform
2013-09-04 11:20:05 -07:00
import test_regression
2013-12-09 17:16:39 -08:00
import tempfile
2014-04-17 21:21:06 -07:00
import pickle
2016-02-05 17:15:26 +01:00
import mmap
2016-03-04 12:42:45 -08:00
import sys
2013-09-03 22:12:28 -07:00
this_dir = os . path . dirname ( __file__ )
@pytest.fixture
2013-09-04 11:20:05 -07:00
def all_types ( ) :
return capnp . load ( os . path . join ( this_dir , ' all_types.capnp ' ) )
2013-09-03 22:12:28 -07:00
2013-09-04 11:20:05 -07:00
def test_roundtrip_file ( all_types ) :
2013-12-09 17:16:39 -08:00
f = tempfile . TemporaryFile ( )
2013-09-04 11:20:05 -07:00
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
2013-09-03 22:12:28 -07:00
msg . write ( f )
2013-12-09 17:16:39 -08:00
f . seek ( 0 )
2013-09-04 11:20:05 -07:00
msg = all_types . TestAllTypes . read ( f )
test_regression . check_all_types ( msg )
2013-09-03 22:12:28 -07:00
2013-09-04 11:20:05 -07:00
def test_roundtrip_file_packed ( all_types ) :
2013-12-09 17:16:39 -08:00
f = tempfile . TemporaryFile ( )
2013-09-04 11:20:05 -07:00
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
2013-09-03 22:16:13 -07:00
msg . write_packed ( f )
2013-12-09 17:16:39 -08:00
f . seek ( 0 )
2013-09-04 11:20:05 -07:00
msg = all_types . TestAllTypes . read_packed ( f )
test_regression . check_all_types ( msg )
2013-09-03 22:16:13 -07:00
2013-09-04 11:20:05 -07:00
def test_roundtrip_bytes ( all_types ) :
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
2013-09-03 22:12:28 -07:00
message_bytes = msg . to_bytes ( )
2013-09-04 11:20:05 -07:00
msg = all_types . TestAllTypes . from_bytes ( message_bytes )
test_regression . check_all_types ( msg )
2013-09-03 22:12:28 -07:00
2016-03-04 13:12:01 -08:00
@pytest.mark.skipif ( sys . version_info [ 0 ] < 3 , reason = " mmap doesn ' t implement the buffer interface under python 2. " )
2016-02-05 17:15:26 +01:00
def test_roundtrip_bytes_mmap ( all_types ) :
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
with tempfile . TemporaryFile ( ) as f :
msg . write ( f )
length = f . tell ( )
f . seek ( 0 )
memory = mmap . mmap ( f . fileno ( ) , length )
msg = all_types . TestAllTypes . from_bytes ( memory )
test_regression . check_all_types ( msg )
2013-11-14 00:23:15 -08:00
def test_roundtrip_bytes_packed ( all_types ) :
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
message_bytes = msg . to_bytes_packed ( )
msg = all_types . TestAllTypes . from_bytes_packed ( message_bytes )
test_regression . check_all_types ( msg )
def test_roundtrip_file_multiple ( all_types ) :
2013-12-09 17:16:39 -08:00
f = tempfile . TemporaryFile ( )
2013-11-14 00:23:15 -08:00
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
msg . write ( f )
msg . write ( f )
msg . write ( f )
2013-12-09 17:16:39 -08:00
f . seek ( 0 )
2015-01-28 16:13:13 -08:00
i = 0
2013-11-14 00:23:15 -08:00
for msg in all_types . TestAllTypes . read_multiple ( f ) :
test_regression . check_all_types ( msg )
2015-01-28 16:13:13 -08:00
i + = 1
assert i == 3
2013-11-14 00:23:15 -08:00
2015-01-28 16:08:47 -08:00
def test_roundtrip_bytes_multiple ( all_types ) :
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
msgs = msg . to_bytes ( )
msgs + = msg . to_bytes ( )
msgs + = msg . to_bytes ( )
2015-01-28 16:13:13 -08:00
i = 0
2015-01-28 16:08:47 -08:00
for msg in all_types . TestAllTypes . read_multiple_bytes ( msgs ) :
test_regression . check_all_types ( msg )
2015-01-28 16:13:13 -08:00
i + = 1
assert i == 3
2015-01-28 16:08:47 -08:00
2013-11-14 00:23:15 -08:00
def test_roundtrip_file_multiple_packed ( all_types ) :
2013-12-09 17:16:39 -08:00
f = tempfile . TemporaryFile ( )
2013-11-14 00:23:15 -08:00
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
msg . write_packed ( f )
msg . write_packed ( f )
msg . write_packed ( f )
2013-12-09 17:16:39 -08:00
f . seek ( 0 )
2015-01-28 16:13:13 -08:00
i = 0
2013-11-14 00:23:15 -08:00
for msg in all_types . TestAllTypes . read_multiple_packed ( f ) :
test_regression . check_all_types ( msg )
2015-01-28 16:13:13 -08:00
i + = 1
assert i == 3
2013-11-14 00:23:15 -08:00
2015-01-28 16:08:47 -08:00
def test_roundtrip_bytes_multiple_packed ( all_types ) :
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
msgs = msg . to_bytes_packed ( )
msgs + = msg . to_bytes_packed ( )
msgs + = msg . to_bytes_packed ( )
2015-01-28 16:13:13 -08:00
i = 0
2015-01-28 16:08:47 -08:00
for msg in all_types . TestAllTypes . read_multiple_bytes_packed ( msgs ) :
test_regression . check_all_types ( msg )
2015-01-28 16:13:13 -08:00
i + = 1
assert i == 3
2015-01-28 16:08:47 -08:00
2015-03-09 23:40:03 -07:00
@pytest.mark.skipif ( platform . python_implementation ( ) == ' PyPy ' , reason = " This works on my local PyPy v2.5.0, but is for some reason broken on TravisCI. Skip for now. " )
2013-09-04 11:20:05 -07:00
def test_roundtrip_dict ( all_types ) :
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
2013-09-03 22:12:28 -07:00
d = msg . to_dict ( )
2013-09-04 11:20:05 -07:00
msg = all_types . TestAllTypes . from_dict ( d )
test_regression . check_all_types ( msg )
2013-12-09 17:13:43 -08:00
def test_file_and_bytes ( all_types ) :
2013-12-09 17:16:39 -08:00
f = tempfile . TemporaryFile ( )
2013-12-09 17:13:43 -08:00
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
msg . write ( f )
2013-12-09 17:16:39 -08:00
f . seek ( 0 )
2013-12-09 17:13:43 -08:00
assert f . read ( ) == msg . to_bytes ( )
def test_file_and_bytes_packed ( all_types ) :
2013-12-09 17:16:39 -08:00
f = tempfile . TemporaryFile ( )
2013-12-09 17:13:43 -08:00
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
msg . write_packed ( f )
2013-12-09 17:16:39 -08:00
f . seek ( 0 )
2013-12-09 17:13:43 -08:00
assert f . read ( ) == msg . to_bytes_packed ( )
2014-04-17 21:21:06 -07:00
def test_pickle ( all_types ) :
msg = all_types . TestAllTypes . new_message ( )
test_regression . init_all_types ( msg )
data = pickle . dumps ( msg )
msg2 = pickle . loads ( data )
test_regression . check_all_types ( msg2 )