Skip to content

Commit

Permalink
Update tests to work with pydantic v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ndevenish committed Jul 19, 2024
1 parent 455af44 commit 8306d5e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/zocalo/cli/configure_rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _(self, permissions: PermissionSpec):
@functools.singledispatch
def _info_to_spec(incoming, infos: list):
cls = type(incoming)
return [cls(**i.dict()) for i in infos]
return [cls(**i.model_dump()) for i in infos]


@functools.singledispatch
Expand Down
18 changes: 11 additions & 7 deletions src/zocalo/util/rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ def binding_declare(self, binding: BindingSpec):
endpoint = f"bindings/{binding.vhost}/e/{binding.source}/{binding.destination_type.value}/{binding.destination}"
response = self.post(
endpoint,
json=binding.dict(
json=binding.model_dump(
exclude_defaults=True,
exclude={"vhost", "source", "destination", "destination_type"},
),
Expand Down Expand Up @@ -848,7 +848,7 @@ def exchange_declare(self, exchange: ExchangeSpec):
endpoint = f"exchanges/{exchange.vhost}/{exchange.name}/"
response = self.put(
endpoint,
json=exchange.dict(exclude_defaults=True, exclude={"name", "vhost"}),
json=exchange.model_dump(exclude_defaults=True, exclude={"name", "vhost"}),
)
response.raise_for_status()

Expand All @@ -873,7 +873,7 @@ def set_policy(self, policy: PolicySpec):
endpoint = f"policies/{policy.vhost}/{policy.name}/"
response = self.put(
endpoint,
json=policy.dict(
json=policy.model_dump(
exclude_defaults=True, exclude={"name", "vhost"}, by_alias=True
),
)
Expand Down Expand Up @@ -902,7 +902,8 @@ def queues(
def queue_declare(self, queue: QueueSpec):
endpoint = f"queues/{queue.vhost}/{queue.name}"
response = self.put(
endpoint, json=queue.dict(exclude_defaults=True, exclude={"name", "vhost"})
endpoint,
json=queue.model_dump(exclude_defaults=True, exclude={"name", "vhost"}),
)
response.raise_for_status()

Expand Down Expand Up @@ -942,7 +943,9 @@ def permissions(

def set_permissions(self, permission: PermissionSpec):
endpoint = f"permissions/{permission.vhost}/{permission.user}/"
submission = permission.dict(exclude_defaults=True, exclude={"vhost", "user"})
submission = permission.model_dump(
exclude_defaults=True, exclude={"vhost", "user"}
)
response = self.put(endpoint, json=submission)
response.raise_for_status()

Expand All @@ -953,7 +956,7 @@ def clear_permissions(self, vhost: str, user: str):

def user_put(self, user: UserSpec):
endpoint = f"users/{user.name}/"
submission = user.dict(exclude_defaults=True, exclude={"name"})
submission = user.model_dump(exclude_defaults=True, exclude={"name"})
submission["tags"] = ",".join(submission["tags"])
response = self.put(endpoint, json=submission)
response.raise_for_status()
Expand All @@ -976,7 +979,8 @@ def vhost(self, name: str) -> VHostSpec:
def add_vhost(self, vhost: VHostSpec):
endpoint = f"vhosts/{vhost.name}/"
response = self.put(
endpoint, json=vhost.dict(exclude_defaults=True, exclude={"name", "vhost"})
endpoint,
json=vhost.model_dump(exclude_defaults=True, exclude={"name", "vhost"}),
)
response.raise_for_status()

Expand Down
6 changes: 4 additions & 2 deletions src/zocalo/util/slurm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ def get_job_info(self, job_id: int) -> models.JobInfo:
endpoint = f"slurm/{self.version}/job/{job_id}"
response = self.get(endpoint)
job_info_resp = models.OpenapiJobInfoResp(**response.json())
jobinfo = next(iter(dict(job_info_resp.jobs).get("__root__", [])))
jobinfo = next(iter(dict(job_info_resp.jobs).get("root", [])))
return jobinfo

def submit_job(
self, job_submission: models.JobSubmitReq
) -> models.JobSubmitResponseMsg:
endpoint = f"slurm/{self.version}/job/submit"
response = self.post(endpoint, json=job_submission.dict(exclude_defaults=True))
response = self.post(
endpoint, json=job_submission.model_dump(exclude_defaults=True)
)
return models.JobSubmitResponseMsg(**response.json())
6 changes: 1 addition & 5 deletions tests/util/test_rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ def test_api_binding_declare(requests_mock, rmqapi, binding_spec):
assert history.method == "POST"
assert history.url.endswith("/api/bindings/zocalo/e/foo/q/bar")
assert history.json() == {
"arguments": binding_spec.arguments,
"routing_key": "bar",
}

Expand Down Expand Up @@ -313,7 +312,6 @@ def test_api_exchange_declare(name, requests_mock, rmqapi):
"type": "fanout",
"auto_delete": True,
"durable": True,
"arguments": {},
}


Expand Down Expand Up @@ -511,9 +509,7 @@ def test_api_add_vhost(requests_mock, rmqapi, vhost_spec):
history = requests_mock.request_history[0]
assert history.method == "PUT"
assert history.url.endswith(f"/api/vhosts/{vhost_spec.name}/")
assert history.json() == {
"tags": [],
}
assert history.json() == {}


def test_api_delete_vhost(requests_mock, rmqapi, vhost_spec):
Expand Down
4 changes: 1 addition & 3 deletions tests/util/test_slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ def test_get_job_info(requests_mock, slurm_api, jobs_response):
)
assert slurm_api.get_job_info(129) == next(
iter(
dict(slurm.models.OpenapiJobInfoResp(**jobs_response).jobs).get(
"__root__", []
)
dict(slurm.models.OpenapiJobInfoResp(**jobs_response).jobs).get("root", [])
)
)

0 comments on commit 8306d5e

Please sign in to comment.