66
77import boto3
88import pytest
9- from sqlmodel import Session , select
109from moto import mock_aws
10+ from sqlmodel import Session , select
11+ from fastapi .testclient import TestClient
1112
1213from app .core .cloud import AmazonCloudStorageClient
1314from app .core .config import settings
1415from app .models import Document
1516from 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
3334def scratch ():
@@ -39,6 +40,10 @@ def scratch():
3940def 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' )
4348def 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