Skip to content

Commit e47b8b1

Browse files
Added test clean up, PAT token to openapi generation (#52)
* Added test clean up, PAT token to openapi generation * Fixed return, removed unnecessary print
1 parent 0c5fa2e commit e47b8b1

File tree

5 files changed

+118
-84
lines changed

5 files changed

+118
-84
lines changed

.github/workflows/openapi.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ jobs:
3535
- name: Create Pull Request
3636
uses: peter-evans/[email protected]
3737
with:
38+
token: ${{ secrets.GHA_REPO_DISPATCH_TOKEN }}
3839
commit-message: Regenerates API Bindings
3940
title: "OpenAPI: Regenerates API Bindings"
4041
body: |

workflow_tests/test_create_content.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88

99
load_dotenv()
1010

11-
configuration = lilt.Configuration(
12-
host=os.environ["API_HOST"],
13-
api_key={
14-
"key": os.environ["API_KEY"]
15-
}
16-
)
1711

1812
sign_cases = [
1913
"none",
@@ -174,17 +168,35 @@ def assert_response(create_content_obj, expected):
174168
assert template_params.summary == expected["template_params"]["summary"]
175169

176170

177-
@pytest.mark.parametrize("sign_case", sign_cases)
178-
def test_sign(sign_case):
171+
@pytest.fixture(scope="module")
172+
def client():
173+
configuration = lilt.Configuration(
174+
host=os.environ["API_HOST"], api_key={"key": os.environ["API_KEY"]}
175+
)
179176
api_client = lilt.ApiClient(configuration)
177+
commit = os.environ.get("GIT_COMMIT_SHA", "no_version_available")
178+
api_client.user_agent = f"lilt-python-e2e-tests/{commit}"
179+
return api_client
180+
181+
182+
@pytest.fixture(scope="module")
183+
def create_api(client):
184+
return lilt.CreateApi(client)
185+
180186

181-
# Sign terms and conditions
182-
api_instance = lilt.CreateApi(api_client)
187+
@pytest.fixture(scope="function")
188+
def sign(create_api):
189+
signed_agreement = lilt.CreateConverterConfigParameters(True)
190+
return create_api.sign_lilt_create_terms(signed_agreement)
191+
192+
193+
@pytest.mark.parametrize("sign_case", sign_cases)
194+
def test_sign(sign_case, create_api):
183195
sign = get_sign(sign_case)
184196

185197
try:
186198
signed_agreement = lilt.CreateConverterConfigParameters(sign)
187-
api_response = api_instance.sign_lilt_create_terms(signed_agreement)
199+
api_response = create_api.sign_lilt_create_terms(signed_agreement)
188200
assert api_response.signed_agreement == bool(sign)
189201
except ValueError as e:
190202
print("Exception when calling CreateApi->sign_lilt_create_terms: %s\n" % e)
@@ -193,14 +205,8 @@ def test_sign(sign_case):
193205

194206

195207
@pytest.mark.parametrize("char_case", generate_content_char_cases)
196-
def test_create_content_chars(char_case):
197-
api_client = lilt.ApiClient(configuration)
198-
199-
# Sign agreement
200-
api_instance = lilt.CreateApi(api_client)
201-
signed_agreement = lilt.CreateConverterConfigParameters(True)
202-
api_response = api_instance.sign_lilt_create_terms(signed_agreement)
203-
assert api_response.signed_agreement
208+
def test_create_content_chars(create_api, sign, char_case):
209+
assert sign.signed_agreement
204210

205211
# Generate content
206212
template_params = lilt.LiltCreateContentTemplateParams(
@@ -219,22 +225,18 @@ def test_create_content_chars(char_case):
219225
preferences=preferences
220226
)
221227

222-
api_instance.generate_lilt_create_content(request_body)
228+
create_api.generate_lilt_create_content(request_body)
223229
time.sleep(5)
224-
api_response = api_instance.get_lilt_create_content()
230+
api_response = create_api.get_lilt_create_content()
225231
latest_content = api_response.contents[-1]
226232
assert_response(latest_content, expected_chars(char_case))
227233

234+
create_api.delete_lilt_create_content(latest_content.id)
228235

229-
@pytest.mark.parametrize("section_case", generate_content_sections_cases)
230-
def test_create_content_sections(section_case):
231-
api_client = lilt.ApiClient(configuration)
232236

233-
# Sign agreement
234-
api_instance = lilt.CreateApi(api_client)
235-
signed_agreement = lilt.CreateConverterConfigParameters(True)
236-
api_response = api_instance.sign_lilt_create_terms(signed_agreement)
237-
assert api_response.signed_agreement
237+
@pytest.mark.parametrize("section_case", generate_content_sections_cases)
238+
def test_create_content_sections(create_api, sign, section_case):
239+
assert sign.signed_agreement
238240

239241
# Generate content
240242
template_params = lilt.LiltCreateContentTemplateParams(
@@ -253,8 +255,10 @@ def test_create_content_sections(section_case):
253255
preferences=preferences
254256
)
255257

256-
api_instance.generate_lilt_create_content(request_body)
258+
create_api.generate_lilt_create_content(request_body)
257259
time.sleep(5)
258-
api_response = api_instance.get_lilt_create_content()
260+
api_response = create_api.get_lilt_create_content()
259261
latest_content = api_response.contents[-1]
260262
assert_response(latest_content, expected_section(section_case))
263+
264+
create_api.delete_lilt_create_content(latest_content.id)

workflow_tests/test_data_source.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@
1313
load_dotenv()
1414

1515

16-
configuration = lilt.Configuration(
17-
host=os.environ["API_HOST"],
18-
api_key={
19-
"key": os.environ["API_KEY"]
20-
}
21-
)
22-
2316
create_data_source_cases = [
2417
"none_src",
2518
"none_trg",
@@ -205,26 +198,36 @@ def assert_query_response(query_object, expected):
205198
assert query_object.source == expected["source"]
206199
assert query_object.target == expected["target"]
207200

201+
@pytest.fixture(scope="module")
202+
def client():
203+
configuration = lilt.Configuration(
204+
host=os.environ["API_HOST"], api_key={"key": os.environ["API_KEY"]}
205+
)
206+
api_client = lilt.ApiClient(configuration)
207+
commit = os.environ.get("GIT_COMMIT_SHA", "no_version_available")
208+
api_client.user_agent = f"lilt-python-e2e-tests/{commit}"
209+
return api_client
210+
211+
212+
@pytest.fixture(scope="module")
213+
def memories_api(client):
214+
return lilt.MemoriesApi(client)
215+
208216

209217
@retry(
210218
retry=retry_if_result(lambda is_processing: is_processing == 1),
211219
stop=stop_after_delay(2 * 60),
212220
wait=wait_exponential()
213221
)
214-
def monitor_file_import(api_instance, memory_id):
215-
api_response = api_instance.get_memory()
222+
def monitor_file_import(memories_api, memory_id):
223+
api_response = memories_api.get_memory()
216224
is_processing = api_response[0].is_processing
217225
print(f"is_processing: {is_processing}")
218226
return is_processing
219227

220228

221229
@pytest.mark.parametrize("data_source_case", create_data_source_cases)
222-
def test_create_data_source_workflow(data_source_case):
223-
api_client = lilt.ApiClient(configuration)
224-
225-
# Create data source
226-
api_instance = lilt.MemoriesApi(api_client)
227-
230+
def test_create_data_source_workflow(memories_api, data_source_case):
228231
try:
229232
data_source_parameters = get_data_source_parameters(data_source_case)
230233
body = lilt.MemoryCreateParameters(
@@ -242,9 +245,11 @@ def test_create_data_source_workflow(data_source_case):
242245
raise e
243246

244247
try:
245-
api_response = api_instance.create_memory(body)
248+
api_response = memories_api.create_memory(body)
246249
pprint(api_response)
247250
assert_data_source_response(api_response, get_expected_data_source(data_source_case))
251+
252+
memories_api.delete_memory(api_response.id)
248253
except ApiException as e:
249254
print("Exception when calling MemoriesApi->create_memory: %s\n" % e)
250255
if data_source_case == "unsupported_languages":
@@ -253,12 +258,8 @@ def test_create_data_source_workflow(data_source_case):
253258
raise e
254259

255260

256-
@pytest.mark.parametrize("tmx_file_case", tmx_file_cases)
257-
def test_upload_tmx_file_workflow(tmx_file_case):
258-
api_client = lilt.ApiClient(configuration)
259-
260-
# Create data source
261-
api_instance = lilt.MemoriesApi(api_client)
261+
@pytest.fixture(scope="function")
262+
def create_fr_to_en_memory(memories_api):
262263
data_source_parameters = get_data_source_parameters("fr_to_en")
263264
body = lilt.MemoryCreateParameters(
264265
name=data_source_parameters["name"],
@@ -267,20 +268,26 @@ def test_upload_tmx_file_workflow(tmx_file_case):
267268
srclocale=data_source_parameters["srclocale"],
268269
trglocale=data_source_parameters["trglocale"]
269270
)
270-
api_response = api_instance.create_memory(body)
271+
api_response = memories_api.create_memory(body)
271272
assert_data_source_response(api_response, get_expected_data_source("fr_to_en"))
272-
memory_id = api_response.id
273+
print(f"Memory ID: {api_response.id}")
274+
yield api_response
275+
memories_api.delete_memory(api_response.id)
276+
277+
278+
@pytest.mark.parametrize("tmx_file_case", tmx_file_cases)
279+
def test_upload_tmx_file_workflow(memories_api, create_fr_to_en_memory, tmx_file_case):
280+
memory_id = create_fr_to_en_memory.id
273281
print(f"Memory ID: {memory_id}")
274282

275283
# Upload file
276-
print(os.getcwd())
277284
tmx_settings = get_tmx_settings(tmx_file_case)
278285
upload_file = open(tmx_settings["body"], "rb")
279286
body = upload_file.read()
280287
print("-----FILE START-----")
281288
print(body)
282289
print("-----FILE END-----")
283-
api_response = api_instance.import_memory_file(
290+
api_response = memories_api.import_memory_file(
284291
memory_id=memory_id,
285292
name=tmx_settings["name"],
286293
body=body
@@ -290,13 +297,14 @@ def test_upload_tmx_file_workflow(tmx_file_case):
290297
assert api_response.is_processing == 1
291298

292299
# Monitor file import
293-
is_processing = monitor_file_import(api_instance, memory_id)
300+
is_processing = monitor_file_import(memories_api, memory_id)
294301

295302
# Query memory
296303
query = "chatte"
297-
api_response = api_instance.query_memory(memory_id, query)
304+
api_response = memories_api.query_memory(memory_id, query)
298305
if tmx_file_case == "wrong_data":
299306
assert api_response == []
300307
else:
301308
assert len(api_response) > 0
302309
assert_query_response(api_response[0], get_expected_query())
310+

workflow_tests/test_instant_translate.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99

1010
load_dotenv()
1111

12-
configuration = lilt.Configuration(
13-
host=os.environ["API_HOST"],
14-
api_key={
15-
"key": os.environ["API_KEY"]
16-
}
17-
)
18-
1912
test_cases = [
2013
"fr_to_en"
2114
]
@@ -41,44 +34,63 @@ def assert_translate_response(response, file_id, memory_id):
4134
assert response.created_at is not None
4235

4336

37+
@pytest.fixture(scope="module")
38+
def client():
39+
configuration = lilt.Configuration(
40+
host=os.environ["API_HOST"], api_key={"key": os.environ["API_KEY"]}
41+
)
42+
api_client = lilt.ApiClient(configuration)
43+
commit = os.environ.get("GIT_COMMIT_SHA", "no_version_available")
44+
api_client.user_agent = f"lilt-python-e2e-tests/{commit}"
45+
return api_client
46+
47+
48+
@pytest.fixture(scope="module")
49+
def files_api(client):
50+
return lilt.FilesApi(client)
51+
52+
@pytest.fixture(scope="module")
53+
def translate_api(client):
54+
return lilt.TranslateApi(client)
55+
56+
4457
@retry(
4558
retry=retry_if_result(lambda status: status != "ReadyForDownload"),
4659
stop=stop_after_delay(2 * 60),
4760
wait=wait_exponential()
4861
)
49-
def monitor_file_translation(translate_instance, translation_id, file_id, memory_id):
50-
api_response = translate_instance.monitor_file_translation(translation_ids=translation_id)
62+
def monitor_file_translation(translate_api, translation_id, file_id, memory_id):
63+
api_response = translate_api.monitor_file_translation(translation_ids=translation_id)
5164
translation_response = api_response[0]
5265
translation_status = translation_response.status
5366
print(f"STATUS: {translation_status}")
5467
assert_translate_response(translation_response, file_id, memory_id)
5568
return translation_status
5669

57-
58-
@pytest.mark.parametrize("test_case", test_cases)
59-
def test_instant_translate_workflow(test_case):
60-
api_client = lilt.ApiClient(configuration)
61-
62-
# Upload file
63-
files_instance = lilt.FilesApi(api_client)
70+
@pytest.fixture(scope="module")
71+
def upload_file(files_api):
6472
name = "translate-fr_to_en.txt"
6573
upload_file = open(f"{translate_file_path}/translate-fr_to_en.txt", "rb")
6674
body = upload_file.read()
6775

68-
api_response = files_instance.upload_file(
76+
api_response = files_api.upload_file(
6977
name,
7078
body
7179
)
7280
assert_upload_response(api_response)
73-
file_id = api_response.id
81+
yield api_response
82+
files_api.delete_file(api_response.id)
83+
84+
85+
@pytest.mark.parametrize("test_case", test_cases)
86+
def test_instant_translate_workflow(upload_file, translate_api, test_case):
87+
file_id = upload_file.id
7488

75-
# Translate file
76-
translate_instance = lilt.TranslateApi(api_client)
7789
memory_id = STAGING_MEMORY_ID
7890
print(f"FILE ID: {file_id}")
7991
print(f"MEMORY ID: {memory_id}")
8092

81-
api_response = translate_instance.batch_translate_file(file_id, memory_id)
93+
api_response = translate_api.batch_translate_file(file_id, memory_id)
8294
translation_response = api_response[0]
8395
translation_id = translation_response.id
8496
translation_status = translation_response.status
@@ -87,7 +99,7 @@ def test_instant_translate_workflow(test_case):
8799
# Monitor translation
88100
try:
89101
translation_status = monitor_file_translation(
90-
translate_instance,
102+
translate_api,
91103
translation_id,
92104
file_id,
93105
memory_id
@@ -98,6 +110,6 @@ def test_instant_translate_workflow(test_case):
98110
print("Translation exceeding time limit. Switching to finished translation.")
99111

100112
# Download translated file
101-
api_response = translate_instance.download_file(translation_id)
113+
api_response = translate_api.download_file(translation_id)
102114
assert "cat" in api_response
103115
assert "hello" in api_response.lower()

workflow_tests/test_verified_translate.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,18 @@ def job(api, memory, uploaded_file):
7373
api.delete_job(response.id)
7474

7575

76+
@retry(
77+
retry=retry_if_result(lambda jobs: len(jobs) == 0),
78+
stop=stop_after_delay(2 * 60),
79+
wait=wait_exponential(),
80+
)
81+
def wait_for_job_creation(api, job_id):
82+
api.deliver_job(job_id)
83+
return api.retrieve_all_jobs(is_delivered="true", is_archived="false")
84+
85+
7686
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")
87+
jobs = wait_for_job_creation(api, job.id)
7988
assert len(jobs) > 0
8089

8190

0 commit comments

Comments
 (0)