11import json
2+ import textwrap
23import threading
34from unittest import mock
45
6+ import geopandas
7+
58# TODO: can we avoid using httpretty?
69# We need it for testing the resilience, which uses an HTTPadapter with Retry
710# but requests-mock also uses an HTTPAdapter for the mocking and basically
811# erases the HTTPAdapter we have set up.
912# httpretty avoids this specific problem because it mocks at the socket level,
1013# But I would rather not have two dependencies with almost the same goal.
1114import httpretty
15+ import pandas
1216import pandas as pd
1317import pytest
1418import requests
15- import shapely .geometry . point as shpt
19+ import shapely .geometry
1620
1721import openeo
1822from openeo import BatchJob
@@ -456,6 +460,26 @@ def start_job(row, connection_provider, connection, **kwargs):
456460 assert set (result .backend_name ) == {"foo" }
457461
458462
463+ JOB_DB_DF_BASICS = pd .DataFrame (
464+ {
465+ "numbers" : [3 , 2 , 1 ],
466+ "names" : ["apple" , "banana" , "coconut" ],
467+ }
468+ )
469+ JOB_DB_GDF_WITH_GEOMETRY = geopandas .GeoDataFrame (
470+ {
471+ "numbers" : [11 , 22 ],
472+ "geometry" : [shapely .geometry .Point (1 , 2 ), shapely .geometry .Point (2 , 1 )],
473+ },
474+ )
475+ JOB_DB_DF_WITH_GEOJSON_STRING = pd .DataFrame (
476+ {
477+ "numbers" : [11 , 22 ],
478+ "geometry" : ['{"type":"Point","coordinates":[1,2]}' , '{"type":"Point","coordinates":[1,2]}' ],
479+ }
480+ )
481+
482+
459483class TestCsvJobDatabase :
460484 def test_read_wkt (self , tmp_path ):
461485 wkt_df = pd .DataFrame (
@@ -467,7 +491,7 @@ def test_read_wkt(self, tmp_path):
467491 path = tmp_path / "jobs.csv"
468492 wkt_df .to_csv (path , index = False )
469493 df = CsvJobDatabase (path ).read ()
470- assert isinstance (df .geometry [0 ], shpt .Point )
494+ assert isinstance (df .geometry [0 ], shapely . geometry .Point )
471495
472496 def test_read_non_wkt (self , tmp_path ):
473497 non_wkt_df = pd .DataFrame (
@@ -481,34 +505,40 @@ def test_read_non_wkt(self, tmp_path):
481505 df = CsvJobDatabase (path ).read ()
482506 assert isinstance (df .geometry [0 ], str )
483507
484- def test_persist_and_read (self , tmp_path ):
485- orig = pd .DataFrame (
486- {
487- "numbers" : [3 , 2 , 1 ],
488- "names" : ["apple" , "banana" , "coconut" ],
489- }
490- )
491- path = tmp_path / "jobs.csv"
508+ @pytest .mark .parametrize (
509+ ["orig" ],
510+ [
511+ pytest .param (JOB_DB_DF_BASICS , id = "pandas basics" ),
512+ pytest .param (JOB_DB_GDF_WITH_GEOMETRY , id = "geopandas with geometry" ),
513+ pytest .param (JOB_DB_DF_WITH_GEOJSON_STRING , id = "pandas with geojson string as geometry" ),
514+ ],
515+ )
516+ def test_persist_and_read (self , tmp_path , orig : pandas .DataFrame ):
517+ path = tmp_path / "jobs.parquet"
492518 CsvJobDatabase (path ).persist (orig )
493519 assert path .exists ()
494520
495521 loaded = CsvJobDatabase (path ).read ()
496- assert list ( loaded .dtypes ) == list ( orig .dtypes )
522+ assert loaded .dtypes . to_dict ( ) == orig .dtypes . to_dict ( )
497523 assert loaded .equals (orig )
524+ assert type (orig ) is type (loaded )
498525
499526
500527class TestParquetJobDatabase :
501- def test_persist_and_read (self , tmp_path ):
502- orig = pd .DataFrame (
503- {
504- "numbers" : [3 , 2 , 1 ],
505- "names" : ["apple" , "banana" , "coconut" ],
506- }
507- )
528+ @pytest .mark .parametrize (
529+ ["orig" ],
530+ [
531+ pytest .param (JOB_DB_DF_BASICS , id = "pandas basics" ),
532+ pytest .param (JOB_DB_GDF_WITH_GEOMETRY , id = "geopandas with geometry" ),
533+ pytest .param (JOB_DB_DF_WITH_GEOJSON_STRING , id = "pandas with geojson string as geometry" ),
534+ ],
535+ )
536+ def test_persist_and_read (self , tmp_path , orig : pandas .DataFrame ):
508537 path = tmp_path / "jobs.parquet"
509538 ParquetJobDatabase (path ).persist (orig )
510539 assert path .exists ()
511540
512541 loaded = ParquetJobDatabase (path ).read ()
513- assert list ( loaded .dtypes ) == list ( orig .dtypes )
542+ assert loaded .dtypes . to_dict ( ) == orig .dtypes . to_dict ( )
514543 assert loaded .equals (orig )
544+ assert type (orig ) is type (loaded )
0 commit comments