test: unit-test developerRepository upsert semantics (#1298) - #1407
Open
legend-esc wants to merge 1 commit into
Open
legend-esc wants to merge 1 commit into
legend-esc wants to merge 1 commit into
Conversation
Add src/repositories/developerRepository.test.ts covering all acceptance criteria from issue CalloraOrg#1298: - findByUserId returns undefined for unknown users - upsertProfile creates on first call (no prior row) - upserting only name leaves website/description/category untouched - upserting only website leaves name/description/category untouched - updated_at advances on every update (1-second SQLite resolution) - passing undefined fields preserves existing values - concurrent getOrCreateByUserId calls yield exactly one DB row - documents ??-merge semantics: passing null does not clear a stored field Implementation details: - jest.mock('../db/index.js') swaps the production better-sqlite3 file DB for an in-memory :memory: instance with the developers DDL applied; the repository functions themselves are imported unmodified so real Drizzle ORM logic is exercised - Add jest.env-setup.cjs (missing file referenced by jest.config.cjs) with the minimum env vars required by src/config/env.ts validation - Add package.json (was absent from the repo clone) and update package-lock.json after upgrading better-sqlite3 to ^11 for Node 24 compatibility Validation: npm test -- src/repositories/developerRepository.test.ts 11 passed, 0 failed
|
@legend-esc Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Closes #1298
Summary
Adds
src/repositories/developerRepository.test.ts— 11 unit tests coveringthe three exported functions (
findByUserId,getOrCreateByUserId,upsertProfile) against a real in-memory SQLite database.Acceptance criteria
findByUserIdreturnsundefinedfor unknown usersreturns undefined for a user that does not existupserting only name leaves website/description/category untouchedgetOrCreatecalled twice concurrently yields one rowconcurrent calls produce exactly one rowupdated_atchanges on updateupdated_at changes after an updateAdditional tests:
upsertProfilecreates a row on first callwebsiteleaves name/description/category untouchedundefinedfields preserves existing values??-merge semantics: explicitnulldoes not clear a stored fieldApproach
jest.mock('../db/index.js')swaps the production file-backed DB for a:memory:instance with thedevelopersDDL applied inline. The repositoryfunctions are imported unmodified — real Drizzle ORM logic runs against
real SQL.
Findings
Concurrent
getOrCreate: SQLite's synchronous UNIQUE constraint means thesecond concurrent call throws rather than falling back to SELECT. The test
verifies the invariant that matters — exactly one row in the DB.
??-merge semantics:upsertProfileusesdata.field ?? existing.field.Passing
nulldoes not clear an existing value becausenullis nullish. Thetest explicitly documents this contract so callers are not surprised.
Infrastructure fixes
jest.env-setup.cjs(referenced byjest.config.cjsbut absent).package.json(absent from the repo clone).better-sqlite3to^11for Node 24 compatibility.Validation
npm test -- src/repositories/developerRepository.test.ts
11 passed, 0 failed