@@ -58,7 +58,8 @@ mod tests {
5858 use crate :: {
5959 CompletionItem , CompletionItemKind , complete,
6060 test_helper:: {
61- CompletionAssertion , assert_complete_results, get_test_deps, get_test_params,
61+ CompletionAssertion , TestCompletionsBuilder , assert_complete_results, get_test_deps,
62+ get_test_params,
6263 } ,
6364 } ;
6465
@@ -381,7 +382,7 @@ mod tests {
381382 }
382383
383384 #[ sqlx:: test( migrator = "pgls_test_utils::MIGRATIONS" ) ]
384- async fn filters_out_by_aliases ( pool : PgPool ) {
385+ async fn filters_out_by_aliases_in_join_on ( pool : PgPool ) {
385386 let setup = r#"
386387 create schema auth;
387388
@@ -400,52 +401,41 @@ mod tests {
400401 );
401402 "# ;
402403
403- pool. execute ( setup) . await . unwrap ( ) ;
404+ TestCompletionsBuilder :: new ( & pool, Some ( setup) )
405+ . prefix_static ( "select u.id, p.content from auth.users u join auth.posts p" )
406+ . type_sql ( "on u<1>.id = p.<2>user_id" )
407+ . comment ( "Should prefer primary indices here." )
408+ . comment ( "We should only get columns from the auth.posts table." )
409+ . snapshot ( )
410+ . await ;
411+ }
404412
405- // test in SELECT clause
406- assert_complete_results (
407- format ! (
408- "select u.id, p.{} from auth.users u join auth.posts p on u.id = p.user_id;" ,
409- QueryWithCursorPosition :: cursor_marker( )
410- )
411- . as_str ( ) ,
412- vec ! [
413- CompletionAssertion :: LabelNotExists ( "uid" . to_string( ) ) ,
414- CompletionAssertion :: LabelNotExists ( "name" . to_string( ) ) ,
415- CompletionAssertion :: LabelNotExists ( "email" . to_string( ) ) ,
416- CompletionAssertion :: Label ( "content" . to_string( ) ) ,
417- CompletionAssertion :: Label ( "created_at" . to_string( ) ) ,
418- CompletionAssertion :: Label ( "pid" . to_string( ) ) ,
419- CompletionAssertion :: Label ( "title" . to_string( ) ) ,
420- CompletionAssertion :: Label ( "user_id" . to_string( ) ) ,
421- ] ,
422- None ,
423- & pool,
424- )
425- . await ;
413+ #[ sqlx:: test( migrator = "pgls_test_utils::MIGRATIONS" ) ]
414+ async fn filters_out_by_aliases_in_select ( pool : PgPool ) {
415+ let setup = r#"
416+ create schema auth;
426417
427- // test in JOIN clause
428- assert_complete_results (
429- format ! (
430- "select u.id, p.content from auth.users u join auth.posts p on u.id = p.{};" ,
431- QueryWithCursorPosition :: cursor_marker( )
432- )
433- . as_str ( ) ,
434- vec ! [
435- CompletionAssertion :: LabelNotExists ( "uid" . to_string( ) ) ,
436- CompletionAssertion :: LabelNotExists ( "name" . to_string( ) ) ,
437- CompletionAssertion :: LabelNotExists ( "email" . to_string( ) ) ,
438- // primary keys are preferred
439- CompletionAssertion :: Label ( "pid" . to_string( ) ) ,
440- CompletionAssertion :: Label ( "content" . to_string( ) ) ,
441- CompletionAssertion :: Label ( "created_at" . to_string( ) ) ,
442- CompletionAssertion :: Label ( "title" . to_string( ) ) ,
443- CompletionAssertion :: Label ( "user_id" . to_string( ) ) ,
444- ] ,
445- None ,
446- & pool,
447- )
448- . await ;
418+ create table auth.users (
419+ uid serial primary key,
420+ name text not null,
421+ email text unique not null
422+ );
423+
424+ create table auth.posts (
425+ pid serial primary key,
426+ user_id int not null references auth.users(uid),
427+ title text not null,
428+ content text,
429+ created_at timestamp default now()
430+ );
431+ "# ;
432+
433+ TestCompletionsBuilder :: new ( & pool, Some ( setup) )
434+ . type_sql ( "select u.id, p.pid<1>" )
435+ . comment ( "We should only get columns from the auth.posts table." )
436+ . append_static ( "from auth.users u join auth.posts p on u.id = p.user_id;" )
437+ . snapshot ( )
438+ . await ;
449439 }
450440
451441 #[ sqlx:: test( migrator = "pgls_test_utils::MIGRATIONS" ) ]
@@ -468,24 +458,12 @@ mod tests {
468458 );
469459 "# ;
470460
471- /*
472- * We are not in the "ON" part of the JOIN clause, so we should not complete columns.
473- */
474- assert_complete_results (
475- format ! (
476- "select u.id, p.content from auth.users u join auth.{}" ,
477- QueryWithCursorPosition :: cursor_marker( )
478- )
479- . as_str ( ) ,
480- vec ! [
481- CompletionAssertion :: KindNotExists ( CompletionItemKind :: Column ) ,
482- CompletionAssertion :: LabelAndKind ( "posts" . to_string( ) , CompletionItemKind :: Table ) ,
483- CompletionAssertion :: LabelAndKind ( "users" . to_string( ) , CompletionItemKind :: Table ) ,
484- ] ,
485- Some ( setup) ,
486- & pool,
487- )
488- . await ;
461+ TestCompletionsBuilder :: new ( & pool, Some ( setup) )
462+ . type_sql ( "select u.uid, p.content from auth<1>.users<2> u join auth.posts p on u.uid = p.user_id" )
463+ . comment ( "Schema suggestions should be prioritized, since we want to push users to specify them." )
464+ . comment ( "Here, we shouldn't have schema completions." )
465+ . snapshot ( )
466+ . await ;
489467 }
490468
491469 #[ sqlx:: test( migrator = "pgls_test_utils::MIGRATIONS" ) ]
0 commit comments