forked from datacontract/datacontract-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test data contract against dataframes or views
Resolves datacontract#175
- Loading branch information
1 parent
cf41b9e
commit 6e89feb
Showing
6 changed files
with
138 additions
and
2 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
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
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
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
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,23 @@ | ||
dataContractSpecification: 0.9.3 | ||
id: dataframetest | ||
info: | ||
title: dataframetest | ||
version: 0.0.1 | ||
owner: my-domain-team | ||
servers: | ||
unittest: | ||
type: dataframe | ||
models: | ||
my_table: | ||
type: table | ||
fields: | ||
field_one: | ||
type: varchar | ||
required: true | ||
unique: true | ||
pattern: "[A-Za-z]{2}-\\d{3}-[A-Za-z]{2}$" | ||
field_two: | ||
type: int | ||
minimum: 10 | ||
field_three: | ||
type: timestamp |
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,62 @@ | ||
import logging | ||
from pathlib import Path | ||
|
||
from dotenv import load_dotenv | ||
from pyspark.sql import SparkSession, Row | ||
from pyspark.sql.types import StructType, StructField, StringType, IntegerType, TimestampType | ||
from datetime import datetime | ||
|
||
from datacontract.data_contract import DataContract | ||
|
||
# logging.basicConfig(level=logging.INFO, force=True) | ||
|
||
datacontract = "fixtures/dataframe/datacontract.yaml" | ||
|
||
load_dotenv(override=True) | ||
|
||
|
||
def test_test_dataframe(tmp_path: Path): | ||
spark = _create_spark_session(tmp_dir=str(tmp_path)) | ||
_prepare_dataframe(spark) | ||
data_contract = DataContract( | ||
data_contract_file=datacontract, | ||
spark=spark, | ||
) | ||
|
||
run = data_contract.test() | ||
|
||
print(run.pretty()) | ||
assert run.result == "passed" | ||
assert all(check.result == "passed" for check in run.checks) | ||
|
||
|
||
def _prepare_dataframe(spark): | ||
schema = StructType([ | ||
StructField("field_one", StringType(), nullable=False), | ||
StructField("field_two", IntegerType(), nullable=True), | ||
StructField("field_three", TimestampType(), nullable=True) | ||
]) | ||
data = [ | ||
Row(field_one="AB-123-CD", field_two=15, | ||
field_three=datetime.strptime("2024-01-01 12:00:00", "%Y-%m-%d %H:%M:%S")), | ||
Row(field_one="XY-456-ZZ", field_two=20, | ||
field_three=datetime.strptime("2024-02-01 12:00:00", "%Y-%m-%d %H:%M:%S")) | ||
] | ||
# Create DataFrame | ||
df = spark.createDataFrame(data, schema=schema) | ||
# Create temporary view | ||
# Name must match the model name in the data contract | ||
df.createOrReplaceTempView("my_table") | ||
|
||
|
||
def _create_spark_session(tmp_dir: str) -> SparkSession: | ||
"""Create and configure a Spark session.""" | ||
spark = ( | ||
SparkSession.builder.appName("datacontract") | ||
.config("spark.sql.warehouse.dir", f"{tmp_dir}/spark-warehouse") | ||
.config("spark.streaming.stopGracefullyOnShutdown", "true") | ||
.getOrCreate() | ||
) | ||
spark.sparkContext.setLogLevel("WARN") | ||
print(f"Using PySpark version {spark.version}") | ||
return spark |