zippa-db 0.1.0

A fast, lightweight, cross-platform database client for PostgreSQL, MySQL, and SQLite.
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Moving around a grid: the arrows, what the keyboard holds, and copying.

use super::*;

#[gpui_kit::test]
fn the_arrows_move_the_selection_and_stop_at_the_edges(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    // `items` has two rows and four columns: id, name, score, payload.
    select_cell(cx, &view, 0, 0);

    focus_grid(cx, &view);
    press(cx, handle, "down");
    cx.run_until_parked();
    assert_eq!(
        selection(cx, &view),
        (Some(1), Some((1, 0))),
        "down should move to the row below, taking the row highlight with it"
    );

    press(cx, handle, "down");
    cx.run_until_parked();
    assert_eq!(
        selection(cx, &view),
        (Some(1), Some((1, 0))),
        "the last row is the last row: the selection must not reappear at the top"
    );

    press(cx, handle, "up");
    press(cx, handle, "up");
    cx.run_until_parked();
    assert_eq!(
        selection(cx, &view),
        (Some(0), Some((0, 0))),
        "the first row is where up stops"
    );

    press(cx, handle, "right");
    cx.run_until_parked();
    assert_eq!(selection(cx, &view).1, Some((0, 1)));

    // Past the last column, and one more for good measure.
    for _ in 0..3 {
        press(cx, handle, "right");
    }
    cx.run_until_parked();
    assert_eq!(
        selection(cx, &view).1,
        Some((0, 3)),
        "the last column must not wrap round to the first"
    );
}

#[gpui_kit::test]
fn the_selection_never_rests_on_the_checkbox_column(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    select_cell(cx, &view, 0, 0);
    focus_grid(cx, &view);

    // The checkbox column sits before the first value, so left from it has
    // nowhere to go rather than landing on a column with nothing to show.
    press(cx, handle, "left");
    cx.run_until_parked();
    assert_eq!(
        selection(cx, &view).1,
        Some((0, 0)),
        "left from the first value should stay on it"
    );

    // Home asks for the first column of the row, which is the checkbox one.
    press(cx, handle, "home");
    cx.run_until_parked();
    assert_eq!(
        selection(cx, &view).1,
        Some((0, 0)),
        "home should land on the first value, not on the checkbox column"
    );
}

#[gpui_kit::test]
fn the_copy_shortcut_copies_the_selected_cell(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    select_cell(cx, &view, 0, 1);
    focus_grid(cx, &view);
    press(cx, handle, "secondary-c");
    cx.run_until_parked();

    assert_eq!(clipboard(cx), Some("alpha".to_string()));
}

#[gpui_kit::test]
fn copying_with_rows_picked_out_takes_the_whole_rows(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    // A cell is selected too: the picked rows are what the user is working
    // with, so they win over it.
    select_cell(cx, &view, 0, 1);
    pick_row(cx, handle, 0);
    pick_through(cx, handle, 1);

    focus_grid(cx, &view);
    press(cx, handle, "secondary-c");
    cx.run_until_parked();

    assert_eq!(
        clipboard(cx),
        Some("1\talpha\t1.5\t<3 bytes>\n2\t\t\t".to_string()),
        "the rows should copy as one line each, columns separated by tabs, \
         and a NULL as nothing"
    );
}

#[gpui_kit::test]
fn space_picks_the_focused_row_out_and_puts_it_back(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    select_cell(cx, &view, 1, 0);
    focus_grid(cx, &view);
    press(cx, handle, "space");
    cx.run_until_parked();
    assert_eq!(picked(cx, &view), vec![1]);

    press(cx, handle, "space");
    cx.run_until_parked();
    assert!(
        picked(cx, &view).is_empty(),
        "space on a picked row should put it back"
    );
}

#[gpui_kit::test]
fn space_does_not_pick_the_row_while_editing_a_cell(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    let grid = view.read_with(cx, |view, _| view.grid_for_test());
    grid.downgrade()
        .update_in(cx, |grid, window, cx| {
            grid.focus_for_test(window, cx);
            grid.select_cell_for_test(0, 1, cx);
        })
        .unwrap();
    cx.run_until_parked();

    grid.downgrade()
        .update_in(cx, |grid, window, cx| {
            grid.begin_edit_for_test(0, 1, window, cx);
        })
        .unwrap();
    cx.run_until_parked();

    // Without a binding that wins over the row's own, the editor's context is
    // deeper than the table's, so this reaches the row picker instead of the
    // input.
    press(cx, handle, "space");
    cx.run_until_parked();

    assert!(
        picked(cx, &view).is_empty(),
        "space while editing a cell must not pick the row out"
    );
    assert_eq!(
        grid.read_with(cx, |grid, cx| grid.editing_for_test(cx)),
        Some((0, 1)),
        "the editor should still be open"
    );
}

#[gpui_kit::test]
fn the_select_all_shortcut_picks_every_row_and_the_shifted_one_clears(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    select_cell(cx, &view, 0, 0);
    focus_grid(cx, &view);
    press(cx, handle, "secondary-a");
    cx.run_until_parked();
    assert_eq!(picked(cx, &view), vec![0, 1]);

    press(cx, handle, "secondary-shift-a");
    cx.run_until_parked();
    assert!(
        picked(cx, &view).is_empty(),
        "the shifted shortcut should put every row back"
    );
}

#[gpui_kit::test]
fn shifted_arrows_take_the_rows_they_pass(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    select_cell(cx, &view, 0, 0);
    focus_grid(cx, &view);
    press(cx, handle, "shift-down");
    cx.run_until_parked();

    assert_eq!(
        picked(cx, &view),
        vec![0, 1],
        "the row it started on goes with the one it moved to"
    );
    assert_eq!(
        selection(cx, &view).1,
        Some((1, 0)),
        "the selection should have moved with the range"
    );

    // Stopping at the edge is the same as for a plain arrow.
    press(cx, handle, "shift-down");
    cx.run_until_parked();
    assert_eq!(picked(cx, &view), vec![0, 1]);
}

#[gpui_kit::test]
fn escape_clears_both_the_selection_and_the_picked_rows(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    select_cell(cx, &view, 0, 0);
    focus_grid(cx, &view);
    press(cx, handle, "secondary-a");
    cx.run_until_parked();
    assert_eq!(picked(cx, &view), vec![0, 1]);

    press(cx, handle, "escape");
    cx.run_until_parked();

    assert!(picked(cx, &view).is_empty());
    assert_eq!(
        selection(cx, &view),
        (None, None),
        "escape should leave nothing highlighted"
    );
}

#[gpui_kit::test]
fn finishing_a_cell_hands_the_keyboard_back_to_the_grid(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    stage_cell(cx, &view, 0, 1, "typed");
    assert!(
        !grid_focused(cx, handle, &view),
        "the editor should hold the keyboard while a cell is open"
    );

    press(cx, handle, "enter");
    cx.run_until_parked();

    assert!(
        grid_focused(cx, handle, &view),
        "enter should close the editor and put the keyboard back on the rows"
    );
    assert_eq!(
        view.read_with(cx, |view, cx| view
            .grid_for_test()
            .read(cx)
            .cell_for_test(0, 1, cx)),
        Some("typed".to_string()),
        "what was typed should still have been staged"
    );

    // And the arrows work again straight away.
    press(cx, handle, "down");
    cx.run_until_parked();
    assert_eq!(selection(cx, &view).1, Some((1, 1)));
}

#[gpui_kit::test]
fn giving_up_on_a_cell_hands_the_keyboard_back_too(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    stage_cell(cx, &view, 0, 1, "never staged");
    press(cx, handle, "escape");
    cx.run_until_parked();

    assert!(
        grid_focused(cx, handle, &view),
        "escape should put the keyboard back on the rows"
    );
    assert_eq!(
        view.read_with(cx, |view, cx| view
            .grid_for_test()
            .read(cx)
            .cell_for_test(0, 1, cx)),
        Some("alpha".to_string()),
        "the cell should show the loaded value again"
    );
}

#[gpui_kit::test]
fn dragging_down_the_boxes_sweeps_a_range(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    sweep_rows(cx, handle, 0, 1);
    cx.run_until_parked();
    assert_eq!(
        picked(cx, &view),
        vec![0, 1],
        "a drag across the boxes should pick up every row it passed"
    );

    // A drag that starts on a picked row puts the range back instead.
    sweep_rows(cx, handle, 0, 1);
    cx.run_until_parked();
    assert!(picked(cx, &view).is_empty());
}

#[gpui_kit::test]
fn a_modified_click_in_a_row_picks_it_out_away_from_the_box(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    // The platform's own modifier takes one row, anywhere in it.
    click_row(cx, handle, 0, Modifiers::secondary_key());
    cx.run_until_parked();
    assert_eq!(picked(cx, &view), vec![0]);

    // Shift takes the range from there.
    click_row(
        cx,
        handle,
        1,
        Modifiers {
            shift: true,
            ..Default::default()
        },
    );
    cx.run_until_parked();
    assert_eq!(picked(cx, &view), vec![0, 1]);
}

#[gpui_kit::test]
fn checking_a_row_leaves_the_keyboard_on_the_grid(cx: &mut TestAppContext) {
    let (_database, handle, view) = table_view(cx);
    cx.run_until_parked();

    select_cell(cx, &view, 0, 0);
    pick_row(cx, handle, 1);
    cx.run_until_parked();

    assert!(
        grid_focused(cx, handle, &view),
        "a box that took the focus would leave the next keystroke going nowhere"
    );
    assert_eq!(
        selection(cx, &view).0,
        Some(1),
        "checking a row should move the keyboard onto it"
    );

    // Which is what makes the keys above keep working after a click.
    press(cx, handle, "shift-up");
    cx.run_until_parked();
    assert_eq!(picked(cx, &view), vec![0, 1]);
}

#[gpui_kit::test]
fn the_menu_copies_the_cell_it_was_opened_on(cx: &mut TestAppContext) {
    let (_database, _handle, view) = table_view(cx);
    cx.run_until_parked();

    // The selection is on one cell and the menu on another: a right click
    // should copy what it landed on.
    select_cell(cx, &view, 0, 0);
    let grid = view.read_with(cx, |view, _| view.grid_for_test());
    grid.update(cx, |grid, cx| grid.copy_cell_for_test(0, 1, cx));
    cx.run_until_parked();

    assert_eq!(clipboard(cx), Some("alpha".to_string()));
}

#[gpui_kit::test]
fn closing_the_value_dialog_hands_the_keyboard_back_to_the_rows(cx: &mut TestAppContext) {
    let (_database, handle, view) = workspace_table(cx);

    let grid = view.read_with(cx, |view, _| view.grid_for_test());
    grid.downgrade()
        .update_in(cx, |grid, window, cx| {
            grid.focus_for_test(window, cx);
            grid.select_cell_for_test(0, 1, cx);
        })
        .unwrap();
    cx.run_until_parked();

    grid.update(cx, |grid, cx| grid.view_cell(0, 1, cx));
    draw_workspace(cx, &handle);

    cx.update_window(handle.window.into(), |_, window, cx| {
        window.draw(cx).clear(cx);
        window.press("escape", cx);
    })
    .unwrap();
    cx.run_until_parked();

    // The dialog took the keyboard; closing it has to give it back, or the
    // arrows answer nothing until the grid is clicked into again.
    cx.update_window(handle.window.into(), |_, window, cx| {
        window.draw(cx).clear(cx);
        window.press("down", cx);
    })
    .unwrap();
    cx.run_until_parked();

    assert_eq!(
        grid.read_with(cx, |grid, cx| grid.selection_for_test(cx)),
        (Some(1), Some((1, 1))),
        "the arrows should work straight after the dialog closes"
    );
}