Merge branch 'r2-backend'
# Conflicts: # app/storage/schema.py
This commit is contained in:
commit
562fa3ab09
15 files changed
+924
-157
No files matched your search
+82
-3
@@ -5,6 +5,7 @@ worker beyond starting and stopping it with the app."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sqlite3
|
||||
from collections.abc import Iterator
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from pathlib import Path
|
||||
@@ -18,6 +19,7 @@ from app.config import Config, ServerConfig
|
||||
from app.main import create_app
|
||||
from app.storage import Store
|
||||
from app.web import MOVED, SETTINGS, TABS
|
||||
from app.web.api import _score_after_change
|
||||
from app.web.pages import SECTIONS
|
||||
from auth import AuthConfig
|
||||
from desk.models import Item
|
||||
@@ -411,7 +413,7 @@ def test_companies_lists_board_and_open_role_count(
|
||||
sub = store.add_subscription("ats", "greenhouse:acme", company="Acme", board="greenhouse")
|
||||
store.add_company("Acme", key="acme", ats="greenhouse", country="DK")
|
||||
store.upsert_role(posting("1", company="Acme"), sub)
|
||||
store.add_company("Other", key="other") # no board found
|
||||
store.add_company("Other", key="other", domain="other.example") # no board found
|
||||
store.add_company("Novo", key="novo", ats="successfactors", # followed, not readable
|
||||
careers_url="https://novo.example/careers")
|
||||
|
||||
@@ -421,13 +423,47 @@ def test_companies_lists_board_and_open_role_count(
|
||||
assert row == {"key": "acme", "name": "Acme", "board": "greenhouse",
|
||||
"subscription_id": sub.id, "on": True, "affinity": 0, "open_roles": 1,
|
||||
"ats": "greenhouse", "ats_slug": "", "careers_url": "",
|
||||
"country": "DK", "failing_since": "", "failing_reason": ""}
|
||||
"country": "DK", "failing_since": "", "failing_reason": "",
|
||||
"from_search": False}
|
||||
[other] = [c for c in res.json() if c["key"] == "other"]
|
||||
assert (other["board"], other["subscription_id"], other["on"], other["ats"]) == (
|
||||
"", None, None, "")
|
||||
[novo] = [c for c in res.json() if c["key"] == "novo"]
|
||||
assert (novo["board"], novo["subscription_id"], novo["ats"], novo["careers_url"]) == (
|
||||
"", None, "successfactors", "https://novo.example/careers")
|
||||
assert not any(c["from_search"] for c in res.json())
|
||||
|
||||
|
||||
def test_judging_a_company_known_only_from_a_search_creates_its_row(
|
||||
client: TestClient, store: Store) -> None:
|
||||
"""Jobindex and Adzuna employers have roles and no company row. The first
|
||||
affinity change makes a minimal one, named by its roles, which the
|
||||
Companies list marks `from_search`, unlike a followed company without a
|
||||
board, and a company whose board was detached."""
|
||||
store.save_agent("A")
|
||||
search = store.add_subscription("search", "jobindex:cph", label="cph", board="jobindex")
|
||||
for i, name in enumerate(("Novo Nordisk A/S", "Novo Nordisk A/S", "NOVO NORDISK A/S")):
|
||||
item = posting(f"n{i}", company=name)
|
||||
item.source = "search"
|
||||
store.upsert_role(item, search)
|
||||
key = "novo-nordisk"
|
||||
assert store.company(key) is None
|
||||
|
||||
res = client.post(f"/api/companies/{key}/affinity", json={"affinity": -1})
|
||||
assert res.status_code == 200, res.text
|
||||
assert (res.json()["name"], res.json()["affinity"]) == ("Novo Nordisk A/S", -1)
|
||||
assert client.post("/api/companies/nobody/affinity", json={"affinity": 1}).status_code == 404
|
||||
|
||||
store.add_company("Pleo", key="pleo", domain="pleo.io") # followed, no board found
|
||||
sub = store.add_subscription("ats", "lever:gone", company="Gone", board="lever")
|
||||
store.add_company("Gone", key="gone")
|
||||
store.set_company_board("gone", "") # detached
|
||||
assert not store.subscription(sub.id).show # type: ignore[union-attr]
|
||||
rows = {c["key"]: c for c in client.get("/api/companies").json()}
|
||||
assert rows["gone"]["board"] == ""
|
||||
assert rows[key]["from_search"] and rows[key]["open_roles"] == 3
|
||||
assert (rows[key]["board"], rows[key]["ats"], rows[key]["careers_url"]) == ("", "", "")
|
||||
assert not rows["pleo"]["from_search"] and not rows["gone"]["from_search"]
|
||||
|
||||
|
||||
def test_companies_say_since_when_a_board_fails(client: TestClient, store: Store,
|
||||
@@ -468,7 +504,7 @@ def test_set_affinity_validates_updates_and_enqueues_a_score(
|
||||
assert res.json()["affinity"] == 2
|
||||
assert store.company("acme", agent.id).affinity == 2 # type: ignore[union-attr]
|
||||
assert store.company("acme", other.id).affinity == 0 # type: ignore[union-attr]
|
||||
assert queue.jobs.last("score", states=("queued", "running", "done")) is not None
|
||||
assert queue.jobs.get(res.json()["score_job"]).kind == "score"
|
||||
|
||||
res = client.post("/api/companies/acme/affinity", json={"affinity": -1, "agent": other.id})
|
||||
assert res.status_code == 200 and res.json()["affinity"] == -1
|
||||
@@ -482,6 +518,49 @@ def test_set_affinity_validates_updates_and_enqueues_a_score(
|
||||
assert client.get("/api/companies", params={"agent": 999}).status_code == 404
|
||||
|
||||
|
||||
def test_a_score_asked_for_during_a_run_is_one_follow_up_after_it(queue: Any) -> None:
|
||||
"""No worker runs here (no client), so the test moves the jobs itself."""
|
||||
first = _score_after_change(queue)
|
||||
assert first is not None and first.dedup_key == "score"
|
||||
assert _score_after_change(queue).id == first.id # queued: it will read the change
|
||||
|
||||
assert queue.jobs.claim({"cpu": 1}).id == first.id
|
||||
during = {_score_after_change(queue).id for _ in range(10)}
|
||||
assert len(during) == 1 and first.id not in during
|
||||
follow = queue.jobs.get(during.pop())
|
||||
assert (follow.depends_on, follow.after, follow.dedup_key) == (
|
||||
[first.id], "done", f"score:after:{first.id}")
|
||||
|
||||
queue.jobs.finish(first.id, "done")
|
||||
assert _score_after_change(queue).id == follow.id # queued, not started: still the one
|
||||
|
||||
assert queue.jobs.claim({"cpu": 1}).id == follow.id
|
||||
after = _score_after_change(queue)
|
||||
assert after.id not in (first.id, follow.id) and after.dedup_key == "score"
|
||||
|
||||
|
||||
def test_score_after_change_retries_the_queues_dedup_race(
|
||||
queue: Any, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""`Jobs.enqueue` re-raises the UNIQUE failure when the live job it
|
||||
collided with finishes before it reads it back."""
|
||||
real, calls = queue.enqueue, []
|
||||
|
||||
def racy(*args: Any, **kwargs: Any) -> Any:
|
||||
calls.append(args)
|
||||
if len(calls) == 1:
|
||||
raise sqlite3.IntegrityError("UNIQUE constraint failed: jobs.dedup_key")
|
||||
return real(*args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(queue, "enqueue", racy)
|
||||
assert _score_after_change(queue) is not None and len(calls) == 2
|
||||
|
||||
|
||||
def test_post_score_answers_with_the_job_to_wait_for(client: TestClient, queue: Any) -> None:
|
||||
res = client.post("/api/score")
|
||||
assert res.status_code == 200
|
||||
assert queue.jobs.get(res.json()["id"]).kind == "score"
|
||||
|
||||
|
||||
# ----------------------------------------------------------- applications
|
||||
|
||||
def test_save_creates_an_application_with_a_saved_event(
|
||||
|
||||
Reference in new issue
Block a user