Skip to content

test: add database metadata and YOLO mapping coverage#1343

Open
priyanshunitr wants to merge 1 commit into
AOSSIE-Org:mainfrom
priyanshunitr:database-coverage-1238
Open

test: add database metadata and YOLO mapping coverage#1343
priyanshunitr wants to merge 1 commit into
AOSSIE-Org:mainfrom
priyanshunitr:database-coverage-1238

Conversation

@priyanshunitr

@priyanshunitr priyanshunitr commented Jun 25, 2026

Copy link
Copy Markdown

Addressed Issues:

Fixes #1238

Screenshots/Recordings:

Screenshot 2026-06-26 021940

TODO: If applicable, add screenshots or recordings that demonstrate the interface before and after the changes.

Additional Notes:

Adds focused backend test coverage for:

  • backend/app/database/metadata.py
  • backend/app/database/yolo_mapping.py

The tests cover metadata table creation, default/empty/invalid metadata states, metadata updates with own and existing cursors, rollback behavior, YOLO class mapping insertion, idempotency, replacement behavior, empty class lists, and duplicate class names.

This PR keeps the scope limited to the metadata and YOLO mapping database modules discussed in #1238.

14 passed in 0.55s
118 passed in 18.58s

### AI Usage Disclosure:

We encourage contributors to use AI tools responsibly when creating Pull Requests. While AI can be a valuable aid, it is essential to ensure that your contributions meet the task requirements, build successfully, include relevant tests, and pass all linters. Submissions that do not meet these standards may be closed without warning to maintain the quality and integrity of the project. Please take the time to understand the changes you are proposing and their impact. AI slop is strongly discouraged and may lead to banning and blocking. Do not spam our repos with AI slop.

Check one of the checkboxes below:

- [ ] This PR does not contain AI-generated code at all.
- [x] This PR contains AI-generated code. I have read the [AI Usage Policy](https://github.com/AOSSIE-Org/Info/blob/main/AI-UsagePolicy.md) and this PR complies with this policy. I have tested the code locally and I am responsible for it.

I have used the following AI models and tools: ChatGPT


## Checklist
<!-- Mark items with [x] to indicate completion -->
- [x] My PR addresses a single issue, fixes a single bug or makes a single improvement.
- [x] My code follows the project's code style and conventions
- [x] If applicable, I have made corresponding changes or additions to the documentation
- [x] If applicable, I have made corresponding changes or additions to tests
- [x] My changes generate no new warnings or errors
- [x] I have joined the [Discord server](https://discord.gg/hjUhu33uAn) and I will share a link to this PR with the project maintainers there
- [x] I have read the [Contribution Guidelines](../CONTRIBUTING.md)
- [x] Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.
- [x] I have filled this PR template completely and carefully, and I understand that my PR may be closed without review otherwise.

@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds pytest coverage for SQLite-backed metadata persistence and YOLO class mapping behavior, using temporary databases and monkeypatched database paths.

Changes

Database persistence test coverage

Layer / File(s) Summary
Metadata persistence
backend/tests/test_metadata.py
Creates a temporary metadata database fixture and tests table initialization, metadata reads for missing/invalid values, update replacement, cursor handling, and rollback on JSON serialization failure.
YOLO mapping persistence
backend/tests/test_yolo_mapping.py
Adds a temporary YOLO mapping database fixture and tests ordered inserts, repeated creation, replacement after class list changes, empty class lists, and duplicate class names.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

Python

Poem

A bunny hopped through rows and keys,
In SQLite fields, both neat and free.
🐇 JSON, mappings, tests align,
Two little databases now shine.
Hop hop—coverage blooms for thee!

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: added database test coverage for metadata and YOLO mapping.
Linked Issues check ✅ Passed The new tests address the linked issue by covering previously untested database functions and increasing backend database coverage.
Out of Scope Changes check ✅ Passed The PR stays within scope by adding tests only for the metadata and YOLO mapping database modules.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/tests/test_metadata.py`:
- Around line 118-128: The current test in
test_update_metadata_rolls_back_when_json_serialization_fails only exercises
json.dumps failure before any database writes, so it does not verify
transactional rollback. Update this test to trigger an exception after
db_update_metadata has started its DELETE/INSERT path in
metadata_db.db_update_metadata, such as by mocking a failing execute or commit
on the database connection, then assert db_get_metadata still returns the
original_metadata. Keep the test focused on the rollback behavior in
backend/app/database/metadata.py and cover the critical transactional path the
PR is meant to protect.

In `@backend/tests/test_yolo_mapping.py`:
- Around line 45-64: Add a regression test for stale YOLO mappings in
db_create_YOLO_classes_table by first seeding the database with existing class
rows, then shrinking yolo_mapping_db.class_names to a shorter list or [] and
calling the method again. Use fetch_mappings on yolo_database to assert removed
classes are actually deleted, not just overwritten, so the test covers the
replacement/clear behavior that currently leaks stale class IDs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e4c5a742-ddec-4199-ad79-ab4b1fd40c8a

📥 Commits

Reviewing files that changed from the base of the PR and between b3eee2c and 36334dd.

📒 Files selected for processing (2)
  • backend/tests/test_metadata.py
  • backend/tests/test_yolo_mapping.py

Comment on lines +118 to +128
# Validates that update failures roll back without deleting previous metadata.
def test_update_metadata_rolls_back_when_json_serialization_fails(metadata_database):
metadata_db.db_create_metadata_table()
original_metadata = {"safe": True}

assert metadata_db.db_update_metadata(original_metadata) is True

with pytest.raises(TypeError):
metadata_db.db_update_metadata({"bad": object()})

assert metadata_db.db_get_metadata() == original_metadata No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

This doesn't actually test rollback after a write begins.

backend/app/database/metadata.py serializes with json.dumps() before it runs DELETE/INSERT, so object() fails before any database state changes. That means this test only proves serialization failure leaves the old row untouched; it will not catch a regression in the transactional rollback path the PR summary calls out. Please force an error after the delete/insert sequence starts (for example by injecting a failing execute/commit) and then assert the original metadata is restored. As per path instructions, verify that all critical functionality is covered by tests.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/tests/test_metadata.py` around lines 118 - 128, The current test in
test_update_metadata_rolls_back_when_json_serialization_fails only exercises
json.dumps failure before any database writes, so it does not verify
transactional rollback. Update this test to trigger an exception after
db_update_metadata has started its DELETE/INSERT path in
metadata_db.db_update_metadata, such as by mocking a failing execute or commit
on the database connection, then assert db_get_metadata still returns the
original_metadata. Keep the test focused on the rollback behavior in
backend/app/database/metadata.py and cover the critical transactional path the
PR is meant to protect.

Source: Path instructions

Comment on lines +45 to +64
# Validates that existing class IDs are replaced when class names change.
def test_create_yolo_classes_table_replaces_existing_class_names(
yolo_database, monkeypatch
):
monkeypatch.setattr(yolo_mapping_db, "class_names", ["old-person", "old-car"])
yolo_mapping_db.db_create_YOLO_classes_table()

monkeypatch.setattr(yolo_mapping_db, "class_names", ["person", "car"])
yolo_mapping_db.db_create_YOLO_classes_table()

assert fetch_mappings(yolo_database) == [(0, "person"), (1, "car")]


# Validates that table creation succeeds when no YOLO classes are configured.
def test_create_yolo_classes_table_handles_empty_class_list(yolo_database, monkeypatch):
monkeypatch.setattr(yolo_mapping_db, "class_names", [])

yolo_mapping_db.db_create_YOLO_classes_table()

assert fetch_mappings(yolo_database) == []

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Add a shrink/clear regression case for stale mappings.

These tests only cover replacement when the new class_names list has the same length, plus an empty list on a fresh database. In backend/app/database/yolo_mapping.py, db_create_YOLO_classes_table() never deletes rows that disappear from class_names, so a populated table followed by ["person"] or [] will leave stale class IDs behind. Since the PR objective explicitly includes replacement behavior and empty class lists, please add a test that starts with existing rows and then shrinks or clears the list. As per path instructions, verify that all critical functionality is covered by tests.

Suggested regression test
+def test_create_yolo_classes_table_removes_stale_rows_when_class_list_shrinks(
+    yolo_database, monkeypatch
+):
+    monkeypatch.setattr(yolo_mapping_db, "class_names", ["person", "car"])
+    yolo_mapping_db.db_create_YOLO_classes_table()
+
+    monkeypatch.setattr(yolo_mapping_db, "class_names", ["person"])
+    yolo_mapping_db.db_create_YOLO_classes_table()
+
+    assert fetch_mappings(yolo_database) == [(0, "person")]
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Validates that existing class IDs are replaced when class names change.
def test_create_yolo_classes_table_replaces_existing_class_names(
yolo_database, monkeypatch
):
monkeypatch.setattr(yolo_mapping_db, "class_names", ["old-person", "old-car"])
yolo_mapping_db.db_create_YOLO_classes_table()
monkeypatch.setattr(yolo_mapping_db, "class_names", ["person", "car"])
yolo_mapping_db.db_create_YOLO_classes_table()
assert fetch_mappings(yolo_database) == [(0, "person"), (1, "car")]
# Validates that table creation succeeds when no YOLO classes are configured.
def test_create_yolo_classes_table_handles_empty_class_list(yolo_database, monkeypatch):
monkeypatch.setattr(yolo_mapping_db, "class_names", [])
yolo_mapping_db.db_create_YOLO_classes_table()
assert fetch_mappings(yolo_database) == []
# Validates that existing class IDs are replaced when class names change.
def test_create_yolo_classes_table_replaces_existing_class_names(
yolo_database, monkeypatch
):
monkeypatch.setattr(yolo_mapping_db, "class_names", ["old-person", "old-car"])
yolo_mapping_db.db_create_YOLO_classes_table()
monkeypatch.setattr(yolo_mapping_db, "class_names", ["person", "car"])
yolo_mapping_db.db_create_YOLO_classes_table()
assert fetch_mappings(yolo_database) == [(0, "person"), (1, "car")]
# Validates that table creation succeeds when no YOLO classes are configured.
def test_create_yolo_classes_table_handles_empty_class_list(yolo_database, monkeypatch):
monkeypatch.setattr(yolo_mapping_db, "class_names", [])
yolo_mapping_db.db_create_YOLO_classes_table()
assert fetch_mappings(yolo_database) == []
def test_create_yolo_classes_table_removes_stale_rows_when_class_list_shrinks(
yolo_database, monkeypatch
):
monkeypatch.setattr(yolo_mapping_db, "class_names", ["person", "car"])
yolo_mapping_db.db_create_YOLO_classes_table()
monkeypatch.setattr(yolo_mapping_db, "class_names", ["person"])
yolo_mapping_db.db_create_YOLO_classes_table()
assert fetch_mappings(yolo_database) == [(0, "person")]
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/tests/test_yolo_mapping.py` around lines 45 - 64, Add a regression
test for stale YOLO mappings in db_create_YOLO_classes_table by first seeding
the database with existing class rows, then shrinking
yolo_mapping_db.class_names to a shorter list or [] and calling the method
again. Use fetch_mappings on yolo_database to assert removed classes are
actually deleted, not just overwritten, so the test covers the replacement/clear
behavior that currently leaks stale class IDs.

Source: Path instructions

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feat: Add test for database

1 participant