SanjanaSanju
SanjanaSanju

Reputation: 297

Python Assert exception

I am having phyton code in 10 databricks cell in a single databricks notebook.

The first cell contains the below code.

df = spark.sql(f"""select * from test.table """)
count= df.count()

Here if count is 0 , I want the notebook to fail, if the count is greater than 1 , I want the rest of the cells in the databricks notebook to execute.

Im trying the below code.

 df = spark.sql(f"""select * from test.table """)
    count= df.count()
    assert count <= 0

The above code is not raising an assertion error, instead it just says query returns no results and executes the next cell in the databricks. Could someone please let me know to raise an assert exception and terminate the notebook without execution further cells.

Thank you.

Upvotes: 1

Views: 3747

Answers (1)

Frank Yellin
Frank Yellin

Reputation: 11297

You have your assertion backwards. You assert what you need to be true.

assert count > 0

(What's that val on the first line? Is that a cut-paste error?)

===

to answer the question below:

if count <= 0:
    raise AssertionError("must return at least one record")

You might look at the list of built-in Python exceptions to see if one of those feels more appropriate than the generic "assertion error" for your circumstances.

Upvotes: 4

Related Questions