File size: 9,673 Bytes
9e64e71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""Unit tests for OraclePolicy action selection."""

from __future__ import annotations

from sql_env.evaluation.policies import Policy
from sql_env.evaluation.oracle_policy import OraclePolicy
from sql_env.models import QuestionRecord, SQLAction, SQLObservation


def _question(
    *,
    text: str,
    tables: list[str],
    gold_sql: str,
    gold_answer: str,
    question_id: str = "q1",
) -> QuestionRecord:
    return QuestionRecord(
        question_id=question_id,
        question_text=text,
        database_name="db",
        gold_sql=gold_sql,
        gold_answer=gold_answer,
        answer_type="string",
        difficulty="easy",
        tables_involved=tables,
    )


def _obs(
    *,
    question: str,
    step_count: int = 0,
    budget_remaining: int = 10,
) -> SQLObservation:
    return SQLObservation(
        question=question,
        schema_info="Available tables:\n- employees\n- departments",
        result="",
        error="",
        step_count=step_count,
        budget_remaining=budget_remaining,
        action_history=[],
        done=False,
        reward=None,
    )


def test_init_builds_lookup_from_questions() -> None:
    first = _question(text="Q1", tables=["t1"], gold_sql="SELECT 1", gold_answer="1")
    second = _question(
        text="Q2",
        tables=["t2"],
        gold_sql="SELECT 2",
        gold_answer="2",
        question_id="q2",
    )

    policy = OraclePolicy([first, second])

    assert set(policy._question_lookup) == {"Q1", "Q2"}


def test_init_empty_questions() -> None:
    policy = OraclePolicy([])

    assert policy._question_lookup == {}


def test_init_single_question() -> None:
    policy = OraclePolicy(
        [_question(text="Q1", tables=[], gold_sql="SELECT 1", gold_answer="1")]
    )

    assert set(policy._question_lookup) == {"Q1"}


def test_init_duplicate_question_text() -> None:
    first = _question(
        text="Q1", tables=["t1"], gold_sql="SELECT 1", gold_answer="first"
    )
    second = _question(
        text="Q1",
        tables=["t2"],
        gold_sql="SELECT 2",
        gold_answer="second",
        question_id="q2",
    )

    policy = OraclePolicy([first, second])

    assert policy._question_lookup["Q1"].gold_answer == "second"


def test_init_state_defaults() -> None:
    policy = OraclePolicy(
        [_question(text="Q1", tables=[], gold_sql="SELECT 1", gold_answer="1")]
    )

    assert policy._current_question is None
    assert policy._tables_to_describe == []
    assert policy._gold_sql_sent is False


def test_select_action_describe_phase() -> None:
    policy = OraclePolicy(
        [
            _question(
                text="Q1",
                tables=["t1", "t2"],
                gold_sql="SELECT * FROM t1",
                gold_answer="A",
            )
        ]
    )

    action = policy.select_action(_obs(question="Q1"))

    assert action == SQLAction(action_type="DESCRIBE", argument="t1")


def test_select_action_describe_second_table() -> None:
    policy = OraclePolicy(
        [
            _question(
                text="Q1",
                tables=["t1", "t2"],
                gold_sql="SELECT * FROM t1",
                gold_answer="A",
            )
        ]
    )
    policy.select_action(_obs(question="Q1"))

    action = policy.select_action(_obs(question="Q1", step_count=1))

    assert action == SQLAction(action_type="DESCRIBE", argument="t2")


def test_select_action_query_phase() -> None:
    policy = OraclePolicy(
        [
            _question(
                text="Q1",
                tables=["t1", "t2"],
                gold_sql="SELECT * FROM t1",
                gold_answer="A",
            )
        ]
    )
    policy.select_action(_obs(question="Q1"))
    policy.select_action(_obs(question="Q1", step_count=1))

    action = policy.select_action(_obs(question="Q1", step_count=2))

    assert action == SQLAction(action_type="QUERY", argument="SELECT * FROM t1")


def test_select_action_answer_phase() -> None:
    policy = OraclePolicy(
        [
            _question(
                text="Q1", tables=["t1"], gold_sql="SELECT * FROM t1", gold_answer="A"
            )
        ]
    )
    policy.select_action(_obs(question="Q1"))
    policy.select_action(_obs(question="Q1", step_count=1))

    action = policy.select_action(_obs(question="Q1", step_count=2))

    assert action == SQLAction(action_type="ANSWER", argument="A")


def test_full_episode_sequence() -> None:
    policy = OraclePolicy(
        [
            _question(
                text="Q1",
                tables=["employees"],
                gold_sql="SELECT COUNT(*) FROM employees",
                gold_answer="3",
            )
        ]
    )

    action_1 = policy.select_action(_obs(question="Q1"))
    action_2 = policy.select_action(_obs(question="Q1", step_count=1))
    action_3 = policy.select_action(_obs(question="Q1", step_count=2))

    assert [action_1.action_type, action_2.action_type, action_3.action_type] == [
        "DESCRIBE",
        "QUERY",
        "ANSWER",
    ]


def test_new_episode_resets_state() -> None:
    q1 = _question(
        text="Q1", tables=["t1"], gold_sql="SELECT * FROM t1", gold_answer="A"
    )
    q2 = _question(
        text="Q2", tables=["t2"], gold_sql="SELECT * FROM t2", gold_answer="B"
    )
    policy = OraclePolicy([q1, q2])

    policy.select_action(_obs(question="Q1"))
    policy.select_action(_obs(question="Q1", step_count=1))
    action = policy.select_action(_obs(question="Q2", step_count=0))

    assert action == SQLAction(action_type="DESCRIBE", argument="t2")
    assert policy._gold_sql_sent is False


def test_new_episode_lookup() -> None:
    q1 = _question(
        text="Q1", tables=["t1"], gold_sql="SELECT * FROM t1", gold_answer="A"
    )
    q2 = _question(
        text="Q2", tables=["t2"], gold_sql="SELECT * FROM t2", gold_answer="B"
    )
    policy = OraclePolicy([q1, q2])

    policy.select_action(_obs(question="Q1"))
    policy.select_action(_obs(question="Q2", step_count=0))

    assert policy._current_question is q2


def test_zero_tables_skips_describe() -> None:
    policy = OraclePolicy(
        [_question(text="Q1", tables=[], gold_sql="SELECT 1", gold_answer="1")]
    )

    action = policy.select_action(_obs(question="Q1"))

    assert action == SQLAction(action_type="QUERY", argument="SELECT 1")


def test_zero_tables_then_answer() -> None:
    policy = OraclePolicy(
        [_question(text="Q1", tables=[], gold_sql="SELECT 1", gold_answer="1")]
    )
    policy.select_action(_obs(question="Q1"))

    action = policy.select_action(_obs(question="Q1", step_count=1))

    assert action == SQLAction(action_type="ANSWER", argument="1")


def test_unknown_question_returns_empty_answer() -> None:
    policy = OraclePolicy(
        [_question(text="Q1", tables=["t1"], gold_sql="SELECT 1", gold_answer="1")]
    )

    action = policy.select_action(_obs(question="UNKNOWN"))

    assert action == SQLAction(action_type="ANSWER", argument="")


def test_unknown_question_no_crash() -> None:
    policy = OraclePolicy(
        [_question(text="Q1", tables=["t1"], gold_sql="SELECT 1", gold_answer="1")]
    )

    result = policy.select_action(_obs(question="UNKNOWN"))

    assert isinstance(result, SQLAction)
    assert result.action_type == "ANSWER"


def test_budget_one_forces_answer() -> None:
    policy = OraclePolicy(
        [
            _question(
                text="Q1",
                tables=["t1", "t2"],
                gold_sql="SELECT * FROM t1",
                gold_answer="A",
            )
        ]
    )
    policy.select_action(_obs(question="Q1"))

    action = policy.select_action(_obs(question="Q1", step_count=1, budget_remaining=1))

    assert action == SQLAction(action_type="ANSWER", argument="A")


def test_budget_one_forces_answer_before_query() -> None:
    policy = OraclePolicy(
        [
            _question(
                text="Q1", tables=["t1"], gold_sql="SELECT * FROM t1", gold_answer="A"
            )
        ]
    )
    policy.select_action(_obs(question="Q1"))

    action = policy.select_action(_obs(question="Q1", step_count=1, budget_remaining=1))

    assert action == SQLAction(action_type="ANSWER", argument="A")


def test_budget_one_unknown_question() -> None:
    policy = OraclePolicy(
        [_question(text="Q1", tables=["t1"], gold_sql="SELECT 1", gold_answer="1")]
    )

    action = policy.select_action(_obs(question="UNKNOWN", budget_remaining=1))

    assert action == SQLAction(action_type="ANSWER", argument="")


def test_select_action_returns_sql_action() -> None:
    policy = OraclePolicy(
        [_question(text="Q1", tables=["t1"], gold_sql="SELECT 1", gold_answer="1")]
    )

    result = policy.select_action(_obs(question="Q1"))

    assert isinstance(result, SQLAction)


def test_select_action_valid_action_types() -> None:
    policy = OraclePolicy(
        [_question(text="Q1", tables=["t1"], gold_sql="SELECT 1", gold_answer="1")]
    )

    actions = [
        policy.select_action(_obs(question="Q1", step_count=0)),
        policy.select_action(_obs(question="Q1", step_count=1)),
        policy.select_action(_obs(question="Q1", step_count=2)),
    ]

    assert {action.action_type for action in actions}.issubset(
        {"DESCRIBE", "QUERY", "ANSWER"}
    )


def test_oracle_satisfies_policy_protocol() -> None:
    policy = OraclePolicy(
        [_question(text="Q1", tables=[], gold_sql="SELECT 1", gold_answer="1")]
    )

    assert isinstance(policy, Policy)


def test_oracle_has_select_action_method() -> None:
    assert callable(getattr(OraclePolicy, "select_action", None))