Skip to content

Commit

Permalink
fix: integration test regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
thearchitector committed Apr 11, 2024
1 parent a37fc7f commit 3e05095
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ examples/readme.py
docsrc/_build
docs/.doctrees
setup_tests.sh
test.csv

.venv
.vscode/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ You will also need the following env variables set for the Exchange integration
3. Install the client
`pip3 install --editable .`
4. Install test deps
`pip3 install "pytest>=5.2.1" "requests-mock>=1.8.0" "pytest-asyncio"`
`pip3 install "pytest<8" "requests-mock>=1.8.0" "pytest-asyncio>0.21"`
5. Run tests
`pytest -sv --host <indico_host> tests/`
_ Only run unit tests `pytest -sv --host <indico_host> tests/unit/`
Expand Down
19 changes: 8 additions & 11 deletions indico/client/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ def kwargs(self) -> "AnyDict":
return {"json": {"query": self.query, "variables": self.variables}}

def parse_payload(self, response: "AnyDict") -> "Any":
# obliterates the return typing so that new queries can type without
# needing to cast/ignore the super, and so that legacy untyped impls
# can continue to use `process_response`
return GraphQLRequest.process_response(self, response)

def process_response(self, response: "AnyDict") -> "ResponseType":
raw_response: "AnyDict" = cast("AnyDict", super().process_response(response))
errors: "List[AnyDict]" = raw_response.pop("errors", [])

Expand All @@ -67,8 +61,12 @@ def process_response(self, response: "AnyDict") -> "ResponseType":
extras=extras,
)

return raw_response["data"]

def process_response(self, response: "AnyDict") -> "ResponseType":
raw_response = self.parse_payload(response)
# technically incorrect, but necessary for backwards compatibility
return cast("ResponseType", raw_response["data"])
return cast("ResponseType", raw_response)


class PagedRequest(GraphQLRequest[ResponseType]):
Expand Down Expand Up @@ -100,8 +98,8 @@ def __init__(self, query: str, variables: "Optional[AnyDict]" = None):
self.has_next_page = True
super().__init__(query, variables=variables)

def process_response(self, response: "AnyDict") -> "ResponseType":
raw_response: "AnyDict" = cast("AnyDict", super().process_response(response))
def parse_payload(self, response: "AnyDict") -> "Any":
raw_response: "AnyDict" = cast("AnyDict", super().parse_payload(response))

_pg = next(iter(raw_response.values())).get("pageInfo")
if not _pg:
Expand All @@ -112,8 +110,7 @@ def process_response(self, response: "AnyDict") -> "ResponseType":
_pg["endCursor"] if self.has_next_page else None
)

# technically incorrect, but necessary for backwards compatibility
return cast("ResponseType", raw_response)
return raw_response


class RequestChain(Generic[ResponseType]):
Expand Down
9 changes: 6 additions & 3 deletions indico/queries/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ class _DocumentExtraction(GraphQLRequest["List[Job]"]):
def __init__(
self,
files: "List[AnyDict]",
json_config: "Optional[AnyDict]" = {"preset_config": "legacy"},
json_config: "Optional[Union[AnyDict, str]]" = {"preset_config": "legacy"},
ocr_engine: "Optional[str]" = None,
):
json_config_json: "Optional[str]" = None
if json_config and isinstance(json_config, dict):
json_config_json = json.dumps(json_config)
if json_config:
if isinstance(json_config, dict):
json_config_json = json.dumps(json_config)
else:
json_config_json = json_config

super().__init__(
query=self.query,
Expand Down
24 changes: 18 additions & 6 deletions indico/queries/model_groups/model_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

from indico.client.request import Delay, GraphQLRequest, RequestChain
from indico.errors import IndicoNotFound

# backwards compat
from indico.queries.workflow_components import AddModelGroupComponent # noqa: F401
from indico.types.jobs import Job
from indico.types.model import Model, ModelOptions
from indico.types.model_group import ModelGroup
Expand Down Expand Up @@ -193,11 +196,14 @@ def __init__(
self,
model_id: int,
data: "List[str]",
predict_options: "Optional[AnyDict]" = None,
predict_options: "Optional[Union[str, AnyDict]]" = None,
):
predict_options_json: "Optional[str]" = None
if predict_options:
predict_options_json = json.dumps(predict_options)
if isinstance(predict_options, dict):
predict_options_json = json.dumps(predict_options)
else:
predict_options_json = predict_options

query = self._args_strings(
model_id=model_id, data=data, predict_options=predict_options_json
Expand Down Expand Up @@ -295,16 +301,22 @@ class UpdateModelGroupSettings(GraphQLRequest["ModelOptions"]):
def __init__(
self,
model_group_id: int,
model_training_options: "Optional[AnyDict]" = None,
predict_options: "Optional[AnyDict]" = None,
model_training_options: "Optional[Union[str, AnyDict]]" = None,
predict_options: "Optional[Union[str, AnyDict]]" = None,
):
model_training_options_json: "Optional[str]" = None
if model_training_options:
model_training_options_json = json.dumps(model_training_options)
if isinstance(model_training_options, dict):
model_training_options = json.dumps(model_training_options)
else:
model_training_options = model_training_options

predict_options_json: "Optional[str]" = None
if predict_options:
predict_options_json = json.dumps(predict_options)
if isinstance(predict_options, dict):
predict_options_json = json.dumps(predict_options)
else:
predict_options_json = predict_options

super().__init__(
self.query,
Expand Down
9 changes: 6 additions & 3 deletions indico/queries/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,16 +495,19 @@ class SubmitReview(GraphQLRequest["Job"]):
def __init__(
self,
submission: "Union[int, Submission]",
changes: "Optional[Union[AnyDict, List[AnyDict]]]" = None,
changes: "Optional[Union[str, AnyDict, List[AnyDict]]]" = None,
rejected: bool = False,
force_complete: "Optional[bool]" = None,
):
changes_json: "Optional[str]" = None
submission_id = submission if isinstance(submission, int) else submission.id
if not changes and not rejected:
raise IndicoInputError("Must provide changes or reject=True")
elif changes and isinstance(changes, (dict, list)):
changes_json = json.dumps(changes)
elif changes:
if isinstance(changes, (dict, list)):
changes_json = json.dumps(changes)
else:
changes_json = changes

_vars = {
"submissionId": submission_id,
Expand Down
7 changes: 5 additions & 2 deletions indico/queries/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ class GetWorkflow(GraphQLRequest["Workflow"]):
"""

def __init__(self, workflow_id: int):
self.list_workflows = ListWorkflows(workflow_ids=[workflow_id])
super().__init__(
ListWorkflows.query,
variables={"datasetIds": None, "workflowIds": [workflow_id], "limit": 100},
)

def process_response(self, response: "Payload") -> "Workflow":
return self.list_workflows.process_response(response)[0]
return Workflow(**super().parse_payload(response)["workflows"]["workflows"][0])


class _ToggleReview(GraphQLRequest["Workflow"]):
Expand Down
9 changes: 6 additions & 3 deletions indico/queries/workflow_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)

if TYPE_CHECKING: # pragma: no cover
from typing import Iterator, List, Optional
from typing import Iterator, List, Optional, Union

from indico.typing import AnyDict, Payload

Expand Down Expand Up @@ -317,7 +317,7 @@ def __init__(
labelset_column_id: "Optional[int]" = None,
new_labelset_args: "Optional[NewLabelsetArguments]" = None,
new_questionnaire_args: "Optional[NewQuestionnaireArguments]" = None,
model_training_options: "Optional[AnyDict]" = None,
model_training_options: "Optional[Union[str, AnyDict]]" = None,
model_type: "Optional[str]" = None,
):
if labelset_column_id is not None and new_labelset_args is not None:
Expand All @@ -332,7 +332,10 @@ def __init__(

model_training_options_json: "Optional[str]" = None
if model_training_options:
model_training_options_json = jsons.dumps(model_training_options)
if isinstance(model_training_options, dict):
model_training_options_json = jsons.dumps(model_training_options)
else:
model_training_options_json = model_training_options

super().__init__(
self.query,
Expand Down
2 changes: 1 addition & 1 deletion indico/types/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, **kwargs: "Any"):
if attr_type == JSONType and v is not None:
v = json.loads(v)

if attr_type == datetime:
if attr_type == datetime and v is not None:
try:
v = datetime.fromtimestamp(float(v))
except ValueError:
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Setup for indico apis
"""

from pathlib import Path

from setuptools import find_packages, setup
Expand All @@ -18,7 +19,7 @@
url="https://github.com/IndicoDataSolutions/indico-client-python",
author="indico",
author_email="[email protected]",
tests_require=["pytest>=5.2.1", "requests-mock>=1.8.0", "pytest-asyncio"],
tests_require=["pytest<8", "requests-mock>=1.8.0", "pytest-asyncio>0.21"],
install_requires=[
"msgpack>=0.5.6",
"msgpack-numpy==0.4.4.3",
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ envlist = py38,py39,py310,py311,py312
parallel_show_output = true
# install pytest in the virtualenv where commands will be executed
deps =
pytest
pytest-asyncio
pytest < 8
pytest-asyncio > 0.21
requests-mock >= 1.8.0
mypy == 1.8
typing_extensions
Expand Down

0 comments on commit 3e05095

Please sign in to comment.