Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Documentation for UDF Unit Testing and Mocking (and minor Stable Diffusion Fix) #1301

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ parts:
title: Code Style
- file: source/dev-guide/contribute/troubleshoot
title: Troubleshooting
- file: source/dev-guide/contribute/unit-test
title: Unit Testing UDFs in EvaDB

- file: source/dev-guide/debugging
title: Debugging EvaDB
Expand Down
80 changes: 80 additions & 0 deletions docs/source/dev-guide/contribute/unit-test.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
Unit Testing UDFs in EvaDB
===========================

Introduction
------------

Unit testing is a crucial aspect of software development. When working with User Defined Functions (UDFs) in EvaDB, it's essential to ensure that they work correctly. This guide will walk you through the process of writing unit tests for UDFs and using mocking to simulate external dependencies.

Setting Up Test Environment
---------------------------

Before writing tests, set up a test environment. This often involves creating a test database or table and populating it with sample data.

.. code-block:: python

def setUp(self) -> None:
self.evadb = get_evadb_for_testing()
self.evadb.catalog().reset()
create_table_query = """CREATE TABLE IF NOT EXISTS TestTable (
prompt TEXT(100));
"""
execute_query_fetch_all(self.evadb, create_table_query)
test_prompts = ["sample prompt"]
for prompt in test_prompts:
insert_query = f"""INSERT INTO TestTable (prompt) VALUES ('{prompt}')"""
execute_query_fetch_all(self.evadb, insert_query)

Mocking External Dependencies
-----------------------------

When testing UDFs that rely on external services or APIs, use mocking to simulate these dependencies.

.. code-block:: python

@patch("requests.get")
@patch("external_library.Method", return_value={"data": [{"url": "mocked_url"}]})
def test_udf(self, mock_method, mock_requests_get):
# Mock the response from the external service
mock_response = MagicMock()
mock_response.content = "mocked content"
mock_requests_get.return_value = mock_response

# Rest of the test code...

Writing the Test
----------------

After setting up the environment and mocking dependencies, write the test for the UDF.

.. code-block:: python

function_name = "ImageDownloadUDF"
query = f"SELECT {function_name}(prompt) FROM TestTable;"
output = execute_query_fetch_all(self.evadb, query)
expected_output = [...] # Expected output
self.assertEqual(output, expected_output)

Cleaning Up After Tests
-----------------------

Clean up any resources used during testing, such as database tables.

.. code-block:: python

def tearDown(self) -> None:
execute_query_fetch_all(self.evadb, "DROP TABLE IF EXISTS TestTable;")

Running the Tests
-----------------

Run the tests using a test runner like `unittest`.

.. code-block:: bash

python -m unittest path_to_your_test_module.py

Conclusion
----------

Unit testing UDFs in EvaDB ensures their correctness and robustness. Mocking allows for simulating external dependencies, making tests faster and more deterministic.
2 changes: 1 addition & 1 deletion evadb/functions/dalle.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def forward(self, text_df):
# Register API key, try configuration manager first
openai.api_key = ConfigurationManager().get_value("third_party", "OPENAI_KEY")
# If not found, try OS Environment Variable
if len(openai.api_key) == 0:
if openai.api_key is None or len(openai.api_key) == 0:
openai.api_key = os.environ.get("OPENAI_KEY", "")
assert (
len(openai.api_key) != 0
Expand Down
2 changes: 1 addition & 1 deletion evadb/functions/stable_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def forward(self, text_df):
"third_party", "REPLICATE_API_TOKEN"
)
# If not found, try OS Environment Variable
if len(replicate_api_key) == 0:
if replicate_api_key is None:
replicate_api_key = os.environ.get("REPLICATE_API_TOKEN", "")
assert (
len(replicate_api_key) != 0
Expand Down