{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c00-043a1253",
   "metadata": {},
   "source": [
    "# Testing Kafka applications — practical\n",
    "\n",
    "Billing's tests live in `module-09/tests/`, in four layers: the charge calculation alone, the\n",
    "service with fake clients, the transactional flow against a real Kafka in a container, and the\n",
    "schema's compatibility. The notebook runs each layer with pytest, then replays the refactor\n",
    "that under-charged drivers and watches the tests catch it.\n",
    "\n",
    "**Lab services:** none of the lab's; the integration test starts its own Kafka container (needs\n",
    "Docker) · **Lesson:** `modules/09-production-ready/02-testing-kafka-applications.md`"
   ]
  },
  {
   "cell_type": "code",
   "id": "c01-103e2077",
   "metadata": {},
   "source": [
    "# Setup: find the lab's shared helpers\n",
    "import sys, pathlib\n",
    "lab_root = next(p for p in [pathlib.Path.cwd(), *pathlib.Path.cwd().parents] if (p / \"common\").is_dir())\n",
    "sys.path.insert(0, str(lab_root))\n",
    "\n",
    "import json, os, shutil, subprocess\n",
    "\n",
    "def pytest(*args, env=None):\n",
    "    \"\"\"Run pytest from the code/ folder, as you would in a terminal or in CI, and show its report.\"\"\"\n",
    "    print(\"$ python -m pytest \" + \" \".join(args))\n",
    "    result = subprocess.run([sys.executable, \"-m\", \"pytest\", *args, \"-p\", \"no:cacheprovider\", \"--color=no\",\n",
    "                             \"--no-header\"], cwd=lab_root, capture_output=True, text=True,\n",
    "                            env={**os.environ, \"PY_COLORS\": \"0\", **(env or {})})\n",
    "    print(result.stdout.strip())"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c02-af40264d",
   "metadata": {},
   "source": [
    "## Step 1: Unit tests of the charge\n",
    "`charge_for()` is a plain function: no Kafka, no network. Seven hand-picked cases, then all 20,000\n",
    "scenario sessions at the 0.50 €/kWh tariff, compared with exact decimal arithmetic."
   ]
  },
  {
   "cell_type": "code",
   "id": "c03-a1bfd9ec",
   "metadata": {},
   "source": [
    "pytest(\"module-09/tests/test_charge.py\", \"-v\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c04-e7cb56b5",
   "metadata": {},
   "source": [
    "## Step 2: The service with fake clients\n",
    "`BillingService` gets its consumer and producer from outside. Fakes record what it asks of them:\n",
    "the order of the transaction's steps, and what happens when a write fails."
   ]
  },
  {
   "cell_type": "code",
   "id": "c05-f7488c45",
   "metadata": {},
   "source": [
    "pytest(\"module-09/tests/test_billing_with_fakes.py\", \"-v\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c06-dbd55100",
   "metadata": {},
   "source": [
    "## Step 3: The transactional flow against a real Kafka\n",
    "The `kafka_bootstrap` fixture starts Kafka 4.3.1 in a throwaway container. The test writes 40\n",
    "sessions, lets billing crash on its 25th charge, restarts it, and reads the charges as an auditor\n",
    "with `read_committed`."
   ]
  },
  {
   "cell_type": "code",
   "id": "c07-0a00fecc",
   "metadata": {},
   "source": [
    "pytest(\"module-09/tests/test_billing_integration.py\", \"-v\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c08-8dc38131",
   "metadata": {},
   "source": [
    "## Step 4: Schema compatibility\n",
    "The tests compare the MeterReading schema in the repo with the published version. Then they try\n",
    "a proposed change: a new field `phase_count`, first without a default, then with one."
   ]
  },
  {
   "cell_type": "code",
   "id": "c09-e2c272c1",
   "metadata": {},
   "source": [
    "pytest(\"module-09/tests/test_schema_compatibility.py\", \"-q\")\n",
    "\n",
    "schema = json.loads((lab_root / \"schemas\" / \"meter_reading.avsc\").read_text())\n",
    "proposals = {\"no default\": {\"name\": \"phase_count\", \"type\": \"int\"},\n",
    "             \"default null\": {\"name\": \"phase_count\", \"type\": [\"null\", \"int\"], \"default\": None}}\n",
    "for label, field in proposals.items():\n",
    "    path = lab_root / \".lab-data\" / \"proposed-meter-reading.avsc\"\n",
    "    path.parent.mkdir(exist_ok=True)\n",
    "    path.write_text(json.dumps({**schema, \"fields\": schema[\"fields\"] + [field]}))\n",
    "    print(f\"\\n--- proposed: phase_count with {label}\")\n",
    "    pytest(\"module-09/tests/test_schema_compatibility.py\", \"-q\", \"--tb=line\",\n",
    "           env={\"METER_READING_SCHEMA\": str(path)})"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c10-434c3adc",
   "metadata": {},
   "source": [
    "## Break it: the refactor\n",
    "A refactor replaced the decimal calculation with one line: `round(energy * price, 2)`. Simpler,\n",
    "and right for whole numbers. The notebook applies it to a copy of `billing.py` and runs the unit\n",
    "tests against the copy."
   ]
  },
  {
   "cell_type": "code",
   "id": "c11-ac6ccd52",
   "metadata": {},
   "source": [
    "refactor = lab_root / \".lab-data\" / \"refactor\"\n",
    "refactor.mkdir(parents=True, exist_ok=True)\n",
    "code = (lab_root / \"module-09\" / \"billing.py\").read_text()\n",
    "before = '\"amount_eur\": str(amount.quantize(CENT, rounding=ROUND_HALF_UP))}'\n",
    "after = '\"amount_eur\": f\"{round(stopped[\\'energy_kwh\\'] * float(price_eur_per_kwh), 2):.2f}\"}'\n",
    "(refactor / \"billing.py\").write_text(code.replace(before, after))\n",
    "print(\"the refactor:\", after)\n",
    "pytest(\"module-09/tests/test_charge.py\", \"-q\", \"--tb=line\", env={\"BILLING_DIR\": str(refactor)})"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c12-1ffad9e8",
   "metadata": {},
   "source": [
    "## Step 5: The whole suite, as CI runs it\n",
    "Everything, and then only the fast tests, the ones to run on every save."
   ]
  },
  {
   "cell_type": "code",
   "id": "c13-cf20a2ef",
   "metadata": {},
   "source": [
    "pytest(\"module-09/tests\", \"-q\")\n",
    "pytest(\"module-09/tests\", \"-q\", \"-m\", \"not integration\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c14-60e487b4",
   "metadata": {},
   "source": [
    "## Clean up"
   ]
  },
  {
   "cell_type": "code",
   "id": "c15-716c2696",
   "metadata": {},
   "source": [
    "shutil.rmtree(refactor, ignore_errors=True)\n",
    "(lab_root / \".lab-data\" / \"proposed-meter-reading.avsc\").unlink(missing_ok=True)\n",
    "print(\"cleaned up\")"
   ],
   "execution_count": null,
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
