11# -*- coding: utf-8 -*-
22import pytest
3+ import os
34import responses
45import json as JSON
56
67from opus .images import *
78
8-
99@pytest .fixture
1010def size ():
1111 return 'med'
1212
13-
14- @pytest .fixture
15- def json (size ):
16- return JSON .loads (open ('tests/api/images/{}.json' .format (size ), 'r' ).read ())
17-
18-
1913@pytest .fixture
20- def images (json , size ):
14+ def images (size ):
15+ json = JSON .loads (open ('tests/api/images/{}.json' .format (size ), 'r' ).read ())
2116 return Images (json , size )
2217
18+ @pytest .fixture
19+ def image (size ):
20+ ring_obs_id = 'S_IMG_CO_ISS_1459551972_N'
21+ json = JSON .loads (open ('tests/api/image/' + size + '/' + ring_obs_id + '.json' , 'r' ).read ())
22+ return Image (ring_obs_id , json ['path' ], json ['data' ][0 ]['img' ])
2323
2424def test_images_meta (images , size ):
2525 assert repr (images ) == 'OPUS API Images object (with 10 images)'
@@ -42,3 +42,43 @@ def test_images_iter(images):
4242 images .next () == images [0 ]
4343 for ii , img in enumerate (images ):
4444 img = images [ii ]
45+
46+
47+ @responses .activate
48+ def test_image_download (image ):
49+ fname = os .path .basename (image .url )
50+
51+ with open ('tests/api/image/med/' + fname , 'rb' ) as img :
52+ responses .add (responses .GET , image .url ,
53+ body = img .read (), status = 200 ,
54+ content_type = 'image/jpeg' ,
55+ stream = True
56+ )
57+
58+ image .download (out = 'tests/' )
59+
60+ assert len (responses .calls ) == 1
61+ assert responses .calls [0 ].request .url == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
62+
63+ assert os .path .isfile ('tests/' + fname )
64+ os .remove ('tests/' + fname )
65+
66+
67+ @responses .activate
68+ def test_image_download_output (image ):
69+ fname = os .path .basename (image .url )
70+
71+ with open ('tests/api/image/med/' + fname , 'rb' ) as img :
72+ responses .add (responses .GET , image .url ,
73+ body = img .read (), status = 200 ,
74+ content_type = 'image/jpeg' ,
75+ stream = True
76+ )
77+
78+ image .download (out = 'tests/test.jpg' )
79+
80+ assert len (responses .calls ) == 1
81+ assert responses .calls [0 ].request .url == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
82+
83+ assert os .path .isfile ('tests/test.jpg' )
84+ os .remove ('tests/test.jpg' )
0 commit comments