Skip to content

feature/add-count-function#7

Merged
cedricve merged 5 commits intomainfrom
feature/add-count-function
Mar 5, 2026
Merged

feature/add-count-function#7
cedricve merged 5 commits intomainfrom
feature/add-count-function

Conversation

@cedricve
Copy link
Member

@cedricve cedricve commented Mar 5, 2026

Description

Motivation

The database abstraction was missing a first‑class way to count documents, forcing callers to either overuse Find/FindOne or reach into database‑specific APIs. This made common use cases (pagination, existence checks, metrics) more awkward and less testable.

Why this improves the project

  • Completes the database interface by adding a Count operation that matches real application needs.
  • Keeps the abstraction consistent: both the MongoDB implementation and the mock now support counting in the same way as other operations.
  • Greatly improves testability with full mock support (queued responses, custom behavior, call tracking, and reset coverage).
  • Enables cleaner, more expressive application code by avoiding workarounds for simple count queries.
  • Minor CI/config cleanup ensures workflows remain up to date and configurable.

Overall, this makes the database layer more complete, easier to test, and more aligned with how it’s actually used.

Add Count(ctx, db, collection, filter, opts...) to DatabaseInterface and implement it for MongoClient and MockDatabase. MongoClient.Count uses Collection.CountDocuments and supports *moptions.CountOptions from opts. MockDatabase is extended with CountFunc, CountQueue, CountCalls, CountResponse and CountCall types, a Count method, ExpectCount and QueueCount helpers, initialization and Reset updates, and comprehensive tests covering default behavior, queued responses, custom CountFunc, errors, and reset behavior.
@codecov
Copy link

codecov bot commented Mar 5, 2026

Codecov Report

❌ Patch coverage is 76.47059% with 8 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/database/mongodb.go 0.00% 7 Missing ⚠️
pkg/database/mock.go 96.29% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@cedricve cedricve merged commit 603d25a into main Mar 5, 2026
5 of 6 checks passed
@cedricve cedricve deleted the feature/add-count-function branch March 5, 2026 16:03
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.

1 participant