Skip to content

Add unit tests for the Firestore data layer - #3942

Draft
shobhitagarwal1612 wants to merge 3 commits into
masterfrom
firebase-test-coverage
Draft

shobhitagarwal1612 wants to merge 3 commits into
masterfrom
firebase-test-coverage

Conversation

@shobhitagarwal1612

Copy link
Copy Markdown
Member

Adds unit tests for the firebase data layer, which the narrowed JaCoCo excludes in the
preceding PR made visible. No production code changes.

  • addMutationToBatch on the LOI and submission document references. CREATE/UPDATE
    merge, DELETE deletes, and an unknown type throws — the last branch was never
    exercised.
  • Cancellation handlers. JobCollectionReference, SurveyDocumentReference and
    TermsOfServiceDocumentReference each swallow CancellationException so an aborted
    sync returns empty rather than failing. Now pinned, via a shared canceledTask()
    helper in FirestoreTestUtil.
  • Subcollection wiring for GroundFirestore, SurveyDocumentReference and
    SubmissionCollectionReference.
  • FirebaseStorageManager path building and download URL resolution, plus
    FirestoreUuidGenerator and CaptureLocationResultConverter round-tripping.

11 test files, following the Robolectric + mocked-Firestore pattern already used by
SurveysCollectionReferenceTest.

Coverage

before after
data/remote/firebase 578/800 (72.3%) 669/800 (83.6%)
:app 77.44% 78.26%

+91 lines. Recovers most of the 1.02pp the excludes change costs.

Not covered

FirestoreDataStore still has 63 uncovered lines. Its methods chain
provider → FirebaseFirestore → GroundFirestore → survey → lois, and
subscribeToSurveyUpdates calls the static Firebase.messaging; mocking that depth asserts
little beyond "the mocks were called". Only applyMutations' user-mismatch precondition is
tested, since that one guards a real correctness property.

Verification

./gradlew testLocalDebugUnitTest jvmTest testAndroidHostTest
./gradlew checkCode jacocoTestReport

@andreia-ferreira PTAL?

`JacocoReport` skips rather than fails when its execution data is missing, so a
wrong or unwritten `.exec` path publishes an empty report instead of breaking
the build. Add a `verifyJacocoExecutionData` task that fails when no execution
data was produced. It has to be a separate task because a check inside the
report task would never run once that task is skipped.

Resolve the execution data through a `fileTree` over the known `.exec`
locations rather than a single hardcoded path. Where AGP writes unit test
coverage depends on whether the `jacoco` plugin is applied before or after AGP:
applying it first makes AGP redirect output to `outputs/unit_test_code_coverage`
instead of the Gradle JaCoCo plugin's `build/jacoco` default. A file tree only
matches files that exist, so it picks up whichever path is actually written and
keeps working if that ordering changes.

Rename `jacocoLocalDebugUnitTestReport` to `jacocoTestReport`. The task is
registered for KMP and Android KMP modules too, where it runs `jvmTest` or
`testAndroidHostTest` and has nothing to do with a local debug variant.

Move the JaCoCo version into the version catalog alongside the other pinned
tool versions.
The exclude list had grown to cover hand-written classes alongside generated
ones, which hid real gaps in the coverage report. Measuring a report built with
excludes disabled showed six patterns matching only hand-written Kotlin:
`migration/*`, `firebase/base/*`, `firebase/schema/*Reference*`,
`FirebaseStorageManager*`, `FirestoreDataStore*` and
`LocationSharedFlowCallback*`. Drop them so the 240 lines they hid, 61 of which
are already covered, are measured like any other source.

Replace `**/*Module*` with `**/di/**`. The old pattern matched 159 classes on
substring alone and would have hidden any hand-written class merely named
"...Module..." anywhere in the tree. Every one of its non-generated matches
lives under `di/`, and the generated `..._HiltModule` classes outside it stay
excluded via `**/*Hilt*`. `**/di/**` also picks up `di/coroutines`, which the
old single-segment matching missed.

Cover the generated code that was leaking into the report in the other
direction. Mapping every source file under `app/build/generated` against the
report found Room's auto-migration implementations being measured, so broaden
`**/LocalDatabase_Impl*` to `**/LocalDatabase_*`. Add `**/*_MembersInjector*`,
`**/*_GeneratedInjector*`, `**/*_AssistedFactory*` and `**/*_ComponentTreeDeps*`
for the remaining Dagger artifacts; those carry no lines, but leaving them in
contradicts what this list is for. All ten generated source roots are now fully
excluded.

Reported coverage for `:app` moves from 78.46% to 77.44%, reflecting
previously hidden code rather than any change in what the tests exercise.
Adds unit tests for the firebase data layer, which the narrowed JaCoCo
excludes made visible. Recovers 91 of the 222 uncovered lines in the
package (58.9% -> 83.6%), taking :app from 77.44% to 78.26%.

The three addMutationToBatch implementations are the substantive ones:
each rejects unknown mutation types, a branch nothing exercised before.
The rest pin down subcollection wiring and the CancellationException
handlers that let an aborted sync return empty rather than fail.

Adds a shared canceledTask() helper to FirestoreTestUtil for driving
those cancellation paths.

FirestoreDataStore is only partly covered: its remaining lines chain
several Firestore layers deep or call the static Firebase.messaging,
so only the applyMutations user-mismatch precondition is tested.
@codecov

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.88%. Comparing base (65f0f47) to head (1e3e447).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3942      +/-   ##
============================================
- Coverage     69.99%   69.88%   -0.11%     
- Complexity     2033     2094      +61     
============================================
  Files           424      441      +17     
  Lines         11647    11862     +215     
  Branches       1522     1543      +21     
============================================
+ Hits           8152     8290     +138     
- Misses         2723     2794      +71     
- Partials        772      778       +6     

see 24 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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