@ -5,7 +5,9 @@ from tempfile import NamedTemporaryFile, TemporaryFile
from pyoxigraph import Literal , NamedNode , Quad , Triple , parse , serialize
from pyoxigraph import Literal , NamedNode , Quad , Triple , parse , serialize
EXAMPLE_TRIPLE = Triple (
EXAMPLE_TRIPLE = Triple (
NamedNode ( " http://example.com/foo " ) , NamedNode ( " http://example.com/p " ) , Literal ( " 1 " )
NamedNode ( " http://example.com/foo " ) ,
NamedNode ( " http://example.com/p " ) ,
Literal ( " éù " ) ,
)
)
EXAMPLE_QUAD = Quad (
EXAMPLE_QUAD = Quad (
NamedNode ( " http://example.com/foo " ) ,
NamedNode ( " http://example.com/foo " ) ,
@ -18,7 +20,7 @@ EXAMPLE_QUAD = Quad(
class TestParse ( unittest . TestCase ) :
class TestParse ( unittest . TestCase ) :
def test_parse_file ( self ) - > None :
def test_parse_file ( self ) - > None :
with NamedTemporaryFile ( ) as fp :
with NamedTemporaryFile ( ) as fp :
fp . write ( b ' <foo> <p> " 1 " . ' )
fp . write ( ' <foo> <p> " éù " . ' . encode ( ) )
fp . flush ( )
fp . flush ( )
self . assertEqual (
self . assertEqual (
list ( parse ( fp . name , " text/turtle " , base_iri = " http://example.com/ " ) ) ,
list ( parse ( fp . name , " text/turtle " , base_iri = " http://example.com/ " ) ) ,
@ -33,7 +35,7 @@ class TestParse(unittest.TestCase):
self . assertEqual (
self . assertEqual (
list (
list (
parse (
parse (
StringIO ( ' <foo> <p> " 1 " . ' ) ,
StringIO ( ' <foo> <p> " éù " . ' ) ,
" text/turtle " ,
" text/turtle " ,
base_iri = " http://example.com/ " ,
base_iri = " http://example.com/ " ,
)
)
@ -41,11 +43,23 @@ class TestParse(unittest.TestCase):
[ EXAMPLE_TRIPLE ] ,
[ EXAMPLE_TRIPLE ] ,
)
)
def test_parse_long_str_io ( self ) - > None :
self . assertEqual (
list (
parse (
StringIO ( ' <foo> <p> " éù " . \n ' * 1024 ) ,
" text/turtle " ,
base_iri = " http://example.com/ " ,
)
) ,
[ EXAMPLE_TRIPLE ] * 1024 ,
)
def test_parse_bytes_io ( self ) - > None :
def test_parse_bytes_io ( self ) - > None :
self . assertEqual (
self . assertEqual (
list (
list (
parse (
parse (
BytesIO ( b ' <foo> <p> " 1 " . ' ) ,
BytesIO ( ' <foo> <p> " éù " . ' . encode ( ) ) ,
" text/turtle " ,
" text/turtle " ,
base_iri = " http://example.com/ " ,
base_iri = " http://example.com/ " ,
)
)
@ -75,15 +89,16 @@ class TestSerialize(unittest.TestCase):
output = BytesIO ( )
output = BytesIO ( )
serialize ( [ EXAMPLE_TRIPLE ] , output , " text/turtle " )
serialize ( [ EXAMPLE_TRIPLE ] , output , " text/turtle " )
self . assertEqual (
self . assertEqual (
output . getvalue ( ) ,
output . getvalue ( ) . decode ( ) ,
b ' <http://example.com/foo> <http://example.com/p> " 1 " . \n ' ,
' <http://example.com/foo> <http://example.com/p> " éù " . \n ' ,
)
)
def test_serialize_to_file ( self ) - > None :
def test_serialize_to_file ( self ) - > None :
with NamedTemporaryFile ( ) as fp :
with NamedTemporaryFile ( ) as fp :
serialize ( [ EXAMPLE_TRIPLE ] , fp . name , " text/turtle " )
serialize ( [ EXAMPLE_TRIPLE ] , fp . name , " text/turtle " )
self . assertEqual (
self . assertEqual (
fp . read ( ) , b ' <http://example.com/foo> <http://example.com/p> " 1 " . \n '
fp . read ( ) . decode ( ) ,
' <http://example.com/foo> <http://example.com/p> " éù " . \n ' ,
)
)
def test_serialize_io_error ( self ) - > None :
def test_serialize_io_error ( self ) - > None :