Survey banner
Switching to Dataiku - a new area to help users who are transitioning from other tools and diving into Dataiku! CHECK IT OUT

Dataiku API tests

Solved!
afostor
Level 2
Dataiku API tests

Hi,

I would like to know if it is possible to write unit tests to a Dataiku API code (in Python) using the API Designer, in order to check if it works according to the desired functionality before the API is deployed.

Thanks


Operating system used: Windows

0 Kudos
1 Solution
AlexT
Dataiker

Hi @afostor ,
There is no specific way to do this in the API endpoint. Usually testing is done using the test queries in API Designer directly.

If you want to perform additional test, you can write a unit test directly in the API endpoint code directly and only invoke this when using param perform_test= true.

import unittest
import traceback

# Insert here initialization code
class TestApiPyFunction(unittest.TestCase):

    def test_api_py_function(self):
        # Test case 1 pass
        result = api_py_function(2, 3, 4)
        self.assertEqual(result, 2 + 3 * 4)  # Expected: 2 + 3 * 4 = 14

        # Test case 2 (Intentional failure)
        result = api_py_function(0, 5, 10)
        self.assertEqual(result, 0 + 5 * 10 + 1)  # Expected: 0 + 5 * 10 + 1 = 51

 
        return "All tests passed successfully."

def api_py_function(param1, param2, param3, perform_test=False):
    result = param1 + param2 * param3
    if perform_test:
        try:
            # Run the test cases if perform_test is True
            # The test cases are defined below in the TestApiPyFunction class
            test_result = TestApiPyFunction().test_api_py_function()
        except AssertionError as e:
            tb = traceback.format_exc()
            return result, tb
        return result, "All tests passed successfully."
    return result

 

View solution in original post

4 Replies
AlexT
Dataiker

Hi @afostor ,
There is no specific way to do this in the API endpoint. Usually testing is done using the test queries in API Designer directly.

If you want to perform additional test, you can write a unit test directly in the API endpoint code directly and only invoke this when using param perform_test= true.

import unittest
import traceback

# Insert here initialization code
class TestApiPyFunction(unittest.TestCase):

    def test_api_py_function(self):
        # Test case 1 pass
        result = api_py_function(2, 3, 4)
        self.assertEqual(result, 2 + 3 * 4)  # Expected: 2 + 3 * 4 = 14

        # Test case 2 (Intentional failure)
        result = api_py_function(0, 5, 10)
        self.assertEqual(result, 0 + 5 * 10 + 1)  # Expected: 0 + 5 * 10 + 1 = 51

 
        return "All tests passed successfully."

def api_py_function(param1, param2, param3, perform_test=False):
    result = param1 + param2 * param3
    if perform_test:
        try:
            # Run the test cases if perform_test is True
            # The test cases are defined below in the TestApiPyFunction class
            test_result = TestApiPyFunction().test_api_py_function()
        except AssertionError as e:
            tb = traceback.format_exc()
            return result, tb
        return result, "All tests passed successfully."
    return result

 

afostor
Level 2
Author

Thanks, I will try this way.

0 Kudos
vaibhav
Level 1

Hello Team,

We have created an endpoint URL and have exposed our model in Dataiku for API testing , now we are able to send one transaction at a time and fetch predictions accordingly , but when i pass multiple transactions or say 2 at a time it gives me error -errorType":"com.dataiku.common.server.DKUControllerBase$MalformedRequestException","message":"Could not parse a SinglePredictionQuery from request body , when i searched about it it tells me that Dataiku endpoint is expecting a single prediction query but is receiving multiple queries. How can this be addressed as we have a requiredment to pass bulk transactions with help of API at a time and model should predict accordingly.. Hope somebody helps out here..would be really helpful..

0 Kudos

Please start a new thread. This conversation has been marked as Solved already and your problem is different than the initial post. 

0 Kudos