Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
Signed-off-by: Zelin Hao <zelinhao@amazon.com>
  • Loading branch information
zelinh committed Feb 4, 2025
1 parent c120e71 commit 57d84a4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/test_workflow/smoke_test/smoke_test_runner_opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def __init__(self, args: TestArgs, test_manifest: TestManifest) -> None:
"Content-Type": "application/json"
}

def download_spec(self, url, local_path):
def download_spec(self, url: str, local_path: str) -> None:
try:
response = requests.get(url, timeout = 10)
response = requests.get(url, timeout=10)
if response.status_code == 200:
with open(local_path, "wb") as file:
file.write(response.content)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# yamllint disable-file
openapi: 3.1.0
info:
title: OpenSearch API Specification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

import unittest
from pathlib import Path
from unittest.mock import MagicMock, Mock, call, patch, mock_open
from unittest.mock import MagicMock, Mock, call, mock_open, patch

import os
import requests
from openapi_core.exceptions import OpenAPIError
from requests.models import Response
Expand Down Expand Up @@ -96,9 +95,10 @@ def test_validate_response_swagger_with_invalid_response(self, mock_get: Mock, m
with self.assertRaises(OpenAPIError):
runner.validate_response_swagger(response)

@patch("test_workflow.smoke_test.smoke_test_runner.TestRecorder")
@patch("requests.get")
@patch("builtins.open", new_callable=mock_open)
def test_download_spec_success(self, mock_file, mock_get):
def test_download_spec_success(self, mock_file: Mock, mock_get: Mock, mock_test_recorder: Mock) -> None:
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = "Mock OpenSearch API Spec Yaml content"
Expand All @@ -114,27 +114,29 @@ def test_download_spec_success(self, mock_file, mock_get):
mock_file.assert_any_call(runner.spec_path, "wb")
mock_file().write.assert_called_once_with("Mock OpenSearch API Spec Yaml content")

@patch("test_workflow.smoke_test.smoke_test_runner.TestRecorder")
@patch("requests.get")
@patch("builtins.open", new_callable=mock_open)
def test_download_spec_fail_local(self, mock_file, mock_get):
def test_download_spec_fail_local(self, mock_file: Mock, mock_get: Mock, mock_test_recorder: Mock) -> None:
# Mock request failure
mock_get.side_effect = requests.RequestException

runner = SmokeTestRunnerOpenSearch(MagicMock(), MagicMock())
SmokeTestRunnerOpenSearch(MagicMock(), MagicMock())

mock_get.assert_called_once()

mock_file().write.assert_not_called()

@patch("test_workflow.smoke_test.smoke_test_runner.TestRecorder")
@patch("requests.get")
@patch("builtins.open", new_callable=mock_open)
def test_download_spec_https_fail(self, mock_file, mock_get):
def test_download_spec_https_fail(self, mock_file: Mock, mock_get: Mock, mock_test_recorder: Mock) -> None:
mock_response = MagicMock()
mock_response.status_code = 404
mock_get.return_value = mock_response

runner = SmokeTestRunnerOpenSearch(MagicMock(), MagicMock())
SmokeTestRunnerOpenSearch(MagicMock(), MagicMock())

mock_get.assert_called_once()

mock_file().write.assert_not_called()
mock_file().write.assert_not_called()

0 comments on commit 57d84a4

Please sign in to comment.