1515
1616# Standard Library
1717import datetime
18- from unittest .mock import (
19- ANY ,
20- patch ,
21- )
18+ from unittest .mock import patch
2219
2320# Django
2421from django .utils import timezone
3734from wger .utils .requests import wger_headers
3835
3936
40- class MockOffResponse :
41- def __init__ (self ):
42- self .status_code = 200
43- self .ok = True
44- self .content = b'2000'
37+ mock_off_response = {
38+ 'image_front_url' : 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg' ,
39+ 'images' : {
40+ 'front_en' : {
41+ 'imgid' : '12345' ,
42+ },
43+ '12345' : {'uploader' : 'Mr Foobar' },
44+ },
45+ }
4546
46- # yapf: disable
47- @staticmethod
48- def json ():
49- return {
50- "product" : {
51- 'image_front_url' :
52- 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg' ,
53- 'images' : {
54- 'front_en' : {
55- 'imgid' : '12345' ,
56- },
57- '12345' : {
58- 'uploader' : 'Mr Foobar'
59- }
60- }
61- },
62- }
63- # yapf: disable
47+
48+ class MockOffImageResponse :
49+ image_bytes = [0 , 1 , 2 , 3 ]
50+ response = type ('obj' , (object ,), {'content' : b'mock_content' })()
6451
6552
6653class MockWgerApiResponse :
6754 def __init__ (self ):
6855 self .status_code = 200
6956 self .content = b'2000'
7057
71- # yapf: disable
7258 @staticmethod
7359 def json ():
7460 return {
75- " count" : 1 ,
76- " next" : None ,
77- " previous" : None ,
78- " results" : [
61+ ' count' : 1 ,
62+ ' next' : None ,
63+ ' previous' : None ,
64+ ' results' : [
7965 {
80- "id" : 1 ,
81- " uuid" : " 188324b5-587f-42d7-9abc-d2ca64c73d45" ,
82- " ingredient_id" : " 12345" ,
83- " ingredient_uuid" : " e9baa8bd-84fc-4756-8d90-5b9739b06cf8" ,
84- " image" : " http://localhost:8000/media/ingredients/1/188324b5-587f-42d7-9abc-d2ca64c73d45.jpg" ,
85- " created" : " 2023-03-15T23:20:10.969369+01:00" ,
86- " last_update" : " 2023-03-15T23:20:10.969369+01:00" ,
87- " size" : 20179 ,
88- " width" : 400 ,
89- " height" : 166 ,
90- " license" : 1 ,
91- " license_author" : " Tester McTest" ,
92- " license_author_url" : " https://example.com/editors/mcLovin" ,
93- " license_title" : "" ,
94- " license_object_url" : "" ,
95- " license_derivative_source_url" : ""
66+ 'id' : 1 ,
67+ ' uuid' : ' 188324b5-587f-42d7-9abc-d2ca64c73d45' ,
68+ ' ingredient_id' : ' 12345' ,
69+ ' ingredient_uuid' : ' e9baa8bd-84fc-4756-8d90-5b9739b06cf8' ,
70+ ' image' : ' http://localhost:8000/media/ingredients/1/188324b5-587f-42d7-9abc-d2ca64c73d45.jpg' ,
71+ ' created' : ' 2023-03-15T23:20:10.969369+01:00' ,
72+ ' last_update' : ' 2023-03-15T23:20:10.969369+01:00' ,
73+ ' size' : 20179 ,
74+ ' width' : 400 ,
75+ ' height' : 166 ,
76+ ' license' : 1 ,
77+ ' license_author' : ' Tester McTest' ,
78+ ' license_author_url' : ' https://example.com/editors/mcLovin' ,
79+ ' license_title' : '' ,
80+ ' license_object_url' : '' ,
81+ ' license_derivative_source_url' : '' ,
9682 }
97- ]
83+ ],
9884 }
99- # yapf: enable
10085
10186
10287class FetchIngredientImageTestCase (WgerTestCase ):
10388 """
10489 Test fetching an ingredient image
10590 """
10691
107- @patch ('requests .get' )
92+ @patch ('openfoodfacts.api.ProductResource .get' )
10893 @patch .object (logger , 'info' )
10994 def test_source (self , mock_logger , mock_request ):
11095 """
@@ -120,7 +105,7 @@ def test_source(self, mock_logger, mock_request):
120105 mock_request .assert_not_called ()
121106 self .assertEqual (result , None )
122107
123- @patch ('requests .get' )
108+ @patch ('openfoodfacts.api.ProductResource .get' )
124109 @patch .object (logger , 'info' )
125110 def test_download_off_setting (self , mock_logger , mock_request ):
126111 """
@@ -136,7 +121,7 @@ def test_download_off_setting(self, mock_logger, mock_request):
136121 mock_request .assert_not_called ()
137122 self .assertEqual (result , None )
138123
139- @patch ('requests. get' , return_value = MockOffResponse () )
124+ @patch ('openfoodfacts.api.ProductResource. get' )
140125 @patch ('wger.nutrition.models.Image.from_json' )
141126 @patch .object (logger , 'info' )
142127 def test_last_new_image_date (self , mock_logger , mock_from_json , mock_request ):
@@ -160,10 +145,17 @@ def test_last_new_image_date(self, mock_logger, mock_from_json, mock_request):
160145 mock_request .assert_not_called ()
161146 self .assertEqual (result , None )
162147
163- @patch ('requests.get' , return_value = MockOffResponse ())
148+ @patch ('wger.nutrition.sync.download_image' , return_value = MockOffImageResponse )
149+ @patch ('openfoodfacts.api.ProductResource.get' , return_value = mock_off_response )
164150 @patch ('wger.nutrition.models.Image.from_json' )
165151 @patch .object (logger , 'info' )
166- def test_last_old_image_date (self , mock_logger , mock_from_json , mock_request ):
152+ def test_last_old_image_date (
153+ self ,
154+ mock_logger ,
155+ mock_from_json ,
156+ mock_request ,
157+ mock_download_image ,
158+ ):
167159 """
168160 Test that images are fetched if we checked a long time ago
169161 """
@@ -180,14 +172,27 @@ def test_last_old_image_date(self, mock_logger, mock_from_json, mock_request):
180172 ):
181173 result = fetch_ingredient_image (1 )
182174 mock_from_json .assert_called ()
175+ mock_download_image .assert_called_with (
176+ 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg' ,
177+ return_struct = True ,
178+ )
183179 mock_logger .assert_any_call ('Fetching image for ingredient 1' )
184- mock_request .assert_called ()
180+ mock_request .assert_called_with (
181+ '1234567890987654321' , fields = ['images' , 'image_front_url' ]
182+ )
185183 self .assertEqual (result , None )
186184
187- @patch ('requests.get' , return_value = MockOffResponse ())
185+ @patch ('wger.nutrition.sync.download_image' , return_value = MockOffImageResponse )
186+ @patch ('openfoodfacts.api.ProductResource.get' , return_value = mock_off_response )
188187 @patch ('wger.nutrition.models.Image.from_json' )
189188 @patch .object (logger , 'info' )
190- def test_download_ingredient_off (self , mock_logger , mock_from_json , mock_request ):
189+ def test_download_ingredient_off (
190+ self ,
191+ mock_logger ,
192+ mock_from_json ,
193+ mock_request ,
194+ mock_download_image ,
195+ ):
191196 """
192197 Test that the image is correctly downloaded
193198
@@ -202,23 +207,21 @@ def test_download_ingredient_off(self, mock_logger, mock_from_json, mock_request
202207 ):
203208 result = fetch_ingredient_image (1 )
204209
210+ # print(mock_request.mock_calls)
211+ mock_request .assert_called_with (
212+ '1234567890987654321' ,
213+ fields = ['images' , 'image_front_url' ],
214+ )
215+ mock_download_image .assert_called_with (
216+ 'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg' ,
217+ return_struct = True ,
218+ )
205219 mock_logger .assert_any_call ('Fetching image for ingredient 1' )
206220 mock_logger .assert_any_call (
207- 'Trying to fetch image from OFF for Test ingredient 1 (UUID: '
208- '7908c204-907f-4b1e-ad4e-f482e9769ade)'
221+ 'Trying to fetch image from OFF for " Test ingredient 1" '
222+ '( 7908c204-907f-4b1e-ad4e-f482e9769ade)'
209223 )
210224 mock_logger .assert_any_call ('Image successfully saved' )
211-
212- # print(mock_request.mock_calls)
213- mock_request .assert_any_call (
214- 'https://world.openfoodfacts.org/api/v2/product/5055365635003.json?fields=images,image_front_url' ,
215- headers = wger_headers (),
216- timeout = ANY ,
217- )
218- mock_request .assert_any_call (
219- 'https://openfoodfacts-images.s3.eu-west-3.amazonaws.com/data/123/456/789/0987654321/12345.jpg' ,
220- headers = wger_headers (),
221- )
222225 mock_from_json .assert_called ()
223226
224227 self .assertEqual (result , None )
0 commit comments