How do you test a publishing rule without the database?
Question
Editors publish playlists of video lessons. A playlist can go live only if:
- it has at least one lesson,
- every lesson is approved,
- the total length is 45 minutes or less.
Today a web handler loads ORM objects, checks this rule and calls commit(). Every test needs a running database.
- Split the code so you can test the rule with plain Python data.
- Another editor adds a lesson between your read and your write. How do you avoid publishing a version the rule never checked?
Show hints (3)
- "WHAT THE RULE NEEDS"
The rule needs only lesson lengths and approval flags. What if it took a plain list and returned a result, with no database at all?
- "WHO CALLS STORAGE"
Let one small function run the steps and take the store as an argument. Then what can a test pass in place of the database?
- "THE SECOND EDITOR"
Remember the revision number you loaded. Publish only if the playlist is still at that revision.
Show checked explanation
Make the rule a pure function: plain data in, a result out. A service function checks permission, loads the playlist, applies the rule and saves through a store it gets as an argument. Tests pass a fake store. For the second editor, publish only if the revision is still the one you loaded.
def check_playlist(lessons, limit=45): if not lessons: return "empty" if not all(lesson["approved"] for lesson in lessons): return "not_approved" if sum(lesson["minutes"] for lesson in lessons) > limit: return "too_long" return "ok" def lesson(minutes, approved=True): return {"minutes": minutes, "approved": approved} print(check_playlist([lesson(18), lesson(11), lesson(9)])) print(check_playlist([lesson(18), lesson(11), lesson(9), lesson(8)])) print(check_playlist([])) print(check_playlist([lesson(18), lesson(5, approved=False)]))
ok too_long empty not_approved
Code run on CPython 3.12 with a fake store, and the SQL run on SQLite. Checked by an editor other than the author.
Walkthrough
Split the code into three parts.
The rule is a pure function: it uses only its arguments and changes nothing. Each test is one line of data. Lessons of 18, 11 and 9 minutes add up to 38: "ok". Add an 8-minute lesson and the total is 46: "too_long".
The service function (often called a use case) runs the steps. It gets the store as an argument:
def publish(user, playlist_id, store): if not user.can_publish: return "forbidden" # before any read revision, lessons = store.load(playlist_id) verdict = check_playlist(lessons) if verdict != "ok": return verdict return store.publish_if_revision(playlist_id, revision)
It needs only load and publish_if_revision. You can describe them with a typing.Protocol.
The real store, often called an adapter, turns those two methods into SQL. Tests pass a fake: a small in-memory class that logs each call.
Now the second editor. You load revision 12, and the rule says "ok". Before your write, another editor adds an unapproved lesson and saves revision 13. publish_if_revision(playlist_id, 12) sees 13, changes nothing and returns "conflict". This is optimistic concurrency: nothing is locked, but the write succeeds only if nobody changed the data since your read. On a conflict, reload and check again. With no other edit, the store publishes and moves the revision from 12 to 13.
The fake's log for each case:
| case | result | store calls |
|---|---|---|
| no other edit | published | load, publish_if_revision |
| other edit first | conflict | load, publish_if_revision |
| 46 minutes | too_long | load |
| empty playlist | empty | load |
| no permission | forbidden | none |
Common mistakes
- Passing ORM objects into the rule. Reading
playlist.lessonscan run a query, so the test needs a database again. Pass plain data. - Checking permission after loading the playlist. A user without the right should not cause any reads.
- Publishing with a plain
UPDATE ... SET status = 'published'. If someone edited in between, you publish a version the rule never saw. Make the write conditional on the revision you loaded. - Trusting the fake for everything. It shows the right calls in the right order, not that the real database does the conditional write atomically. Test the real store on a real database.
Follow-up questions
- "How does the SQL store make the write conditional?" One statement checks and writes:
UPDATE playlists SET status = 'published', revision = revision + 1 WHERE id = ? AND revision = ?. If it changes 0 rows, someone changed the playlist first: return"conflict". This only works if every edit, adding a lesson too, raises the playlist's revision. - "The publish worked, but the response was lost. The client retries. What happens?" The retry sends revision 12, but the playlist is now at 13, so it gets
"conflict". Nothing is published twice, but the client can't tell its first call worked. If it must know, send an idempotency key, a unique id per operation. The store saves the result under that key and returns it on a retry. - "Is a separate layer always worth it?" No. It adds mapping code and one more layer to read. It pays off when the rule changes often or needs many tests. For a simple form that saves what it gets, call the database directly. A folder named
domaindoesn't help if the rule still imports the ORM.
What a strong answer covers
Tests the rule with plain data, the service function with a fake store, and the real store on a real database. Checks permission first. Publishes only if the revision is unchanged.
Open in interactive practice deck →