cara/caimira/tests/test_data_service.py

82 lines
2.9 KiB
Python
Raw Normal View History

2023-07-11 12:36:36 +00:00
import unittest
2023-11-09 14:39:58 +00:00
from unittest.mock import Mock, patch
2023-07-11 12:36:36 +00:00
from caimira.store.data_service import DataService
2023-07-11 12:36:36 +00:00
class DataServiceTests(unittest.TestCase):
def setUp(self):
# Set up any necessary test data or configurations
2023-11-09 14:39:58 +00:00
self.credentials = {"email": "test@example.com", "password": "password123"}
2023-07-11 12:36:36 +00:00
self.data_service = DataService(self.credentials)
2023-11-09 14:39:58 +00:00
@patch("requests.post")
def test_login_successful(self, mock_post):
2023-07-11 12:36:36 +00:00
# Mock successful login response
2023-11-09 14:39:58 +00:00
mock_response = Mock()
mock_response.status_code = 200
mock_response.json.return_value = {"access_token": "dummy_token"}
mock_post.return_value = mock_response
2023-07-11 12:36:36 +00:00
# Call the login method
2023-11-09 14:39:58 +00:00
access_token = self.data_service._login()
2023-07-11 12:36:36 +00:00
# Assert that the access token is returned correctly
self.assertEqual(access_token, "dummy_token")
# Verify that the fetch method was called with the expected arguments
2023-11-09 14:39:58 +00:00
mock_post.assert_called_once_with(
"https://caimira-data-api.app.cern.ch/login",
json=dict(email="test@example.com", password="password123"),
headers={"Content-Type": "application/json"},
2023-07-11 12:36:36 +00:00
)
2023-11-09 14:39:58 +00:00
@patch("requests.post")
def test_login_error(self, mock_post):
2023-07-11 12:36:36 +00:00
# Mock login error response
2023-11-09 14:39:58 +00:00
mock_post.return_value = Mock()
mock_post.return_value.status_code = 500
2023-07-11 12:36:36 +00:00
# Call the login method
2023-11-09 14:39:58 +00:00
access_token = self.data_service._login()
2023-07-11 12:36:36 +00:00
# Assert that the login method returns None in case of an error
self.assertIsNone(access_token)
2023-11-09 14:39:58 +00:00
@patch("requests.get")
@patch.object(DataService, "_login")
def test_fetch_successful(self, mock_login, mock_get):
2023-07-11 12:36:36 +00:00
# Mock successful fetch response
2023-11-09 14:39:58 +00:00
mock_get.return_value = Mock()
mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = {"data": "dummy_data"}
2023-07-11 12:36:36 +00:00
# Call the fetch method with a mock access token
2023-11-09 14:39:58 +00:00
mock_login.return_value = "dummy_token"
data = self.data_service.fetch()
2023-07-11 12:36:36 +00:00
# Assert that the data is returned correctly
self.assertEqual(data, {"data": "dummy_data"})
# Verify that the fetch method was called with the expected arguments
2023-11-09 14:39:58 +00:00
mock_get.assert_called_once_with(
"https://caimira-data-api.app.cern.ch/data",
headers={
"Authorization": "Bearer dummy_token",
"Content-Type": "application/json",
},
2023-07-11 12:36:36 +00:00
)
2023-11-09 14:39:58 +00:00
@patch("requests.get")
@patch.object(DataService, "_login")
def test_fetch_error(self, mock_login, mock_get):
2023-07-11 12:36:36 +00:00
# Mock fetch error response
2023-11-09 14:39:58 +00:00
mock_get.return_value = Mock()
mock_get.return_value.status_code = 500
2023-07-11 12:36:36 +00:00
# Call the fetch method with a mock access token
2023-11-09 14:39:58 +00:00
mock_login.return_value = "dummy_token"
data = self.data_service.fetch()
2023-07-11 12:36:36 +00:00
# Assert that the fetch method returns None in case of an error
self.assertIsNone(data)