@@ -154,6 +154,14 @@ func TestExecuteFileScript(t *testing.T) {
154154 // Create temporary directory for test files
155155 tmpDir := t .TempDir ()
156156
157+ anyArgs := func (i int ) []any {
158+ args := make ([]any , i )
159+ for j := range i {
160+ args [j ] = pgxmock .AnyArg ()
161+ }
162+ return args
163+ }
164+
157165 t .Run ("SQL file execution" , func (t * testing.T ) {
158166 // Create temporary SQL file
159167 sqlFile := filepath .Join (tmpDir , "test.sql" )
@@ -240,6 +248,16 @@ func TestExecuteFileScript(t *testing.T) {
240248 cmdOpts .Start .Validate = false
241249 cmdOpts .Start .Replace = false
242250
251+ mockPool .ExpectQuery ("SELECT EXISTS" ).
252+ WithArgs ("test_chain" ).
253+ WillReturnRows (pgxmock .NewRows ([]string {"exists" }).AddRow (false ))
254+ mockPool .ExpectQuery ("INSERT INTO timetable\\ .chain" ).
255+ WithArgs (anyArgs (9 )... ).
256+ WillReturnRows (pgxmock .NewRows ([]string {"chain_id" }).AddRow (1 ))
257+ mockPool .ExpectQuery ("INSERT INTO timetable\\ .task" ).
258+ WithArgs (anyArgs (10 )... ).
259+ WillReturnRows (pgxmock .NewRows ([]string {"task_id" }).AddRow (1 ))
260+
243261 err = mockpge .ExecuteFileScript (context .Background (), cmdOpts )
244262 assert .NoError (t , err )
245263 })
@@ -263,7 +281,7 @@ func TestExecuteFileScript(t *testing.T) {
263281 assert .NoError (t , err )
264282 })
265283
266- t .Run ("File without extension - YAML content " , func (t * testing.T ) {
284+ t .Run ("File without extension" , func (t * testing.T ) {
267285 // Create file without extension with YAML content
268286 noExtFile := filepath .Join (tmpDir , "test_no_ext" )
269287 yamlContent := `chains:
@@ -278,92 +296,17 @@ func TestExecuteFileScript(t *testing.T) {
278296 cmdOpts .Start .File = noExtFile
279297
280298 err = mockpge .ExecuteFileScript (context .Background (), cmdOpts )
281- assert .NoError (t , err )
282- })
283-
284- t .Run ("File without extension - SQL content" , func (t * testing.T ) {
285- // Create file without extension with SQL content
286- noExtFile := filepath .Join (tmpDir , "test_no_ext_sql" )
287- sqlContent := "SELECT 1;"
288- err := os .WriteFile (noExtFile , []byte (sqlContent ), 0644 )
289- assert .NoError (t , err )
290-
291- // Mock the SQL execution
292- mockPool .ExpectExec ("SELECT 1;" ).WillReturnResult (pgxmock .NewResult ("SELECT" , 1 ))
293-
294- cmdOpts := config.CmdOptions {}
295- cmdOpts .Start .File = noExtFile
296-
297- err = mockpge .ExecuteFileScript (context .Background (), cmdOpts )
298- assert .NoError (t , err )
299- })
300-
301- t .Run ("File not found error - SQL file" , func (t * testing.T ) {
302- cmdOpts := config.CmdOptions {}
303- cmdOpts .Start .File = "/nonexistent/file.sql"
304-
305- err := mockpge .ExecuteFileScript (context .Background (), cmdOpts )
306299 assert .Error (t , err )
307300 })
308301
309- t .Run ("File not found error - unknown extension " , func (t * testing.T ) {
302+ t .Run ("File not found error" , func (t * testing.T ) {
310303 cmdOpts := config.CmdOptions {}
311- cmdOpts .Start .File = "/nonexistent/file.txt "
304+ cmdOpts .Start .File = "/nonexistent/file.sql "
312305
313306 err := mockpge .ExecuteFileScript (context .Background (), cmdOpts )
314307 assert .Error (t , err )
315308 })
316309
317- t .Run ("Unknown file extension defaults to content detection" , func (t * testing.T ) {
318- // Create file with unknown extension containing SQL
319- unknownFile := filepath .Join (tmpDir , "test.unknown" )
320- sqlContent := "SELECT 2;"
321- err := os .WriteFile (unknownFile , []byte (sqlContent ), 0644 )
322- assert .NoError (t , err )
323-
324- // Mock the SQL execution
325- mockPool .ExpectExec ("SELECT 2;" ).WillReturnResult (pgxmock .NewResult ("SELECT" , 1 ))
326-
327- cmdOpts := config.CmdOptions {}
328- cmdOpts .Start .File = unknownFile
329-
330- err = mockpge .ExecuteFileScript (context .Background (), cmdOpts )
331- assert .NoError (t , err )
332- })
333-
334- t .Run ("Empty file" , func (t * testing.T ) {
335- // Create empty file
336- emptyFile := filepath .Join (tmpDir , "empty.txt" )
337- err := os .WriteFile (emptyFile , []byte ("" ), 0644 )
338- assert .NoError (t , err )
339-
340- // Mock empty SQL execution
341- mockPool .ExpectExec ("" ).WillReturnResult (pgxmock .NewResult ("SELECT" , 0 ))
342-
343- cmdOpts := config.CmdOptions {}
344- cmdOpts .Start .File = emptyFile
345-
346- err = mockpge .ExecuteFileScript (context .Background (), cmdOpts )
347- assert .NoError (t , err )
348- })
349-
350- t .Run ("YAML file with whitespace prefix" , func (t * testing.T ) {
351- // Create file with leading whitespace before chains:
352- whitespaceFile := filepath .Join (tmpDir , "whitespace" )
353- yamlContent := `
354-
355- chains:
356- - name: test_chain`
357- err := os .WriteFile (whitespaceFile , []byte (yamlContent ), 0644 )
358- assert .NoError (t , err )
359-
360- cmdOpts := config.CmdOptions {}
361- cmdOpts .Start .File = whitespaceFile
362-
363- err = mockpge .ExecuteFileScript (context .Background (), cmdOpts )
364- assert .NoError (t , err )
365- })
366-
367310 t .Run ("YAML import with replace flag" , func (t * testing.T ) {
368311 // Create temporary YAML file
369312 yamlFile := filepath .Join (tmpDir , "test_replace.yaml" )
@@ -380,14 +323,6 @@ chains:
380323 cmdOpts .Start .Validate = false
381324 cmdOpts .Start .Replace = true
382325
383- anyArgs := func (i int ) []any {
384- args := make ([]any , i )
385- for j := range i {
386- args [j ] = pgxmock .AnyArg ()
387- }
388- return args
389- }
390-
391326 mockPool .ExpectExec ("SELECT timetable\\ .delete_job" ).
392327 WithArgs ("test_chain_replace" ).
393328 WillReturnResult (pgxmock .NewResult ("DELETE" , 1 ))
@@ -424,22 +359,4 @@ INSERT INTO test VALUES (1);`
424359 assert .NoError (t , err )
425360 })
426361
427- t .Run ("Content detection with mixed content" , func (t * testing.T ) {
428- // Create file with content that doesn't start with "chains:"
429- mixedFile := filepath .Join (tmpDir , "mixed_content" )
430- mixedContent := `# This is a comment
431- # chains: this is just a comment, not actual YAML
432- SELECT 1;`
433- err := os .WriteFile (mixedFile , []byte (mixedContent ), 0644 )
434- assert .NoError (t , err )
435-
436- // Mock the SQL execution since it doesn't start with "chains:" - use regex pattern
437- mockPool .ExpectExec (`# This is a comment.*# chains:.*SELECT 1;` ).WillReturnResult (pgxmock .NewResult ("SELECT" , 1 ))
438-
439- cmdOpts := config.CmdOptions {}
440- cmdOpts .Start .File = mixedFile
441-
442- err = mockpge .ExecuteFileScript (context .Background (), cmdOpts )
443- assert .NoError (t , err )
444- })
445362}
0 commit comments