Skip to content

Commit

Permalink
fix: ci does not attempt to do API calls
Browse files Browse the repository at this point in the history
Except for getting the swagger. Facilities to selectively run these tests are forthcoming
  • Loading branch information
tazlin committed Jul 6, 2023
1 parent f8644ce commit 6affe11
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/maintests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
- name: Run pre-commit
uses: pre-commit/[email protected]
- name: Run unit tests
run: tox -e tests
run: tox -e tests-no-api-calls
2 changes: 1 addition & 1 deletion .github/workflows/prtests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
- name: Run pre-commit
uses: pre-commit/[email protected]
- name: Run unit tests
run: tox -e tests
run: tox -e tests-no-api-calls
4 changes: 2 additions & 2 deletions src/horde_sdk/ai_horde_api/endpoints.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from strenum import StrEnum

# AI_HORDE_BASE_URL = "https://dev.aihorde.net/api/"
AI_HORDE_BASE_URL = "http://localhost:9834/api/"
AI_HORDE_BASE_URL = "https://aihorde.net/api/"
# AI_HORDE_BASE_URL = "http://localhost:9834/api/"


class AI_HORDE_API_URL_Literals(StrEnum):
Expand Down
54 changes: 27 additions & 27 deletions tests/ai_horde_api/test_api_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@
from horde_sdk.generic_api.apimodels import BaseResponse


@pytest.fixture
def default_image_gen_request() -> ImageGenerateAsyncRequest:
return ImageGenerateAsyncRequest(
apikey="0000000000",
prompt="a cat in a hat",
models=["Deliberate"],
)

class TestAIHordeAPIClient:
@pytest.fixture
def default_image_gen_request(self) -> ImageGenerateAsyncRequest:
return ImageGenerateAsyncRequest(
apikey="0000000000",
prompt="a cat in a hat",
models=["Deliberate"],
)

def test_AIHordeAPIClient_init():
AIHordeAPIClient()
def test_AIHordeAPIClient_init(self):
AIHordeAPIClient()

def test_AIHordeAPIClient_async(self, default_image_gen_request: ImageGenerateAsyncRequest):
client = AIHordeAPIClient()

def test_AIHordeAPIClient_async(default_image_gen_request: ImageGenerateAsyncRequest):
client = AIHordeAPIClient()
api_response: ImageGenerateAsyncResponse | RequestErrorResponse = client.generate_image_async(
default_image_gen_request
)
api_response: ImageGenerateAsyncResponse | RequestErrorResponse = client.generate_image_async(
default_image_gen_request
)

if isinstance(api_response, RequestErrorResponse):
pytest.fail(f"API Response was an error: {api_response.message}")
if isinstance(api_response, RequestErrorResponse):
pytest.fail(f"API Response was an error: {api_response.message}")

assert isinstance(api_response, ImageGenerateAsyncResponse)
assert isinstance(api_response, ImageGenerateAsyncResponse)

cancel_request = CancelImageGenerateRequest(apikey="0000000000", id=api_response.id)
cancel_response: BaseResponse | RequestErrorResponse = client.submit_request(cancel_request)
cancel_request = CancelImageGenerateRequest(apikey="0000000000", id=api_response.id)
cancel_response: BaseResponse | RequestErrorResponse = client.submit_request(cancel_request)

if isinstance(cancel_response, RequestErrorResponse):
pytest.fail(
f"API Response was an error: {cancel_response.message}"
f"Please note that the job ({api_response.id}) is orphaned and will continue to run on the server"
"until it is finished, it times out or it is cancelled manually."
)
if isinstance(cancel_response, RequestErrorResponse):
pytest.fail(
f"API Response was an error: {cancel_response.message}"
f"Please note that the job ({api_response.id}) is orphaned and will continue to run on the server"
"until it is finished, it times out or it is cancelled manually."
)

assert isinstance(cancel_response, CancelImageGenerateRequest.get_expected_response_type())
assert isinstance(cancel_response, CancelImageGenerateRequest.get_expected_response_type())
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def pytest_collection_modifyitems(items):
"""Modifies test items in place to ensure test modules run in a given order."""
MODULE_ORDER = ["tests_generic", "test_utils", "test_dynamically_check_apimodels"]
# `test.scripts` must run first because it downloads the legacy database
module_mapping = {item: item.module.__name__ for item in items}

sorted_items = items.copy()

# Iteratively move tests of each module to the end of the test queue
for module in MODULE_ORDER:
sorted_items = [it for it in sorted_items if module_mapping[it] != module] + [
it for it in sorted_items if module_mapping[it] == module
]

items[:] = sorted_items
13 changes: 13 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,16 @@ deps =
-r requirements.txt
commands =
pytest tests {posargs} --cov


[testenv:tests-no-api-calls]
description = same as 'tests', but does not reach out to the APIs
skip_install = false
deps =
pytest>=7
pytest-sugar
pytest-cov
requests
-r requirements.txt
commands =
pytest tests {posargs} --ignore-glob=*api_calls.py --cov

0 comments on commit 6affe11

Please sign in to comment.