Skip to content

Commit

Permalink
Type conformance updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ManicJamie committed Apr 10, 2024
1 parent f64411f commit 68f5062
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 26 deletions.
27 changes: 22 additions & 5 deletions src/speedruncompy/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from enum import Enum
from numbers import Real
from typing import Any, Optional, Union, get_type_hints, get_origin, get_args
from typing import Any, Optional, Union, get_type_hints, get_origin, get_args, _SpecialForm, _type_check
from json import JSONEncoder

from .enums import *
Expand All @@ -32,6 +32,13 @@
SUPPRESS_FIELD_WARNINGS = False
"""Suppress warnings that a type is """

class _OptFieldMarker(): pass
@_SpecialForm
def OptField(self, parameters):
"""Field that may not be present. Will return `None` if not present."""
arg = _type_check(parameters, f"{self} requires a single type.")
return Union[arg, type(_OptFieldMarker)]

class srcpyJSONEncoder(JSONEncoder):
"""Converts Datatypes to dicts when encountered"""
def default(self, o: Any) -> Any:
Expand Down Expand Up @@ -498,6 +505,8 @@ class User(Datatype):
staticAssets: list[StaticAsset]
supporterIconType: Optional[int] # enum 0-2?
supporterIconPosition: Optional[int] # enum 0-1?
titleId: Optional[str]
"""ID for a title given for completing a Challenge"""

class UserStats(Datatype):
userId: str
Expand Down Expand Up @@ -618,12 +627,15 @@ class Run(Datatype):
hasSplits: bool
obsolete: Optional[bool]
place: Optional[int]
issues: Optional[None]
playerIds: list[str]
valueIds: list[str]
orphaned: Optional[bool]
estimated: Optional[bool] #TODO: Figure out what this means
"""Only shown in GetModerationRuns"""
issues: Optional[list] #TODO: fails when present

class ChallengeStanding(Datatype):
challengeId: str
place: int
registeredPlayerIds: list[str]
prizeAmount: int
Expand Down Expand Up @@ -756,8 +768,8 @@ class Resource(Datatype):
class Stream(Datatype):
id: str
gameId: Optional[str]
userId: str
areaId: str
userId: Optional[str]
areaId: Optional[str]
url: str
title: str
previewUrl: str
Expand Down Expand Up @@ -1003,4 +1015,9 @@ class SupporterSubscription(Datatype):
"""Default 0, undocumented but assume timestamp otherwise"""
cancelAtPeriodEnd: bool
canceledAt: int # assume timestamp


class Title(Datatype):
id: str
title: str
comment: str
referenceUrl: str
4 changes: 2 additions & 2 deletions src/speedruncompy/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,8 @@ def perform_async(self, retries=5, delay=1, **kwargs) -> Coroutine[Any, Any, r_G
return super().perform_async(retries, delay, **kwargs)

class GetConversationMessages(PostRequest):
def __init__(self, **params) -> None:
super().__init__("GetConversationMessages", returns=r_GetConversationMessages, **params)
def __init__(self, conversationId, **params) -> None:
super().__init__("GetConversationMessages", returns=r_GetConversationMessages, conversationId=conversationId, **params)

def perform(self, retries=5, delay=1, **kwargs) -> r_GetConversationMessages:
return super().perform(retries, delay, **kwargs)
Expand Down
6 changes: 6 additions & 0 deletions src/speedruncompy/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class r_GetChallenge(Datatype):
userList: list[User]
challengeRunCount: int
gameFollowerCount: int
titleList: list[Title]

class r_GetChallengeLeaderboard(Datatype):
challengeRunList: list[ChallengeRun]
Expand Down Expand Up @@ -162,6 +163,7 @@ class r_GetSearch(Datatype):
pageList: list[Article] #TODO: check
seriesList: list[Series]
userList: list[User]
challengeList: list[Challenge] #TODO: check

class r_GetSeriesList(Datatype):
seriesList: list[Series]
Expand Down Expand Up @@ -211,6 +213,7 @@ class r_GetUserPopoverData(Datatype):
userSocialConnectionList: list[UserSocialConnection]
games: list[Game]
"""Contains games sometimes:tm:"""
titleList: list[Title]

"""POST responses"""

Expand Down Expand Up @@ -256,6 +259,9 @@ class r_GetModerationRuns(Datatype):
players: list[Player]
regions: list[Region]
runs: list[Run]
values: list[Value]
variables: list[Variable]
users: list[User]

class r_GetNotifications(Datatype):
unreadCount: int
Expand Down
36 changes: 17 additions & 19 deletions test/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
article_slug = "the-worlds-first-speedrunning-dog-at-agdq-2024"
challenge_id = "5e3eoymq"
challenge_run_id = "m9vk8gy3"
conversation_id = "4xEDO" # ManicJamie <-> Hornet_Bot

logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger().addHandler(logging.FileHandler("testing.log", "w"))
Expand Down Expand Up @@ -386,17 +387,14 @@ def test_GetCommentable(self):
log_result(result)
check_datatype_coverage(result)

@pytest.mark.skip("method stub")
def test_GetConversationMessages(self):
result = GetConversationMessages(_api=self.api).perform()
result = GetConversationMessages(_api=self.api, conversationId=conversation_id).perform()
log_result(result)
check_datatype_coverage(result)

@pytest.mark.skip("method stub")
def test_GetConversationMessages_unauthed(self):
result = GetConversationMessages().perform()
log_result(result)
check_datatype_coverage(result)
with pytest.raises(Unauthorized):
GetConversationMessages(conversationId=conversation_id).perform()

def test_GetConversations(self):
result = GetConversations(_api=self.api).perform()
Expand All @@ -413,39 +411,33 @@ def test_GetForumReadStatus(self):
log_result(result)
check_datatype_coverage(result)

@pytest.mark.skip(reason="Test stub")
def test_GetForumReadStatus_unauthed(self):
result = GetForumReadStatus(forumIds=[forum_id]).perform()
log_result(result)
check_datatype_coverage(result)

@pytest.mark.skip(reason="Test stub")
def test_GetGameSettings(self):
result = ...
result = GetGameSettings(_api=self.api, gameId=game_id).perform()
log_result(result)
check_datatype_coverage(result)

@pytest.mark.skip(reason="Test stub")
def test_GetGameSettings_unauthed(self):
result = ...
log_result(result)
check_datatype_coverage(result)
with pytest.raises(Unauthorized) as e:
GetGameSettings(gameId=game_id).perform()

@pytest.mark.skip(reason="Test stub")
def test_GetModerationGames(self):
result = ...
result = GetModerationGames(_api=self.api).perform()
log_result(result)
check_datatype_coverage(result)

@pytest.mark.skip(reason="Test stub")
def test_GetModerationRuns(self):
result = ...
result = GetModerationRuns(_api=self.api, gameId=game_id).perform()
log_result(result)
check_datatype_coverage(result)

@pytest.mark.skip(reason="Test stub")
def test_GetModerationRuns_paginated(self):
result = ...
result = GetModerationRuns(_api=self.api, gameId=game_id).perform_all()
log_result(result)
check_datatype_coverage(result)

Expand All @@ -454,10 +446,16 @@ def test_GetModerationRuns_paginated_raw(self):
result = ...
log_result(result)
check_datatype_coverage(result)

@pytest.mark.skip(reason="Test stub")
def test_GetModerationRuns_unauthed(self):
result = ...
log_result(result)
check_datatype_coverage(result)

@pytest.mark.skip(reason="Test stub")
def test_GetNotifcations(self):
result = ...
result = result = GetNotifications(_api=self.api).perform()
log_result(result)
check_datatype_coverage(result)

Expand Down

0 comments on commit 68f5062

Please sign in to comment.