33import tempfile
44import uuid
55from pathlib import Path
6+ from unittest .mock import MagicMock
67
7- import responses
8+ import pytest
9+ import respx
10+ from httpx import Response
811
9- from api .constants import ARCSECOND_API_URL_DEV
1012from arcsecond import (
11- AllSkyCameraImageFileUploader ,
12- AllSkyCameraImageUploadContext ,
13- ArcsecondConfig ,
1413 DatasetFileUploader ,
1514 DatasetUploadContext ,
1615)
1716from arcsecond .cloud .uploader .constants import Status , Substatus
18- from arcsecond .options import State
1917from tests .utils import (
2018 prepare_successful_login ,
21- prepare_upload_allskyimage ,
22- prepare_upload_files ,
19+ prepare_upload_files , random_string ,
2320)
2421
2522
26- @responses .activate
27- def test_full_upload_process_datafiles ():
23+ @pytest .fixture
24+ def mock_config ():
25+ """Create a mock ArcsecondConfig."""
26+ config = MagicMock ()
27+ config .username = "test_user"
28+ config .upload_key = "test_key"
29+ config .api_name = random_string ()
30+ config .api_server = "http://mock.example.com"
31+ config .access_key = None # very important, because access_key takes precedence on upload_key in headers setting.
32+ config .upload_key = '1234567890'
33+ return config
34+
35+
36+ @respx .mock
37+ def test_full_upload_process_datafiles (mock_config ):
2838 dataset_uuid = str (uuid .uuid4 ())
2939 telescope_uuid = str (uuid .uuid4 ())
3040 org_subdomain = "test-portal"
3141
32- prepare_successful_login (org_subdomain )
33- prepare_upload_files (dataset_uuid , telescope_uuid , org_subdomain )
42+ prepare_successful_login (mock_config , org_subdomain )
43+ prepare_upload_files (mock_config , dataset_uuid , telescope_uuid , org_subdomain )
3444
35- # file upload
36- datafile_id = random .randint (1 , 1000 )
37- responses .post (
38- "/" .join ([ARCSECOND_API_URL_DEV , org_subdomain , "datafiles" ]) + "/" ,
39- status = 201 ,
40- json = {"status" : "success" , "id" : datafile_id },
41- )
42- # update metadata
43- responses .patch (
44- "/" .join ([ARCSECOND_API_URL_DEV , org_subdomain , "datafiles" , str (datafile_id )])
45- + "/" ,
46- status = 200 ,
47- json = {"id" : datafile_id },
48- )
45+ respx .get (
46+ "/" .join ([mock_config .api_server , org_subdomain , "datasets" , dataset_uuid ]) + "/"
47+ ).mock (Response (201 , json = {"name" : "dummy" , "uuid" : dataset_uuid }))
4948
50- state = State (verbose = False , api_name = "cloud" )
51- config = {
52- "cloud" : {
53- "username" : "dummy" ,
54- "upload_key" : "1234" ,
55- "api_server" : ARCSECOND_API_URL_DEV ,
56- }
57- }
58- config = ArcsecondConfig (state , config ) # it will read your config file.
49+ datafile_id = random .randint (1 , 1000 )
50+ respx .post (
51+ "/" .join ([mock_config .api_server , org_subdomain , "datafiles" ]) + "/"
52+ ).mock (Response (201 , json = {"status" : "success" , "id" : datafile_id }))
5953
6054 context = DatasetUploadContext (
61- config ,
55+ mock_config ,
6256 input_dataset_uuid_or_name = dataset_uuid ,
6357 input_telescope_uuid = telescope_uuid ,
6458 is_raw_data = True ,
@@ -84,54 +78,3 @@ def test_full_upload_process_datafiles():
8478 assert status .value == Status .OK .value
8579 assert substatus .value == Substatus .DONE .value
8680 assert error is None
87-
88-
89- @responses .activate
90- def test_full_upload_process_allskyimages ():
91- camera_uuid = str (uuid .uuid4 ())
92- org_subdomain = "test-portal"
93-
94- prepare_successful_login (org_subdomain )
95- prepare_upload_allskyimage (camera_uuid , org_subdomain )
96-
97- # file upload
98- image_id = random .randint (1 , 1000 )
99- responses .post (
100- "/" .join ([ARCSECOND_API_URL_DEV , org_subdomain , "allskycameras" , camera_uuid , "images" ]) + "/" ,
101- status = 201 ,
102- json = {"status" : "success" , "id" : image_id },
103- )
104-
105- state = State (verbose = False , api_name = "cloud" )
106- config = {
107- "cloud" : {
108- "username" : "dummy" ,
109- "upload_key" : "1234" ,
110- "api_server" : ARCSECOND_API_URL_DEV ,
111- }
112- }
113- config = ArcsecondConfig (state , config ) # it will read your config file.
114-
115- context = AllSkyCameraImageUploadContext (
116- config , input_camera_uuid = camera_uuid , org_subdomain = org_subdomain
117- )
118-
119- context .validate () # important step to perform before uploading.
120- fixtures_dir = Path (__file__ ).parent .parent .parent .parent / "fixtures"
121- fixture_files = list (fixtures_dir .glob ("*.fits" ))
122-
123- for fixture_file in fixture_files :
124- # Create a temporary directory and copy the fixture file there
125- with tempfile .TemporaryDirectory () as temp_dir :
126- temp_path = Path (temp_dir ) / fixture_file .name
127- shutil .copy (fixture_file , temp_path )
128-
129- # Use the actual file for uploading
130- uploader = AllSkyCameraImageFileUploader (
131- context , str (temp_path ), display_progress = False
132- )
133-
134- status , substatus , error = uploader .upload_file ()
135- assert status .value == Status .OK .value
136- assert substatus .value == Substatus .DONE .value
137- assert error is None
0 commit comments