Repository navigation
refactor!: rewrite the mock as a stateful fake backed by SQLite - #4
Merged
Merged
Conversation
The rewrite types its handlers and checks its responses against the spec. sherpa-api is private, so a committed copy keeps builds and CI working without access to it.
The mock reread its JSON files on every request and rewrote whole files on writes, so concurrent writes could lose updates or corrupt a file. Data now lives in SQLite, seeded from the fixtures on first start, and survives restarts. The data directory holds the database instead of the JSON files, and the undocumented profile image endpoints are gone.
Tables were only created when missing, so a schema change never reached an existing database. Staging keeps guests created through the API, so resetting on a change isn't an option.
Profile queries sat in the route file while user queries had their own module. Both now live next to each other, seeding moves out of the database module, and the profile insert is shared between seeding and the create endpoint.
The handlers only checked that fields were present, so a body with a wrong type, like a null image on a profile update, crashed with a 500 instead of Sherpa's 422. Checking against the spec's request schemas replaces the hand-written checks, and one error handler now answers in the error format of the API the request went to.
A non-numeric cursor compared as zero and returned every user from the start.
Database calls run one at a time, so the test couldn't fail.
Each of Sherpa's APIs has its own spec section and error format, so a folder per API keeps an endpoint, its errors and its tests together, apart from the data store they share.
The fixtures used to be served as they were, so profiles sat apart and every object carried its toType. Now only the seed reads them, so a user's file holds everything about that user and the mock adds the toType when it answers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The mock served JSON files and saved a profile change by rewriting the whole file, so two writes at the same time could lose an update or leave a broken file. Nothing checked its data or its answers against the Sherpa spec, and the guest account endpoints still to come would have been built on top of it.
Summary
The mock is now a fake that keeps state: users, memberships, role assignments and profiles live in SQLite, so a write shows up in later reads and survives a restart.
Deployment
The data folder now holds the database instead of JSON files, and the image no longer ships a data folder.