<gh_stars>1-10
from unittest import TestCase
from unittest.mock import MagicMock, Mock, patch

from src.grafana_api.model import APIModel
from src.grafana_api.search import Search


class SearchTestCase(TestCase):
    @patch("src.grafana_api.api.Api.call_the_api")
    def test_search(self, call_the_api_mock):
        model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
        search: Search = Search(grafana_api_model=model)

        mock: Mock = Mock()
        mock.json = Mock(return_value=list(["test"]))

        call_the_api_mock.return_value = mock

        self.assertEqual(["test"], search.search(search_query="Test"))

    @patch("src.grafana_api.api.Api.call_the_api")
    def test_search_invalid_empty_list(self, call_the_api_mock):
        model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
        search: Search = Search(grafana_api_model=model)

        mock: Mock = Mock()
        mock.json = Mock(return_value=list())

        call_the_api_mock.return_value = mock

        with self.assertRaises(Exception):
            search.search(search_query="test")

    @patch("src.grafana_api.api.Api.call_the_api")
    def test_search_invalid_output(self, call_the_api_mock):
        model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
        search: Search = Search(grafana_api_model=model)

        call_the_api_mock.side_effect = Exception

        with self.assertRaises(Exception):
            search.search(search_query=MagicMock())

from unittest import TestCase
from unittest.mock import MagicMock, Mock, patch

from src.grafana_api.model import APIModel
from src.grafana_api.search import Search


class SearchTestCase(TestCase):
    @patch("src.grafana_api.api.Api.call_the_api")
    def test_search(self, call_the_api_mock):
        model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
        search: Search = Search(grafana_api_model=model)

        mock: Mock = Mock()
        mock.json = Mock(return_value=list(["test"]))

        call_the_api_mock.return_value = mock

        self.assertEqual(["test"], search.search(search_query="Test"))

    @patch("src.grafana_api.api.Api.call_the_api")
    def test_search_invalid_empty_list(self, call_the_api_mock):
        model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
        search: Search = Search(grafana_api_model=model)

        mock: Mock = Mock()
        mock.json = Mock(return_value=list())

        call_the_api_mock.return_value = mock

        with self.assertRaises(Exception):
            search.search(search_query="test")

    @patch("src.grafana_api.api.Api.call_the_api")
    def test_search_invalid_output(self, call_the_api_mock):
        model: APIModel = APIModel(host=MagicMock(), token=MagicMock())
        search: Search = Search(grafana_api_model=model)

        call_the_api_mock.side_effect = Exception

        with self.assertRaises(Exception):
            search.search(search_query=MagicMock())