11package org .labkey .test .tests .query ;
22
3+ import org .assertj .core .api .Assertions ;
34import org .junit .BeforeClass ;
4- import org .junit .Before ;
55import org .junit .Test ;
66import org .junit .experimental .categories .Category ;
7- import org .labkey .api .query .QueryHelper ;
87import org .labkey .test .BaseWebDriverTest ;
8+ import org .labkey .test .Locator ;
9+ import org .labkey .test .categories .Daily ;
10+ import org .labkey .test .pages .list .EditListDefinitionPage ;
911import org .labkey .test .params .FieldDefinition ;
1012import org .labkey .test .params .list .IntListDefinition ;
11- import org .labkey .test .util .TestDataGenerator ;
13+
14+ import org .labkey .test .params .list .VarListDefinition ;
15+ import org .labkey .test .util .DataRegionTable ;
16+ import org .labkey .test .util .EscapeUtil ;
1217
1318import java .util .Arrays ;
1419import java .util .List ;
1520
16- import static org .junit .Assert .*;
1721
18- @ Category ({})
22+ @ Category ({Daily . class })
1923public class QueryLookupTest extends BaseWebDriverTest
2024{
2125 private static final String PROJECT_NAME = "QueryLookupTest" + TRICKY_CHARACTERS_FOR_PROJECT_NAMES ;
22- private static final String USER = "template_user@querylookuptest.test" ;
23- private static final String LIST_NAME = TestDataGenerator .randomFieldName ("list" , "<>[]{};,`\" ~!@#$%^*=|?\\ " );
24- private static final FieldDefinition .ColumnType LIST_KEY_TYPE = FieldDefinition .ColumnType .Integer ;
25- private static final String LIST_KEY_NAME = TestDataGenerator .randomFieldName ("listkey" );
26+ private static final String LIST_NAME = "l&ist q" ;
27+
2628 private static final FieldDefinition NAME_COLUMN =
2729 new FieldDefinition ("Name" , FieldDefinition .ColumnType .String );
2830 private static final FieldDefinition TSHIRT_COLUMN =
@@ -32,7 +34,6 @@ public class QueryLookupTest extends BaseWebDriverTest
3234 protected void doCleanup (boolean afterTest )
3335 {
3436 _containerHelper .deleteProject (getProjectName (), afterTest );
35- _userHelper .deleteUsers (afterTest , USER );
3637 }
3738
3839 @ BeforeClass
@@ -46,32 +47,87 @@ public static void setupProject() throws Exception
4647 private void doSetup () throws Exception
4748 {
4849 _containerHelper .createProject (PROJECT_NAME , null );
49- _userHelper .createUser (USER );
5050
5151 // create a list
52- var dgen = new IntListDefinition (LIST_NAME )
53- .setFields (List .of (
54- new FieldDefinition (LIST_KEY_NAME , LIST_KEY_TYPE ),
55- NAME_COLUMN ,
56- TSHIRT_COLUMN ))
52+ var dgen = new VarListDefinition (LIST_NAME )
53+ .setFields (List .of (NAME_COLUMN , TSHIRT_COLUMN ))
5754 .create (createDefaultConnection (), PROJECT_NAME );
5855 dgen .withGeneratedRows (10 )
5956 .insertRows ();
57+ }
58+
59+ // Issue 49511 Setting lookup to custom query shows "Error: Lookup target table does not exist."
60+ @ Test
61+ public void testLookupToQueryColumn () throws Exception
62+ {
63+ var insertedRows = executeSelectRowCommand ("lists" , LIST_NAME ).getRows ();
64+ var itemNames = insertedRows .stream ().map (a -> a .get ("name" ).toString ()).toList ();
65+ String secondList = "secondList" ;
6066
67+ // create a query from LIST_NAME list, with a key defined in the query xml
68+ String queryName = "query from list" ;
69+ String querySql = """
70+ SELECT [list_name].Name,
71+ [list_name].TShirt
72+ FROM [list_name]
73+ """ .replace ("[list_name]" , EscapeUtil .getSqlQuotedValue (LIST_NAME ));
74+ String queryXml = """
75+ <tables xmlns="http://labkey.org/data/xml">
76+ <table tableName="[query_name]" tableDbType="NOT_IN_DB">
77+ <columns>
78+ <column columnName="Name">
79+ <isKeyField>true</isKeyField>
80+ </column>
81+ </columns>
82+ </table>
83+ </tables>
84+ """ .replace ("[query_name]" , EscapeUtil .getMarkupEscapedValue (queryName ))
85+ .replace ("[list_key]" , EscapeUtil .getMarkupEscapedValue (NAME_COLUMN .getName ()));
6186 // create a query on the list
6287 goToSchemaBrowser ();
63- //var queryPage = navigateToQuery("lists", LIST_NAME);
64- var queryPage = createNewQuery ("lists" , LIST_NAME );
88+ createQuery (getProjectName (), queryName , "lists" , querySql , queryXml , false );
6589
66- log ("foo" );
67- }
90+ // now create another list, with a lookup to the custom query
91+ new IntListDefinition (secondList , "Key" )
92+ .setFields (List .of (NAME_COLUMN ,
93+ new FieldDefinition ("lookup" , new FieldDefinition .StringLookup (getProjectName (), "lists" , queryName ))))
94+ .create (createDefaultConnection (), PROJECT_NAME );
6895
96+ // insert data into the list
97+ goToManageLists ();
98+ waitAndClickAndWait (Locator .linkWithText (secondList ));
99+ var importPage = DataRegionTable .DataRegion (getDriver ()).withName ("query" ).waitFor ()
100+ .clickImportBulkData ();
101+ StringBuilder builder = new StringBuilder ("Name\t lookup\n " );
102+ int textIndex = 0 ;
103+ for (String name : itemNames )
104+ {
105+ builder .append (String .format ("text-%d\t %s\n " , textIndex , name ));
106+ textIndex ++;
107+ }
108+ importPage .setText (builder .toString ());
109+ importPage .submit ();
110+
111+ // verify the query-list-items resolve here
112+ var secondListDataRegion = DataRegionTable .DataRegion (getDriver ()).withName ("query" ).waitFor ();
113+ var resolvedLookupsInSecondList = secondListDataRegion .getColumnDataAsText ("lookup" );
114+ checker ().withScreenshot ("query lookup values not resolved" )
115+ .wrapAssertion (()-> Assertions .assertThat (resolvedLookupsInSecondList )
116+ .as ("the expected sample items were not resolved via query lookup" )
117+ .containsAll (itemNames ));
118+
119+ // navigate to the secondlist edit page and verify the details on the lookup field row are correct
120+ secondListDataRegion .clickHeaderButtonAndWait ("Design" );
121+ var listEditPage = new EditListDefinitionPage (getDriver ());
122+ var lookupFieldRow = listEditPage .getFieldsPanel ().getField ("Lookup" );
123+ checker ().withScreenshot ("unexpected lookup details" )
124+ .wrapAssertion (()-> Assertions .assertThat (lookupFieldRow .detailsMessage ())
125+ .as ("query lookup table not resolved" )
126+ .isEqualTo (String .format ("/%s > lists > %s" , PROJECT_NAME , queryName )));
127+ // click the link in the details message, expect to navigate to a view of the query
128+ clickAndWait (Locator .linkWithText (queryName ).findElement (lookupFieldRow ));
129+ assertTextPresent ("lists Schema" , queryName , PROJECT_NAME );
69130
70- // Issue 49511 Setting lookup to custom query shows "Error: Lookup target table does not exist."
71- @ Test
72- public void testSomething ()
73- {
74- assertTrue ("Failing stub test" , false );
75131 }
76132
77133 @ Override
0 commit comments