Data Engineer Professional · Domain 1: Developing Code for Data Processing using Python and SQL · 23% of the exam
Task 1.11: Develop unit and integration tests using assertDataFrameEqual, assertSchemaEqual, DataFrame.transform, and testing frameworks to ensure code correctness.
Writing transformations as functions, chaining them with transform, the pyspark.testing helpers and their options, pytest with Databricks Connect, and integration tests in a bundle target.
Study it
Testing transformations: transform, assertDataFrameEqual, assertSchemaEqual and pytest
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the Databricks documentation page that proves it.
Question 1 · choose 1
A unit test compares the output of a transformation with an expected DataFrame. The transformation runs a shuffle, so row order changes from run to run, and an average column differs from the expected value in the sixth decimal place. The test must pass for these differences but fail on any real mismatch in values or schema. Which assertion fits?
Row order is ignored by default (checkRowOrder=False), and rtol sets the relative tolerance for float comparisons; schema and other values are still checked.
This compares only the schemas, so wrong values would pass.
Dassert actual.collect() == expected.collect()
Incorrect
Plain list equality depends on row order and on exact float values.
pyspark.testing.assertDataFrameEqual compares schema and rows. By default it ignores row order, and rtol and atol control approximate equality for float columns. assertSchemaEqual is for schema-only checks.
A transformation returns the right columns and values, but in a different column order than the expected DataFrame in the test. The test should compare columns by name. Which call fits?
Nullability is not the problem here; the column order is.
By default assertDataFrameEqual compares columns in the order they appear. ignoreColumnOrder=True compares them by name instead; ignoreColumnName, ignoreNullable and checkRowOrder control other parts of the comparison.
Functions defined in Python notebooks are hard to unit test. Where does Databricks recommend keeping the functions and their unit tests?
AIn the same notebook, in a cell below the code they test
BIn a job that runs the notebooks and checks their output
CIn SQL UDFs that wrap the Python functions in Unity Catalog
DIn Python files outside the notebooks, imported by them
Show the answer and why
AIn the same notebook, in a cell below the code they test
Incorrect
That mixes tests with production code and is not the recommendation.
BIn a job that runs the notebooks and checks their output
Incorrect
That is an end-to-end run, not unit testing of functions.
CIn SQL UDFs that wrap the Python functions in Unity Catalog
Incorrect
SQL UDFs are the recommendation for SQL notebooks, not Python ones.
DIn Python files outside the notebooks, imported by them
Correct
For Python notebooks, Databricks recommends storing functions and their tests outside notebooks.
For Python and R notebooks, Databricks recommends storing functions and their unit tests outside of notebooks. For SQL notebooks, store functions as SQL UDFs; for Scala, use separate notebooks.