|
| 1 | +"""Unit tests for `make_generate_sql_node` PipelineConfig flag plumbing. |
| 2 | +
|
| 3 | +Closes Kimi audit 2026-05-25 P1.5: NLSQL_M_SCHEMA / NLSQL_DAC moved off |
| 4 | +`os.environ` reads inside the node into typed `PipelineConfig` fields. The |
| 5 | +tests assert the node respects the flags directly instead of monkey-patching |
| 6 | +the environment. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from dataclasses import dataclass, field |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from nl_sql.agent.nodes.generate_sql import make_generate_sql_node |
| 16 | +from nl_sql.agent.state import PipelineState |
| 17 | +from nl_sql.llm.providers.base import GenerateRequest, GenerateResponse |
| 18 | +from nl_sql.schema_index.indexer import SchemaQueryHit |
| 19 | +from nl_sql.schema_index.retriever import ContextBundle |
| 20 | + |
| 21 | + |
| 22 | +@dataclass(slots=True) |
| 23 | +class _RecordingProvider: |
| 24 | + name: str = "fake" |
| 25 | + model: str = "fake-1" |
| 26 | + prompts: list[str] = field(default_factory=list) |
| 27 | + response: str = '{"sql": "SELECT 1", "rationale": "stub", "confidence": 0.9, "tables_used": []}' |
| 28 | + |
| 29 | + def generate(self, req: GenerateRequest) -> GenerateResponse: |
| 30 | + self.prompts.append(req.prompt) |
| 31 | + return GenerateResponse(text=self.response, model=self.model) |
| 32 | + |
| 33 | + |
| 34 | +def _stub_context() -> ContextBundle: |
| 35 | + hit = SchemaQueryHit( |
| 36 | + chunk_id="customers-overview", |
| 37 | + table_name="customers", |
| 38 | + db_id="demo", |
| 39 | + text="Table customers(id INTEGER, name TEXT)", |
| 40 | + distance=0.1, |
| 41 | + metadata={ |
| 42 | + "table_name": "customers", |
| 43 | + "columns": "id,name", |
| 44 | + "primary_key": "id", |
| 45 | + "samples": "[{'id': 1, 'name': 'Alice'}]", |
| 46 | + }, |
| 47 | + ) |
| 48 | + return ContextBundle( |
| 49 | + db_id="demo", |
| 50 | + question="How many customers?", |
| 51 | + schema_hits=[hit], |
| 52 | + fk_neighbours=[], |
| 53 | + fewshots=[], |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def _state(question: str = "How many customers?") -> PipelineState: |
| 58 | + return { |
| 59 | + "question": question, |
| 60 | + "dialect": "sqlite", |
| 61 | + "context": _stub_context(), |
| 62 | + "plan": "", |
| 63 | + "trace": [], |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +def _run_with_flags(use_m_schema: bool, use_dac_prompt: bool) -> str: |
| 68 | + provider = _RecordingProvider() |
| 69 | + node = make_generate_sql_node( |
| 70 | + provider, use_m_schema=use_m_schema, use_dac_prompt=use_dac_prompt |
| 71 | + ) |
| 72 | + node(_state()) |
| 73 | + return provider.prompts[0] |
| 74 | + |
| 75 | + |
| 76 | +def test_default_uses_plain_template_and_default_schema_render() -> None: |
| 77 | + prompt = _run_with_flags(use_m_schema=False, use_dac_prompt=False) |
| 78 | + # Default `generate_sql.txt` is plain — DAC variant injects a Decompose |
| 79 | + # / sub-question instruction. Verify default does not have it. |
| 80 | + assert "Decompose" not in prompt |
| 81 | + assert "sub-question" not in prompt.lower() |
| 82 | + |
| 83 | + |
| 84 | +def test_use_dac_prompt_switches_template() -> None: |
| 85 | + prompt = _run_with_flags(use_m_schema=False, use_dac_prompt=True) |
| 86 | + # `generate_sql_dac.txt` instructs the model to decompose multi-clause |
| 87 | + # questions before composing SQL — at least one of these keywords must |
| 88 | + # be present in the DAC variant but absent in the default. |
| 89 | + assert "Decompose" in prompt or "sub-question" in prompt.lower(), ( |
| 90 | + f"Expected DAC prompt markers in selected template, got: {prompt[:400]}" |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def test_use_m_schema_changes_schema_rendering() -> None: |
| 95 | + default_prompt = _run_with_flags(use_m_schema=False, use_dac_prompt=False) |
| 96 | + mschema_prompt = _run_with_flags(use_m_schema=True, use_dac_prompt=False) |
| 97 | + # M-Schema renderer emits a `# Columns` / `# Foreign keys` block; the |
| 98 | + # default verbose renderer dumps raw `hit.text`. We only need to assert |
| 99 | + # the rendered schema portion of the prompt differs between flags, since |
| 100 | + # the prompt template / question / fewshot are identical. |
| 101 | + assert default_prompt != mschema_prompt, ( |
| 102 | + "use_m_schema=True must change the rendered schema block in the prompt" |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.parametrize( |
| 107 | + ("use_m_schema", "use_dac_prompt"), |
| 108 | + [(True, True), (True, False), (False, True), (False, False)], |
| 109 | +) |
| 110 | +def test_flags_do_not_leak_across_node_factory_calls( |
| 111 | + use_m_schema: bool, |
| 112 | + use_dac_prompt: bool, |
| 113 | +) -> None: |
| 114 | + """Regression guard: each `make_generate_sql_node` call captures its own |
| 115 | + flags; flipping flags on a later call must not retroactively affect a |
| 116 | + previously-built node (catches accidental shared mutable default). |
| 117 | + """ |
| 118 | + provider_a = _RecordingProvider() |
| 119 | + node_a = make_generate_sql_node( |
| 120 | + provider_a, use_m_schema=use_m_schema, use_dac_prompt=use_dac_prompt |
| 121 | + ) |
| 122 | + provider_b = _RecordingProvider() |
| 123 | + _ = make_generate_sql_node( |
| 124 | + provider_b, use_m_schema=not use_m_schema, use_dac_prompt=not use_dac_prompt |
| 125 | + ) |
| 126 | + node_a(_state()) |
| 127 | + prompt = provider_a.prompts[0] |
| 128 | + expected_dac = "Decompose" in prompt or "sub-question" in prompt.lower() |
| 129 | + assert expected_dac is use_dac_prompt |
0 commit comments