Skip to content

Commit 4818fc9

Browse files
Tests for bugs
1 parent 3c7f85d commit 4818fc9

4 files changed

Lines changed: 684 additions & 0 deletions

File tree

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//! Tests for FlightSQL query execution to ensure it executes the correct queries
19+
//! and doesn't accidentally execute queries from other tabs
20+
21+
use datafusion_dft::tui::AppEvent;
22+
use ratatui::crossterm::event;
23+
24+
use crate::tui_cases::TestApp;
25+
26+
/// Test that ALT+Enter in FlightSQL tab's edit mode doesn't trigger execution
27+
/// in SQL tab. This test verifies that the FlightSQL tab maintains its own
28+
/// execution context separate from the SQL tab.
29+
///
30+
/// This test primarily ensures that the handler doesn't accidentally reference
31+
/// the wrong tab's state (which was the bug).
32+
#[tokio::test(flavor = "multi_thread")]
33+
async fn flightsql_alt_enter_uses_flightsql_tab() {
34+
let mut test_app = TestApp::new().await;
35+
36+
// Switch to FlightSQL tab
37+
let flightsql_key = event::KeyEvent::new(event::KeyCode::Char('2'), event::KeyModifiers::NONE);
38+
test_app.handle_app_event(AppEvent::Key(flightsql_key)).unwrap();
39+
40+
// Enter edit mode
41+
let edit_key = event::KeyEvent::new(event::KeyCode::Char('e'), event::KeyModifiers::NONE);
42+
test_app.handle_app_event(AppEvent::Key(edit_key)).unwrap();
43+
44+
// Verify we're in edit mode
45+
assert!(test_app.state().flightsql_tab.editor_editable());
46+
47+
// Simulate ALT+Enter (which would execute the query)
48+
let alt_enter = event::KeyEvent::new(event::KeyCode::Enter, event::KeyModifiers::ALT);
49+
test_app.handle_app_event(AppEvent::Key(alt_enter)).unwrap();
50+
51+
// The test passes if we don't panic trying to access the wrong tab's state.
52+
// In the buggy version, this would try to call sql_tab.sql() instead of
53+
// flightsql_tab.sql(), and set_execution_task on the wrong tab.
54+
}
55+
56+
/// Test that Enter key in FlightSQL normal mode triggers execution
57+
#[tokio::test(flavor = "multi_thread")]
58+
async fn flightsql_enter_in_normal_mode_executes() {
59+
let mut test_app = TestApp::new().await;
60+
61+
// Switch to FlightSQL tab
62+
let flightsql_key = event::KeyEvent::new(event::KeyCode::Char('2'), event::KeyModifiers::NONE);
63+
test_app.handle_app_event(AppEvent::Key(flightsql_key)).unwrap();
64+
65+
// Should be in normal mode (not editable)
66+
assert!(!test_app.state().flightsql_tab.editor_editable());
67+
68+
// Press Enter in normal mode
69+
let enter = event::KeyEvent::new(event::KeyCode::Enter, event::KeyModifiers::NONE);
70+
test_app.handle_app_event(AppEvent::Key(enter)).unwrap();
71+
72+
// Test passes if no panic occurs
73+
// The execution would be triggered on the FlightSQL tab
74+
}
75+
76+
/// Test that ALT+Enter in SQL tab's edit mode doesn't affect FlightSQL tab
77+
#[tokio::test(flavor = "multi_thread")]
78+
async fn sql_alt_enter_doesnt_affect_flightsql() {
79+
let mut test_app = TestApp::new().await;
80+
81+
// Start on SQL tab (default)
82+
assert!(matches!(
83+
test_app.state().tabs.selected,
84+
datafusion_dft::tui::ui::SelectedTab::SQL
85+
));
86+
87+
// Enter edit mode in SQL tab
88+
let edit_key = event::KeyEvent::new(event::KeyCode::Char('e'), event::KeyModifiers::NONE);
89+
test_app.handle_app_event(AppEvent::Key(edit_key)).unwrap();
90+
91+
// Verify we're in edit mode
92+
assert!(test_app.state().sql_tab.editable());
93+
94+
// Press ALT+Enter in SQL tab
95+
let alt_enter = event::KeyEvent::new(event::KeyCode::Enter, event::KeyModifiers::ALT);
96+
test_app.handle_app_event(AppEvent::Key(alt_enter)).unwrap();
97+
98+
// The test verifies that execution happens on SQL tab, not FlightSQL tab
99+
// In the buggy FlightSQL handler, it was doing the reverse (accessing sql_tab
100+
// when it should access flightsql_tab)
101+
}
102+
103+
/// Test that switching between tabs and executing queries works correctly
104+
#[tokio::test(flavor = "multi_thread")]
105+
async fn switching_tabs_maintains_separate_execution_contexts() {
106+
let mut test_app = TestApp::new().await;
107+
108+
// Start on SQL tab
109+
assert!(matches!(
110+
test_app.state().tabs.selected,
111+
datafusion_dft::tui::ui::SelectedTab::SQL
112+
));
113+
114+
// Enter edit mode and execute in SQL tab
115+
let edit_key = event::KeyEvent::new(event::KeyCode::Char('e'), event::KeyModifiers::NONE);
116+
test_app.handle_app_event(AppEvent::Key(edit_key)).unwrap();
117+
118+
let alt_enter = event::KeyEvent::new(event::KeyCode::Enter, event::KeyModifiers::ALT);
119+
test_app.handle_app_event(AppEvent::Key(alt_enter)).unwrap();
120+
121+
// Exit edit mode
122+
let esc = event::KeyEvent::new(event::KeyCode::Esc, event::KeyModifiers::NONE);
123+
test_app.handle_app_event(AppEvent::Key(esc)).unwrap();
124+
125+
// Switch to FlightSQL tab
126+
let flightsql_key = event::KeyEvent::new(event::KeyCode::Char('2'), event::KeyModifiers::NONE);
127+
test_app.handle_app_event(AppEvent::Key(flightsql_key)).unwrap();
128+
129+
// Enter edit mode in FlightSQL tab
130+
let edit_key = event::KeyEvent::new(event::KeyCode::Char('e'), event::KeyModifiers::NONE);
131+
test_app.handle_app_event(AppEvent::Key(edit_key)).unwrap();
132+
133+
// Execute in FlightSQL tab
134+
let alt_enter = event::KeyEvent::new(event::KeyCode::Enter, event::KeyModifiers::ALT);
135+
test_app.handle_app_event(AppEvent::Key(alt_enter)).unwrap();
136+
137+
// Test passes if both executions happened on their respective tabs
138+
// without mixing up state
139+
}
140+
141+
/// Test that Ctrl+Enter doesn't execute queries (only ALT+Enter should in edit mode)
142+
#[tokio::test(flavor = "multi_thread")]
143+
async fn ctrl_enter_doesnt_execute_in_flightsql_edit_mode() {
144+
let mut test_app = TestApp::new().await;
145+
146+
// Switch to FlightSQL tab
147+
let flightsql_key = event::KeyEvent::new(event::KeyCode::Char('2'), event::KeyModifiers::NONE);
148+
test_app.handle_app_event(AppEvent::Key(flightsql_key)).unwrap();
149+
150+
// Enter edit mode
151+
let edit_key = event::KeyEvent::new(event::KeyCode::Char('e'), event::KeyModifiers::NONE);
152+
test_app.handle_app_event(AppEvent::Key(edit_key)).unwrap();
153+
154+
assert!(test_app.state().flightsql_tab.editor_editable());
155+
156+
// Try Ctrl+Enter (should not execute)
157+
let ctrl_enter = event::KeyEvent::new(event::KeyCode::Enter, event::KeyModifiers::CONTROL);
158+
test_app.handle_app_event(AppEvent::Key(ctrl_enter)).unwrap();
159+
160+
// Should still be in edit mode (Ctrl+Enter shouldn't trigger execution or exit)
161+
assert!(test_app.state().flightsql_tab.editor_editable());
162+
}
163+
164+
/// Test that plain Enter in edit mode doesn't execute (only ALT+Enter should)
165+
#[tokio::test(flavor = "multi_thread")]
166+
async fn plain_enter_doesnt_execute_in_flightsql_edit_mode() {
167+
let mut test_app = TestApp::new().await;
168+
169+
// Switch to FlightSQL tab
170+
let flightsql_key = event::KeyEvent::new(event::KeyCode::Char('2'), event::KeyModifiers::NONE);
171+
test_app.handle_app_event(AppEvent::Key(flightsql_key)).unwrap();
172+
173+
// Enter edit mode
174+
let edit_key = event::KeyEvent::new(event::KeyCode::Char('e'), event::KeyModifiers::NONE);
175+
test_app.handle_app_event(AppEvent::Key(edit_key)).unwrap();
176+
177+
assert!(test_app.state().flightsql_tab.editor_editable());
178+
179+
// Press plain Enter (should just insert newline in editor, not execute)
180+
let enter = event::KeyEvent::new(event::KeyCode::Enter, event::KeyModifiers::NONE);
181+
test_app.handle_app_event(AppEvent::Key(enter)).unwrap();
182+
183+
// Should still be in edit mode
184+
assert!(test_app.state().flightsql_tab.editor_editable());
185+
}

0 commit comments

Comments
 (0)