"""Test the jsonschema for jobs.""" from typing import Any import jsonschema import pytest from spec.jsonschema import jobs as jobs_schemas from video.constants import job_io_fields, job_statuses, job_types def get_jobs_response_json() -> dict[str, Any]: """Get example jobs response json.""" return { "id": 9, "parent_id": None, "type": job_types.TRANSFER_PRORES_FROM_S3_TO_RIPPER, "outputs": {}, "inputs": { job_io_fields.PRORES_MEZZANINE_OUTPUT_S3_KEY: "applesauce", job_io_fields.UPC: 999938768934, }, "status": "COMPLETE", "context": { "id": 123, "correlation_id": "d9fec90a-902e-11e8-b24f-acbc32b98ae7", "product_id": 112428, "vendor_id": 123456, "subaccount_id": None, "user_id": "alw:45454", "datetime": "2018-07-25T17:20:10Z", }, } def get_create_job_request_json() -> dict[str, Any]: """Get create job request json.""" return { "type": job_types.TRANSFER_PRORES_FROM_S3_TO_RIPPER, "inputs": { job_io_fields.PRORES_MEZZANINE_OUTPUT_S3_KEY: "applesauce", job_io_fields.UPC: 999938768934, }, "context": { "product_id": 112428, }, } def get_change_job_status_request_json() -> dict[str, Any]: """Get change job status request json.""" return {"id": 3, "status": "COMPLETE"} def get_create_job_inputs_request_json() -> dict[str, Any]: """Get create job inputs request json.""" return { "id": 3, "status": "SETUP", "inputs": { job_io_fields.PRORES_MEZZANINE_OUTPUT_S3_KEY: "applesauce", job_io_fields.UPC: 999938768934, }, } def get_create_job_outputs_request_json() -> dict[str, Any]: """Get create job outputs request json.""" return {"id": 3, "status": "PROGRESSING", "outputs": {}} def get_jobs_response_no_io_json() -> dict[str, Any]: """Get example jobs response json with null inputs and outputs.""" jobs_response_no_io_json = get_jobs_response_json() jobs_response_no_io_json.update({"inputs": None, "outputs": None}) return jobs_response_no_io_json @pytest.mark.parametrize( ("test_description", "jobs_response_json", "test_schema"), [ ( "test successful response validation", get_jobs_response_json(), jobs_schemas.build_response_schema_jobs( job_types.TRANSFER_PRORES_FROM_S3_TO_RIPPER ), ), ( "test successful response validation with no inputs or outputs", get_jobs_response_no_io_json(), jobs_schemas.build_response_schema_jobs( job_types.TRANSFER_PRORES_FROM_S3_TO_RIPPER ), ), ( "test successful create job request validation", get_create_job_request_json(), jobs_schemas.build_request_schema_create_job( job_types.TRANSFER_PRORES_FROM_S3_TO_RIPPER ), ), ( "test successful change job status request validation", get_change_job_status_request_json(), jobs_schemas.build_request_schema_change_job_status( job_type=job_types.TRANSFER_PRORES_FROM_S3_TO_RIPPER, current_job_status=job_statuses.PROGRESSING, ), ), ( "test successful create job inputs request validation", get_create_job_inputs_request_json(), jobs_schemas.build_request_schema_create_job_inputs( job_types.TRANSFER_PRORES_FROM_S3_TO_RIPPER ), ), ( "test successful create job outputs request validation", get_create_job_outputs_request_json(), jobs_schemas.build_request_schema_create_job_outputs( job_types.TRANSFER_PRORES_FROM_S3_TO_RIPPER ), ), ], ) def test_successful_validation( test_description: str, jobs_response_json: dict[str, Any], test_schema: dict[str, Any], ) -> None: """Test successful validation.""" # test that no exception is thrown jsonschema.validate(jobs_response_json, test_schema) @pytest.mark.parametrize( ("test_description", "jobs_response_json", "test_schema"), [ ( "test failed response validation", {101: 12}, jobs_schemas.build_response_schema_jobs( job_types.TRANSFER_FROM_BROWSER_TO_S3 ), ), ( "test failed response validation with no inputs or outputs", {101: 12}, jobs_schemas.build_response_schema_jobs( job_types.TRANSFER_FROM_BROWSER_TO_S3 ), ), ( "test failed create job request validation", {101: 12}, jobs_schemas.build_request_schema_create_job( job_types.TRANSFER_FROM_BROWSER_TO_S3 ), ), ( "test failed change job status request validation", {101: 12}, jobs_schemas.build_request_schema_change_job_status( job_type=job_types.TRANSFER_FROM_BROWSER_TO_S3, current_job_status=job_statuses.PROGRESSING, ), ), ( "test failed create job inputs request validation", {101: 12}, jobs_schemas.build_request_schema_create_job_inputs( job_types.TRANSFER_FROM_BROWSER_TO_S3 ), ), ( "test failed create job outputs request validation", {101: 12}, jobs_schemas.build_request_schema_create_job_outputs( job_types.TRANSFER_FROM_BROWSER_TO_S3 ), ), ], ) def test_failed_validation( test_description: str, jobs_response_json: dict[str, Any], test_schema: dict[str, Any], ) -> None: """Test failed validation.""" with pytest.raises(jsonschema.ValidationError) as excinfo: jsonschema.validate(jobs_response_json, test_schema) assert excinfo.value.validator is not None assert excinfo.value.validator_value is not None assert excinfo.value.message is not None