Add in-memory MongoDB mock; unit-test WordRepository; replace WordRepositoryMock#4195
Closed
Add in-memory MongoDB mock; unit-test WordRepository; replace WordRepositoryMock#4195
Conversation
- Add 'using MongoDB.Driver.Search' for IMongoSearchIndexManager - Add InsertOneAsync(T, CancellationToken) overload forwarding token - Add ReplaceOne/ReplaceOneAsync overloads accepting UpdateOptions - Wrap MapReduce methods in #pragma warning disable/restore CS0618 - Add DropCollection/DropCollectionAsync overloads with DropCollectionOptions+name - Fix missing 'using MongoDB.Driver' in WordRepositoryTestHelper - Fix using directive issues in WordRepositoryTests (swap Generic for System) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…it tests; replace WordRepositoryMock Co-authored-by: imnasnainaec <6411521+imnasnainaec@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Implement Mongo transactions for error handling
Add in-memory MongoDB mock; unit-test WordRepository; replace WordRepositoryMock
Mar 7, 2026
Collaborator
|
This was exploratory. Maintaining this fully mocked MongoDb looks like more work than keeping the necessary complexities of WordRepository synced up WordRepositoryMock. |
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.
MongoDbContextMock.DbthrewNotSupportedException, making it impossible to unit-testWordRepositorydirectly. Tests relied onWordRepositoryMock, a hand-written stub that bypassed all real query logic.Approach: custom in-memory
IMongoDatabaseRather than Moq (can't evaluate
FilterDefinition<T>— queries always return presets, hiding filter bugs) or EphemeralMongo/Mongo2Go (blocked by CI egress), this PR implements a lightweight in-memory MongoDB layer:InMemoryMongoCollection<T>— storesBsonDocuments; evaluatesFilterDefinition<T>via a BSON filter evaluator supporting$and,$in,$exists,$elemMatch, and dot-notation. Auto-generates ObjectIds whenId = "", mirroring real MongoDB behavior.InMemoryMongoDatabase— lazily createsInMemoryMongoCollection<T>instances keyed by collection name.MongoDbContextMock—Dbnow returnsInMemoryMongoDatabase; transaction mock provides a Moq-based non-nullIClientSessionHandlerequired by MongoDB extension method null guards.WordRepository unit tests
Backend.Tests/Repositories/WordRepositoryTests.cs— 37 tests exercising the realWordRepositoryagainst the in-memory backend: CRUD, frontier operations,IsInFrontier/AreInFrontier, domain counts, audio filters, and transaction rollback paths.WordRepositoryTestHelper replaces WordRepositoryMock
WordRepositoryTestHelperwraps the realWordRepository(delegates allIWordRepositorymethods) and re-exposes the test-only surface previously onWordRepositoryMock:All seven test suites (
WordControllerTests,AudioControllerTests,MergeControllerTests,LiftControllerTests,WordServiceTests,StatisticsServiceTests,MergeServiceTests) updated to useWordRepositoryTestHelper.Util.RandomWord()now produces valid ObjectId-formatIds (required by[BsonRepresentation(BsonType.ObjectId)]).MergeServiceTestshardcoded short IDs ("1","ii", etc.) replaced with named 24-hex-char constants.✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
This change is