Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoOpenCosmos committed Jan 24, 2025
1 parent 0f1479f commit df656da
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,6 @@ cython_debug/

# Ignore config.yaml
config/config.yaml

# Ignore .vscode
.vscode/
16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ test = [
find = {}

[tool.bandit]
skips = ["B101"] # Example: ignore assert statement usage if needed
targets = ["datacosmos"] # Adjust to point to your project directory

[tool.pydocstyle]
convention = "google"
Expand All @@ -58,6 +60,7 @@ exclude = [
".tox",
".venv",
".vscode",
"__pycache__",
"__pypackages__",
"_build",
"buck-out",
Expand Down Expand Up @@ -89,3 +92,16 @@ line-length = 88

[tool.isort]
profile = "black"

[tool.flake8]
max-line-length = 88
exclude = [
".venv",
"__pycache__",
"build",
"dist",
]
ignore = [
"E203", # Whitespace before ':', handled by Black
"W503", # Line break before binary operator
]
33 changes: 33 additions & 0 deletions tests/unit/datacosmos/client/test_client_delete_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from unittest.mock import patch, MagicMock
from datacosmos.client import DatacosmosClient
from config.config import Config


@patch("datacosmos.client.DatacosmosClient._authenticate_and_initialize_client")

Check failure on line 6 in tests/unit/datacosmos/client/test_client_delete_request.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/unit/datacosmos/client/test_client_delete_request.py#L6

Line too long (80 > 79 characters) (E501)
def test_delete_request(mock_auth_client):
"""
Test that the client performs a DELETE request correctly.
"""
# Mock the HTTP client
mock_http_client = MagicMock()
mock_response = MagicMock()
mock_response.status_code = 204
mock_http_client.request.return_value = mock_response
mock_auth_client.return_value = mock_http_client

config = Config(
client_id="test-client-id",
client_secret="test-client-secret",
token_url="https://mock.token.url/oauth/token",
audience="https://mock.audience",
)
client = DatacosmosClient(config=config)
response = client.delete("https://mock.api/some-endpoint")

# Assertions
assert response.status_code == 204
mock_http_client.request.assert_called_once_with(
"DELETE",
"https://mock.api/some-endpoint"
)
mock_auth_client.call_count == 2
35 changes: 35 additions & 0 deletions tests/unit/datacosmos/client/test_client_get_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from unittest.mock import patch, MagicMock
from datacosmos.client import DatacosmosClient
from config.config import Config


@patch("datacosmos.client.DatacosmosClient._authenticate_and_initialize_client")

Check failure on line 6 in tests/unit/datacosmos/client/test_client_get_request.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/unit/datacosmos/client/test_client_get_request.py#L6

Line too long (80 > 79 characters) (E501)
def test_client_get_request(mock_auth_client):
"""
Test that the client performs a GET request correctly.
"""
# Mock the HTTP client
mock_http_client = MagicMock()
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"message": "success"}
mock_http_client.request.return_value = mock_response
mock_auth_client.return_value = mock_http_client

config = Config(
client_id="test-client-id",
client_secret="test-client-secret",
token_url="https://mock.token.url/oauth/token",
audience="https://mock.audience",
)
client = DatacosmosClient(config=config)
response = client.get("https://mock.api/some-endpoint")

# Assertions
assert response.status_code == 200
assert response.json() == {"message": "success"}
mock_http_client.request.assert_called_once_with(
"GET",
"https://mock.api/some-endpoint"
)
mock_auth_client.call_count == 2
38 changes: 38 additions & 0 deletions tests/unit/datacosmos/client/test_client_post_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from unittest.mock import patch, MagicMock
from datacosmos.client import DatacosmosClient
from config.config import Config


@patch("datacosmos.client.DatacosmosClient._authenticate_and_initialize_client")

Check failure on line 6 in tests/unit/datacosmos/client/test_client_post_request.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/unit/datacosmos/client/test_client_post_request.py#L6

Line too long (80 > 79 characters) (E501)
def test_post_request(mock_auth_client):
"""
Test that the client performs a POST request correctly.
"""
# Mock the HTTP client
mock_http_client = MagicMock()
mock_response = MagicMock()
mock_response.status_code = 201
mock_response.json.return_value = {"message": "created"}
mock_http_client.request.return_value = mock_response
mock_auth_client.return_value = mock_http_client

config = Config(
client_id="test-client-id",
client_secret="test-client-secret",
token_url="https://mock.token.url/oauth/token",
audience="https://mock.audience",
)
client = DatacosmosClient(config=config)
response = client.post(
"https://mock.api/some-endpoint", json={"key": "value"}
)

# Assertions
assert response.status_code == 201
assert response.json() == {"message": "created"}
mock_http_client.request.assert_called_once_with(
"POST",
"https://mock.api/some-endpoint",
json={"key": "value"}
)
mock_auth_client.call_count == 2
38 changes: 38 additions & 0 deletions tests/unit/datacosmos/client/test_client_put_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from unittest.mock import patch, MagicMock
from datacosmos.client import DatacosmosClient
from config.config import Config


@patch("datacosmos.client.DatacosmosClient._authenticate_and_initialize_client")

Check failure on line 6 in tests/unit/datacosmos/client/test_client_put_request.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/unit/datacosmos/client/test_client_put_request.py#L6

Line too long (80 > 79 characters) (E501)
def test_put_request(mock_auth_client):
"""
Test that the client performs a PUT request correctly.
"""
# Mock the HTTP client
mock_http_client = MagicMock()
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {"message": "updated"}
mock_http_client.request.return_value = mock_response
mock_auth_client.return_value = mock_http_client

config = Config(
client_id="test-client-id",
client_secret="test-client-secret",
token_url="https://mock.token.url/oauth/token",
audience="https://mock.audience",
)
client = DatacosmosClient(config=config)
response = client.put(
"https://mock.api/some-endpoint", json={"key": "updated-value"}
)

# Assertions
assert response.status_code == 200
assert response.json() == {"message": "updated"}
mock_http_client.request.assert_called_once_with(
"PUT",
"https://mock.api/some-endpoint",
json={"key": "updated-value"}
)
mock_auth_client.call_count == 2
7 changes: 3 additions & 4 deletions tests/unit/datacosmos/client/test_client_token_refreshing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


@patch("datacosmos.client.DatacosmosClient._authenticate_and_initialize_client")

Check failure on line 7 in tests/unit/datacosmos/client/test_client_token_refreshing.py

View workflow job for this annotation

GitHub Actions / Flake8

tests/unit/datacosmos/client/test_client_token_refreshing.py#L7

Line too long (80 > 79 characters) (E501)
def test_token_refreshing(mock_auth_client):
def test_client_token_refreshing(mock_auth_client):
"""
Test that the client refreshes the token when it expires.
"""
Expand All @@ -31,7 +31,7 @@ def test_token_refreshing(mock_auth_client):
client.token_expiry = datetime.now(timezone.utc) - timedelta(seconds=1)

# Make a GET request (should trigger token refresh)
response = client.get("https://mock.api/some-endpoint", headers={"Authorization": f"Bearer {client.token}"})
response = client.get("https://mock.api/some-endpoint")

# Assertions
assert response.status_code == 200
Expand All @@ -45,6 +45,5 @@ def test_token_refreshing(mock_auth_client):
# Verify the request was made correctly
mock_http_client.request.assert_called_once_with(
"GET",
"https://mock.api/some-endpoint",
headers={"Authorization": f"Bearer {client.token}"},
"https://mock.api/some-endpoint"
)

0 comments on commit df656da

Please sign in to comment.