── OPTIONAL READING · AIOS DEEP OPERATOR CERTIFICATION ──
test_job_board.py
About 2 min read
Copy the source below into a file with this exact name.
"""The exercise's functional contract. These tests prove behaviour, not looks.
Keep every test passing while you redesign. They pass on the plain starting page and they must still pass on your
redesign: that is the point. A redesign that keeps them green can still look wrong, and only the screenshot
comparison against your agreed target can tell you that.
Run from this folder: python3 -m unittest -v
"""
import json
import unittest
from html.parser import HTMLParser
from pathlib import Path
import job_board as render
HERE = Path(__file__).resolve().parent
VOID = {"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source", "track", "wbr"}
class Element:
def __init__(self, tag, attrs):
self.tag, self.attrs, self.text = tag, dict(attrs), ""
class Page(HTMLParser):
"""Every element with its attributes and the text inside it."""
def __init__(self, markup):
super().__init__()
self.elements, self.stack = [], []
self.feed(markup)
def handle_starttag(self, tag, attrs):
element = Element(tag, attrs)
self.elements.append(element)
if tag not in VOID:
self.stack.append(element)
def handle_endtag(self, tag):
for i in range(len(self.stack) - 1, -1, -1):
if self.stack[i].tag == tag:
del self.stack[i:]
return
def handle_data(self, data):
for element in self.stack:
element.text += data
def having(self, attr, value=None):
return [e for e in self.elements if attr in e.attrs and (value is None or e.attrs[attr] == value)]
def load():
return json.loads((HERE / "jobs.json").read_text())
class ContractTest(unittest.TestCase):
def setUp(self):
self.data = load()
self.page = Page(render.render(self.data))
def test_every_job_appears_exactly_once_with_a_deep_link_anchor(self):
for job in self.data["jobs"]:
found = self.page.having("data-job-id", str(job["id"]))
self.assertEqual(len(found), 1, job["id"])
self.assertEqual(found[0].attrs.get("id"), f"job-{job['id']}")
self.assertEqual(found[0].attrs.get("data-status"), job["status"])
def test_status_counts_match_the_snapshot(self):
for key, _ in render.STATUSES:
shown = self.page.having("data-count", key)
self.assertEqual(len(shown), 1, key)
expected = sum(1 for j in self.data["jobs"] if j["status"] == key)
self.assertEqual(shown[0].text.strip(), str(expected), key)
def test_owner_decisions_open_the_external_quote_tool_and_nothing_approves_in_page(self):
links = {e.attrs["href"]: e for e in self.page.having("data-external", "quote")}
for job in self.data["jobs"]:
if job["quote_url"]:
self.assertIn(job["quote_url"], links, job["id"])
self.assertIn(self.data["quote_tool"], links[job["quote_url"]].text)
for element in self.page.elements:
if element.tag in ("button", "a", "input"):
label = (element.text + " " + element.attrs.get("value", "") + " " +
element.attrs.get("aria-label", "")).lower()
self.assertNotIn("approve", label, "approval happens in the quote tool, not on this page")
def test_blocked_jobs_say_blocked_and_show_their_reason(self):
reasons = [e.text.strip() for e in self.page.having("data-blocker")]
for job in self.data["jobs"]:
if job["status"] == "blocked":
self.assertIn(job["blocker"], reasons)
card = self.page.having("data-job-id", str(job["id"]))[0]
self.assertIn("Blocked", card.text)
def test_each_mechanic_shows_their_open_job_count(self):
for name in self.data["mechanics"]:
shown = self.page.having("data-mechanic", name)
self.assertEqual(len(shown), 1, name)
expected = sum(1 for j in self.data["jobs"] if j["mechanic"] == name and j["status"] != "ready")
self.assertEqual(shown[0].attrs.get("data-open-count"), str(expected), name)
def test_the_snapshot_time_is_shown(self):
shown = self.page.having("data-snapshot")
self.assertEqual(len(shown), 1)
self.assertIn(self.data["snapshot"], shown[0].text)
def test_an_empty_snapshot_says_so(self):
empty = dict(self.data, jobs=[])
page = Page(render.render(empty))
self.assertEqual(len(page.having("data-empty")), 1)
for key, _ in render.STATUSES:
self.assertEqual(page.having("data-count", key)[0].text.strip(), "0")
if __name__ == "__main__":
unittest.main()