Skip to content

Commit 670d667

Browse files
committed
fix tests
1 parent e311ed4 commit 670d667

3 files changed

Lines changed: 91 additions & 26 deletions

File tree

internal/persistence/filequeue/job_test.go

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

internal/persistence/filequeue/queuefile_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ func TestQueueFile_FindByDAGRunID(t *testing.T) {
8686
job, err := qf.FindByDAGRunID(th.Context, "test-dag")
8787
require.NoError(t, err, "expected no error when finding job by dag-run ID")
8888
require.NotNil(t, job, "expected job to be not nil")
89-
require.Equal(t, "test-name", job.DAGRun.Name, "expected job name to be 'test-name'")
90-
require.Equal(t, "test-dag", job.DAGRun.ID, "expected job ID to be 'test'")
9189

92-
// Check if the item has the correct prefix
93-
require.Regexp(t, "^item_high_", job.FileName, "expected job file name to start with 'high_'")
90+
data, err := job.Data()
91+
require.NoError(t, err, "expected no error when getting job data")
92+
require.Equal(t, "test-name", data.Name, "expected job name to be 'test-name'")
93+
require.Equal(t, "test-dag", data.ID, "expected job ID to be 'test'")
94+
95+
// Check if the item has the correct prefix (ID is derived from filename)
96+
require.Regexp(t, "^item_high_", job.ID(), "expected job ID to start with 'item_high_'")
9497
}
9598

9699
func TestQueueFile_Pop(t *testing.T) {

internal/persistence/filequeue/store.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,7 @@ func (s *Store) All(ctx context.Context) ([]execution.QueuedItemData, error) {
9999
sort.Strings(files)
100100

101101
for _, file := range files {
102-
data, err := parseQueueFileName(file, filepath.Base(file))
103-
if err != nil {
104-
logger.Error(ctx, "Failed to parse queue file name",
105-
tag.File(file),
106-
tag.Error(err))
107-
continue
108-
}
109-
items = append(items, NewJob(file, data))
102+
items = append(items, NewJob(file))
110103
}
111104
}
112105

0 commit comments

Comments
 (0)