@@ -10,7 +10,7 @@ import (
1010 "github.com/stretchr/testify/require"
1111)
1212
13- func TestJob (t * testing.T ) {
13+ func TestQueuedFile (t * testing.T ) {
1414 t .Parallel ()
1515
1616 // Create a temporary directory for the test
@@ -30,32 +30,101 @@ func TestJob(t *testing.T) {
3030 err = os .WriteFile (testFilePath , fileContent , 0644 )
3131 require .NoError (t , err , "expected no error when writing test file" )
3232
33- // Create a new job
34- job := NewJob (testFilePath , itemData )
33+ // Create a new QueuedFile
34+ queuedFile := NewJob (testFilePath )
3535
36- // Check if the job ID is correct
37- require .Equal (t , "test-file" , job .ID (), "expected job ID to be 'test-file'" )
36+ // Check if the ID is correct
37+ require .Equal (t , "test-file" , queuedFile .ID (), "expected job ID to be 'test-file'" )
3838
39- // Check if the job data is correct
40- jobData , err := job .Data ()
39+ // Check if the data is correct (lazy loading from file)
40+ jobData , err := queuedFile .Data ()
4141 require .NoError (t , err , "expected no error when getting job data" )
4242 require .Equal (t , "test-name" , jobData .Name , "expected job name to be 'test-name'" )
4343 require .Equal (t , "test-dag" , jobData .ID , "expected job ID to be 'test-dag'" )
4444}
4545
46- func TestJob_DataError (t * testing.T ) {
46+ func TestQueuedFile_DataError (t * testing.T ) {
4747 t .Parallel ()
4848
49- // Create a job with a non-existent file
50- job := NewJob ("/nonexistent/path/test-file.json" , ItemData {
49+ // Create a QueuedFile with a non-existent file
50+ queuedFile := NewJob ("/nonexistent/path/test-file.json" )
51+
52+ // Check if Data() returns an error for non-existent file
53+ _ , err := queuedFile .Data ()
54+ require .Error (t , err , "expected error when reading non-existent file" )
55+ }
56+
57+ func TestQueuedFile_ExtractJob (t * testing.T ) {
58+ t .Parallel ()
59+
60+ // Create a temporary directory for the test
61+ tmpDir := t .TempDir ()
62+
63+ // Create the test file with proper JSON content
64+ testFilePath := filepath .Join (tmpDir , "test-file.json" )
65+ itemData := ItemData {
5166 FileName : "test-file.json" ,
5267 DAGRun : execution.DAGRunRef {
5368 Name : "test-name" ,
5469 ID : "test-dag" ,
5570 },
56- })
71+ }
72+ fileContent , err := json .Marshal (itemData )
73+ require .NoError (t , err , "expected no error when marshaling item data" )
74+ err = os .WriteFile (testFilePath , fileContent , 0644 )
75+ require .NoError (t , err , "expected no error when writing test file" )
5776
58- // Check if Data() returns an error for non-existent file
59- _ , err := job .Data ()
60- require .Error (t , err , "expected error when reading non-existent file" )
77+ // Create a QueuedFile and extract the Job
78+ queuedFile := NewJob (testFilePath )
79+ job , err := queuedFile .ExtractJob ()
80+ require .NoError (t , err , "expected no error when extracting job" )
81+
82+ // Delete the file to simulate what happens after Pop()
83+ err = os .Remove (testFilePath )
84+ require .NoError (t , err , "expected no error when removing test file" )
85+
86+ // The extracted Job should still have the cached data
87+ jobData , err := job .Data ()
88+ require .NoError (t , err , "expected no error when getting job data after file deletion" )
89+ require .Equal (t , "test-name" , jobData .Name , "expected job name to be 'test-name'" )
90+ require .Equal (t , "test-dag" , jobData .ID , "expected job ID to be 'test-dag'" )
91+ }
92+
93+ func TestQueuedFile_Caching (t * testing.T ) {
94+ t .Parallel ()
95+
96+ // Create a temporary directory for the test
97+ tmpDir := t .TempDir ()
98+
99+ // Create the test file with proper JSON content
100+ testFilePath := filepath .Join (tmpDir , "test-file.json" )
101+ itemData := ItemData {
102+ FileName : "test-file.json" ,
103+ DAGRun : execution.DAGRunRef {
104+ Name : "test-name" ,
105+ ID : "test-dag" ,
106+ },
107+ }
108+ fileContent , err := json .Marshal (itemData )
109+ require .NoError (t , err , "expected no error when marshaling item data" )
110+ err = os .WriteFile (testFilePath , fileContent , 0644 )
111+ require .NoError (t , err , "expected no error when writing test file" )
112+
113+ // Create a QueuedFile
114+ queuedFile := NewJob (testFilePath )
115+
116+ // First call to Data() should load from file
117+ jobData1 , err := queuedFile .Data ()
118+ require .NoError (t , err , "expected no error when getting job data" )
119+ require .Equal (t , "test-name" , jobData1 .Name )
120+
121+ // Delete the file
122+ err = os .Remove (testFilePath )
123+ require .NoError (t , err , "expected no error when removing test file" )
124+
125+ // Second call to Data() should use cached data
126+ jobData2 , err := queuedFile .Data ()
127+ require .NoError (t , err , "expected no error when getting cached job data" )
128+ require .Equal (t , "test-name" , jobData2 .Name )
129+ require .Equal (t , "test-dag" , jobData2 .ID )
61130}
0 commit comments