Skip to content

test: add unit tests for CardUtils#20385

Open
Ayush-Patel-56 wants to merge 1 commit intoankidroid:mainfrom
Ayush-Patel-56:test-cardutils
Open

test: add unit tests for CardUtils#20385
Ayush-Patel-56 wants to merge 1 commit intoankidroid:mainfrom
Ayush-Patel-56:test-cardutils

Conversation

@Ayush-Patel-56
Copy link
Contributor

Purpose / Description

Added unit tests for CardUtils. This class had no test coverage before.

Fixes

Approach

Created CardUtilsTest.kt with 7 test cases covering both public methods:

getNotes():

  • Empty collection handling
  • Single card scenario
  • Multiple cards from same note (verifies unique filtering)
  • Multiple cards from different notes

getDeckIdForCard():

  • Regular deck (oDid = 0)
  • Filtered/cram deck (oDid handling)
  • Explicit zero oDid edge case

How Has This Been Tested?

Ran unit tests locally:
./gradlew :AnkiDroid:testPlayDebugUnitTest --tests "com.ichi2.anki.CardUtilsTest"
All 7 tests passed with 0 failures.

Learning (optional, can help others)

  • Studied existing test patterns from ImportUtilsTest and NoteServiceTest to match project conventions
  • Used RobolectricTest base class which provides helper methods like addBasicNote() and addDynamicDeck()
  • Learned that filtered decks use the oDid field to store the original deck ID when cards are moved to cram decks

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers!

In future: It's a huge timesaver to see if code is unused. We've still got... 15 years of cruft (many of which my own) to deal with.

Fun fact:

This code came from 2018, when multiselect in the Card Browser was added: d98f676. It was in Java at the time.

@Test
fun getNotes_emptyCollection_returnsEmptySet() {
val cards = emptyList<com.ichi2.anki.libanki.Card>()
val notes = CardUtils.getNotes(col, cards)
Copy link
Member

@david-allison david-allison Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ getNotes is unused, and should be deleted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done - removed the trivial empty collection test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method in CardUtils should be deleted, not just the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, removed getNotes() from CardUtils.kt as well

@david-allison david-allison added the Needs Author Reply Waiting for a reply from the original author label Mar 4, 2026
@Ayush-Patel-56 Ayush-Patel-56 force-pushed the test-cardutils branch 2 times, most recently from 985ae14 to 37cca2e Compare March 5, 2026 04:27
Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you're not using an LLM for this? The comments seem very redundant, as they feel t be duplicated in the assertion message

val note = addBasicNote("Front", "Back")
val card = note.firstCard()

// Verify oDid is 0 for regular decks
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Verify oDid is 0 for regular decks

col.sched.rebuildFilteredDeck(filteredDid)
card.load()

// In a filtered deck, oDid should be set
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// In a filtered deck, oDid should be set

val note = addBasicNote("Front", "Back")
val card = note.firstCard()

// Move card to a filtered deck
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Move card to a filtered deck

val note = addBasicNote("Front", "Back")
val card = note.firstCard()

// Regular deck has oDid = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Regular deck has oDid = 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Author Reply Waiting for a reply from the original author Needs Review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants