
Answer-first summary for fast verification
Answer: `assert row_count > 0, 'Row count should be more than 0'`
Unit testing is a critical practice for ensuring code quality and consistency, especially in notebooks. It involves testing small, self-contained units of code early and frequently to identify issues swiftly and clarify code assumptions. The `assert` keyword is instrumental in unit testing or debugging, as it checks if a condition evaluates to `True`. If the condition is `False`, an `AssertionError` is raised, optionally with a custom message. For example: `x = 'hello'` followed by `assert x == 'goodbye', 'x should be 'hello''` will raise an `AssertionError` with the specified message. Thus, the correct statement to test if `row_count` is greater than zero is: `assert row_count > 0, 'Row count should be more than 0'`.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
A data engineer is tasked with processing data and wishes to perform unit testing to ensure that a query returns more than zero rows. The engineer has written the following code snippet to obtain the row count: row_count = spark.sql('select count(*) from table_name').collect()[0][0]. Which Python statement correctly tests this functionality to confirm the row count exceeds zero?
A
assert that row_count > 0, else 'Row count should be more than 0'
B
assert(row_count > 0, 'Row count should be more than 0')
C
assert row_count > 0, 'Row count should be more than 0'
D
assert if row_count > 0, else 'Row count should be more than 0'
E
assert(if row_count > 0, else 'Row count should be more than 0')
No comments yet.