Skip to content

Add in-memory MongoDB mock; unit-test WordRepository; replace WordRepositoryMock#4195

Closed
Copilot wants to merge 3 commits intomt2from
copilot/sub-pr-4194
Closed

Add in-memory MongoDB mock; unit-test WordRepository; replace WordRepositoryMock#4195
Copilot wants to merge 3 commits intomt2from
copilot/sub-pr-4194

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 7, 2026

MongoDbContextMock.Db threw NotSupportedException, making it impossible to unit-test WordRepository directly. Tests relied on WordRepositoryMock, a hand-written stub that bypassed all real query logic.

Approach: custom in-memory IMongoDatabase

Rather 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> — stores BsonDocuments; evaluates FilterDefinition<T> via a BSON filter evaluator supporting $and, $in, $exists, $elemMatch, and dot-notation. Auto-generates ObjectIds when Id = "", mirroring real MongoDB behavior.
  • InMemoryMongoDatabase — lazily creates InMemoryMongoCollection<T> instances keyed by collection name.
  • MongoDbContextMockDb now returns InMemoryMongoDatabase; transaction mock provides a Moq-based non-null IClientSessionHandle required by MongoDB extension method null guards.

WordRepository unit tests

Backend.Tests/Repositories/WordRepositoryTests.cs — 37 tests exercising the real WordRepository against the in-memory backend: CRUD, frontier operations, IsInFrontier/AreInFrontier, domain counts, audio filters, and transaction rollback paths.

WordRepositoryTestHelper replaces WordRepositoryMock

WordRepositoryTestHelper wraps the real WordRepository (delegates all IWordRepository methods) and re-exposes the test-only surface previously on WordRepositoryMock:

// Test-only helpers, not on IWordRepository
Task<Word> Add(Word word)                  // insert into words only
Task<Word> AddFrontier(Word word)          // single-word frontier insert
Task DeleteAllWords(string projectId)
void SetGetFrontierDelay(Task<bool> delay) // for concurrency tests

All seven test suites (WordControllerTests, AudioControllerTests, MergeControllerTests, LiftControllerTests, WordServiceTests, StatisticsServiceTests, MergeServiceTests) updated to use WordRepositoryTestHelper.

Util.RandomWord() now produces valid ObjectId-format Ids (required by [BsonRepresentation(BsonType.ObjectId)]). MergeServiceTests hardcoded 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 Reviewable

Copilot AI and others added 2 commits March 7, 2026 13:11
- 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
@imnasnainaec
Copy link
Copy Markdown
Collaborator

This was exploratory. Maintaining this fully mocked MongoDb looks like more work than keeping the necessary complexities of WordRepository synced up WordRepositoryMock.

@imnasnainaec imnasnainaec deleted the copilot/sub-pr-4194 branch March 19, 2026 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants