"""Test for transcoding order model.""" from typing import Any from unittest import mock import pytest from sqlalchemy.exc import SQLAlchemyError from transcoding.constants import error, transcoding from transcoding.models import transcoding_job from tests.testutils import db, seed @pytest.fixture def valid_transcoding_job_data() -> dict[str, Any]: """Get valid data for create_transcoding_job.""" transcoding_order_id = 2 return { "expected": { "transcoding_job_id": len(seed.transcoding_job_seed_data) + 1, "transcoding_order_id": transcoding_order_id, "status": transcoding.REQUESTED_STATUS, "description": None, "attempt": 0, "output_bucket": None, "output_key": None, "container": None, "channels": None, "codec": None, "sample_rate": None, "bit_rate": 1411200, "bit_depth": None, "duration": None, }, "transcoding_order_id": transcoding_order_id, "input": {"bit_rate": 1411200}, } @db.test_schema def test_create_new_transcoding_job(valid_transcoding_job_data: dict[str, Any]) -> None: """Test successful create of new transcoding_job.""" # test function call response = transcoding_job.create_transcoding_job( valid_transcoding_job_data["transcoding_order_id"], valid_transcoding_job_data["input"], ) # checking assert response.status == 200 actual_result = response.message # delete unmocked TIMESTAMP del actual_result["created_timestamp"] del actual_result["updated_timestamp"] assert actual_result == valid_transcoding_job_data["expected"] @db.test_schema def test_create_new_asset_upload_sql_error( valid_transcoding_job_data: dict[str, Any], ) -> None: """Test failure create of new transcoding_job.""" # data preparation error_mock = SQLAlchemyError(500) package_path = "transcoding.connectors.mysql.db_session" # mocking with mock.patch(package_path, side_effect=error_mock): # test function call response = transcoding_job.create_transcoding_job( valid_transcoding_job_data["transcoding_order_id"], valid_transcoding_job_data["input"], ) # checking assert response.status == 500 @db.test_schema def test_get_transcoding_job_by_id_success() -> None: """Test successful get of existing transcoding_job.""" # data preparation expected_result = seed.transcoding_job_seed_data[0] existed_id = expected_result["transcoding_job_id"] # test function call response = transcoding_job.get_transcoding_job_by_id(transcoding_job_id=existed_id) # checking assert response assert response.status == 200 actual_result = response.message # delete unmocked TIMESTAMP del actual_result["created_timestamp"] del actual_result["updated_timestamp"] assert actual_result == expected_result @db.test_schema def test_get_transcoding_job_by_id_not_found() -> None: """Test get_transcoding_job not found.""" # data preparation entry_name = "Transcoding Job" expected_error = error.ERROR_NOT_FOUND_MESSAGE.format(entry_name) not_existed_id = len(seed.transcoding_job_seed_data[1]) + 1 # test function call response = transcoding_job.get_transcoding_job_by_id( transcoding_job_id=not_existed_id ) # checking assert not response assert response.status == 404 error_message = response.errors["message"] assert error_message == expected_error @db.test_schema def test_get_transcoding_job_by_id_failure() -> None: """Test get_transcoding_job_by_id db failure.""" # data preparation error_mock = SQLAlchemyError(500) package_path = "transcoding.connectors.mysql.db_session" # mocking with mock.patch(package_path, side_effect=error_mock): # test function call response = transcoding_job.get_transcoding_job_by_id(transcoding_job_id=0) # checking assert response.status == 500 @db.test_schema def test_get_transcoding_jobs_by_order_id_success() -> None: """Test successful get of transcoding_job by transcoding_order_id.""" # data preparation expected_result = seed._transcoding_jobs_for_transcoding_order_1 expected_transcoding_order_id = 1 # test function call response = transcoding_job.get_transcoding_jobs_by_order_id( transcoding_order_id=expected_transcoding_order_id ) # checking assert response assert response.status == 200 # delete unmocked TIMESTAMP actual_result = [] for entry in response.message: del entry["created_timestamp"] del entry["updated_timestamp"] actual_result.append(entry) assert actual_result == expected_result @db.test_schema def test_get_transcoding_jobs_by_order_id_failure() -> None: """Test get_transcoding_jobs_by_order_id db failure.""" # data preparation error_mock = SQLAlchemyError(500) package_path = "transcoding.connectors.mysql.db_session" # mocking with mock.patch(package_path, side_effect=error_mock): # test function call response = transcoding_job.get_transcoding_jobs_by_order_id( transcoding_order_id=0 ) # checking assert response.status == 500 @db.test_schema def test_update_transcoding_job_attempt_counter_success() -> None: """Test successful update attempt value in existing entry by id.""" # data preparation source_job = seed.transcoding_job_seed_data[0] expected_attempt_value = source_job["attempt"] + 1 # test function call response = transcoding_job.update_transcoding_job_attempt_counter( transcoding_job_id=source_job["transcoding_job_id"], attempt=expected_attempt_value, ) # checking assert response assert response.status == 200 assert response.message["transcoding_job_id"] == source_job["transcoding_job_id"] assert response.message["attempt"] == expected_attempt_value @db.test_schema def test_update_transcoding_job_attempt_counter_failure() -> None: """Test update_transcoding_job_attempt_counter db failure.""" # data preparation error_mock = SQLAlchemyError(500) package_path = "transcoding.connectors.mysql.db_session" # mocking with mock.patch(package_path, side_effect=error_mock): # test function call response = transcoding_job.update_transcoding_job_attempt_counter( transcoding_job_id=1, attempt=1 ) # checking assert response.status == 500 @db.test_schema def test_update_transcoding_job_status_success() -> None: """Test successful update status value in existing entry by id.""" # data preparation source_job = seed.transcoding_job_seed_data[0] expected_status = transcoding.PROCESSING_STATUS expected_description = "RETRYABLE_ERROR" expected_output_bucket = "bucket" expected_output_key = "filename" # test function call response = transcoding_job.update_transcoding_job_status( transcoding_job_id=source_job["transcoding_job_id"], new_status=expected_status, new_description=expected_description, output_bucket=expected_output_bucket, output_key=expected_output_key, ) # checking assert response assert response.status == 200 assert response.message["transcoding_job_id"] == source_job["transcoding_job_id"] assert response.message["status"] == expected_status assert response.message["description"] == expected_description assert response.message["output_bucket"] == expected_output_bucket assert response.message["output_key"] == expected_output_key @db.test_schema def test_update_transcoding_job_status_failure() -> None: """Test update_transcoding_job_status db failure.""" # data preparation error_mock = SQLAlchemyError(500) package_path = "transcoding.connectors.mysql.db_session" # mocking with mock.patch(package_path, side_effect=error_mock): # test function call response = transcoding_job.update_transcoding_job_status( transcoding_job_id=1, new_status=transcoding.PROCESSING_STATUS ) # checking assert response.status == 500 @db.test_schema def test_update_transcoding_job_multiple_records_failure() -> None: """Test _update_transcoding_job failure.""" # data preparation filters = [(transcoding_job.TranscodingJob.transcoding_order_id == 1)] updated_values = {"attempt": 1} # test function call response = transcoding_job._update_transcoding_job( filters=filters, updated_values=updated_values ) # checking assert not response assert response.status == 500 assert response.errors["message"] == error.ERROR_MESSAGE_TRANSCODING_JOB_NOT_UPDATED