-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
187 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,6 +162,7 @@ cython_debug/ | |
|
||
|
||
test.py | ||
etest.py | ||
sonar-project.properties | ||
wget-log | ||
.scannerwork | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class UnsupportedConfigurationError(Exception): | ||
pass |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
import requests | ||
|
||
from metricheq.connectors.git_providers.github import ( | ||
GitHubClient, | ||
GitHubConfig, | ||
GitHubConnector, | ||
) | ||
|
||
|
||
class TestGitHubClient(unittest.TestCase): | ||
def setUp(self): | ||
self.config = GitHubConfig(api_key="test_api_key") | ||
|
||
def test_initialization(self): | ||
client = GitHubClient(self.config) | ||
self.assertEqual(client.base_url, "https://api.github.com") | ||
self.assertIsNotNone(client.authenticator) | ||
|
||
@patch("requests.Session.send") | ||
def test_make_request(self, mock_send): | ||
mock_response = Mock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = {"data": "test"} | ||
mock_send.return_value = mock_response | ||
|
||
client = GitHubClient(self.config) | ||
response = client.make_request("/test") | ||
|
||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual(response.json(), {"data": "test"}) | ||
|
||
|
||
class TestGitHubConnector(unittest.TestCase): | ||
def setUp(self): | ||
self.config = GitHubConfig(api_key="test_api_key") | ||
|
||
@patch("metricheq.connectors.git_providers.github.GitHubClient") | ||
def test_connector_initialization(self, mock_client): | ||
connector = GitHubConnector.from_config(self.config) | ||
self.assertIsInstance(connector, GitHubConnector) | ||
|
||
@patch("metricheq.connectors.git_providers.github.GitHubClient.make_request") | ||
def test_ensure_connectivity(self, mock_make_request): | ||
mock_make_request.return_value.status_code = 200 | ||
connector = GitHubConnector.from_config(self.config) | ||
|
||
connector.ensure_connectivity() | ||
|
||
@patch("metricheq.connectors.git_providers.github.GitHubClient.make_request") | ||
def test_ensure_connectivity_failure(self, mock_make_request): | ||
mock_make_request.side_effect = requests.RequestException | ||
|
||
connector = GitHubConnector.from_config(self.config) | ||
|
||
with self.assertRaises(ConnectionError): | ||
connector.ensure_connectivity() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
from metricheq.connectors.sonar import SonarConnector | ||
from metricheq.extractors.sonar import SonarMeasuresExtractor, SonarMetricType | ||
|
||
|
||
class TestSonarMeasuresExtractor(unittest.TestCase): | ||
def setUp(self): | ||
self.mock_connector = Mock(spec=SonarConnector) | ||
self.mock_connector.client = Mock() | ||
self.params = {"component": "my_component", "metric_key": SonarMetricType.BUGS} | ||
|
||
def test_initialization(self): | ||
extractor = SonarMeasuresExtractor(self.mock_connector, self.params) | ||
self.assertIsInstance(extractor, SonarMeasuresExtractor) | ||
|
||
# TODO: MORE TESTS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
import requests | ||
from metricheq.connectors.base import ( | ||
BearerTokenAuthenticator, | ||
UserPasswordBasicAuthenticator, | ||
) | ||
from metricheq.connectors.sonar import ( | ||
SonarClient, | ||
SonarConnector, | ||
SonarTokenConfig, | ||
SonarUserPasswordConfig, | ||
) | ||
from metricheq.exceptions.exceptions import UnsupportedConfigurationError | ||
|
||
|
||
class TestSonarClient(unittest.TestCase): | ||
def test_init_with_token_config(self): | ||
token_config = SonarTokenConfig( | ||
host_url="http://example.com", user_token="abcd1234" | ||
) | ||
|
||
client = SonarClient(token_config) | ||
|
||
self.assertIsInstance(client.authenticator, BearerTokenAuthenticator) | ||
|
||
def test_init_with_user_password_config(self): | ||
user_password_config = SonarUserPasswordConfig( | ||
host_url="http://example.com", username="younes", password="1234" | ||
) | ||
|
||
client = SonarClient(user_password_config) | ||
self.assertIsInstance(client.authenticator, UserPasswordBasicAuthenticator) | ||
|
||
def test_with_unsupported_config(self): | ||
with self.assertRaises(UnsupportedConfigurationError): | ||
SonarClient({}) | ||
|
||
@patch("requests.Session.send") | ||
def test_make_request_success(self, mock_send): | ||
mock_response = Mock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = {} | ||
mock_send.return_value = mock_response | ||
|
||
config = SonarTokenConfig(host_url="http://example.com", user_token="token123") | ||
client = SonarClient(config) | ||
response = client.make_request("/api/test") | ||
|
||
self.assertEqual(response.status_code, 200) | ||
|
||
|
||
class TestSonarConnector(unittest.TestCase): | ||
@patch("metricheq.connectors.sonar.SonarClient") | ||
def test_connector_initialization(self, mock_client): | ||
connector = SonarConnector.from_config({}) | ||
self.assertIsInstance(connector, SonarConnector) | ||
|
||
@patch("metricheq.connectors.sonar.SonarClient") | ||
def test_ensure_connectivity(self, mock_client): | ||
mock_client_instance = mock_client.return_value | ||
mock_client_instance.make_request.return_value.json.return_value = { | ||
"health": "GREEN" | ||
} | ||
|
||
connector = SonarConnector(mock_client_instance) | ||
self.assertTrue(connector.ensure_connectivity()) | ||
|
||
@patch("metricheq.connectors.sonar.SonarClient") | ||
def test_connectivity_with_unhealthy_response(self, mock_client): | ||
mock_client_instance = mock_client.return_value | ||
mock_client_instance.make_request.return_value.json.return_value = { | ||
"health": "RED" | ||
} | ||
|
||
connector = SonarConnector(mock_client_instance) | ||
with self.assertRaises(Exception): | ||
connector.ensure_connectivity() | ||
|
||
@patch("metricheq.connectors.sonar.SonarClient") | ||
def test_connectivity_with_request_exception(self, mock_client): | ||
mock_client_instance = mock_client.return_value | ||
mock_client_instance.make_request.side_effect = requests.RequestException | ||
|
||
connector = SonarConnector(mock_client_instance) | ||
with self.assertRaises(ConnectionError): | ||
connector.ensure_connectivity() | ||
|
||
@patch("metricheq.connectors.sonar.SonarClient") | ||
def test_method_calls_with_mocks(self, mock_client): | ||
mock_client_instance = mock_client.return_value | ||
mock_client_instance.make_request.return_value.json.return_value = { | ||
"health": "GREEN" | ||
} | ||
|
||
connector = SonarConnector(mock_client_instance) | ||
connector.ensure_connectivity() | ||
|
||
mock_client_instance.make_request.assert_called_once_with("/api/system/health") |
This file was deleted.
Oops, something went wrong.