Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

test: rewrite constraint_analyzer high-value tests with 29 focused tests (#2579)

LearningCircuitbf6cdaf653de
diff --git a/tests/advanced_search_system/constraints/test_constraint_analyzer_high_value.py b/tests/advanced_search_system/constraints/test_constraint_analyzer_high_value.py
index 5a6fb0a1e0..a0148b5536 100644
--- a/tests/advanced_search_system/constraints/test_constraint_analyzer_high_value.py
+++ b/tests/advanced_search_system/constraints/test_constraint_analyzer_high_value.py
@@ -1,257 +1,345 @@
"""High-value tests for constraint_analyzer.py.
-Covers edge cases and error paths:
-- Model output parsing with unusual formatting
-- Constraint parsing with multi-colon values
-- Weight parsing boundary conditions
-- Empty / whitespace-only model outputs
-- Constraint type fallback behavior
+Covers pure logic in ConstraintAnalyzer:
+- _parse_constraint_type(): all 8 valid types, case insensitivity, unknown defaults
+- _parse_weight(): int, float, string number, string with annotation, empty/no-number
+- extract_constraints(): mocked model, single/multiple constraints, missing fields, IDs
"""
-from unittest.mock import Mock
+from unittest.mock import MagicMock
-
-from local_deep_research.advanced_search_system.constraints.constraint_analyzer import (
- ConstraintAnalyzer,
-)
from local_deep_research.advanced_search_system.constraints.base_constraint import (
ConstraintType,
)
+from local_deep_research.advanced_search_system.constraints.constraint_analyzer import (
+ ConstraintAnalyzer,
+)
-class TestExtractConstraintsEdgeCases:
- """Edge-case tests for extract_constraints."""
+def _make_analyzer():
+ """Create a ConstraintAnalyzer with a MagicMock model, bypassing __init__."""
+ analyzer = ConstraintAnalyzer.__new__(ConstraintAnalyzer)
+ analyzer.model = MagicMock()
+ return analyzer
- def test_response_with_only_whitespace(self):
- """Whitespace-only model output should yield zero constraints."""
- mock_model = Mock()
- mock_model.invoke.return_value = Mock(content=" \n\n \t ")
- analyzer = ConstraintAnalyzer(mock_model)
- constraints = analyzer.extract_constraints("What year?")
- assert constraints == []
+# ---------------------------------------------------------------------------
+# _parse_constraint_type tests
+# ---------------------------------------------------------------------------
+
- def test_response_with_no_constraint_markers(self):
- """Free-form text without CONSTRAINT_ headers should yield zero constraints."""
- mock_model = Mock()
- mock_model.invoke.return_value = Mock(
- content="Here are some thoughts about the question. No structure here."
+class TestParseConstraintType:
+ """Tests for _parse_constraint_type covering all 8 types and edge cases."""
+
+ def test_type_property(self):
+ analyzer = _make_analyzer()
+ assert (
+ analyzer._parse_constraint_type("property")
+ == ConstraintType.PROPERTY
)
- analyzer = ConstraintAnalyzer(mock_model)
- constraints = analyzer.extract_constraints("What year?")
- assert constraints == []
+ def test_type_name_pattern(self):
+ analyzer = _make_analyzer()
+ assert (
+ analyzer._parse_constraint_type("name_pattern")
+ == ConstraintType.NAME_PATTERN
+ )
- def test_constraint_value_containing_colons(self):
- """Values that themselves contain colons should be parsed correctly."""
- mock_model = Mock()
- mock_model.invoke.return_value = Mock(
- content="""\
-CONSTRAINT_1:
-Type: temporal
-Description: The event happened at a specific time
-Value: Time: 3:45 PM on January 1st
-Weight: 0.9
-"""
+ def test_type_event(self):
+ analyzer = _make_analyzer()
+ assert analyzer._parse_constraint_type("event") == ConstraintType.EVENT
+
+ def test_type_statistic(self):
+ analyzer = _make_analyzer()
+ assert (
+ analyzer._parse_constraint_type("statistic")
+ == ConstraintType.STATISTIC
)
- analyzer = ConstraintAnalyzer(mock_model)
- constraints = analyzer.extract_constraints("When did it happen?")
- assert len(constraints) == 1
- assert "3:45 PM" in constraints[0].value
+ def test_type_temporal(self):
+ analyzer = _make_analyzer()
+ assert (
+ analyzer._parse_constraint_type("temporal")
+ == ConstraintType.TEMPORAL
+ )
- def test_single_constraint_without_trailing_newline(self):
- """A single constraint with no trailing newline should still be captured."""
- mock_model = Mock()
- mock_model.invoke.return_value = Mock(
- content="CONSTRAINT_1:\nType: location\nDescription: Located in Europe\nValue: Europe\nWeight: 0.6"
+ def test_type_location(self):
+ analyzer = _make_analyzer()
+ assert (
+ analyzer._parse_constraint_type("location")
+ == ConstraintType.LOCATION
)
- analyzer = ConstraintAnalyzer(mock_model)
- constraints = analyzer.extract_constraints("Where is it?")
- assert len(constraints) == 1
- assert constraints[0].type == ConstraintType.LOCATION
- assert constraints[0].weight == 0.6
-
- def test_three_constraints_ids_sequential(self):
- """Three constraints should get sequential IDs c1, c2, c3."""
- mock_model = Mock()
- mock_model.invoke.return_value = Mock(
- content="""\
-CONSTRAINT_1:
-Type: property
-Description: A
-Value: a
-
-CONSTRAINT_2:
-Type: event
-Description: B
-Value: b
-
-CONSTRAINT_3:
-Type: temporal
-Description: C
-Value: c
-"""
+ def test_type_comparison(self):
+ analyzer = _make_analyzer()
+ assert (
+ analyzer._parse_constraint_type("comparison")
+ == ConstraintType.COMPARISON
)
- analyzer = ConstraintAnalyzer(mock_model)
- constraints = analyzer.extract_constraints("Test")
- assert len(constraints) == 3
- assert [c.id for c in constraints] == ["c1", "c2", "c3"]
+ def test_type_existence(self):
+ analyzer = _make_analyzer()
+ assert (
+ analyzer._parse_constraint_type("existence")
+ == ConstraintType.EXISTENCE
+ )
- def test_constraint_with_think_tags(self):
- """Think tags wrapping constraint content should be stripped."""
- mock_model = Mock()
- mock_model.invoke.return_value = Mock(
- content="""\
-<think>
-I need to create constraints for this query...
-</think>
-CONSTRAINT_1:
-Type: comparison
-Description: Must be greater than 100
-Value: > 100
-Weight: 0.95
-"""
+ def test_case_insensitive_upper(self):
+ analyzer = _make_analyzer()
+ assert (
+ analyzer._parse_constraint_type("PROPERTY")
+ == ConstraintType.PROPERTY
)
- analyzer = ConstraintAnalyzer(mock_model)
- constraints = analyzer.extract_constraints("Which has more than 100?")
- assert len(constraints) == 1
- assert constraints[0].type == ConstraintType.COMPARISON
-
- def test_mixed_valid_and_invalid_blocks(self):
- """Mix of valid and invalid constraint blocks."""
- mock_model = Mock()
- mock_model.invoke.return_value = Mock(
- content="""\
-CONSTRAINT_1:
-Type: property
-Description: Valid first
-Value: first value
-Weight: 0.8
-
-CONSTRAINT_2:
-Type: property
-
-CONSTRAINT_3:
-Type: existence
-Description: Valid third
-Value: exists
-Weight: 0.5
-"""
+ def test_case_insensitive_mixed(self):
+ analyzer = _make_analyzer()
+ assert (
+ analyzer._parse_constraint_type("Temporal")
+ == ConstraintType.TEMPORAL
)
- analyzer = ConstraintAnalyzer(mock_model)
- constraints = analyzer.extract_constraints("Test")
- assert len(constraints) == 2
- assert constraints[0].description == "Valid first"
- assert constraints[1].description == "Valid third"
+ def test_case_insensitive_name_pattern_mixed(self):
+ analyzer = _make_analyzer()
+ assert (
+ analyzer._parse_constraint_type("Name_Pattern")
+ == ConstraintType.NAME_PATTERN
+ )
- def test_model_invocation_passes_query_in_prompt(self):
- """The query text should appear in the prompt sent to the model."""
- mock_model = Mock()
- mock_model.invoke.return_value = Mock(content="")
- analyzer = ConstraintAnalyzer(mock_model)
+ def test_unknown_type_defaults_to_property(self):
+ analyzer = _make_analyzer()
+ assert (
+ analyzer._parse_constraint_type("something_else")
+ == ConstraintType.PROPERTY
+ )
- specific_query = "What is the tallest mountain in Antarctica?"
- analyzer.extract_constraints(specific_query)
+ def test_empty_string_defaults_to_property(self):
+ analyzer = _make_analyzer()
+ assert analyzer._parse_constraint_type("") == ConstraintType.PROPERTY
- call_args = mock_model.invoke.call_args[0][0]
- assert specific_query in call_args
+# ---------------------------------------------------------------------------
+# _parse_weight tests
+# ---------------------------------------------------------------------------
-class TestParseWeightBoundaryConditions:
- """Boundary and edge-case tests for _parse_weight."""
- def test_weight_zero_integer(self):
- analyzer = ConstraintAnalyzer(Mock())
- assert analyzer._parse_weight(0) == 0.0
+class TestParseWeight:
+ """Tests for _parse_weight covering numeric, string, and fallback inputs."""
- def test_weight_zero_float(self):
- analyzer = ConstraintAnalyzer(Mock())
- assert analyzer._parse_weight(0.0) == 0.0
+ def test_integer_input(self):
+ analyzer = _make_analyzer()
+ assert analyzer._parse_weight(1) == 1.0
- def test_weight_zero_string(self):
- analyzer = ConstraintAnalyzer(Mock())
- assert analyzer._parse_weight("0") == 0.0
+ def test_float_input(self):
+ analyzer = _make_analyzer()
+ assert analyzer._parse_weight(0.75) == 0.75
- def test_weight_greater_than_one(self):
- """Weight > 1.0 should be returned as-is (no clamping)."""
- analyzer = ConstraintAnalyzer(Mock())
- assert analyzer._parse_weight(5.0) == 5.0
+ def test_string_plain_number(self):
+ """String containing just a number like '0.8'."""
+ analyzer = _make_analyzer()
+ assert analyzer._parse_weight("0.8") == 0.8
- def test_weight_negative_float(self):
- """Negative float weight should be returned as-is."""
- analyzer = ConstraintAnalyzer(Mock())
- assert analyzer._parse_weight(-0.5) == -0.5
+ def test_string_with_annotation(self):
+ """String like '0.8 (high importance)' extracts 0.8."""
+ analyzer = _make_analyzer()
+ assert analyzer._parse_weight("0.8 (high importance)") == 0.8
- def test_weight_string_with_leading_text(self):
- """String with leading text before the number."""
- analyzer = ConstraintAnalyzer(Mock())
- result = analyzer._parse_weight("approximately 0.85")
- assert result == 0.85
+ def test_string_no_number_returns_default(self):
+ """String with no digits at all returns default 1.0."""
+ analyzer = _make_analyzer()
+ assert analyzer._parse_weight("high") == 1.0
- def test_weight_string_integer_only(self):
- """String containing only an integer (no decimal)."""
- analyzer = ConstraintAnalyzer(Mock())
- result = analyzer._parse_weight("1")
- assert result == 1.0
+ def test_empty_string_returns_default(self):
+ """Empty string returns default 1.0."""
+ analyzer = _make_analyzer()
+ assert analyzer._parse_weight("") == 1.0
- def test_weight_none_returns_default(self):
- """None value should return default 1.0."""
- analyzer = ConstraintAnalyzer(Mock())
- result = analyzer._parse_weight(None)
- assert result == 1.0
+ def test_none_returns_default(self):
+ """None value returns default 1.0."""
+ analyzer = _make_analyzer()
+ assert analyzer._parse_weight(None) == 1.0
- def test_weight_list_returns_default(self):
- """Non-numeric, non-string type returns default."""
- analyzer = ConstraintAnalyzer(Mock())
- result = analyzer._parse_weight([0.5])
- assert result == 1.0
- def test_weight_bool_true(self):
- """Boolean True is an int subclass, should return 1.0."""
- analyzer = ConstraintAnalyzer(Mock())
- result = analyzer._parse_weight(True)
- assert result == 1.0
+# ---------------------------------------------------------------------------
+# extract_constraints tests (mocked model)
+# ---------------------------------------------------------------------------
- def test_weight_bool_false(self):
- """Boolean False is an int subclass, should return 0.0."""
- analyzer = ConstraintAnalyzer(Mock())
- result = analyzer._parse_weight(False)
- assert result == 0.0
+class TestExtractConstraints:
+ """Tests for extract_constraints with a mocked model."""
-class TestParseConstraintTypeEdgeCases:
- """Edge-case tests for _parse_constraint_type."""
+ @staticmethod
+ def _mock_response(analyzer, content):
+ """Configure the analyzer's model to return the given content string."""
+ analyzer.model.invoke.return_value = MagicMock(content=content)
- def test_empty_string_defaults_to_property(self):
- """Empty string type defaults to PROPERTY."""
- analyzer = ConstraintAnalyzer(Mock())
- assert analyzer._parse_constraint_type("") == ConstraintType.PROPERTY
+ def test_single_constraint_parsed(self):
+ analyzer = _make_analyzer()
+ self._mock_response(
+ analyzer,
+ (
+ "CONSTRAINT_1:\n"
+ "Type: property\n"
+ "Description: Must be a red fruit\n"
+ "Value: red fruit\n"
+ "Weight: 0.9\n"
+ ),
+ )
+ constraints = analyzer.extract_constraints("What red fruit is this?")
- def test_all_valid_types_case_insensitive(self):
- """All constraint types should parse correctly in mixed case."""
- analyzer = ConstraintAnalyzer(Mock())
- expected_map = {
- "property": ConstraintType.PROPERTY,
- "name_pattern": ConstraintType.NAME_PATTERN,
- "event": ConstraintType.EVENT,
- "statistic": ConstraintType.STATISTIC,
- "temporal": ConstraintType.TEMPORAL,
- "location": ConstraintType.LOCATION,
- "comparison": ConstraintType.COMPARISON,
- "existence": ConstraintType.EXISTENCE,
- }
- for input_str, expected_type in expected_map.items():
- assert analyzer._parse_constraint_type(input_str) == expected_type
+ assert len(constraints) == 1
+ assert constraints[0].type == ConstraintType.PROPERTY
+ assert constraints[0].description == "Must be a red fruit"
+ assert constraints[0].value == "red fruit"
+ assert constraints[0].weight == 0.9
+ assert constraints[0].id == "c1"
+
+ def test_multiple_constraints_parsed(self):
+ analyzer = _make_analyzer()
+ self._mock_response(
+ analyzer,
+ (
+ "CONSTRAINT_1:\n"
+ "Type: temporal\n"
+ "Description: Happened in 2020\n"
+ "Value: 2020\n"
+ "Weight: 0.8\n"
+ "\n"
+ "CONSTRAINT_2:\n"
+ "Type: location\n"
+ "Description: In Europe\n"
+ "Value: Europe\n"
+ "Weight: 0.7\n"
+ ),
+ )
+ constraints = analyzer.extract_constraints("Where and when?")
- def test_unknown_type_defaults_to_property(self):
- """Unknown type string defaults to PROPERTY."""
- analyzer = ConstraintAnalyzer(Mock())
- assert (
- analyzer._parse_constraint_type("unknown_type")
- == ConstraintType.PROPERTY
+ assert len(constraints) == 2
+ assert constraints[0].type == ConstraintType.TEMPORAL
+ assert constraints[1].type == ConstraintType.LOCATION
+
+ def test_last_constraint_without_trailing_marker(self):
+ """The last constraint has no CONSTRAINT_ marker after it; must be captured."""
+ analyzer = _make_analyzer()
+ self._mock_response(
+ analyzer,
+ (
+ "CONSTRAINT_1:\n"
+ "Type: event\n"
+ "Description: First\n"
+ "Value: first\n"
+ "\n"
+ "CONSTRAINT_2:\n"
+ "Type: existence\n"
+ "Description: Second and last\n"
+ "Value: exists\n"
+ "Weight: 0.5\n"
+ ),
+ )
+ constraints = analyzer.extract_constraints("test query")
+
+ assert len(constraints) == 2
+ assert constraints[1].id == "c2"
+ assert constraints[1].description == "Second and last"
+
+ def test_missing_description_skips_constraint(self):
+ """A constraint block missing 'description' should be skipped."""
+ analyzer = _make_analyzer()
+ self._mock_response(
+ analyzer,
+ ("CONSTRAINT_1:\nType: property\nValue: some value\nWeight: 0.8\n"),
+ )
+ constraints = analyzer.extract_constraints("test")
+
+ assert len(constraints) == 0
+
+ def test_missing_value_skips_constraint(self):
+ """A constraint block missing 'value' should be skipped."""
+ analyzer = _make_analyzer()
+ self._mock_response(
+ analyzer,
+ (
+ "CONSTRAINT_1:\n"
+ "Type: property\n"
+ "Description: Something\n"
+ "Weight: 0.8\n"
+ ),
+ )
+ constraints = analyzer.extract_constraints("test")
+
+ assert len(constraints) == 0
+
+ def test_ids_are_sequential(self):
+ analyzer = _make_analyzer()
+ self._mock_response(
+ analyzer,
+ (
+ "CONSTRAINT_1:\n"
+ "Type: property\n"
+ "Description: A\n"
+ "Value: a\n"
+ "\n"
+ "CONSTRAINT_2:\n"
+ "Type: event\n"
+ "Description: B\n"
+ "Value: b\n"
+ "\n"
+ "CONSTRAINT_3:\n"
+ "Type: comparison\n"
+ "Description: C\n"
+ "Value: c\n"
+ ),
+ )
+ constraints = analyzer.extract_constraints("test")
+
+ assert [c.id for c in constraints] == ["c1", "c2", "c3"]
+
+ def test_empty_model_response_returns_no_constraints(self):
+ analyzer = _make_analyzer()
+ self._mock_response(analyzer, "")
+ constraints = analyzer.extract_constraints("anything")
+
+ assert constraints == []
+
+ def test_invalid_block_between_valid_ones_skipped(self):
+ """Invalid block (missing required fields) between valid blocks is skipped."""
+ analyzer = _make_analyzer()
+ self._mock_response(
+ analyzer,
+ (
+ "CONSTRAINT_1:\n"
+ "Type: property\n"
+ "Description: Valid first\n"
+ "Value: v1\n"
+ "\n"
+ "CONSTRAINT_2:\n"
+ "Type: statistic\n"
+ "\n"
+ "CONSTRAINT_3:\n"
+ "Type: existence\n"
+ "Description: Valid third\n"
+ "Value: v3\n"
+ ),
)
+ constraints = analyzer.extract_constraints("test")
+
+ assert len(constraints) == 2
+ assert constraints[0].id == "c1"
+ assert constraints[1].id == "c2"
+ assert constraints[1].description == "Valid third"
+
+ def test_default_weight_when_omitted(self):
+ """Weight defaults to 1.0 when not specified in the constraint block."""
+ analyzer = _make_analyzer()
+ self._mock_response(
+ analyzer,
+ (
+ "CONSTRAINT_1:\n"
+ "Type: property\n"
+ "Description: No weight\n"
+ "Value: val\n"
+ ),
+ )
+ constraints = analyzer.extract_constraints("test")
+
+ assert len(constraints) == 1
+ assert constraints[0].weight == 1.0