Open lesson

Mission: stop the fragile report

Command palette

This mission workspace keeps code and notes in this browser tab. Export them before closing it. Running code is practice, not an automatic grade for the mission.

Brief

Write positive and negative tests for the v2 contract and explain how CI can block publication if the rule regresses.

The corrected report is due to Imani. Just before release, a supplier-reader code change arrives; Voss insists on publishing anyway. Mara lets you inspect five practice checks: complete E8, missing operator, empty operator, numeric operator, and old version. Explain what change they would catch and why release should wait for a fix. The meeting is urgent in the story, but the exercise has no real time limit.

Your mission

  1. Classify the five tests as positive or negative.
  2. Explain the result if the reader stops checking operator_id.
  3. Describe CI steps between code change and publication.
  4. Name two claims that all-green tests still cannot support.

How to check your work

  • One positive and four negative tests are identified.
  • Removing the check fails at least one negative test.
  • CI reports failure and publication waits for correction and review.
  • Tests prove neither data authenticity nor a real operator's identity.

Course example

import unittest

def accepts(event):
    if not isinstance(event, dict) or event.get("schema_version") != 2:
        return False
    for field in ("event_id", "event_type", "event_at", "vehicle_label",
                  "operator_id", "source_file"):
        if not isinstance(event.get(field), str) or not event[field].strip():
            return False
    return True

GOOD = {
    "schema_version": 2, "event_id": "E8", "event_type": "CHECK",
    "event_at": "2026-04-13T11:50:00Z", "vehicle_label": "NOCTIS-01",
    "operator_id": "OP-8", "source_file": "supplier-page3.json"
}

class ContractTests(unittest.TestCase):
    def test_complete_v2_is_accepted(self):
        self.assertTrue(accepts(GOOD))

    def test_missing_operator_is_rejected(self):
        bad = {key: value for key, value in GOOD.items() if key != "operator_id"}
        self.assertFalse(accepts(bad))

    def test_empty_operator_is_rejected(self):
        self.assertFalse(accepts({**GOOD, "operator_id": "  "}))

    def test_wrong_operator_type_is_rejected(self):
        self.assertFalse(accepts({**GOOD, "operator_id": 17}))

    def test_old_version_is_rejected_by_v2_reader(self):
        self.assertFalse(accepts({**GOOD, "schema_version": 1}))

suite = unittest.defaultTestLoader.loadTestsFromTestCase(ContractTests)
result = unittest.TextTestRunner(verbosity=1).run(suite)
if not result.wasSuccessful():
    raise SystemExit(1)

Code

Use Tab to leave the editor. Ctrl+Enter runs the source. The lines beginning with # are task reminders. Python ignores them. Write your code beneath them.

Python console

Loading laboratory…

Explore Python or SQL here; this is not an operating-system shell.

This is a disposable browser experiment. The code run here is not recorded as a completed mission.

Each command starts a fresh disposable worker: it runs a captured copy of your current source, then your command with the lesson fixtures. Changes are discarded afterward. Commands never save source or award XP. Ctrl+Enter executes the command.

Virtual workspace files

Source and declarative assets are readable here without JavaScript. Python input files are created at runtime; SQL setup initializes the disposable database.

source.py

Current editor source. Inspection shows your current edits.

# Classify the five tests as positive or negative.
# Explain the result if the reader stops checking operator_id.
# Describe CI steps between code change and publication.
# Name two claims that all-green tests still cannot support.

exemple.py

Declarative lesson asset; this is not a file created in the Python filesystem.

import unittest

def accepts(event):
    if not isinstance(event, dict) or event.get("schema_version") != 2:
        return False
    for field in ("event_id", "event_type", "event_at", "vehicle_label",
                  "operator_id", "source_file"):
        if not isinstance(event.get(field), str) or not event[field].strip():
            return False
    return True

GOOD = {
    "schema_version": 2, "event_id": "E8", "event_type": "CHECK",
    "event_at": "2026-04-13T11:50:00Z", "vehicle_label": "NOCTIS-01",
    "operator_id": "OP-8", "source_file": "supplier-page3.json"
}

class ContractTests(unittest.TestCase):
    def test_complete_v2_is_accepted(self):
        self.assertTrue(accepts(GOOD))

    def test_missing_operator_is_rejected(self):
        bad = {key: value for key, value in GOOD.items() if key != "operator_id"}
        self.assertFalse(accepts(bad))

    def test_empty_operator_is_rejected(self):
        self.assertFalse(accepts({**GOOD, "operator_id": "  "}))

    def test_wrong_operator_type_is_rejected(self):
        self.assertFalse(accepts({**GOOD, "operator_id": 17}))

    def test_old_version_is_rejected_by_v2_reader(self):
        self.assertFalse(accepts({**GOOD, "schema_version": 1}))

suite = unittest.defaultTestLoader.loadTestsFromTestCase(ContractTests)
result = unittest.TextTestRunner(verbosity=1).run(suite)
if not result.wasSuccessful():
    raise SystemExit(1)