Skip to content

Commit 3293d07

Browse files
committed
Use object to upload documents
1 parent 85b07be commit 3293d07

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

backend/app/tests/api/routes/documents/test_upload.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,29 @@
66

77
import boto3
88
import pytest
9-
from sqlmodel import Session, select
109
from moto import mock_aws
10+
from sqlmodel import Session, select
11+
from fastapi.testclient import TestClient
1112

1213
from app.core.cloud import AmazonCloudStorageClient
1314
from app.core.config import settings
1415
from app.models import Document
1516
from app.tests.utils.document import (
1617
Route,
1718
WebCrawler,
18-
crawler,
1919
)
2020

21-
def upload(route: Route, scratch: Path, crawler: WebCrawler):
22-
(mtype, _) = mimetypes.guess_type(str(scratch))
23-
with scratch.open('rb') as fp:
24-
return crawler.client.post(
25-
str(route),
26-
headers=crawler.superuser_token_headers,
27-
files={
28-
'src': (str(scratch), fp, mtype),
29-
},
30-
)
21+
class WebUploader(WebCrawler):
22+
def put(self, route: Route, scratch: Path):
23+
(mtype, _) = mimetypes.guess_type(str(scratch))
24+
with scratch.open('rb') as fp:
25+
return self.client.post(
26+
str(route),
27+
headers=self.superuser_token_headers,
28+
files={
29+
'src': (str(scratch), fp, mtype),
30+
},
31+
)
3132

3233
@pytest.fixture
3334
def scratch():
@@ -39,6 +40,10 @@ def scratch():
3940
def route():
4041
return Route('cp')
4142

43+
@pytest.fixture
44+
def uploader(client: TestClient, superuser_token_headers: dict[str, str]):
45+
return WebUploader(client, superuser_token_headers)
46+
4247
@pytest.fixture(scope='class')
4348
def aws_setup():
4449
os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
@@ -55,12 +60,12 @@ def test_adds_to_database(
5560
db: Session,
5661
route: Route,
5762
scratch: Path,
58-
crawler: WebCrawler,
63+
uploader: WebUploader,
5964
):
6065
aws = AmazonCloudStorageClient()
6166
aws.create()
6267

63-
response = upload(route, scratch, crawler)
68+
response = uploader.put(route, scratch)
6469
doc_id = (response
6570
.json()
6671
.get('id'))
@@ -76,12 +81,12 @@ def test_adds_to_S3(
7681
self,
7782
route: Route,
7883
scratch: Path,
79-
crawler: Route,
84+
uploader: WebUploader,
8085
):
8186
aws = AmazonCloudStorageClient()
8287
aws.create()
8388

84-
response = upload(route, scratch, crawler)
89+
response = uploader.put(route, scratch)
8590
url = urlparse(response.json().get('object_store_url'))
8691
key = Path(url.path)
8792
key = key.relative_to(key.root)

0 commit comments

Comments
 (0)