The Asset Compute project defines a pattern for easily creating and executing tests of Asset Compute workers.
Asset Compute workers’ tests are broken into test suites, and within each test suite, one or more test cases asserting a condition to test.
The structure of tests in an Asset Compute project are as follows:
/actions/<worker-name>/index.js
...
/test/
asset-compute/
<worker-name>/ <--- Test suite for the worker, must match the yaml key for this worker in manifest.yml
<test-case-1>/ <--- Specific test case
file.jpg <--- Input file (ie. `source.path` or `source.url`)
params.json <--- Parameters (ie. `rendition.instructions`)
rendition.png <--- Expected output file (ie. `rendition.path`)
<test-case-2>/ <--- Another specific test case for this worker
...
Each test cast can have the following files:
file.<extension>
.link
)rendition.<extension>
params.json
validate
diff
commandmock-<host-name>.json
This test case asserts the parameterized input (params.json
) for the input file (file.jpg
) generates the expected PNG rendition (rendition.png
).
First delete the auto-generated simple-worker
tests case at /test/asset-compute/simple-worker
as this is invalid, as our worker no longer simply copies the source to the rendition.
Create a new test case folder at /test/asset-compute/worker/success-parameterized
to test a successful execution of the worker that generates a PNG rendition.
In the success-parameterized
folder, add the test input file for this test case and name it file.jpg
.
In the success-parameterized
folder, add a new file named params.json
that defines the input parameters of the worker:
{
"size": "400",
"contrast": "0.25",
"brightness": "-0.50"
}
These are the same key/values passed into the Development Tool’s Asset Compute profile definition, less the worker
key.
Add the expected rendition file to this test case and name it rendition.png
. This file represents the expected output of the worker for the given input file.jpg
.
From the command line, run the tests the project root by executing aio app test
This test case tests to ensure the worker throws the appropriate error when the contrast
parameter is set to an invalid value.
Create a new test case folder at /test/asset-compute/worker/error-contrast
to test a erring execution of the worker due to an invalid contrast
parameter value.
In the error-contrast
folder, add the test input file for this test case and name it file.jpg
. The contents of this file is immaterial to this test, it just needs to exist to get past the “Corrupt source” check, in order to reach the rendition.instructions
validity checks, that this test case validates.
In the error-contrast
folder, add a new file named params.json
that defines the input parameters of the worker with the contents:
{
"contrast": "10",
"errorReason": "rendition_instructions_error"
}
contrast
parameters to 10
, an invalid value, as contrast must be between -1 and 1, to throw a RenditionInstructionsError
.errorReason
key to the “reason” associated with the expected error. This invalid contrast parameter throws the custom error, RenditionInstructionsError
, therefore set the errorReason
to this error’s reason, orrendition_instructions_error
to assert it is thrown.Since no rendition should be generated during an erring execution, no rendition.<extension>
file is necessary.
Run the test suite from the root of the project by executing the command aio app test
The final test cases are available on Github at: