summaryrefslogtreecommitdiffstats
path: root/tests/test_smart_completion_multiple_schemata.py
blob: 5c9c9af48e57a71203b20dc5e8b1894eb29938ca (plain)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
import itertools
from metadata import (
    MetaData,
    alias,
    name_join,
    fk_join,
    join,
    schema,
    table,
    function,
    wildcard_expansion,
    column,
    get_result,
    result_set,
    qual,
    no_qual,
    parametrize,
)
from utils import completions_to_set

metadata = {
    "tables": {
        "public": {
            "users": ["id", "email", "first_name", "last_name"],
            "orders": ["id", "ordered_date", "status", "datestamp"],
            "select": ["id", "localtime", "ABC"],
        },
        "custom": {
            "users": ["id", "phone_number"],
            "Users": ["userid", "username"],
            "products": ["id", "product_name", "price"],
            "shipments": ["id", "address", "user_id"],
        },
        "Custom": {"projects": ["projectid", "name"]},
        "blog": {
            "entries": ["entryid", "entrytitle", "entrytext"],
            "tags": ["tagid", "name"],
            "entrytags": ["entryid", "tagid"],
            "entacclog": ["entryid", "username", "datestamp"],
        },
    },
    "functions": {
        "public": [
            ["func1", [], [], [], "", False, False, False, False],
            ["func2", [], [], [], "", False, False, False, False],
        ],
        "custom": [
            ["func3", [], [], [], "", False, False, False, False],
            [
                "set_returning_func",
                ["x"],
                ["integer"],
                ["o"],
                "integer",
                False,
                False,
                True,
                False,
            ],
        ],
        "Custom": [["func4", [], [], [], "", False, False, False, False]],
        "blog": [
            [
                "extract_entry_symbols",
                ["_entryid", "symbol"],
                ["integer", "text"],
                ["i", "o"],
                "",
                False,
                False,
                True,
                False,
            ],
            [
                "enter_entry",
                ["_title", "_text", "entryid"],
                ["text", "text", "integer"],
                ["i", "i", "o"],
                "",
                False,
                False,
                False,
                False,
            ],
        ],
    },
    "datatypes": {"public": ["typ1", "typ2"], "custom": ["typ3", "typ4"]},
    "foreignkeys": {
        "custom": [("public", "users", "id", "custom", "shipments", "user_id")],
        "blog": [
            ("blog", "entries", "entryid", "blog", "entacclog", "entryid"),
            ("blog", "entries", "entryid", "blog", "entrytags", "entryid"),
            ("blog", "tags", "tagid", "blog", "entrytags", "tagid"),
        ],
    },
    "defaults": {
        "public": {
            ("orders", "id"): "nextval('orders_id_seq'::regclass)",
            ("orders", "datestamp"): "now()",
            ("orders", "status"): "'PENDING'::text",
        }
    },
}

testdata = MetaData(metadata)
cased_schemas = [schema(x) for x in ("public", "blog", "CUSTOM", '"Custom"')]
casing = (
    "SELECT",
    "Orders",
    "User_Emails",
    "CUSTOM",
    "Func1",
    "Entries",
    "Tags",
    "EntryTags",
    "EntAccLog",
    "EntryID",
    "EntryTitle",
    "EntryText",
)
completers = testdata.get_completers(casing)


@parametrize("completer", completers(filtr=True, casing=False, qualify=no_qual))
@parametrize("table", ["users", '"users"'])
def test_suggested_column_names_from_shadowed_visible_table(completer, table):
    result = get_result(completer, "SELECT  FROM " + table, len("SELECT "))
    assert completions_to_set(result) == completions_to_set(
        testdata.columns_functions_and_keywords("users")
    )


@parametrize("completer", completers(filtr=True, casing=False, qualify=no_qual))
@parametrize(
    "text",
    [
        "SELECT  from custom.users",
        "WITH users as (SELECT 1 AS foo) SELECT  from custom.users",
    ],
)
def test_suggested_column_names_from_qualified_shadowed_table(completer, text):
    result = get_result(completer, text, position=text.find("  ") + 1)
    assert completions_to_set(result) == completions_to_set(
        testdata.columns_functions_and_keywords("users", "custom")
    )


@parametrize("completer", completers(filtr=True, casing=False, qualify=no_qual))
@parametrize("text", ["WITH users as (SELECT 1 AS foo) SELECT  from users"])
def test_suggested_column_names_from_cte(completer, text):
    result = completions_to_set(get_result(completer, text, text.find("  ") + 1))
    assert result == completions_to_set(
        [column("foo")] + testdata.functions_and_keywords()
    )


@parametrize("completer", completers(casing=False))
@parametrize(
    "text",
    [
        "SELECT * FROM users JOIN custom.shipments ON ",
        """SELECT *
    FROM public.users
    JOIN custom.shipments ON """,
    ],
)
def test_suggested_join_conditions(completer, text):
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        [
            alias("users"),
            alias("shipments"),
            name_join("shipments.id = users.id"),
            fk_join("shipments.user_id = users.id"),
        ]
    )


@parametrize("completer", completers(filtr=True, casing=False, aliasing=False))
@parametrize(
    ("query", "tbl"),
    itertools.product(
        (
            "SELECT * FROM public.{0} RIGHT OUTER JOIN ",
            """SELECT *
    FROM {0}
    JOIN """,
        ),
        ("users", '"users"', "Users"),
    ),
)
def test_suggested_joins(completer, query, tbl):
    result = get_result(completer, query.format(tbl))
    assert completions_to_set(result) == completions_to_set(
        testdata.schemas_and_from_clause_items()
        + [join(f"custom.shipments ON shipments.user_id = {tbl}.id")]
    )


@parametrize("completer", completers(filtr=True, casing=False, qualify=no_qual))
def test_suggested_column_names_from_schema_qualifed_table(completer):
    result = get_result(completer, "SELECT  from custom.products", len("SELECT "))
    assert completions_to_set(result) == completions_to_set(
        testdata.columns_functions_and_keywords("products", "custom")
    )


@parametrize(
    "text",
    [
        "INSERT INTO orders(",
        "INSERT INTO orders (",
        "INSERT INTO public.orders(",
        "INSERT INTO public.orders (",
    ],
)
@parametrize("completer", completers(filtr=True, casing=False))
def test_suggested_columns_with_insert(completer, text):
    assert completions_to_set(get_result(completer, text)) == completions_to_set(
        testdata.columns("orders")
    )


@parametrize("completer", completers(filtr=True, casing=False, qualify=no_qual))
def test_suggested_column_names_in_function(completer):
    result = get_result(
        completer, "SELECT MAX( from custom.products", len("SELECT MAX(")
    )
    assert completions_to_set(result) == completions_to_set(
        testdata.columns_functions_and_keywords("products", "custom")
    )


@parametrize("completer", completers(casing=False, aliasing=False))
@parametrize(
    "text",
    ["SELECT * FROM Custom.", "SELECT * FROM custom.", 'SELECT * FROM "custom".'],
)
@parametrize("use_leading_double_quote", [False, True])
def test_suggested_table_names_with_schema_dot(
    completer, text, use_leading_double_quote
):
    if use_leading_double_quote:
        text += '"'
        start_position = -1
    else:
        start_position = 0

    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        testdata.from_clause_items("custom", start_position)
    )


@parametrize("completer", completers(casing=False, aliasing=False))
@parametrize("text", ['SELECT * FROM "Custom".'])
@parametrize("use_leading_double_quote", [False, True])
def test_suggested_table_names_with_schema_dot2(
    completer, text, use_leading_double_quote
):
    if use_leading_double_quote:
        text += '"'
        start_position = -1
    else:
        start_position = 0

    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        testdata.from_clause_items("Custom", start_position)
    )


@parametrize("completer", completers(filtr=True, casing=False))
def test_suggested_column_names_with_qualified_alias(completer):
    result = get_result(completer, "SELECT p. from custom.products p", len("SELECT p."))
    assert completions_to_set(result) == completions_to_set(
        testdata.columns("products", "custom")
    )


@parametrize("completer", completers(filtr=True, casing=False, qualify=no_qual))
def test_suggested_multiple_column_names(completer):
    result = get_result(
        completer, "SELECT id,  from custom.products", len("SELECT id, ")
    )
    assert completions_to_set(result) == completions_to_set(
        testdata.columns_functions_and_keywords("products", "custom")
    )


@parametrize("completer", completers(filtr=True, casing=False))
def test_suggested_multiple_column_names_with_alias(completer):
    result = get_result(
        completer, "SELECT p.id, p. from custom.products p", len("SELECT u.id, u.")
    )
    assert completions_to_set(result) == completions_to_set(
        testdata.columns("products", "custom")
    )


@parametrize("completer", completers(filtr=True, casing=False))
@parametrize(
    "text",
    [
        "SELECT x.id, y.product_name FROM custom.products x JOIN custom.products y ON ",
        "SELECT x.id, y.product_name FROM custom.products x JOIN custom.products y ON JOIN public.orders z ON z.id > y.id",
    ],
)
def test_suggestions_after_on(completer, text):
    position = len(
        "SELECT x.id, y.product_name FROM custom.products x JOIN custom.products y ON "
    )
    result = get_result(completer, text, position)
    assert completions_to_set(result) == completions_to_set(
        [
            alias("x"),
            alias("y"),
            name_join("y.price = x.price"),
            name_join("y.product_name = x.product_name"),
            name_join("y.id = x.id"),
        ]
    )


@parametrize("completer", completers())
def test_suggested_aliases_after_on_right_side(completer):
    text = "SELECT x.id, y.product_name FROM custom.products x JOIN custom.products y ON x.id = "
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set([alias("x"), alias("y")])


@parametrize("completer", completers(filtr=True, casing=False, aliasing=False))
def test_table_names_after_from(completer):
    text = "SELECT * FROM "
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        testdata.schemas_and_from_clause_items()
    )


@parametrize("completer", completers(filtr=True, casing=False))
def test_schema_qualified_function_name(completer):
    text = "SELECT custom.func"
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        [
            function("func3()", -len("func")),
            function("set_returning_func()", -len("func")),
        ]
    )


@parametrize("completer", completers(filtr=True, casing=False, aliasing=False))
def test_schema_qualified_function_name_after_from(completer):
    text = "SELECT * FROM custom.set_r"
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        [
            function("set_returning_func()", -len("func")),
        ]
    )


@parametrize("completer", completers(filtr=True, casing=False, aliasing=False))
def test_unqualified_function_name_not_returned(completer):
    text = "SELECT * FROM set_r"
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set([])


@parametrize("completer", completers(filtr=True, casing=False, aliasing=False))
def test_unqualified_function_name_in_search_path(completer):
    completer.search_path = ["public", "custom"]
    text = "SELECT * FROM set_r"
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        [
            function("set_returning_func()", -len("func")),
        ]
    )


@parametrize("completer", completers(filtr=True, casing=False))
@parametrize(
    "text",
    [
        "SELECT 1::custom.",
        "CREATE TABLE foo (bar custom.",
        "CREATE FUNCTION foo (bar INT, baz custom.",
        "ALTER TABLE foo ALTER COLUMN bar TYPE custom.",
    ],
)
def test_schema_qualified_type_name(completer, text):
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(testdata.types("custom"))


@parametrize("completer", completers(filtr=True, casing=False))
def test_suggest_columns_from_aliased_set_returning_function(completer):
    result = get_result(
        completer, "select f. from custom.set_returning_func() f", len("select f.")
    )
    assert completions_to_set(result) == completions_to_set(
        testdata.columns("set_returning_func", "custom", "functions")
    )


@parametrize("completer", completers(filtr=True, casing=False, qualify=no_qual))
@parametrize(
    "text",
    [
        "SELECT * FROM custom.set_returning_func()",
        "SELECT * FROM Custom.set_returning_func()",
        "SELECT * FROM Custom.Set_Returning_Func()",
    ],
)
def test_wildcard_column_expansion_with_function(completer, text):
    position = len("SELECT *")

    completions = get_result(completer, text, position)

    col_list = "x"
    expected = [wildcard_expansion(col_list)]

    assert expected == completions


@parametrize("completer", completers(filtr=True, casing=False))
def test_wildcard_column_expansion_with_alias_qualifier(completer):
    text = "SELECT p.* FROM custom.products p"
    position = len("SELECT p.*")

    completions = get_result(completer, text, position)

    col_list = "id, p.product_name, p.price"
    expected = [wildcard_expansion(col_list)]

    assert expected == completions


@parametrize("completer", completers(filtr=True, casing=False))
@parametrize(
    "text",
    [
        """
    SELECT count(1) FROM users;
    CREATE FUNCTION foo(custom.products _products) returns custom.shipments
    LANGUAGE SQL
    AS $foo$
    SELECT 1 FROM custom.shipments;
    INSERT INTO public.orders(*) values(-1, now(), 'preliminary');
    SELECT 2 FROM custom.users;
    $foo$;
    SELECT count(1) FROM custom.shipments;
    """,
        "INSERT INTO public.orders(*",
        "INSERT INTO public.Orders(*",
        "INSERT INTO public.orders (*",
        "INSERT INTO public.Orders (*",
        "INSERT INTO orders(*",
        "INSERT INTO Orders(*",
        "INSERT INTO orders (*",
        "INSERT INTO Orders (*",
        "INSERT INTO public.orders(*)",
        "INSERT INTO public.Orders(*)",
        "INSERT INTO public.orders (*)",
        "INSERT INTO public.Orders (*)",
        "INSERT INTO orders(*)",
        "INSERT INTO Orders(*)",
        "INSERT INTO orders (*)",
        "INSERT INTO Orders (*)",
    ],
)
def test_wildcard_column_expansion_with_insert(completer, text):
    position = text.index("*") + 1
    completions = get_result(completer, text, position)

    expected = [wildcard_expansion("ordered_date, status")]
    assert expected == completions


@parametrize("completer", completers(filtr=True, casing=False))
def test_wildcard_column_expansion_with_table_qualifier(completer):
    text = 'SELECT "select".* FROM public."select"'
    position = len('SELECT "select".*')

    completions = get_result(completer, text, position)

    col_list = 'id, "select"."localtime", "select"."ABC"'
    expected = [wildcard_expansion(col_list)]

    assert expected == completions


@parametrize("completer", completers(filtr=True, casing=False, qualify=qual))
def test_wildcard_column_expansion_with_two_tables(completer):
    text = 'SELECT * FROM public."select" JOIN custom.users ON true'
    position = len("SELECT *")

    completions = get_result(completer, text, position)

    cols = (
        '"select".id, "select"."localtime", "select"."ABC", '
        "users.id, users.phone_number"
    )
    expected = [wildcard_expansion(cols)]
    assert completions == expected


@parametrize("completer", completers(filtr=True, casing=False))
def test_wildcard_column_expansion_with_two_tables_and_parent(completer):
    text = 'SELECT "select".* FROM public."select" JOIN custom.users u ON true'
    position = len('SELECT "select".*')

    completions = get_result(completer, text, position)

    col_list = 'id, "select"."localtime", "select"."ABC"'
    expected = [wildcard_expansion(col_list)]

    assert expected == completions


@parametrize("completer", completers(filtr=True, casing=False))
@parametrize(
    "text",
    [
        "SELECT U. FROM custom.Users U",
        "SELECT U. FROM custom.USERS U",
        "SELECT U. FROM custom.users U",
        'SELECT U. FROM "custom".Users U',
        'SELECT U. FROM "custom".USERS U',
        'SELECT U. FROM "custom".users U',
    ],
)
def test_suggest_columns_from_unquoted_table(completer, text):
    position = len("SELECT U.")
    result = get_result(completer, text, position)
    assert completions_to_set(result) == completions_to_set(
        testdata.columns("users", "custom")
    )


@parametrize("completer", completers(filtr=True, casing=False))
@parametrize(
    "text", ['SELECT U. FROM custom."Users" U', 'SELECT U. FROM "custom"."Users" U']
)
def test_suggest_columns_from_quoted_table(completer, text):
    position = len("SELECT U.")
    result = get_result(completer, text, position)
    assert completions_to_set(result) == completions_to_set(
        testdata.columns("Users", "custom")
    )


texts = ["SELECT * FROM ", "SELECT * FROM public.Orders O CROSS JOIN "]


@parametrize("completer", completers(filtr=True, casing=False, aliasing=False))
@parametrize("text", texts)
def test_schema_or_visible_table_completion(completer, text):
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        testdata.schemas_and_from_clause_items()
    )


@parametrize("completer", completers(aliasing=True, casing=False, filtr=True))
@parametrize("text", texts)
def test_table_aliases(completer, text):
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        testdata.schemas()
        + [
            table("users u"),
            table("orders o" if text == "SELECT * FROM " else "orders o2"),
            table('"select" s'),
            function("func1() f"),
            function("func2() f"),
        ]
    )


@parametrize("completer", completers(aliasing=True, casing=True, filtr=True))
@parametrize("text", texts)
def test_aliases_with_casing(completer, text):
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        cased_schemas
        + [
            table("users u"),
            table("Orders O" if text == "SELECT * FROM " else "Orders O2"),
            table('"select" s'),
            function("Func1() F"),
            function("func2() f"),
        ]
    )


@parametrize("completer", completers(aliasing=False, casing=True, filtr=True))
@parametrize("text", texts)
def test_table_casing(completer, text):
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        cased_schemas
        + [
            table("users"),
            table("Orders"),
            table('"select"'),
            function("Func1()"),
            function("func2()"),
        ]
    )


@parametrize("completer", completers(aliasing=False, casing=True))
def test_alias_search_without_aliases2(completer):
    text = "SELECT * FROM blog.et"
    result = get_result(completer, text)
    assert result[0] == table("EntryTags", -2)


@parametrize("completer", completers(aliasing=False, casing=True))
def test_alias_search_without_aliases1(completer):
    text = "SELECT * FROM blog.e"
    result = get_result(completer, text)
    assert result[0] == table("Entries", -1)


@parametrize("completer", completers(aliasing=True, casing=True))
def test_alias_search_with_aliases2(completer):
    text = "SELECT * FROM blog.et"
    result = get_result(completer, text)
    assert result[0] == table("EntryTags ET", -2)


@parametrize("completer", completers(aliasing=True, casing=True))
def test_alias_search_with_aliases1(completer):
    text = "SELECT * FROM blog.e"
    result = get_result(completer, text)
    assert result[0] == table("Entries E", -1)


@parametrize("completer", completers(aliasing=True, casing=True))
def test_join_alias_search_with_aliases1(completer):
    text = "SELECT * FROM blog.Entries E JOIN blog.e"
    result = get_result(completer, text)
    assert result[:2] == [
        table("Entries E2", -1),
        join("EntAccLog EAL ON EAL.EntryID = E.EntryID", -1),
    ]


@parametrize("completer", completers(aliasing=False, casing=True))
def test_join_alias_search_without_aliases1(completer):
    text = "SELECT * FROM blog.Entries JOIN blog.e"
    result = get_result(completer, text)
    assert result[:2] == [
        table("Entries", -1),
        join("EntAccLog ON EntAccLog.EntryID = Entries.EntryID", -1),
    ]


@parametrize("completer", completers(aliasing=True, casing=True))
def test_join_alias_search_with_aliases2(completer):
    text = "SELECT * FROM blog.Entries E JOIN blog.et"
    result = get_result(completer, text)
    assert result[0] == join("EntryTags ET ON ET.EntryID = E.EntryID", -2)


@parametrize("completer", completers(aliasing=False, casing=True))
def test_join_alias_search_without_aliases2(completer):
    text = "SELECT * FROM blog.Entries JOIN blog.et"
    result = get_result(completer, text)
    assert result[0] == join("EntryTags ON EntryTags.EntryID = Entries.EntryID", -2)


@parametrize("completer", completers())
def test_function_alias_search_without_aliases(completer):
    text = "SELECT blog.ees"
    result = get_result(completer, text)
    first = result[0]
    assert first.start_position == -3
    assert first.text == "extract_entry_symbols()"
    assert first.display_text == "extract_entry_symbols(_entryid)"


@parametrize("completer", completers())
def test_function_alias_search_with_aliases(completer):
    text = "SELECT blog.ee"
    result = get_result(completer, text)
    first = result[0]
    assert first.start_position == -2
    assert first.text == "enter_entry(_title := , _text := )"
    assert first.display_text == "enter_entry(_title, _text)"


@parametrize("completer", completers(filtr=True, casing=True, qualify=no_qual))
def test_column_alias_search(completer):
    result = get_result(completer, "SELECT et FROM blog.Entries E", len("SELECT et"))
    cols = ("EntryText", "EntryTitle", "EntryID")
    assert result[:3] == [column(c, -2) for c in cols]


@parametrize("completer", completers(casing=True))
def test_column_alias_search_qualified(completer):
    result = get_result(
        completer, "SELECT E.ei FROM blog.Entries E", len("SELECT E.ei")
    )
    cols = ("EntryID", "EntryTitle")
    assert result[:3] == [column(c, -2) for c in cols]


@parametrize("completer", completers(casing=False, filtr=False, aliasing=False))
def test_schema_object_order(completer):
    result = get_result(completer, "SELECT * FROM u")
    assert result[:3] == [
        table(t, pos=-1) for t in ("users", 'custom."Users"', "custom.users")
    ]


@parametrize("completer", completers(casing=False, filtr=False, aliasing=False))
def test_all_schema_objects(completer):
    text = "SELECT * FROM "
    result = get_result(completer, text)
    assert completions_to_set(result) >= completions_to_set(
        [table(x) for x in ("orders", '"select"', "custom.shipments")]
        + [function(x + "()") for x in ("func2",)]
    )


@parametrize("completer", completers(filtr=False, aliasing=False, casing=True))
def test_all_schema_objects_with_casing(completer):
    text = "SELECT * FROM "
    result = get_result(completer, text)
    assert completions_to_set(result) >= completions_to_set(
        [table(x) for x in ("Orders", '"select"', "CUSTOM.shipments")]
        + [function(x + "()") for x in ("func2",)]
    )


@parametrize("completer", completers(casing=False, filtr=False, aliasing=True))
def test_all_schema_objects_with_aliases(completer):
    text = "SELECT * FROM "
    result = get_result(completer, text)
    assert completions_to_set(result) >= completions_to_set(
        [table(x) for x in ("orders o", '"select" s', "custom.shipments s")]
        + [function(x) for x in ("func2() f",)]
    )


@parametrize("completer", completers(casing=False, filtr=False, aliasing=True))
def test_set_schema(completer):
    text = "SET SCHEMA "
    result = get_result(completer, text)
    assert completions_to_set(result) == completions_to_set(
        [schema("'blog'"), schema("'Custom'"), schema("'custom'"), schema("'public'")]
    )