From 714637f5b21b26c80886350b77031a0155eb43c7 Mon Sep 17 00:00:00 2001 From: mshriver Date: Mon, 24 Nov 2025 13:01:11 -0500 Subject: [PATCH] api module test coverage --- test/test_admin_project_management_api.py | 202 +++++++++++++----- test/test_artifact_api.py | 238 +++++++++++++++------ test/test_dashboard_api.py | 202 +++++++++++++----- test/test_project_api.py | 222 +++++++++++++++----- test/test_result_api.py | 198 +++++++++++++----- test/test_run_api.py | 239 +++++++++++++++++----- 6 files changed, 971 insertions(+), 330 deletions(-) diff --git a/test/test_admin_project_management_api.py b/test/test_admin_project_management_api.py index b0d3689..b159f9e 100644 --- a/test/test_admin_project_management_api.py +++ b/test/test_admin_project_management_api.py @@ -1,58 +1,150 @@ -""" -Ibutsu API +"""Tests for AdminProjectManagementApi.""" -A system to store and query test results - -The version of the OpenAPI document: 2.8.3 -Generated by OpenAPI Generator (https://openapi-generator.tech) - -Do not edit the class manually. -""" - -import unittest +from urllib.parse import parse_qs, urlparse +from uuid import uuid4 from ibutsu_client.api.admin_project_management_api import AdminProjectManagementApi - - -class TestAdminProjectManagementApi(unittest.TestCase): - """AdminProjectManagementApi unit test stubs""" - - def setUp(self) -> None: - self.api = AdminProjectManagementApi() - - def tearDown(self) -> None: - pass - - def test_admin_add_project(self) -> None: - """Test case for admin_add_project - - Administration endpoint to manually add a project. Only accessible to superadmins. - """ - - def test_admin_delete_project(self) -> None: - """Test case for admin_delete_project - - Administration endpoint to delete a project. Only accessible to superadmins. - """ - - def test_admin_get_project(self) -> None: - """Test case for admin_get_project - - Administration endpoint to return a project. Only accessible to superadmins. - """ - - def test_admin_get_project_list(self) -> None: - """Test case for admin_get_project_list - - Administration endpoint to return a list of projects. Only accessible to superadmins. - """ - - def test_admin_update_project(self) -> None: - """Test case for admin_update_project - - Administration endpoint to update a project. Only accessible to superadmins. - """ - - -if __name__ == "__main__": - unittest.main() +from ibutsu_client.models.project import Project +from ibutsu_client.models.project_list import ProjectList + + +class TestAdminProjectManagementApi: + """AdminProjectManagementApi Tests""" + + def test_admin_add_project(self, mock_api_client, mock_rest_response): + """Test case for admin_add_project""" + api = AdminProjectManagementApi(api_client=mock_api_client) + project_id = uuid4() + project_data = { + "id": str(project_id), + "name": "New Project", + "title": "New Project Title", + } + + # Mock the API response + mock_response = mock_rest_response(data=project_data, status=201) + mock_api_client.call_api.return_value = mock_response + + # Call the API + new_project = Project(name="New Project", title="New Project Title") + response = api.admin_add_project(project=new_project) + + # Verify result + assert isinstance(response, Project) + assert str(response.id) == str(project_id) + assert response.name == "New Project" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "POST" + assert args[1].endswith("/admin/project") + + def test_admin_delete_project(self, mock_api_client, mock_rest_response): + """Test case for admin_delete_project""" + api = AdminProjectManagementApi(api_client=mock_api_client) + project_id = uuid4() + + # Mock the API response + mock_response = mock_rest_response(status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + api.admin_delete_project(id=project_id) + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "DELETE" + assert args[1].endswith(f"/admin/project/{project_id}") + + def test_admin_get_project(self, mock_api_client, mock_rest_response): + """Test case for admin_get_project""" + api = AdminProjectManagementApi(api_client=mock_api_client) + project_id = uuid4() + project_data = { + "id": str(project_id), + "name": "My Project", + } + + # Mock the API response + mock_response = mock_rest_response(data=project_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.admin_get_project(id=project_id) + + # Verify result + assert isinstance(response, Project) + assert str(response.id) == str(project_id) + assert response.name == "My Project" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "GET" + assert args[1].endswith(f"/admin/project/{project_id}") + + def test_admin_get_project_list(self, mock_api_client, mock_rest_response): + """Test case for admin_get_project_list""" + api = AdminProjectManagementApi(api_client=mock_api_client) + + project_list_data = { + "projects": [ + {"id": str(uuid4()), "name": "Project 1"}, + {"id": str(uuid4()), "name": "Project 2"}, + ], + "pagination": {"page": 1, "pageSize": 25, "totalItems": 2, "totalPages": 1}, + } + + # Mock the API response + mock_response = mock_rest_response(data=project_list_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.admin_get_project_list(page=1, page_size=25) + + # Verify result + assert isinstance(response, ProjectList) + assert len(response.projects) == 2 + assert response.projects[0].name == "Project 1" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "GET" + + parsed_url = urlparse(args[1]) + assert parsed_url.path.endswith("/admin/project") + query_params = parse_qs(parsed_url.query) + + assert query_params["page"] == ["1"] + assert query_params["pageSize"] == ["25"] + + def test_admin_update_project(self, mock_api_client, mock_rest_response): + """Test case for admin_update_project""" + api = AdminProjectManagementApi(api_client=mock_api_client) + project_id = uuid4() + project_data = { + "id": str(project_id), + "name": "Updated Project", + } + + # Mock the API response + mock_response = mock_rest_response(data=project_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + update_project = Project(name="Updated Project") + response = api.admin_update_project(id=project_id, project=update_project) + + # Verify result + assert isinstance(response, Project) + assert str(response.id) == str(project_id) + assert response.name == "Updated Project" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "PUT" + assert args[1].endswith(f"/admin/project/{project_id}") diff --git a/test/test_artifact_api.py b/test/test_artifact_api.py index 7c032f1..b879858 100644 --- a/test/test_artifact_api.py +++ b/test/test_artifact_api.py @@ -1,64 +1,180 @@ -""" -Ibutsu API +"""Tests for ArtifactApi.""" -A system to store and query test results - -The version of the OpenAPI document: 2.8.3 -Generated by OpenAPI Generator (https://openapi-generator.tech) - -Do not edit the class manually. -""" - -import unittest +from uuid import uuid4 from ibutsu_client.api.artifact_api import ArtifactApi - - -class TestArtifactApi(unittest.TestCase): - """ArtifactApi unit test stubs""" - - def setUp(self) -> None: - self.api = ArtifactApi() - - def tearDown(self) -> None: - pass - - def test_delete_artifact(self) -> None: - """Test case for delete_artifact - - Delete an artifact - """ - - def test_download_artifact(self) -> None: - """Test case for download_artifact - - Download an artifact - """ - - def test_get_artifact(self) -> None: - """Test case for get_artifact - - Get a single artifact - """ - - def test_get_artifact_list(self) -> None: - """Test case for get_artifact_list - - Get a (filtered) list of artifacts - """ - - def test_upload_artifact(self) -> None: - """Test case for upload_artifact - - Uploads a test run artifact - """ - - def test_view_artifact(self) -> None: - """Test case for view_artifact - - Stream an artifact directly to the client/browser - """ - - -if __name__ == "__main__": - unittest.main() +from ibutsu_client.models.artifact import Artifact +from ibutsu_client.models.artifact_list import ArtifactList + + +class TestArtifactApi: + """ArtifactApi Tests""" + + def test_get_artifact_list(self, mock_api_client, mock_rest_response): + """Test case for get_artifact_list""" + api = ArtifactApi(api_client=mock_api_client) + + artifact_list_data = { + "artifacts": [ + {"id": str(uuid4()), "filename": "test1.txt", "mime_type": "text/plain"}, + {"id": str(uuid4()), "filename": "test2.png", "mime_type": "image/png"}, + ], + "pagination": {"page": 1, "pageSize": 25, "totalItems": 2, "totalPages": 1}, + } + + # Mock the API response + mock_response = mock_rest_response(data=artifact_list_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.get_artifact_list(page=1, page_size=25) + + # Verify result + assert isinstance(response, ArtifactList) + assert len(response.artifacts) == 2 + assert response.artifacts[0].filename == "test1.txt" + assert response.pagination.total_items == 2 + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "GET" + assert "/artifact" in args[1] + assert "page=1" in args[1] + assert "pageSize=25" in args[1] + + def test_get_artifact(self, mock_api_client, mock_rest_response): + """Test case for get_artifact""" + api = ArtifactApi(api_client=mock_api_client) + artifact_id = uuid4() + artifact_data = { + "id": str(artifact_id), + "filename": "test.txt", + "mime_type": "text/plain", + } + + # Mock the API response + mock_response = mock_rest_response(data=artifact_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.get_artifact(id=artifact_id) + + # Verify result + assert isinstance(response, Artifact) + assert str(response.id) == str(artifact_id) + assert response.filename == "test.txt" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "GET" + assert args[1].endswith(f"/artifact/{artifact_id}") + + def test_download_artifact(self, mock_api_client, mock_rest_response): + """Test case for download_artifact""" + api = ArtifactApi(api_client=mock_api_client) + artifact_id = uuid4() + file_content = b"file content" + + # Mock the API response + # Note: download_artifact returns bytearray, so we mock the raw data + mock_response = mock_rest_response(status=200) + mock_response.data = file_content + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.download_artifact(id=artifact_id) + + # Verify result + assert response == file_content + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "GET" + assert args[1].endswith(f"/artifact/{artifact_id}/download") + + def test_view_artifact(self, mock_api_client, mock_rest_response): + """Test case for view_artifact""" + api = ArtifactApi(api_client=mock_api_client) + artifact_id = uuid4() + file_content = b"file content" + + # Mock the API response + mock_response = mock_rest_response(status=200) + mock_response.data = file_content + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.view_artifact(id=artifact_id) + + # Verify result + assert response == file_content + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "GET" + assert args[1].endswith(f"/artifact/{artifact_id}/view") + + def test_upload_artifact(self, mock_api_client, mock_rest_response): + """Test case for upload_artifact""" + api = ArtifactApi(api_client=mock_api_client) + run_id = uuid4() + filename = "test.txt" + file_content = b"content" + + artifact_data = { + "id": str(uuid4()), + "filename": filename, + "result_id": None, + "run_id": str(run_id), + } + + # Mock the API response + mock_response = mock_rest_response(data=artifact_data, status=201) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.upload_artifact(filename=filename, file=file_content, run_id=run_id) + + # Verify result + assert isinstance(response, Artifact) + assert response.filename == filename + assert str(response.run_id) == str(run_id) + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "POST" + assert args[1].endswith("/artifact") + + # Verify form params include file and metadata + # args[4] is post_params + post_params = args[4] + # Check if file is in post_params + # post_params is a list of tuples + # param[1] should be (filename, filedata, mimetype) + file_found = any( + param[0] == "file" and param[1][1] == file_content for param in post_params + ) + assert file_found + + def test_delete_artifact(self, mock_api_client, mock_rest_response): + """Test case for delete_artifact""" + api = ArtifactApi(api_client=mock_api_client) + artifact_id = uuid4() + + # Mock the API response + mock_response = mock_rest_response(status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + api.delete_artifact(id=artifact_id) + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "DELETE" + assert args[1].endswith(f"/artifact/{artifact_id}") diff --git a/test/test_dashboard_api.py b/test/test_dashboard_api.py index 0018085..a6cb58d 100644 --- a/test/test_dashboard_api.py +++ b/test/test_dashboard_api.py @@ -1,58 +1,150 @@ -""" -Ibutsu API +"""Tests for DashboardApi.""" -A system to store and query test results - -The version of the OpenAPI document: 2.8.3 -Generated by OpenAPI Generator (https://openapi-generator.tech) - -Do not edit the class manually. -""" - -import unittest +from urllib.parse import parse_qs, urlparse +from uuid import uuid4 from ibutsu_client.api.dashboard_api import DashboardApi - - -class TestDashboardApi(unittest.TestCase): - """DashboardApi unit test stubs""" - - def setUp(self) -> None: - self.api = DashboardApi() - - def tearDown(self) -> None: - pass - - def test_add_dashboard(self) -> None: - """Test case for add_dashboard - - Create a dashboard - """ - - def test_delete_dashboard(self) -> None: - """Test case for delete_dashboard - - Delete a dashboard - """ - - def test_get_dashboard(self) -> None: - """Test case for get_dashboard - - Get a single dashboard by ID - """ - - def test_get_dashboard_list(self) -> None: - """Test case for get_dashboard_list - - Get a list of dashboards - """ - - def test_update_dashboard(self) -> None: - """Test case for update_dashboard - - Update a dashboard - """ - - -if __name__ == "__main__": - unittest.main() +from ibutsu_client.models.dashboard import Dashboard +from ibutsu_client.models.dashboard_list import DashboardList + + +class TestDashboardApi: + """DashboardApi Tests""" + + def test_add_dashboard(self, mock_api_client, mock_rest_response): + """Test case for add_dashboard""" + api = DashboardApi(api_client=mock_api_client) + dashboard_id = uuid4() + dashboard_data = { + "id": str(dashboard_id), + "title": "New Dashboard", + "description": "Description", + } + + # Mock the API response + mock_response = mock_rest_response(data=dashboard_data, status=201) + mock_api_client.call_api.return_value = mock_response + + # Call the API + new_dashboard = Dashboard(title="New Dashboard", description="Description") + response = api.add_dashboard(dashboard=new_dashboard) + + # Verify result + assert isinstance(response, Dashboard) + assert str(response.id) == str(dashboard_id) + assert response.title == "New Dashboard" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "POST" + assert args[1].endswith("/dashboard") + + def test_delete_dashboard(self, mock_api_client, mock_rest_response): + """Test case for delete_dashboard""" + api = DashboardApi(api_client=mock_api_client) + dashboard_id = uuid4() + + # Mock the API response + mock_response = mock_rest_response(status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + api.delete_dashboard(id=dashboard_id) + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "DELETE" + assert args[1].endswith(f"/dashboard/{dashboard_id}") + + def test_get_dashboard(self, mock_api_client, mock_rest_response): + """Test case for get_dashboard""" + api = DashboardApi(api_client=mock_api_client) + dashboard_id = uuid4() + dashboard_data = { + "id": str(dashboard_id), + "title": "My Dashboard", + } + + # Mock the API response + mock_response = mock_rest_response(data=dashboard_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.get_dashboard(id=dashboard_id) + + # Verify result + assert isinstance(response, Dashboard) + assert str(response.id) == str(dashboard_id) + assert response.title == "My Dashboard" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "GET" + assert args[1].endswith(f"/dashboard/{dashboard_id}") + + def test_get_dashboard_list(self, mock_api_client, mock_rest_response): + """Test case for get_dashboard_list""" + api = DashboardApi(api_client=mock_api_client) + + dashboard_list_data = { + "dashboards": [ + {"id": str(uuid4()), "title": "Dashboard 1"}, + {"id": str(uuid4()), "title": "Dashboard 2"}, + ], + "pagination": {"page": 1, "pageSize": 25, "totalItems": 2, "totalPages": 1}, + } + + # Mock the API response + mock_response = mock_rest_response(data=dashboard_list_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.get_dashboard_list(page=1, page_size=25) + + # Verify result + assert isinstance(response, DashboardList) + assert len(response.dashboards) == 2 + assert response.dashboards[0].title == "Dashboard 1" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "GET" + + parsed_url = urlparse(args[1]) + assert parsed_url.path.endswith("/dashboard") + query_params = parse_qs(parsed_url.query) + + assert query_params["page"] == ["1"] + assert query_params["pageSize"] == ["25"] + + def test_update_dashboard(self, mock_api_client, mock_rest_response): + """Test case for update_dashboard""" + api = DashboardApi(api_client=mock_api_client) + dashboard_id = uuid4() + dashboard_data = { + "id": str(dashboard_id), + "title": "Updated Dashboard", + } + + # Mock the API response + mock_response = mock_rest_response(data=dashboard_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + update_dashboard = Dashboard(title="Updated Dashboard") + response = api.update_dashboard(id=dashboard_id, dashboard=update_dashboard) + + # Verify result + assert isinstance(response, Dashboard) + assert str(response.id) == str(dashboard_id) + assert response.title == "Updated Dashboard" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "PUT" + assert args[1].endswith(f"/dashboard/{dashboard_id}") diff --git a/test/test_project_api.py b/test/test_project_api.py index bf0fd41..971ec31 100644 --- a/test/test_project_api.py +++ b/test/test_project_api.py @@ -1,58 +1,170 @@ -""" -Ibutsu API +"""Tests for ProjectApi.""" -A system to store and query test results - -The version of the OpenAPI document: 2.8.3 -Generated by OpenAPI Generator (https://openapi-generator.tech) - -Do not edit the class manually. -""" - -import unittest +from urllib.parse import parse_qs, urlparse +from uuid import uuid4 from ibutsu_client.api.project_api import ProjectApi - - -class TestProjectApi(unittest.TestCase): - """ProjectApi unit test stubs""" - - def setUp(self) -> None: - self.api = ProjectApi() - - def tearDown(self) -> None: - pass - - def test_add_project(self) -> None: - """Test case for add_project - - Create a project - """ - - def test_get_filter_params(self) -> None: - """Test case for get_filter_params - - Get a project's filterable parameters - """ - - def test_get_project(self) -> None: - """Test case for get_project - - Get a single project by ID - """ - - def test_get_project_list(self) -> None: - """Test case for get_project_list - - Get a list of projects - """ - - def test_update_project(self) -> None: - """Test case for update_project - - Update a project - """ - - -if __name__ == "__main__": - unittest.main() +from ibutsu_client.models.project import Project +from ibutsu_client.models.project_list import ProjectList + + +class TestProjectApi: + """ProjectApi Tests""" + + def test_add_project(self, mock_api_client, mock_rest_response): + """Test case for add_project""" + api = ProjectApi(api_client=mock_api_client) + project_id = uuid4() + project_data = { + "id": str(project_id), + "name": "test-project", + "title": "Test Project", + } + + # Mock the API response + mock_response = mock_rest_response(data=project_data, status=201) + mock_api_client.call_api.return_value = mock_response + + # Create project object to send + project_item = Project(name="test-project", title="Test Project") + + # Call the API + response = api.add_project(project=project_item) + + # Verify the response is deserialized correctly + assert isinstance(response, Project) + assert str(response.id) == str(project_id) + assert response.name == "test-project" + + # Verify call_api was called with correct arguments + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "POST" # method + assert args[1].endswith("/project") # url + + # Body is serialized to dict + assert isinstance(args[3], dict) + assert args[3]["name"] == "test-project" + assert args[3]["title"] == "Test Project" + + def test_get_project(self, mock_api_client, mock_rest_response): + """Test case for get_project""" + api = ProjectApi(api_client=mock_api_client) + project_id = uuid4() + project_data = { + "id": str(project_id), + "name": "test-project", + } + + # Mock the API response + mock_response = mock_rest_response(data=project_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.get_project(id=str(project_id)) + + # Verify result + assert isinstance(response, Project) + assert str(response.id) == str(project_id) + assert response.name == "test-project" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "GET" + assert args[1].endswith(f"/project/{project_id}") + + def test_get_project_list(self, mock_api_client, mock_rest_response): + """Test case for get_project_list""" + api = ProjectApi(api_client=mock_api_client) + + project_list_data = { + "projects": [ + {"id": str(uuid4()), "name": "project1"}, + {"id": str(uuid4()), "name": "project2"}, + ], + "pagination": {"page": 1, "pageSize": 25, "totalItems": 2, "totalPages": 1}, + } + + # Mock the API response + mock_response = mock_rest_response(data=project_list_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.get_project_list(page=1, page_size=25, owner_id="user1") + + # Verify result + assert isinstance(response, ProjectList) + assert len(response.projects) == 2 + assert response.projects[0].name == "project1" + assert response.pagination.total_items == 2 + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "GET" + + parsed_url = urlparse(args[1]) + assert parsed_url.path.endswith("/project") + query_params = parse_qs(parsed_url.query) + + assert query_params["page"] == ["1"] + assert query_params["pageSize"] == ["25"] + assert query_params["ownerId"] == ["user1"] + + def test_update_project(self, mock_api_client, mock_rest_response): + """Test case for update_project""" + api = ProjectApi(api_client=mock_api_client) + project_id = uuid4() + project_data = { + "id": str(project_id), + "name": "updated-project", + } + + # Mock the API response + mock_response = mock_rest_response(data=project_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Update object + project_update = Project(name="updated-project") + + # Call the API + response = api.update_project(id=project_id, project=project_update) + + # Verify result + assert isinstance(response, Project) + assert str(response.id) == str(project_id) + assert response.name == "updated-project" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "PUT" + assert args[1].endswith(f"/project/{project_id}") + + # Body is serialized + assert isinstance(args[3], dict) + assert args[3]["name"] == "updated-project" + + def test_get_filter_params(self, mock_api_client, mock_rest_response): + """Test case for get_filter_params""" + api = ProjectApi(api_client=mock_api_client) + project_id = uuid4() + filter_params = ["env", "component", "tag"] + + # Mock the API response + mock_response = mock_rest_response(data=filter_params, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call API + response = api.get_filter_params(id=str(project_id)) + + # Verify result + assert isinstance(response, list) + assert response == filter_params + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "GET" + assert args[1].endswith(f"/project/filter-params/{project_id}") diff --git a/test/test_result_api.py b/test/test_result_api.py index 297bc87..a122d12 100644 --- a/test/test_result_api.py +++ b/test/test_result_api.py @@ -1,52 +1,152 @@ -""" -Ibutsu API +"""Tests for ResultApi.""" -A system to store and query test results - -The version of the OpenAPI document: 2.8.3 -Generated by OpenAPI Generator (https://openapi-generator.tech) - -Do not edit the class manually. -""" - -import unittest +from urllib.parse import parse_qs, urlparse +from uuid import uuid4 from ibutsu_client.api.result_api import ResultApi - - -class TestResultApi(unittest.TestCase): - """ResultApi unit test stubs""" - - def setUp(self) -> None: - self.api = ResultApi() - - def tearDown(self) -> None: - pass - - def test_add_result(self) -> None: - """Test case for add_result - - Create a test result - """ - - def test_get_result(self) -> None: - """Test case for get_result - - Get a single result - """ - - def test_get_result_list(self) -> None: - """Test case for get_result_list - - Get the list of results. - """ - - def test_update_result(self) -> None: - """Test case for update_result - - Updates a single result - """ - - -if __name__ == "__main__": - unittest.main() +from ibutsu_client.models.result import Result +from ibutsu_client.models.result_list import ResultList + + +class TestResultApi: + """ResultApi Tests""" + + def test_add_result(self, mock_api_client, mock_rest_response): + """Test case for add_result""" + api = ResultApi(api_client=mock_api_client) + result_id = uuid4() + result_data = { + "id": str(result_id), + "test_id": "test_case_1", + "result": "passed", + "duration": 1.5, + } + + # Mock the API response + mock_response = mock_rest_response(data=result_data, status=201) + mock_api_client.call_api.return_value = mock_response + + # Create result object to send + result_item = Result(test_id="test_case_1", result="passed", duration=1.5) + + # Call the API + response = api.add_result(result=result_item) + + # Verify the response is deserialized correctly + assert isinstance(response, Result) + assert str(response.id) == str(result_id) + assert response.test_id == "test_case_1" + assert response.result == "passed" + + # Verify call_api was called with correct arguments + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "POST" # method + assert args[1].endswith("/result") # url + + # Body is serialized to dict + assert isinstance(args[3], dict) + assert args[3]["test_id"] == "test_case_1" + assert args[3]["result"] == "passed" + assert args[3]["duration"] == 1.5 + + def test_get_result(self, mock_api_client, mock_rest_response): + """Test case for get_result""" + api = ResultApi(api_client=mock_api_client) + result_id = uuid4() + result_data = { + "id": str(result_id), + "test_id": "test_case_1", + "result": "passed", + } + + # Mock the API response + mock_response = mock_rest_response(data=result_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.get_result(id=result_id) + + # Verify result + assert isinstance(response, Result) + assert str(response.id) == str(result_id) + assert response.test_id == "test_case_1" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "GET" + assert args[1].endswith(f"/result/{result_id}") + + def test_get_result_list(self, mock_api_client, mock_rest_response): + """Test case for get_result_list""" + api = ResultApi(api_client=mock_api_client) + + result_list_data = { + "results": [ + {"id": str(uuid4()), "test_id": "test_1", "result": "passed"}, + {"id": str(uuid4()), "test_id": "test_2", "result": "failed"}, + ], + "pagination": {"page": 1, "pageSize": 25, "totalItems": 2, "totalPages": 1}, + } + + # Mock the API response + mock_response = mock_rest_response(data=result_list_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.get_result_list(page=1, page_size=25, filter=["result=passed"]) + + # Verify result + assert isinstance(response, ResultList) + assert len(response.results) == 2 + assert response.results[0].test_id == "test_1" + assert response.pagination.total_items == 2 + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "GET" + + parsed_url = urlparse(args[1]) + assert parsed_url.path.endswith("/result") + query_params = parse_qs(parsed_url.query) + + assert query_params["page"] == ["1"] + assert query_params["pageSize"] == ["25"] + assert query_params["filter"] == ["result=passed"] + + def test_update_result(self, mock_api_client, mock_rest_response): + """Test case for update_result""" + api = ResultApi(api_client=mock_api_client) + result_id = uuid4() + result_data = { + "id": str(result_id), + "test_id": "test_case_1", + "result": "failed", # Updated result + } + + # Mock the API response + mock_response = mock_rest_response(data=result_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Update object + result_update = Result(result="failed") + + # Call the API + response = api.update_result(id=result_id, result=result_update) + + # Verify result + assert isinstance(response, Result) + assert str(response.id) == str(result_id) + assert response.result == "failed" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "PUT" + assert args[1].endswith(f"/result/{result_id}") + + # Body is serialized + assert isinstance(args[3], dict) + assert args[3]["result"] == "failed" diff --git a/test/test_run_api.py b/test/test_run_api.py index b5eeb0e..22ace4e 100644 --- a/test/test_run_api.py +++ b/test/test_run_api.py @@ -1,58 +1,187 @@ -""" -Ibutsu API +"""Tests for RunApi.""" -A system to store and query test results - -The version of the OpenAPI document: 2.8.3 -Generated by OpenAPI Generator (https://openapi-generator.tech) - -Do not edit the class manually. -""" - -import unittest +from urllib.parse import parse_qs, urlparse +from uuid import uuid4 from ibutsu_client.api.run_api import RunApi - - -class TestRunApi(unittest.TestCase): - """RunApi unit test stubs""" - - def setUp(self) -> None: - self.api = RunApi() - - def tearDown(self) -> None: - pass - - def test_add_run(self) -> None: - """Test case for add_run - - Create a run - """ - - def test_bulk_update(self) -> None: - """Test case for bulk_update - - Update multiple runs with common metadata - """ - - def test_get_run(self) -> None: - """Test case for get_run - - Get a single run by ID (uuid required) - """ - - def test_get_run_list(self) -> None: - """Test case for get_run_list - - Get a list of the test runs - """ - - def test_update_run(self) -> None: - """Test case for update_run - - Update a single run - """ - - -if __name__ == "__main__": - unittest.main() +from ibutsu_client.models.run import Run +from ibutsu_client.models.run_list import RunList +from ibutsu_client.models.update_run import UpdateRun + + +class TestRunApi: + """RunApi Tests""" + + def test_add_run(self, mock_api_client, mock_rest_response): + """Test case for add_run""" + api = RunApi(api_client=mock_api_client) + run_id = uuid4() + run_data = { + "id": str(run_id), + "component": "test-component", + "env": "ci", + } + + # Mock the API response + mock_response = mock_rest_response(data=run_data, status=201) + mock_api_client.call_api.return_value = mock_response + + # Create run object to send + run_item = Run(component="test-component", env="ci") + + # Call the API + response = api.add_run(run=run_item) + + # Verify the response is deserialized correctly + assert isinstance(response, Run) + assert str(response.id) == str(run_id) + assert response.component == "test-component" + + # Verify call_api was called with correct arguments + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "POST" # method + assert args[1].endswith("/run") # url + + # Body is serialized to dict + assert isinstance(args[3], dict) + assert args[3]["component"] == "test-component" + assert args[3]["env"] == "ci" + + def test_get_run(self, mock_api_client, mock_rest_response): + """Test case for get_run""" + api = RunApi(api_client=mock_api_client) + run_id = uuid4() + run_data = { + "id": str(run_id), + "component": "test-component", + } + + # Mock the API response + mock_response = mock_rest_response(data=run_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.get_run(id=run_id) + + # Verify result + assert isinstance(response, Run) + assert str(response.id) == str(run_id) + assert response.component == "test-component" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _ = mock_api_client.call_api.call_args + assert args[0] == "GET" + assert args[1].endswith(f"/run/{run_id}") + + def test_get_run_list(self, mock_api_client, mock_rest_response): + """Test case for get_run_list""" + api = RunApi(api_client=mock_api_client) + + run_list_data = { + "runs": [ + {"id": str(uuid4()), "component": "comp1"}, + {"id": str(uuid4()), "component": "comp2"}, + ], + "pagination": {"page": 1, "pageSize": 25, "totalItems": 2, "totalPages": 1}, + } + + # Mock the API response + mock_response = mock_rest_response(data=run_list_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Call the API + response = api.get_run_list(page=1, page_size=25, filter=["env=ci"]) + + # Verify result + assert isinstance(response, RunList) + assert len(response.runs) == 2 + assert response.runs[0].component == "comp1" + assert response.pagination.total_items == 2 + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "GET" + + parsed_url = urlparse(args[1]) + assert parsed_url.path.endswith("/run") + query_params = parse_qs(parsed_url.query) + + assert query_params["page"] == ["1"] + assert query_params["pageSize"] == ["25"] + assert query_params["filter"] == ["env=ci"] + + def test_update_run(self, mock_api_client, mock_rest_response): + """Test case for update_run""" + api = RunApi(api_client=mock_api_client) + run_id = uuid4() + run_data = { + "id": str(run_id), + "component": "updated-component", + } + + # Mock the API response + mock_response = mock_rest_response(data=run_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Update object + run_update = Run(component="updated-component") + + # Call the API + response = api.update_run(id=run_id, run=run_update) + + # Verify result + assert isinstance(response, Run) + assert str(response.id) == str(run_id) + assert response.component == "updated-component" + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "PUT" + assert args[1].endswith(f"/run/{run_id}") + + # Body is serialized + assert isinstance(args[3], dict) + assert args[3]["component"] == "updated-component" + + def test_bulk_update(self, mock_api_client, mock_rest_response): + """Test case for bulk_update""" + api = RunApi(api_client=mock_api_client) + + run_list_data = { + "runs": [ + {"id": str(uuid4()), "component": "comp1", "metadata": {"new": "val"}}, + {"id": str(uuid4()), "component": "comp2", "metadata": {"new": "val"}}, + ], + "pagination": {"page": 1, "pageSize": 25, "totalItems": 2, "totalPages": 1}, + } + + # Mock the API response + mock_response = mock_rest_response(data=run_list_data, status=200) + mock_api_client.call_api.return_value = mock_response + + # Update object + update_data = UpdateRun(metadata={"new": "val"}) + + # Call API + response = api.bulk_update(filter=["env=ci"], update_run=update_data) + + # Verify result + assert isinstance(response, RunList) + assert len(response.runs) == 2 + + # Verify call + mock_api_client.call_api.assert_called_once() + args, _kwargs = mock_api_client.call_api.call_args + assert args[0] == "POST" + + parsed_url = urlparse(args[1]) + assert parsed_url.path.endswith("/runs/bulk-update") + query_params = parse_qs(parsed_url.query) + assert query_params["filter"] == ["env=ci"] + + assert isinstance(args[3], dict) + assert args[3]["metadata"] == {"new": "val"}