Today is a public holiday. I was working on a feature for a somewhat urgent work project anyway. The system manages
leads, which are referenced from all over the codebase. So we maintain a denormalised state record for each lead to
make lookups trivial. My feature required that state to have an effective_generated_at field, which is the lead’s
generated_at timestamp, but falls back to created_at if it’s not set. I just wanted to get the actual feature done
rather than touching the underlying state synchronisation logic.
The implementation is trivial enough. It’s a lookup on the lead, which is already in scope when the denormalisation code
runs. I described the context to my clanker1, linked to similar code, and instructed it to cover the populate path
for existing database records to be backfilled. The denormalisation is triggered through Django Signals, which is
indirect and easy to miss in future changes. So I also asked for tests as a safety net that would cover the fallback
logic and backfill.
I had been arguing with fellow developers for months that tests should mirror human intention, so they shouldn’t be written by clankers. What did I do? Submit the prompt, of course.
Four test cases came back. There were minor discrepancies like unnecessary abbreviations (ts), missing @freezetime,
and formatting differences, but those aren’t relevant to our story. What is relevant is that the populate test deletes
all denormalised state records and re-creates them. The code should populate existing records. We only verifed that
it can create new ones.
The tests run. They cover the relevant code paths. They are a safety net for the wrong thing. Ouch…
class TestLeadDenormalizedState(TestCase):
def test_populate__effective_generated_at__uses_generated_at(self):
ts = datetime(2026, 1, 15, 9, 0, 0, tzinfo=timezone.utc)
lead = LeadFactory(generated_at=ts)
LeadDenormalizedState.objects.all().delete()
LeadDenormalizedState.populate()
state = LeadDenormalizedState.objects.get(lead=lead)
self.assertEqual(state.effective_generated_at, ts)The tests are fine now. I rewrote them to be the actual safety net I intended. What I can’t rewrite though is that I now know what I’ll do when I’m impatient and a deadline is closing in.
And the worst of it? It’s the opposite of what I’ve been telling everyone else to do.
-
After reading Clanker: A Word For The Machine by Armin Ronacher I adopted the term into my own head. ↩︎