@@ -157,3 +157,105 @@ def test_cannot_delete_qc_state_in_use(api: APIWrapper, qc_states, study, datase
157157 # now clean up/stop using it
158158 dataset_row_to_remove = [{"lsid" : inserted_lsid }]
159159 api .query .delete_rows (SCHEMA_NAME , QUERY_NAME , dataset_row_to_remove )
160+
161+ LISTS_SCHEMA = "lists"
162+ PARENT_LIST_NAME = "parent_list"
163+ PARENT_LIST_DEFINITION = {
164+ "kind" : "IntList" ,
165+ "domainDesign" : {
166+ "name" : PARENT_LIST_NAME ,
167+ "fields" : [
168+ {"name" : "rowId" , "rangeURI" : "int" },
169+ {
170+ "name" : "name" ,
171+ "rangeURI" : "string" ,
172+ "required" : True ,
173+ },
174+ ],
175+ },
176+ "indices" : {
177+ "columnNames" : ["name" ],
178+ "unique" : True ,
179+ },
180+ "options" : {"keyName" : "rowId" , "keyType" : "AutoIncrementInteger" },
181+ }
182+ CHILD_LIST_NAME = "child_list"
183+ CHILD_LIST_DEFINITION = {
184+ "kind" : "IntList" ,
185+ "domainDesign" : {
186+ "name" : CHILD_LIST_NAME ,
187+ "fields" : [
188+ {"name" : "rowId" , "rangeURI" : "int" },
189+ {
190+ "name" : "name" ,
191+ "rangeURI" : "string" ,
192+ "required" : True ,
193+ },
194+ {
195+ "name" : "parent" ,
196+ "lookupQuery" : "parent_list" ,
197+ "lookupSchema" : "lists" ,
198+ "rangeURI" : "int" ,
199+ },
200+ ],
201+ },
202+ "options" : {"keyName" : "rowId" , "keyType" : "AutoIncrementInteger" },
203+ }
204+
205+ parent_data = """name
206+ parent_one
207+ parent_two
208+ parent_three
209+ """
210+
211+ child_data = """name,parent
212+ child_one,parent_one
213+ child_two,parent_two
214+ child_three,parent_three
215+ """
216+
217+ @pytest .fixture
218+ def parent_list_fixture (api : APIWrapper ):
219+ api .domain .create (PARENT_LIST_DEFINITION )
220+ created_list = api .domain .get (LISTS_SCHEMA , PARENT_LIST_NAME )
221+ yield created_list
222+ # clean up
223+ api .domain .drop (LISTS_SCHEMA , PARENT_LIST_NAME )
224+
225+
226+ @pytest .fixture
227+ def child_list_fixture (api : APIWrapper ):
228+ api .domain .create (CHILD_LIST_DEFINITION )
229+ created_list = api .domain .get (LISTS_SCHEMA , CHILD_LIST_NAME )
230+ yield created_list
231+ # clean up
232+ api .domain .drop (LISTS_SCHEMA , CHILD_LIST_NAME )
233+
234+
235+ def test_import_rows (api : APIWrapper , parent_list_fixture , child_list_fixture , tmpdir ):
236+ parent_data_path = tmpdir .join ("parent_data.csv" )
237+ parent_data_path .write (parent_data )
238+ child_data_path = tmpdir .join ("child_data.csv" )
239+ child_data_path .write (child_data )
240+
241+ # Should succeed
242+ parent_file = parent_data_path .open ()
243+ resp = api .query .import_rows ("lists" , PARENT_LIST_NAME , data_file = parent_file )
244+ parent_file .close ()
245+ assert resp ["success" ] == True
246+ assert resp ["rowCount" ] == 3
247+
248+ # Should fail, because data doesn't use rowIds and import_lookup_by_alternate_key defaults to False
249+ child_file = child_data_path .open ()
250+ resp = api .query .import_rows ("lists" , CHILD_LIST_NAME , data_file = child_file )
251+ child_file .close ()
252+ assert resp ["success" ] == False
253+ assert resp ["errorCount" ] == 1
254+ assert resp ["errors" ][0 ]["exception" ] == "Could not convert value 'parent_one' (String) for Integer field 'parent'"
255+
256+ # Should pass, because import_lookup_by_alternate_key is True
257+ child_file = child_data_path .open ()
258+ resp = api .query .import_rows ("lists" , CHILD_LIST_NAME , data_file = child_file , import_lookup_by_alternate_key = True )
259+ child_file .close ()
260+ assert resp ["success" ] == True
261+ assert resp ["rowCount" ] == 3
0 commit comments