55import os
66
77from opus .api import API
8- from opus .cli import read , data , metadata , image
8+ from opus .cli import read , data , metadata , image , files
99
1010
1111@pytest .fixture
1212def api ():
1313 return API ('http://localhost/' )
1414
1515def test_read ():
16- assert read ([ 'foo' ] ) == {}
17- assert read ([ '--foo' ] ) == {}
16+ assert read ('foo' . split () ) == {}
17+ assert read ('--foo' . split () ) == {}
1818
19- assert read ([ '--foo' , ' bar'] ) == {'foo' : 'bar' }
20- assert read ([ '--foo=bar' ] ) == {'foo' : 'bar' }
21- assert read ([ '--foo' , ' --bar'] ) == {}
19+ assert read ('--foo bar' . split () ) == {'foo' : 'bar' }
20+ assert read ('--foo=bar' . split () ) == {'foo' : 'bar' }
21+ assert read ('--foo --bar' . split () ) == {}
2222
23- assert read ([ '--foo=bar' , ' 123'] ) == {'foo' : 'bar' }
24- assert read ([ '--foo=123' , ' --bar' , ' abc'] ) == {'foo' : '123' , 'bar' : 'abc' }
23+ assert read ('--foo=bar 123' . split () ) == {'foo' : 'bar' }
24+ assert read ('--foo=123 --bar abc' . split () ) == {'foo' : '123' , 'bar' : 'abc' }
2525
26- assert read ([ '--foo' , ' 123' , ' abc'] ) == {'foo' : '123' }
27- assert read ([ '--foo' , ' 123' , ' --bar'] ) == {'foo' : '123' }
28- assert read ([ '--foo' , ' --bar' , ' 123'] ) == {'bar' : '123' }
29- assert read ([ '123' , ' --foo' , ' --bar'] ) == {}
26+ assert read ('--foo 123 abc' . split () ) == {'foo' : '123' }
27+ assert read ('--foo 123 --bar' . split () ) == {'foo' : '123' }
28+ assert read ('--foo --bar 123' . split () ) == {'bar' : '123' }
29+ assert read ('123 --foo --bar' . split () ) == {}
3030
31- assert read ([ "--foo='bar'" ] ) == {'foo' : 'bar' }
31+ assert read ("--foo='bar'" . split () ) == {'foo' : 'bar' }
3232 assert read (["--foo='123 abc'" ]) == {'foo' : '123 abc' }
3333 assert read (["--foo" , "'123 abc'" ]) == {'foo' : '123 abc' }
3434
@@ -39,7 +39,7 @@ def test_cli_data(api):
3939 'http://localhost/data.json' ,
4040 body = json )
4141
42- argv = [ '--limit' , ' 100']
42+ argv = '--limit 100' . split ()
4343 resp = data (argv , api = api )
4444
4545 assert len (responses .calls ) == 1
@@ -64,7 +64,7 @@ def test_cli_data_limite_none(api):
6464 'http://localhost/data.json' ,
6565 body = json )
6666
67- argv = [ '--all' , ' --planet' , ' Saturn']
67+ argv = '--all --planet Saturn' . split ()
6868 resp = data (argv , api = api )
6969
7070 assert len (responses .calls ) == 2
@@ -94,7 +94,7 @@ def test_cli_metadata(api):
9494 'http://localhost/metadata/S_IMG_CO_ISS_1459551972_N.json' ,
9595 body = json )
9696
97- argv = [ 'S_IMG_CO_ISS_1459551972_N' ]
97+ argv = 'S_IMG_CO_ISS_1459551972_N' . split ()
9898 resp = metadata (argv , api = api )
9999
100100 assert len (responses .calls ) == 1
@@ -111,7 +111,7 @@ def test_cli_image(api):
111111 'http://localhost/image/med/S_IMG_CO_ISS_1459551972_N.json' ,
112112 body = img )
113113
114- argv = [ 'S_IMG_CO_ISS_1459551972_N' , ' --size' , ' med']
114+ argv = 'S_IMG_CO_ISS_1459551972_N --size med' . split ()
115115 resp = image (argv , api = api )
116116
117117 assert len (responses .calls ) == 1
@@ -123,22 +123,22 @@ def test_cli_image(api):
123123
124124@responses .activate
125125def test_cli_image_download (api ):
126- fname = 'N1459551972_1_med.jpg'
127- jpg = 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
128-
129126 img = open ('tests/api/image/med/S_IMG_CO_ISS_1459551972_N.json' , 'r' ).read ()
130127 responses .add (responses .GET ,
131128 'http://localhost/image/med/S_IMG_CO_ISS_1459551972_N.json' ,
132129 body = img )
133130
131+ fname = 'N1459551972_1_med.jpg'
132+ jpg = 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
133+
134134 with open ('tests/api/image/med/' + fname , 'rb' ) as img :
135135 responses .add (responses .GET , jpg ,
136136 body = img .read (), status = 200 ,
137137 content_type = 'image/jpeg' ,
138138 stream = True
139139 )
140140
141- argv = [ 'S_IMG_CO_ISS_1459551972_N' , ' --output' , ' tests/test.jpg']
141+ argv = 'S_IMG_CO_ISS_1459551972_N --output tests/test.jpg' . split ()
142142 resp = image (argv , api = api )
143143
144144 assert len (responses .calls ) == 2
@@ -148,3 +148,50 @@ def test_cli_image_download(api):
148148 assert resp == 'tests/test.jpg'
149149 assert os .path .isfile ('tests/test.jpg' )
150150 os .remove ('tests/test.jpg' )
151+
152+
153+ @responses .activate
154+ def test_cli_files (api ):
155+ json = open ('tests/api/files/S_IMG_CO_ISS_1459551972_N.json' , 'r' ).read ()
156+ responses .add (responses .GET ,
157+ 'http://localhost/files/S_IMG_CO_ISS_1459551972_N.json' ,
158+ body = json )
159+
160+ argv = 'S_IMG_CO_ISS_1459551972_N' .split ()
161+ resp = files (argv , api = api )
162+
163+ assert len (responses .calls ) == 1
164+ assert responses .calls [0 ].request .url == 'http://localhost/files/S_IMG_CO_ISS_1459551972_N.json'
165+ assert responses .calls [0 ].response .text == json
166+
167+ assert 'OPUS API Files for observation: S_IMG_CO_ISS_1459551972_N' in resp
168+ assert 'LBL: https://pds-rings.seti.org/holdings/calibrated/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_CALIB.LBL' in resp
169+
170+
171+ @responses .activate
172+ def test_cli_files_downlad (api ):
173+ json = open ('tests/api/files/S_IMG_CO_ISS_1459551972_N.json' , 'r' ).read ()
174+ responses .add (responses .GET ,
175+ 'http://localhost/files/S_IMG_CO_ISS_1459551972_N.json' ,
176+ body = json )
177+
178+ fname = 'N1459551972_1.LBL'
179+ lbl = 'https://pds-rings.seti.org/holdings/volumes/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1.LBL'
180+
181+ with open ('tests/data/' + fname , 'rb' ) as img :
182+ responses .add (responses .GET , lbl ,
183+ body = img .read (), status = 200 ,
184+ content_type = 'image/txt' ,
185+ stream = True
186+ )
187+
188+ argv = 'S_IMG_CO_ISS_1459551972_N -f RAW_IMAGE LBL --output tests/test.lbl' .split ()
189+ resp = files (argv , api = api )
190+
191+ assert len (responses .calls ) == 2
192+ assert responses .calls [0 ].request .url == 'http://localhost/files/S_IMG_CO_ISS_1459551972_N.json'
193+ assert responses .calls [1 ].request .url == lbl
194+
195+ assert resp == 'tests/test.lbl'
196+ assert os .path .isfile ('tests/test.lbl' )
197+ os .remove ('tests/test.lbl' )
0 commit comments