Skip to content

fix: guard against falsy-value traps in psycopg2, chromadb, weaviate mocks#780

Open
koushik1359 wants to merge 1 commit intorocketride-org:developfrom
koushik1359:fix/mock-truthiness-748
Open

fix: guard against falsy-value traps in psycopg2, chromadb, weaviate mocks#780
koushik1359 wants to merge 1 commit intorocketride-org:developfrom
koushik1359:fix/mock-truthiness-748

Conversation

@koushik1359
Copy link
Copy Markdown

@koushik1359 koushik1359 commented May 7, 2026

Problem

Three test mocks mishandled falsy values (0, False, 0.0), causing tests
to pass against mocks while failing against real services in production.

psycopg2bool subclasses int in Python, so isinstance(False, int)
returns True. SQL WHERE params like isDeleted=False were being matched
as LIMIT values in _handle_semantic_search and _extract_limit.

chromadbif limit and ... treated limit=0 as falsy, returning
all rows instead of zero rows when get(limit=0) was called.

weaviate — Sort key distance if distance else 1.0 collapsed 0.0
(exact match, the best possible cosine distance) to 1.0, ranking
perfect matches last instead of first.

Fix

  • psycopg2: added not isinstance(p, bool) guard at both int-detection sites
  • chromadb: replaced if limit with if limit is not None
  • weaviate: replaced if distance with if distance is not None

Resolves #748

Summary by CodeRabbit

  • Tests
    • Improved test mock accuracy for edge cases involving zero and false parameter values, ensuring proper handling in parameter validation and result sorting scenarios.

…mocks

Three test mocks mishandled falsy values, causing mock tests to pass
while real-service tests would fail:

psycopg2: bool is a subclass of int in Python, so isinstance(False, int)
returns True. SQL WHERE params like isDeleted=False were incorrectly
matched as LIMIT values. Fixed by adding not isinstance(p, bool) guards
in _handle_semantic_search and _extract_limit.

chromadb: 'if limit and ...' treated limit=0 as falsy, returning all
rows instead of zero rows. Fixed with explicit 'limit is not None' check.

weaviate: sort key 'distance if distance else 1.0' collapsed 0.0
(exact match, best possible distance) to 1.0, ranking perfect matches
last. Fixed with 'distance if distance is not None else 1.0'.

Resolves rocketride-org#748
@github-actions github-actions Bot added the module:nodes Python pipeline nodes label May 7, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2026

No description provided.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

This PR corrects three Python truthiness and isinstance bugs across test mocks for ChromaDB, PostgreSQL, and Weaviate. The fixes replace falsy-based conditionals and type checks with explicit None guards and boolean exclusions to ensure zero-valued parameters and distances are treated as valid.

Changes

Test Mock Truthiness and isinstance Fixes

Layer / File(s) Summary
ChromaDB Limit Parameter
nodes/test/mocks/chromadb/__init__.py
MockCollection.get() limit check now uses limit is not None instead of truthiness, so limit=0 correctly enforces zero-row results.
PostgreSQL Parameter Type Checking
nodes/test/mocks/psycopg2/__init__.py
Semantic search and limit extraction now require isinstance(p, int) and not isinstance(p, bool) to prevent booleans from being interpreted as numeric limits.
Weaviate Vector Distance Sorting
nodes/test/mocks/weaviate/__init__.py
MockQuery.near_vector sort key now checks metadata.distance is not None instead of truthiness, so zero-distance exact matches are not collapsed into fallback scores.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related issues

  • #747: Issue requesting these identical fixes across all three test mocks (chromadb, psycopg2, weaviate) to correct truthiness/isinstance traps in parameter handling and sorting logic.

Poem

🐰 Hop, hop, the mocks now see—
Zero's valid, booleans flee!
No more truthiness in the way,
Distances sort right today.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely summarizes the main change: fixing falsy-value handling bugs in three test mocks.
Linked Issues check ✅ Passed All three objectives from issue #748 are fully met: psycopg2 mock guards against booleans, chromadb mock treats limit=0 correctly, and weaviate mock preserves 0.0 distances.
Out of Scope Changes check ✅ Passed All changes are scoped to the three specified mock files with minimal, targeted fixes; no unrelated modifications or scope creep detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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 and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
nodes/test/mocks/psycopg2/__init__.py (1)

155-158: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Same falsy-value trap remains in _handle_select_queryif limit: should be if limit is not None:.

_extract_limit returns Optional[int], so a non-boolean integer 0 in the params list would be returned as a valid limit but then silently skipped here because if limit: is if 0:False. This is the identical class of bug being fixed across the other three mocks in this PR, and it lives in the same file.

🐛 Proposed fix
-        if limit:
+        if limit is not None:
             results = results[:limit]
🤖 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 `@nodes/test/mocks/psycopg2/__init__.py` around lines 155 - 158, In
_handle_select_query, the LIMIT check uses a falsy test that skips valid 0
limits; change the conditional that currently reads "if limit:" to explicitly
check for None (i.e., "if limit is not None:") so a returned integer 0 from
_extract_limit is applied; locate the _handle_select_query function and the
_extract_limit helper and update the limit application accordingly.
nodes/test/mocks/chromadb/__init__.py (1)

174-186: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

limit=0 still yields 1 result — check must precede the append.

The item is appended at line 174 before the guard at line 185. When limit=0, the check evaluates 0 is not None and len(results['ids']) >= 0True on the very first iteration — but only after the first item has already been added. The PR's stated goal of "return 0 rows for limit=0" is not achieved.

🐛 Proposed fix — move the limit guard before the append
+            # Apply limit (guard before append so limit=0 correctly returns empty)
+            if limit is not None and len(results['ids']) >= limit:
+                break
+
             results['ids'].append(id)
             if 'metadatas' in include:
                 results['metadatas'].append(self._serialize_metadata(metadata))
@@ -182,9 +182,6 @@
             if 'embeddings' in include:
                 results['embeddings'].append(data.get('embedding'))

             count += 1

-            # Apply limit
-            if limit is not None and len(results['ids']) >= limit:
-                break
🤖 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 `@nodes/test/mocks/chromadb/__init__.py` around lines 174 - 186, The loop
appends an item to results['ids'] (and optionally
metadatas/documents/embeddings) before checking the limit, so limit=0 still
returns one row; move the limit guard that checks "if limit is not None and
len(results['ids']) >= limit" to the top of the iteration (before appending) so
no items are added when the current length already meets the limit; update the
block around results['ids'].append(id) /
results['metadatas'].append(self._serialize_metadata(metadata)) /
results['documents'].append(document) /
results['embeddings'].append(data.get('embedding')) to perform the limit check
first and then append, keeping the existing count increment and break behavior.
🤖 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.

Outside diff comments:
In `@nodes/test/mocks/chromadb/__init__.py`:
- Around line 174-186: The loop appends an item to results['ids'] (and
optionally metadatas/documents/embeddings) before checking the limit, so limit=0
still returns one row; move the limit guard that checks "if limit is not None
and len(results['ids']) >= limit" to the top of the iteration (before appending)
so no items are added when the current length already meets the limit; update
the block around results['ids'].append(id) /
results['metadatas'].append(self._serialize_metadata(metadata)) /
results['documents'].append(document) /
results['embeddings'].append(data.get('embedding')) to perform the limit check
first and then append, keeping the existing count increment and break behavior.

In `@nodes/test/mocks/psycopg2/__init__.py`:
- Around line 155-158: In _handle_select_query, the LIMIT check uses a falsy
test that skips valid 0 limits; change the conditional that currently reads "if
limit:" to explicitly check for None (i.e., "if limit is not None:") so a
returned integer 0 from _extract_limit is applied; locate the
_handle_select_query function and the _extract_limit helper and update the limit
application accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: e1eabc41-818e-4c12-a216-cdd1c555cc5f

📥 Commits

Reviewing files that changed from the base of the PR and between 5c88da5 and 5f831bb.

📒 Files selected for processing (3)
  • nodes/test/mocks/chromadb/__init__.py
  • nodes/test/mocks/psycopg2/__init__.py
  • nodes/test/mocks/weaviate/__init__.py

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

Labels

module:nodes Python pipeline nodes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant