Modeling Guide for SAP Data Hub

Creating Tests

When you create a new operator extending BaseOperator, you can also create unit tests using Python's unit testing framework.

We recommend that you create a folder called test in your project root (side-by-side with the folder "my_solution" in our sample hierarchy) and inside it use the same folder structure you used when creating the operator. For example, for the operator appendString, we will create the following folders:

test/operators/com/mydomain/util/appendString/

And create a file named test_append_string.py inside appendString folder. This file is shown below:

import unittest
from Queue import Queue
from operators.com.mydomain.appendString.operator import AppendString

class TestAppendString(unittest.TestCase):

    def testUpper(self):
        op = AppendString.new(inst_id="1", op_id="anything")
        qin1 = Queue(maxsize=1)
        op.inqs["inString"] = qin1
        qout1 = Queue(maxsize=1)
        op.outqs["outString"] = qout1

        input_config = "config_test"
        op.config.stringToAppend = input_config
        op.config.method = "UPPERCASE"

        op.base_init()  # This will execute the _init method and also other things.

        op.start()  # Start the operator's thread.

        try:
            input_str = "input_test|"
            qin1.put(input_str)  # Send data in the 'inString' input port.
            ret = qout1.get()  # Gets data from the 'outString' output port.

            self.assertEqual((input_str + input_config).upper(), ret)  # Verify that the output is equal to the expected result.
        finally:
            op.stop()  # stop the operator no matter what happens. Otherwise the test may hang forever.

You can make an script like the one below to run unittests for all python files starting with the prefix test:

PYSIX_PATH=<PYSIX_SUBENGINE_PATH>  # replace <PYSIX_SUBENGINE_PATH> by the path to your pysix_subengine folder.

SCRIPT_DIR=$(dirname "$0")
cd $SCRIPT_DIR  # changes working dir to the folder where the script is located (which should be the 'test' folder).

SUBENGINE_ROOT=$(realpath ../my_pysolution/vrep/vflow/subengines/com/sap/python27)  # Gets absolute path of python27 folder.

export PYTHONPATH=$PYTHONPATH:$PYSIX_PATH:$SUBENGINE_ROOT  # Append paths to pythonpath.

# Or call with python3.6 if your operators are written in python3.6
python2.7 -m unittest discover -s . -p "test*.py" -v

cd -

The script above should be placed inside the test directory you created.

You can check the official documentation of Python's unittest module by accessing this linkInformation published on non-SAP site.