from unittest import TestCase
import unittest.mock
from unittest.mock import patch
from pygtop.gtop import get_json_from_gtop

class JsonTests(TestCase):

    def setUp(self):
        self.mock_response = unittest.mock.Mock()
        self.mock_response.text = '{"name": "superdrug", "ligandId": 1}'
        self.mock_response.status_code = 200


    @patch("requests.get")
    def test_can_process_json(self, mock_get):
        mock_get.return_value = self.mock_response
        result = get_json_from_gtop("ligands/x/")
        self.assertIsInstance(result, dict)
        self.assertEqual(result, {"name": "superdrug", "ligandId": 1})


    @patch("requests.get")
    def test_can_process_json_with_strange_response(self, mock_get):
        self.mock_response.text = ""
        mock_get.return_value = self.mock_response
        result = get_json_from_gtop("ligands/1/")
        self.assertIs(result, None)
        self.mock_response.text = "A non-JSON sentence"
        result = get_json_from_gtop("ligands/x/")
        self.assertIs(result, None)


    @patch("requests.get")
    def test_can_process_json_500_error(self, mock_get):
        self.mock_response.status_code = 500
        mock_get.return_value = self.mock_response
        result = get_json_from_gtop("ligands/x/")
        self.assertIs(result, None)



class RetryTests(TestCase):

    def setUp(self):
        self.mock_response = unittest.mock.Mock()
        self.mock_response.text = ""
        self.mock_response.status_code = 500


    @patch("requests.get")
    def test_json_retriever_will_try_five_times(self, mock_get):
        mock_get.return_value = self.mock_response
        result = get_json_from_gtop("ligands/x/")
        self.assertEqual(mock_get.call_count, 5)


    @patch("requests.get")
    def test_json_retriever_attempt_number_can_be_varied(self, mock_get):
        mock_get.return_value = self.mock_response
        result = get_json_from_gtop("ligands/x/", attempts=3)
        self.assertEqual(mock_get.call_count, 3)
        result = get_json_from_gtop("ligands/x/", attempts=1)
        self.assertEqual(mock_get.call_count, 1 + 3)
        result = get_json_from_gtop("ligands/x/", attempts=9)
        self.assertEqual(mock_get.call_count, 9 + 1 + 3)


    @patch("requests.get")
    def test_attempts_must_be_int(self, mock_get):
        mock_get.return_value = self.mock_response
        with self.assertRaises(TypeError):
            get_json_from_gtop("ligands/x/", attempts=1.5)
        with self.assertRaises(TypeError):
            get_json_from_gtop("ligands/x/", attempts="x")


    @patch("requests.get")
    def test_attempts_must_be_int(self, mock_get):
        mock_get.return_value = self.mock_response
        with self.assertRaises(ValueError):
            get_json_from_gtop("ligands/x/", attempts=-1)
        with self.assertRaises(ValueError):
            get_json_from_gtop("ligands/x/", attempts=0)

@@ -2625,35 +2625,40 @@
 ttempts_must_be_
-int
+positive
 (self, mock_get)
@@ -2848,32 +2848,32 @@
 es(ValueError):%0A
-
             get_
@@ -2913,8 +2913,551 @@
 mpts=0)%0A
+%0A%0A    @patch(%22requests.get%22)%0A    def test_can_get_correct_value_on_last_attempt(self, mock_get):%0A        ok_response = unittest.mock.Mock()%0A        ok_response.status_code = 200%0A        ok_response.text = '%7B%22name%22: %22superdrug%22, %22ligandId%22: 1%7D'%0A        mock_get.side_effect = %5B%0A         self.mock_response, self.mock_response, self.mock_response, ok_response%0A        %5D%0A        self.assertEqual(%0A         get_json_from_gtop(%22/ligands/x/%22),%0A         %7B%22name%22: %22superdrug%22, %22ligandId%22: 1%7D%0A        )%0A        self.assertEqual(mock_get.call_count, 4)%0A

Test for eventual json retrieval success