Open lesson

Mission: the returning bug

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 tests for ordinary results, boundaries and impossible inputs, and keep a check so a fixed bug cannot quietly return.

In the evidence room, Mara has ten minutes in the story before the meeting. Voss has prepared an accusation against your tool. Three batches arrive: A has 6 rows and 5 IDs; B has 0 and 0; C has 2 rows and 3 IDs. An old bug returned -1 for C. Prepare checks Mara can show Imani.

Your mission

  1. Calculate A’s expected result and write an assert.
  2. Give B’s expected reaction and explain why it is valid.
  3. Give C’s expected reaction and show the matching unittest check.
  4. Name the test to keep after the fix and say what it cannot prove.

How to check your work

  • A gives 1 and assert repeats(6, 5) == 1 passes.
  • B gives 0: no receipt in an empty batch can repeat.
  • C raises ValueError, never -1.
  • The rejection test for C stays in the suite; receipt provenance needs a separate inquiry.

Course example

Workspace version: the three tests are loaded explicitly from their class, then run in the browser.

import unittest

def repeats(rows, distinct):
    if rows < 0 or distinct < 0 or distinct > rows:
        raise ValueError('impossible count')
    return rows - distinct

assert repeats(5, 4) == 1

class TestRepeats(unittest.TestCase):
    def test_normal(self):
        self.assertEqual(repeats(5, 4), 1)

    def test_boundary(self):
        self.assertEqual(repeats(0, 0), 0)

    def test_invalid(self):
        with self.assertRaises(ValueError):
            repeats(3, 4)

if __name__ == '__main__':
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestRepeats)
    result = unittest.TextTestRunner().run(suite)
    assert result.wasSuccessful()

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.

# Calculate A’s expected result and write an `assert`.
# Give B’s expected reaction and explain why it is valid.
# Give C’s expected reaction and show the matching `unittest` check.
# Name the test to keep after the fix and say what it cannot prove.

exemple.py

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

import unittest

def repeats(rows, distinct):
    if rows < 0 or distinct < 0 or distinct > rows:
        raise ValueError('impossible count')
    return rows - distinct

assert repeats(5, 4) == 1

class TestRepeats(unittest.TestCase):
    def test_normal(self):
        self.assertEqual(repeats(5, 4), 1)

    def test_boundary(self):
        self.assertEqual(repeats(0, 0), 0)

    def test_invalid(self):
        with self.assertRaises(ValueError):
            repeats(3, 4)

if __name__ == '__main__':
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestRepeats)
    result = unittest.TextTestRunner().run(suite)
    assert result.wasSuccessful()