Skip to content

Commit

Permalink
Use fstrings for server code
Browse files Browse the repository at this point in the history
  • Loading branch information
plars committed Jan 9, 2025
1 parent 4afcc22 commit 810dbf3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions server/src/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_version():
version = pkg_resources.get_distribution("testflinger").version
except pkg_resources.DistributionNotFound:
version = "devel"
return "Testflinger Server v{}".format(version)
return f"Testflinger Server v{version}"


@v1.post("/job")
Expand Down Expand Up @@ -699,7 +699,7 @@ def job_position_get(job_id):
try:
queue = job_data.json.get("job_queue")
except (AttributeError, TypeError):
return "Invalid json returned for id: {}\n".format(job_id), 400
return f"Invalid json returned for id: {job_id}\n", 400
# Get all jobs with job_queue=queue and return only the _id
jobs = database.mongo.db.jobs.find(
{"job_data.job_queue": queue, "result_data.job_state": "waiting"},
Expand Down
4 changes: 2 additions & 2 deletions server/src/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def create_flask_app(config=None):
@tf_app.errorhandler(NotFound)
def handle_404(exc):
tf_log.error("[404] Not found: %s", request.url)
return "Not found: {}\n".format(exc), 404
return f"Not found: {exc}\n", 404

@tf_app.errorhandler(ConnectionFailure)
def handle_timeout(exc):
Expand All @@ -80,7 +80,7 @@ def handle_timeout(exc):
@tf_app.errorhandler(Exception)
def unhandled_exception(exc):
tf_log.exception("Unhandled Exception: %s", (exc))
return "Unhandled Exception: {}\n".format(exc), 500
return f"Unhandled Exception: {exc}\n", 500

tf_app.register_blueprint(views)
tf_app.register_blueprint(v1, url_prefix="/v1")
Expand Down
12 changes: 6 additions & 6 deletions server/tests/test_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test_initial_job_state(mongo_app):
# Place a job on the queue
output = app.post("/v1/job", json=job_data)
job_id = output.json.get("job_id")
result_url = "/v1/result/{}".format(job_id)
result_url = f"/v1/result/{job_id}"
response = app.get(result_url)
assert "waiting" == response.json.get("job_state")

Expand All @@ -175,7 +175,7 @@ def test_resubmit_job_state(mongo_app):
job_id = output.json.get("job_id")
job_data["job_id"] = job_id
output = app.post("/v1/job", json=job_data)
result_url = "/v1/result/{}".format(job_id)
result_url = f"/v1/result/{job_id}"
updated_data = app.get(result_url).json
assert "waiting" == updated_data.get("job_state")

Expand Down Expand Up @@ -345,7 +345,7 @@ def test_job_get_id_with_data(mongo_app):
# Place a job on the queue
output = app.post("/v1/job", json=job_data)
job_id = output.json.get("job_id")
job_url = "/v1/job/{}".format(job_id)
job_url = f"/v1/job/{job_id}"
# Request the original json for the job
app.get(job_url)
output = app.get(job_url)
Expand All @@ -365,7 +365,7 @@ def test_job_position(mongo_app):
for pos in range(3):
output = app.post("/v1/job", json=job_data)
job_id.append(output.json.get("job_id"))
output = app.get("/v1/job/{}/position".format(job_id[pos]))
output = app.get(f"/v1/job/{job_id[pos]}/position")
# Initial position should increment for each job as we add them
assert output.text == str(pos)

Expand All @@ -374,11 +374,11 @@ def test_job_position(mongo_app):
# The job we get should be the first one that was added
assert output.json.get("job_id") == job_id[0]
# The position of the remaining jobs should decrement
assert app.get("/v1/job/{}/position".format(job_id[2])).text == "1"
assert app.get(f"/v1/job/{job_id[2]}/position").text == "1"
# Cancel the next job in the queue
output = app.post(f"/v1/job/{job_id[1]}/action", json={"action": "cancel"})
# The position of the remaining job should decrement again
assert app.get("/v1/job/{}/position".format(job_id[2])).text == "0"
assert app.get(f"/v1/job/{job_id[2]}/position").text == "0"


def test_action_post(mongo_app):
Expand Down

0 comments on commit 810dbf3

Please sign in to comment.