Skip to content

Commit 81aa2ef

Browse files
committed
test(files): update
1 parent 8400caf commit 81aa2ef

File tree

6 files changed

+49
-55
lines changed

6 files changed

+49
-55
lines changed

QA/py/tests/api_wrappers.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,19 @@ def api_check_job_failed(fastapi, job_id, expected_message):
8787

8888

8989
REMOVE_FILE_URL = "/user_files/rm/"
90-
UPLOAD_FILE_URL = "/user_files/"
90+
MY_FILES_URL = "/user_files/"
91+
92+
93+
def upload_file(fastapi, local_file, dest_path, auth):
94+
with open(local_file, "rb") as fin:
95+
upload_rsp = fastapi.post(
96+
MY_FILES_URL,
97+
headers=auth,
98+
data={
99+
"path": dest_path
100+
}, # /!\ If no pathparam error-> random use-once directory!
101+
files={"file": fin},
102+
)
103+
assert upload_rsp.status_code == 200
104+
srv_file_path = upload_rsp.json()
105+
return srv_file_path

QA/py/tests/holes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from tests.credentials import USER_AUTH, CREATOR_AUTH
1111
from tests.test_classification import OBJECT_SET_SUMMARY_URL
1212
from tests.test_import import PLAIN_FILE_PATH, do_test_import
13-
from tests.api_wrappers import UPLOAD_FILE_URL
13+
from tests.api_wrappers import MY_FILES_URL
1414

1515
MYFILES_URL = "/my_files/{sub_path}"
1616
COMMON_FILES_URL = "/common_files/?path={sub_path}"
@@ -58,7 +58,7 @@ def put_path(fastapi, path1="file", path2=None, tag=None, should_fail=False):
5858
files_params = {"file": (path1, fin)}
5959
params = {"path": path2, "tag": tag}
6060
upload_rsp = fastapi.post(
61-
UPLOAD_FILE_URL, headers=CREATOR_AUTH, data=params, files=files_params
61+
MY_FILES_URL, headers=CREATOR_AUTH, data=params, files=files_params
6262
)
6363
if upload_rsp.status_code == 200:
6464
assert not should_fail

QA/py/tests/test_import.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
api_file_import,
2626
api_wait_for_stable_job,
2727
api_check_job_questions,
28-
UPLOAD_FILE_URL,
28+
MY_FILES_URL,
2929
REMOVE_FILE_URL,
30+
upload_file,
3031
)
3132
from tests.credentials import ADMIN_AUTH, ADMIN_USER_ID, CREATOR_USER_ID, CREATOR_AUTH
3233
from tests.jobs import (
@@ -655,16 +656,10 @@ def test_import_breaking_unicity(fastapi):
655656
def test_uvp6_via_myfile(fastapi, caplog):
656657
prj_id = create_project(CREATOR_USER_ID, "UVP6 via MyFiles")
657658
local_file = SHARED_DIR / V6_FILE
658-
with open(local_file, "rb") as fin:
659-
upload_rsp = fastapi.post(
660-
UPLOAD_FILE_URL,
661-
headers=CREATOR_AUTH,
662-
files={"file": fin},
663-
)
664-
assert upload_rsp.status_code == 200
659+
upload_file(fastapi, local_file, local_file.name, CREATOR_AUTH)
665660
no_zip = local_file.name.replace(".zip", "")
666661

667-
list_rsp = fastapi.get(f"{UPLOAD_FILE_URL}", headers=CREATOR_AUTH)
662+
list_rsp = fastapi.get(f"{MY_FILES_URL}", headers=CREATOR_AUTH)
668663
assert list_rsp.status_code == 200
669664
user_files = list_rsp.json()
670665
assert no_zip in [a_file["name"] for a_file in user_files["entries"]]

QA/py/tests/test_import_simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from tests.credentials import ADMIN_USER_ID, CREATOR_AUTH, CREATOR_USER_ID
2626
from tests.test_import import PLAIN_DIR, create_project, PLAIN_FILE_PATH
2727
from tests.jobs import check_job_ok
28-
from tests.api_wrappers import api_wait_for_stable_job, UPLOAD_FILE_URL
28+
from tests.api_wrappers import api_wait_for_stable_job, MY_FILES_URL
2929

3030
IMPORT_IMAGES_URL = "/simple_import/{project_id}?dry_run={dry_run}"
3131

@@ -125,7 +125,7 @@ def do_simple_import_images(fastapi, title):
125125
prj_id = create_project(CREATOR_USER_ID, title)
126126
with open(PLAIN_FILE_PATH, "rb") as fin:
127127
upload_rsp = fastapi.post(
128-
UPLOAD_FILE_URL,
128+
MY_FILES_URL,
129129
headers=CREATOR_AUTH,
130130
files={"file": fin},
131131
)

QA/py/tests/test_my_files.py

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55
# Exhibit some not-so-intuitive behavior of /my_files and associated upload
66
#
77

8-
import logging
98
import shutil
109
from typing import Dict
1110

1211
import pytest
1312
from starlette import status
1413

1514
from tests.credentials import CREATOR_AUTH, CREATOR_USER_ID
16-
from tests.jobs import (
17-
api_check_job_ok,
18-
)
1915
from tests.api_wrappers import (
2016
api_file_import,
2117
api_wait_for_stable_job,
2218
api_check_job_errors,
23-
UPLOAD_FILE_URL,
19+
MY_FILES_URL,
20+
upload_file,
21+
api_check_job_ok,
2422
)
2523
from tests.test_import import (
2624
SHARED_DIR,
@@ -41,20 +39,33 @@ def test_my_files(fastapi, tstlogs, title):
4139

4240
DEST_FILE_NAME = "LOKI_46-24hours_01.zip"
4341
DEST_DIR_NAME = "LOKI_46-24hours_01"
42+
4443
# Copy an existing test file into current dir, simulating client side
4544
shutil.copyfile(SHARED_DIR / V6_FILE, tstlogs / DEST_FILE_NAME)
4645
# Upload this file
47-
upload_file(fastapi, DEST_FILE_NAME, DIRPATH + "/" + DEST_FILE_NAME, tstlogs)
46+
remote_path = upload_file(
47+
fastapi,
48+
tstlogs / DEST_FILE_NAME,
49+
DIRPATH + SEPARATOR + DEST_FILE_NAME,
50+
CREATOR_AUTH,
51+
)
52+
assert DIRPATH in remote_path # The subdirectory was created
4853

4954
# And another
5055
DEST_FILE_NAME2 = "readme.txt"
5156
# Copy an existing test file into current dir, simulating client side
5257
shutil.copyfile(SHARED_DIR / "HOWTO.txt", tstlogs / DEST_FILE_NAME2)
5358
# Upload this file
54-
upload_file(fastapi, DEST_FILE_NAME2, DIRPATH + "/" + DEST_FILE_NAME2, tstlogs)
59+
upload_file(
60+
fastapi,
61+
tstlogs / DEST_FILE_NAME2,
62+
DIRPATH + SEPARATOR + DEST_FILE_NAME2,
63+
CREATOR_AUTH,
64+
)
65+
assert DIRPATH in remote_path # The subdirectory was created
5566

5667
# The pathparam becomes a top-level directory
57-
list_rsp = fastapi.get(UPLOAD_FILE_URL + SEPARATOR, headers=CREATOR_AUTH)
68+
list_rsp = fastapi.get(MY_FILES_URL + SEPARATOR, headers=CREATOR_AUTH)
5869
assert list_rsp.status_code == 200
5970
my_files_root: Dict = list_rsp.json()
6071
assert my_files_root["path"] == ""
@@ -73,17 +84,13 @@ def test_my_files(fastapi, tstlogs, title):
7384

7485
# The files are stored in the subdirectory
7586
DIRDEST = DIRPATH + SEPARATOR + DEST_DIR_NAME
76-
list_rsp = fastapi.get(UPLOAD_FILE_URL + SEPARATOR + DIRDEST, headers=CREATOR_AUTH)
87+
list_rsp = fastapi.get(MY_FILES_URL + SEPARATOR + DIRDEST, headers=CREATOR_AUTH)
7788
assert list_rsp.status_code == 200
7889
my_files_subdir: Dict = list_rsp.json()
7990
assert my_files_subdir["path"] == DIRDEST
8091
assert (
8192
len(my_files_subdir["entries"]) == 1
8293
) # The second file being .txt will have size 0 on re-read
83-
# test no longer valid as the zip is deleted after successful unzip
84-
# the_file = [fil for fil in my_files_subdir["entries"] if fil["size"] == 22654][0]
85-
# del the_file["mtime"] # Unpredictable
86-
# assert the_file == {"name": DEST_FILE_NAME, "size": 22654, "type": "F"}
8794

8895
# Import the file without the right path -> Error
8996
req = {"source_path": DEST_FILE_NAME}
@@ -94,33 +101,9 @@ def test_my_files(fastapi, tstlogs, title):
94101
errors = api_check_job_errors(fastapi, job_id)
95102
assert "FileNotFoundError" in "".join(errors)
96103

97-
# Import the file with the right path
98-
# The below (unfortunately) hard-coded path is valid on the current configuration of EcoTaxa
99-
file_path = "/tmp/ecotaxa_user.{}/{}/{}".format(
100-
CREATOR_USER_ID, # Should come from /api/users/me
101-
DIRPATH, # existing tag, created the on the first file creation with it
102-
DEST_FILE_NAME, # can come from an entry in GET /my_files/DIRPATH
103-
)
104-
req = {"source_path": file_path}
104+
req = {"source_path": DIRDEST}
105105
rsp = api_file_import(fastapi, prj_id, req, auth=CREATOR_AUTH)
106106
assert rsp.status_code == status.HTTP_200_OK
107-
job_id = rsp.json()["job_id"]
108-
api_wait_for_stable_job(fastapi, job_id)
109-
# api_check_job_ok(fastapi, job_id) # TODO: It's _not_ anymore the right path
110-
111-
112-
def upload_file(fastapi, dest_file_name, pathparam, tstlogs):
113-
print("uploadfile----------" + str(dest_file_name), pathparam)
114-
with open(tstlogs / dest_file_name, "rb") as fin:
115-
upload_rsp = fastapi.post(
116-
UPLOAD_FILE_URL,
117-
headers=CREATOR_AUTH,
118-
data={
119-
"path": pathparam
120-
}, # /!\ If no pathparam error-> random use-once directory!
121-
files={"file": fin},
122-
)
123-
assert upload_rsp.status_code == 200
124-
srv_file_path = upload_rsp.json()
125-
print("srv_file", srv_file_path)
126-
assert DIRPATH in srv_file_path
107+
job = api_wait_for_stable_job(fastapi, rsp.json()["job_id"])
108+
api_wait_for_stable_job(fastapi, job.id)
109+
api_check_job_ok(fastapi, job.id)

py/BO/Bundle.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import shutil
88
import zipfile
99
from pathlib import Path
10+
1011
# noinspection PyPackageRequirements
1112
from typing import Callable, List, Dict, Tuple, Generator, Set
1213

0 commit comments

Comments
 (0)