|
| 1 | +import pytest |
| 2 | +from uuid import uuid4 |
| 3 | +from datetime import datetime |
| 4 | +from backend.common.models.api import ( |
| 5 | + FileLog, FileRecord, FileProcessUpdate, FileProcessUpdateJSONEncoder, |
| 6 | + QueueBatch, BatchRecord, |
| 7 | + LogType, AgentType, AuthorRole, ProcessStatus, FileResult, TranslateType |
| 8 | +) |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def common_datetime(): |
| 12 | + return datetime.now() |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def uuid_pair(): |
| 16 | + return str(uuid4()), str(uuid4()) |
| 17 | + |
| 18 | +def test_filelog_fromdb_and_dict(uuid_pair, common_datetime): |
| 19 | + log_id, file_id = uuid_pair |
| 20 | + data = { |
| 21 | + "log_id": log_id, |
| 22 | + "file_id": file_id, |
| 23 | + "description": "test log", |
| 24 | + "last_candidate": "some_candidate", |
| 25 | + "log_type": "SUCCESS", |
| 26 | + "agent_type": "migrator", |
| 27 | + "author_role": "user", |
| 28 | + "timestamp": common_datetime.isoformat(), |
| 29 | + } |
| 30 | + log = FileLog.fromdb(data) |
| 31 | + assert log.log_id.hex == log_id.replace("-", "") |
| 32 | + assert log.dict()["log_type"] == "info" |
| 33 | + |
| 34 | + assert log.dict()["author_role"] == "user" |
| 35 | + |
| 36 | +def test_filerecord_fromdb_and_dict(uuid_pair, common_datetime): |
| 37 | + file_id, batch_id = uuid_pair |
| 38 | + data = { |
| 39 | + "file_id": file_id, |
| 40 | + "batch_id": batch_id, |
| 41 | + "original_name": "file.sql", |
| 42 | + "blob_path": "/blob/file.sql", |
| 43 | + "translated_path": "/translated/file.sql", |
| 44 | + "status": "in_progress", |
| 45 | + "file_result": "warning", |
| 46 | + "error_count": 2, |
| 47 | + "syntax_count": 5, |
| 48 | + "created_at": common_datetime.isoformat(), |
| 49 | + "updated_at": common_datetime.isoformat(), |
| 50 | + } |
| 51 | + record = FileRecord.fromdb(data) |
| 52 | + assert record.file_id.hex == file_id.replace("-", "") |
| 53 | + assert record.dict()["status"] == "ready_to_process" |
| 54 | + assert record.dict()["file_result"] == "warning" |
| 55 | + |
| 56 | +def test_fileprocessupdate_dict(uuid_pair): |
| 57 | + file_id, batch_id = uuid_pair |
| 58 | + update = FileProcessUpdate( |
| 59 | + file_id=file_id, |
| 60 | + batch_id=batch_id, |
| 61 | + process_status=ProcessStatus.COMPLETED, |
| 62 | + file_result=FileResult.SUCCESS, |
| 63 | + agent_type=AgentType.FIXER, |
| 64 | + agent_message="Translation done", |
| 65 | + ) |
| 66 | + result = update.dict() |
| 67 | + assert result["process_status"] == "completed" |
| 68 | + assert result["file_result"] == "success" |
| 69 | + assert result["agent_type"] == "fixer" |
| 70 | + assert result["agent_message"] == "Translation done" |
| 71 | + |
| 72 | +def test_fileprocessupdate_json_encoder(uuid_pair): |
| 73 | + file_id, batch_id = uuid_pair |
| 74 | + update = FileProcessUpdate( |
| 75 | + file_id=file_id, |
| 76 | + batch_id=batch_id, |
| 77 | + process_status=ProcessStatus.FAILED, |
| 78 | + file_result=FileResult.ERROR, |
| 79 | + agent_type=AgentType.HUMAN, |
| 80 | + agent_message="Something failed", |
| 81 | + ) |
| 82 | + json_string = FileProcessUpdateJSONEncoder().encode(update) |
| 83 | + assert "failed" in json_string |
| 84 | + assert "human" in json_string |
| 85 | + |
| 86 | +def test_queuebatch_dict(uuid_pair, common_datetime): |
| 87 | + batch_id, _ = uuid_pair |
| 88 | + batch = QueueBatch( |
| 89 | + batch_id=batch_id, |
| 90 | + user_id="user123", |
| 91 | + translate_from="en", |
| 92 | + translate_to="tsql", |
| 93 | + created_at=common_datetime, |
| 94 | + updated_at=common_datetime, |
| 95 | + status=ProcessStatus.IN_PROGRESS, |
| 96 | + ) |
| 97 | + result = batch.dict() |
| 98 | + assert result["status"] == "in_process" |
| 99 | + assert result["user_id"] == "user123" |
| 100 | + |
| 101 | +def test_batchrecord_fromdb_and_dict(uuid_pair, common_datetime): |
| 102 | + batch_id, _ = uuid_pair |
| 103 | + data = { |
| 104 | + "batch_id": batch_id, |
| 105 | + "user_id": "user123", |
| 106 | + "file_count": 3, |
| 107 | + "created_at": common_datetime.isoformat(), |
| 108 | + "updated_at": common_datetime.isoformat(), |
| 109 | + "status": "completed", |
| 110 | + "from_language": "Informix", |
| 111 | + "to_language": "T-SQL" |
| 112 | + } |
| 113 | + record = BatchRecord.fromdb(data) |
| 114 | + assert record.status == ProcessStatus.COMPLETED |
| 115 | + assert record.from_language == TranslateType.INFORMIX |
| 116 | + assert record.to_language == TranslateType.TSQL |
| 117 | + assert record.dict()["status"] == "completed" |
0 commit comments