11from api import app
22from json import load
33from unittest import TestCase
4+ from unittest .mock import MagicMock , patch
5+
6+ import requests
47
58
69class TestIntegrations (TestCase ):
@@ -12,23 +15,48 @@ def test_get_atted_api5(self):
1215 :return:
1316 """
1417 # Valid data
15- response = self .app_client .get ("/proxy/atted_api5/At1g01010/5" )
16- # Note: pytest is running from project root. So path is relative to project root
1718 with open ("tests/data/get_atted_api5.json" ) as json_file :
1819 expected = load (json_file )
19- self .assertEqual (response .json , expected )
2020
21- # Invalid gene
22- response = self . app_client . get ( "/proxy/atted_api5/At1g0101x/5" )
23- expected = { "wasSuccessful" : False , "error" : "Invalid gene id" }
24- self . assertEqual ( response . json , expected )
21+ mock_success = MagicMock ()
22+ mock_success . status_code = 200
23+ mock_success . json . return_value = expected
24+ mock_success . raise_for_status . return_value = None
2525
26- # If no data, the service should return this response
27- response = self .app_client .get ("/proxy/atted_api5/At1g01011/5" )
28- expected = {"error" : "No gene ID specified." , "status_code" : 400 }
29- self .assertEqual (response .json , expected )
26+ mock_no_data = MagicMock ()
27+ mock_no_data .status_code = 200
28+ mock_no_data .json .return_value = {"error" : "No gene ID specified." , "status_code" : 400 }
29+ mock_no_data .raise_for_status .return_value = None
30+
31+ with patch ("api.resources.proxy.requests.get" , side_effect = [mock_success , mock_no_data ]):
32+ response = self .app_client .get ("/proxy/atted_api5/At1g01010/5" )
33+
34+ self .assertEqual (response .json , expected )
35+
36+ # Invalid gene
37+ response = self .app_client .get ("/proxy/atted_api5/At1g0101x/5" )
38+ expected = {"wasSuccessful" : False , "error" : "Invalid gene id" }
39+ self .assertEqual (response .json , expected )
40+
41+ # If no data, the service should return this response
42+ response = self .app_client .get ("/proxy/atted_api5/At1g01011/5" )
43+ expected = {"error" : "No gene ID specified." , "status_code" : 400 }
44+ self .assertEqual (response .json , expected )
45+
46+ # Invalid topN count
47+ response = self .app_client .get ("proxy/atted_api5/At1g01010/9999999999999999999" )
48+ expected = {"wasSuccessful" : False , "error" : "Invalid count" }
49+ self .assertEqual (response .json , expected )
50+
51+ def test_get_atted_api5_handles_external_error (self ):
52+ failure_response = MagicMock ()
53+ failure_response .status_code = 403
54+ http_error = requests .exceptions .HTTPError (response = failure_response )
55+ failure_response .raise_for_status .side_effect = http_error
56+
57+ with patch ("api.resources.proxy.requests.get" , return_value = failure_response ):
58+ response = self .app_client .get ("/proxy/atted_api5/At1g01010/5" )
3059
31- # Invalid topN count
32- response = self .app_client .get ("proxy/atted_api5/At1g01010/9999999999999999999" )
33- expected = {"wasSuccessful" : False , "error" : "Invalid count" }
60+ expected = {"wasSuccessful" : False , "error" : "External API request failed with status code 403" }
61+ self .assertEqual (response .status_code , 403 )
3462 self .assertEqual (response .json , expected )
0 commit comments