Skip to content

Commit

Permalink
Merge pull request #1011 from rommapp/misc/migrate-moby-handler-to-async
Browse files Browse the repository at this point in the history
misc: Migrate MobyGamesHandler to async
  • Loading branch information
adamantike authored Jul 23, 2024
2 parents 634e68e + 7c6dfa8 commit b9da728
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 92 deletions.
2 changes: 1 addition & 1 deletion backend/endpoints/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ async def update_rom(
cleaned_data.get("moby_id", "")
and int(cleaned_data.get("moby_id", "")) != rom.moby_id
):
moby_rom = meta_moby_handler.get_rom_by_id(cleaned_data["moby_id"])
moby_rom = await meta_moby_handler.get_rom_by_id(cleaned_data["moby_id"])
cleaned_data.update(moby_rom)
path_screenshots = fs_resource_handler.get_rom_screenshots(
rom=rom,
Expand Down
8 changes: 3 additions & 5 deletions backend/endpoints/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def search_rom(
igdb_matched_roms = meta_igdb_handler.get_matched_roms_by_id(
int(search_term)
)
moby_matched_roms = meta_moby_handler.get_matched_roms_by_id(
moby_matched_roms = await meta_moby_handler.get_matched_roms_by_id(
int(search_term)
)
except ValueError as exc:
Expand All @@ -74,7 +74,7 @@ async def search_rom(
igdb_matched_roms = meta_igdb_handler.get_matched_roms_by_name(
search_term, _get_main_platform_igdb_id(rom.platform)
)
moby_matched_roms = meta_moby_handler.get_matched_roms_by_name(
moby_matched_roms = await meta_moby_handler.get_matched_roms_by_name(
search_term, rom.platform.moby_id
)

Expand Down Expand Up @@ -123,8 +123,6 @@ async def search_cover(
detail="No SteamGridDB enabled",
)

covers = await meta_sgdb_handler.get_details(
requests_client=request.app.requests_client, search_term=search_term
)
covers = await meta_sgdb_handler.get_details(search_term=search_term)

return [SearchCoverSchema.model_validate(cover) for cover in covers]
2 changes: 2 additions & 0 deletions backend/endpoints/sockets/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from rq import Worker
from rq.job import Job
from sqlalchemy.inspection import inspect
from utils.context import initialize_context

STOP_SCAN_FLAG: Final = "scan:stop"

Expand Down Expand Up @@ -78,6 +79,7 @@ def _should_scan_rom(scan_type: ScanType, rom: Rom, selected_roms: list):
)


@initialize_context()
async def scan_platforms(
platform_ids: list[int],
scan_type: ScanType = ScanType.QUICK,
Expand Down
11 changes: 8 additions & 3 deletions backend/endpoints/tests/test_assets.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import pytest
from fastapi.testclient import TestClient
from main import app

client = TestClient(app)

@pytest.fixture
def client():
with TestClient(app) as client:
yield client

def test_delete_saves(access_token, save):

def test_delete_saves(client, access_token, save):
response = client.post(
"/saves/delete",
headers={"Authorization": f"Bearer {access_token}"},
Expand All @@ -16,7 +21,7 @@ def test_delete_saves(access_token, save):
assert body["msg"] == "Successfully deleted 1 saves"


def test_delete_states(access_token, state):
def test_delete_states(client, access_token, state):
response = client.post(
"/states/delete",
headers={"Authorization": f"Bearer {access_token}"},
Expand Down
9 changes: 7 additions & 2 deletions backend/endpoints/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import pytest
from fastapi.testclient import TestClient
from main import app

client = TestClient(app)

@pytest.fixture
def client():
with TestClient(app) as client:
yield client

def test_config():

def test_config(client):
response = client.get("/config")
assert response.status_code == 200

Expand Down
9 changes: 7 additions & 2 deletions backend/endpoints/tests/test_heartbeat.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import pytest
from fastapi.testclient import TestClient
from main import app
from utils import get_version

client = TestClient(app)

@pytest.fixture
def client():
with TestClient(app) as client:
yield client

def test_heartbeat():

def test_heartbeat(client):
response = client.get("/heartbeat")
assert response.status_code == 200

Expand Down
18 changes: 11 additions & 7 deletions backend/endpoints/tests/test_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from main import app
from models.user import Role

client = TestClient(app)

@pytest.fixture
def client():
with TestClient(app) as client:
yield client


@pytest.fixture(autouse=True)
Expand All @@ -15,7 +19,7 @@ def clear_cache():
sync_cache.flushall()


def test_login_logout(admin_user):
def test_login_logout(client, admin_user):
response = client.get("/login")

assert response.status_code == 405
Expand All @@ -33,7 +37,7 @@ def test_login_logout(admin_user):
assert response.json()["msg"] == "Successfully logged out"


def test_get_all_users(access_token):
def test_get_all_users(client, access_token):
response = client.get("/users", headers={"Authorization": f"Bearer {access_token}"})
assert response.status_code == 200

Expand All @@ -42,7 +46,7 @@ def test_get_all_users(access_token):
assert users[0]["username"] == "test_admin"


def test_get_user(access_token, editor_user):
def test_get_user(client, access_token, editor_user):
response = client.get(
f"/users/{editor_user.id}", headers={"Authorization": f"Bearer {access_token}"}
)
Expand All @@ -52,7 +56,7 @@ def test_get_user(access_token, editor_user):
assert user["username"] == "test_editor"


def test_create_user(access_token):
def test_create_user(client, access_token):
response = client.post(
"/users",
params={
Expand All @@ -69,7 +73,7 @@ def test_create_user(access_token):
assert user["role"] == "viewer"


def test_update_user(access_token, editor_user):
def test_update_user(client, access_token, editor_user):
assert editor_user.role == Role.EDITOR

response = client.put(
Expand All @@ -83,7 +87,7 @@ def test_update_user(access_token, editor_user):
assert user["role"] == "viewer"


def test_delete_user(access_token, editor_user):
def test_delete_user(client, access_token, editor_user):
response = client.delete(
f"/users/{editor_user.id}", headers={"Authorization": f"Bearer {access_token}"}
)
Expand Down
21 changes: 13 additions & 8 deletions backend/endpoints/tests/test_oauth.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import pytest
from endpoints.auth import ACCESS_TOKEN_EXPIRE_MINUTES
from fastapi.exceptions import HTTPException
from fastapi.testclient import TestClient
from handler.auth.base_handler import WRITE_SCOPES
from main import app

client = TestClient(app)

@pytest.fixture
def client():
with TestClient(app) as client:
yield client

def test_refreshing_oauth_token_basic(refresh_token):

def test_refreshing_oauth_token_basic(client, refresh_token):
response = client.post(
"/token",
data={
Expand All @@ -23,7 +28,7 @@ def test_refreshing_oauth_token_basic(refresh_token):
assert body["expires"] == ACCESS_TOKEN_EXPIRE_MINUTES * 60


def test_refreshing_oauth_token_without_refresh_token():
def test_refreshing_oauth_token_without_refresh_token(client):
try:
client.post(
"/token",
Expand All @@ -36,7 +41,7 @@ def test_refreshing_oauth_token_without_refresh_token():
assert e.detail == "Missing refresh token"


def test_refreshing_oauth_token_with_invalid_refresh_token():
def test_refreshing_oauth_token_with_invalid_refresh_token(client):
try:
client.post(
"/token",
Expand All @@ -50,7 +55,7 @@ def test_refreshing_oauth_token_with_invalid_refresh_token():
assert e.detail == "Invalid refresh token"


def test_auth_via_upass(admin_user):
def test_auth_via_upass(client, admin_user):
response = client.post(
"/token",
data={
Expand All @@ -68,7 +73,7 @@ def test_auth_via_upass(admin_user):
assert body["expires"] == ACCESS_TOKEN_EXPIRE_MINUTES * 60


def test_auth_via_upass_with_invalid_credentials(admin_user):
def test_auth_via_upass_with_invalid_credentials(client, admin_user):
try:
client.post(
"/token",
Expand All @@ -83,7 +88,7 @@ def test_auth_via_upass_with_invalid_credentials(admin_user):
assert e.detail == "Invalid username or password"


def test_auth_via_upass_with_excess_scopes(viewer_user):
def test_auth_via_upass_with_excess_scopes(client, viewer_user):
try:
client.post(
"/token",
Expand All @@ -99,7 +104,7 @@ def test_auth_via_upass_with_excess_scopes(viewer_user):
assert e.detail == "Insufficient scope"


def test_auth_with_invalid_grant_type():
def test_auth_with_invalid_grant_type(client):
try:
client.post(
"/token",
Expand Down
9 changes: 7 additions & 2 deletions backend/endpoints/tests/test_platform.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import pytest
from fastapi.testclient import TestClient
from main import app

client = TestClient(app)

@pytest.fixture
def client():
with TestClient(app) as client:
yield client

def test_get_platforms(access_token, platform):

def test_get_platforms(client, access_token, platform):
response = client.get("/platforms")
assert response.status_code == 403

Expand Down
9 changes: 7 additions & 2 deletions backend/endpoints/tests/test_raw.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import pytest
from fastapi.testclient import TestClient
from main import app

client = TestClient(app)

@pytest.fixture
def client():
with TestClient(app) as client:
yield client

def test_get_raw_asset(access_token):

def test_get_raw_asset(client, access_token):
response = client.get(
"/raw/assets/users/557365723a31/saves/n64/mupen64/Super Mario 64 (J) (Rev A).sav"
)
Expand Down
21 changes: 14 additions & 7 deletions backend/endpoints/tests/test_rom.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from unittest.mock import patch

import pytest
from fastapi.testclient import TestClient
from handler.filesystem.roms_handler import FSRomsHandler
from handler.metadata.igdb_handler import IGDBBaseHandler
from main import app

client = TestClient(app)

@pytest.fixture
def client():
with TestClient(app) as client:
yield client

def test_get_rom(access_token, rom):

def test_get_rom(client, access_token, rom):
response = client.get(
f"/roms/{rom.id}",
headers={"Authorization": f"Bearer {access_token}"},
Expand All @@ -17,7 +24,7 @@ def test_get_rom(access_token, rom):
assert body["id"] == rom.id


def test_get_all_roms(access_token, rom, platform):
def test_get_all_roms(client, access_token, rom, platform):
response = client.get(
"/roms",
headers={"Authorization": f"Bearer {access_token}"},
Expand All @@ -30,9 +37,9 @@ def test_get_all_roms(access_token, rom, platform):
assert body[0]["id"] == rom.id


@patch("endpoints.rom.fs_rom_handler.rename_file")
@patch("endpoints.rom.meta_igdb_handler.get_rom_by_id")
def test_update_rom(rename_file_mock, get_rom_by_id_mock, access_token, rom):
@patch.object(FSRomsHandler, "rename_file")
@patch.object(IGDBBaseHandler, "get_rom_by_id")
def test_update_rom(rename_file_mock, get_rom_by_id_mock, client, access_token, rom):
response = client.put(
f"/roms/{rom.id}",
headers={"Authorization": f"Bearer {access_token}"},
Expand Down Expand Up @@ -67,7 +74,7 @@ def test_update_rom(rename_file_mock, get_rom_by_id_mock, access_token, rom):
assert get_rom_by_id_mock.called


def test_delete_roms(access_token, rom):
def test_delete_roms(client, access_token, rom):
response = client.post(
"/roms/delete",
headers={"Authorization": f"Bearer {access_token}"},
Expand Down
Loading

0 comments on commit b9da728

Please sign in to comment.