Skip to content

test: add unit tests for get_sections_for_filings and get_diffs_for_filings batch queries #2

Description

@AshayK003

Problem

db.py has two batch query functions that accept a list of filing IDs and enforce a 500-item limit via ValueError. Neither the validation path nor the actual query behavior is tested.

Functions to Test

get_sections_for_filings(conn, filing_ids)diffiq/db.py:180

  • Raises ValueError when len(filing_ids) > 500
  • Returns {} for empty list
  • Returns sections grouped by filing_id

get_diffs_for_filings(conn, filing_ids, stock_id)diffiq/db.py:262

  • Raises ValueError when len(filing_ids) > 500
  • Returns {} for empty list
  • Returns diffs grouped by filing_id_new, filtered by stock_id

Suggested Test Structure

class TestGetSectionsForFilings:
    def test_empty_list_returns_empty_dict(self, db):
        """Empty filing_ids list returns empty dict."""
        result = get_sections_for_filings(db, [])
        assert result == {}

    def test_raises_on_too_many_ids(self, db):
        """ValueError raised when filing_ids exceeds 500."""
        with pytest.raises(ValueError, match="max 500"):
            get_sections_for_filings(db, list(range(501)))

    def test_groups_sections_by_filing(self, db):
        """Sections are grouped by filing_id in the result dict."""
        stock_id = upsert_stock(db, "500295", "VEDL")
        f1 = insert_filing(db, stock_id, "u1", "2026-01-01", "F1", "https://ex.com/1.pdf")
        f2 = insert_filing(db, stock_id, "u2", "2026-01-02", "F2", "https://ex.com/2.pdf")

        insert_sections(db, f1, [
            {"header": "Opinion", "text": "v1", "chunk_hash": None, "page_num": 1, "section_idx": 0},
        ])
        insert_sections(db, f2, [
            {"header": "Opinion", "text": "v2", "chunk_hash": None, "page_num": 1, "section_idx": 0},
            {"header": "Basis", "text": "b2", "chunk_hash": None, "page_num": 2, "section_idx": 1},
        ])
        db.commit()

        result = get_sections_for_filings(db, [f1, f2])
        assert f1 in result
        assert f2 in result
        assert len(result[f1]) == 1
        assert len(result[f2]) == 2


class TestGetDiffsForFilings:
    def test_empty_list_returns_empty_dict(self, db):
        """Empty filing_ids list returns empty dict."""
        result = get_diffs_for_filings(db, [], stock_id=1)
        assert result == {}

    def test_raises_on_too_many_ids(self, db):
        """ValueError raised when filing_ids exceeds 500."""
        with pytest.raises(ValueError, match="max 500"):
            get_diffs_for_filings(db, list(range(501)), stock_id=1)

    def test_filters_by_stock_id(self, db):
        """Only diffs matching the given stock_id are returned."""
        s1 = upsert_stock(db, "500295", "VEDL")
        s2 = upsert_stock(db, "500180", "HDFCBANK")
        f1 = insert_filing(db, s1, "u1", "2026-01-01", "F1", "https://ex.com/1.pdf")
        f2 = insert_filing(db, s2, "u2", "2026-01-02", "F2", "https://ex.com/2.pdf")

        # Insert sections for FK
        db.execute("INSERT INTO sections (id, filing_id, header, text, section_idx) VALUES (1, ?, 'X', 'x', 0)", (f1,))
        db.execute("INSERT INTO sections (id, filing_id, header, text, section_idx) VALUES (2, ?, 'Y', 'y', 0)", (f2,))

        insert_diff(db, {"stock_id": s1, "filing_id_new": f1, "section_id_new": 1,
                          "section_header": "X", "diff_text": "d1", "changed": 1})
        insert_diff(db, {"stock_id": s2, "filing_id_new": f2, "section_id_new": 2,
                          "section_header": "Y", "diff_text": "d2", "changed": 1})
        db.commit()

        result = get_diffs_for_filings(db, [f1, f2], stock_id=s1)
        assert f1 in result
        assert f2 not in result  # Different stock_id

Verification

python -m pytest tests/test_db.py -v

Difficulty

Good First Issue — straightforward validation tests, uses existing fixtures and patterns from tests/test_db.py.

Labels

good first issue, tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomerstestsTest coverage improvements

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions