-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
python_modules/airline-demo/airline_demo_tests/test_types.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import sys | ||
import tempfile | ||
|
||
from airline_demo.resources import spark_session_local, tempfile_resource | ||
from airline_demo.types import SparkDataFrameParquetSerializationStrategy | ||
|
||
|
||
class MockResources(object): | ||
def __init__(self, spark, tempfile_): | ||
self.spark = spark | ||
self.tempfile = tempfile_ | ||
|
||
|
||
class MockPipelineContext(object): | ||
def __init__(self, spark, tempfile_): | ||
self.resources = MockResources(spark, tempfile_) | ||
|
||
|
||
def test_spark_data_frame_serialization(): | ||
spark = spark_session_local.resource_fn(None) | ||
if sys.version_info > (3,): | ||
tempfile_ = tempfile_resource.resource_fn(None).__next__() | ||
else: | ||
tempfile_ = tempfile_resource.resource_fn(None).next() | ||
|
||
try: | ||
serialization_strategy = SparkDataFrameParquetSerializationStrategy() | ||
|
||
pipeline_context = MockPipelineContext(spark, tempfile_) | ||
|
||
df = spark.createDataFrame([('Foo', 1), ('Bar', 2)]) | ||
|
||
with tempfile.NamedTemporaryFile(delete=False) as tempfile_obj: | ||
serialization_strategy.serialize_value(pipeline_context, df, tempfile_obj) | ||
|
||
tempfile_obj.close() | ||
|
||
with open(tempfile_obj.name, 'r+b') as tempfile_obj: | ||
new_df = serialization_strategy.deserialize_value(pipeline_context, tempfile_obj) | ||
|
||
assert set(map(lambda x: x[0], new_df.collect())) == set(['Bar', 'Foo']) | ||
assert set(map(lambda x: x[1], new_df.collect())) == set([1, 2]) | ||
finally: | ||
tempfile_.close() |