|
| 1 | +from dotenv import load_dotenv |
| 2 | + |
| 3 | +import os |
| 4 | +from io import BytesIO |
| 5 | +from zipfile import ZipFile |
| 6 | + |
| 7 | +import pytest |
| 8 | +import lilt |
| 9 | + |
| 10 | +from tenacity import ( |
| 11 | + retry, |
| 12 | + stop_after_delay, |
| 13 | + wait_exponential, |
| 14 | + retry_if_result, |
| 15 | +) |
| 16 | + |
| 17 | +load_dotenv() |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(scope="module") |
| 21 | +def client(): |
| 22 | + configuration = lilt.Configuration( |
| 23 | + host=os.environ["API_HOST"], api_key={"key": os.environ["API_KEY"]} |
| 24 | + ) |
| 25 | + api_client = lilt.ApiClient(configuration) |
| 26 | + commit = os.environ.get("GIT_COMMIT_SHA", "no_version_available") |
| 27 | + api_client.user_agent = f"lilt-python-e2e-tests/{commit}" |
| 28 | + return api_client |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(scope="module") |
| 32 | +def api(client): |
| 33 | + return lilt.JobsApi(client) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture(scope="module") |
| 37 | +def uploaded_file(client): |
| 38 | + api = lilt.FilesApi(client) |
| 39 | + name = "test_file.txt" |
| 40 | + body = b"hello world" |
| 41 | + response = api.upload_file(name, body) |
| 42 | + yield response |
| 43 | + api.delete_file(response.id) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(scope="module") |
| 47 | +def memory(client): |
| 48 | + api = lilt.MemoriesApi(client) |
| 49 | + body = lilt.MemoryCreateParameters( |
| 50 | + name="test_memory", |
| 51 | + srclang="en", |
| 52 | + srclocale="US", |
| 53 | + trglang="de", |
| 54 | + trglocale="DE", |
| 55 | + ) |
| 56 | + response = api.create_memory(body) |
| 57 | + yield response |
| 58 | + api.delete_memory(response.id) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture(scope="module") |
| 62 | +def job(api, memory, uploaded_file): |
| 63 | + lang = lilt.LanguagePair(trg_lang=memory.trglang, memory_id=memory.id) |
| 64 | + body = lilt.JobCreateParameters( |
| 65 | + name="test_job", |
| 66 | + src_lang="en", |
| 67 | + src_locale="US", |
| 68 | + language_pairs=[lang], |
| 69 | + file_ids=[uploaded_file.id], |
| 70 | + ) |
| 71 | + response = api.create_job(body) |
| 72 | + yield response |
| 73 | + api.delete_job(response.id) |
| 74 | + |
| 75 | + |
| 76 | +def test_verified_translate_monitor_job_status(api, job): |
| 77 | + api.deliver_job(job.id) |
| 78 | + jobs = api.retrieve_all_jobs(is_delivered="true", is_archived="false") |
| 79 | + assert len(jobs) > 0 |
| 80 | + |
| 81 | + |
| 82 | +@retry( |
| 83 | + retry=retry_if_result(lambda is_processing: is_processing != 0), |
| 84 | + stop=stop_after_delay(2 * 60), |
| 85 | + wait=wait_exponential(), |
| 86 | +) |
| 87 | +def wait_for_job_export(api, job_id): |
| 88 | + job = api.get_job(job_id) |
| 89 | + return job.is_processing |
| 90 | + |
| 91 | + |
| 92 | +def test_verified_translate_download_job(api, job): |
| 93 | + api.export_job(job.id, type="files") |
| 94 | + wait_for_job_export(api, job.id) |
| 95 | + response = api.download_job(job.id, _preload_content=False) |
| 96 | + zip_file = BytesIO(response.data) |
| 97 | + with ZipFile(zip_file) as zip_ref: |
| 98 | + contents = zip_ref.infolist() |
| 99 | + assert len(contents) == 2 |
| 100 | + translated_file = contents[1] |
| 101 | + with zip_ref.open(translated_file) as tf: |
| 102 | + assert tf.read() == b"hello world" |
| 103 | + |
| 104 | + |
| 105 | +def test_verified_translate_archive_job(api, job): |
| 106 | + api.archive_job(job.id) |
| 107 | + updated_job = api.get_job(job.id) |
| 108 | + assert updated_job.status == "archived" |
0 commit comments