package jobqueue import ( "github.com/stretchr/testify/assert" "io/ioutil" "os" "testing" "time" ) func TestCreateJobAndSchedulerFromInput(t *testing.T) { tests := []struct { name string input JobImport wantJob GenericJob wantSched Scheduler wantErr bool }{ { name: "Shell Runnable and Interval Scheduler", input: JobImport{ ID: "1", Priority: 1, Timeout: 10 * time.Second, MaxRetries: 3, RetryDelay: 2 * time.Second, Runnable: RunnableImport{ Type: "Shell", Data: map[string]any{"ScriptPath": "script.sh"}, }, Scheduler: SchedulerImport{ Type: "Interval", Interval: 1 * time.Minute, }, }, wantJob: GenericJob(&Job[ShellResult]{ /* Initialization */ }), wantSched: &IntervalScheduler{Interval: 1 * time.Minute}, wantErr: false, }, { name: "Shell Runnable and Cron Scheduler", input: JobImport{ ID: "1", Priority: 1, Runnable: RunnableImport{ Type: "Shell", Data: map[string]any{"ScriptPath": "script.sh"}, }, Scheduler: SchedulerImport{ Type: "Cron", Spec: "*/5 * * * *", }, }, wantJob: GenericJob(&Job[ShellResult]{ /* Initialization */ }), wantSched: &CronScheduler{ /* Initialization */ }, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { gotJob, gotSchedule, err := CreateJobAndSchedulerFromImport(tt.input) if (err != nil) != tt.wantErr { t.Errorf("CreateJobAndSchedulerFromImport() error = %v, wantErr %v", err, tt.wantErr) return } assert.Equal(t, JobID(tt.input.ID), gotJob.GetID(), "Job ID mismatch") assert.Equal(t, Priority(tt.input.Priority), gotJob.GetPriority(), "Job Priority mismatch") assert.Equal(t, tt.input.Scheduler.Type, gotSchedule.GetType(), "Scheduler Type mismatch") }) } } func TestReadJsonFile(t *testing.T) { testContent := `[{"id": "1", "priority": 1}]` tempFile, err := ioutil.TempFile("", "test.json") if err != nil { t.Fatal(err) } defer os.Remove(tempFile.Name()) if _, err := tempFile.Write([]byte(testContent)); err != nil { t.Fatal(err) } tempFile.Close() jobs, err := ReadJsonFile(tempFile.Name()) if err != nil { t.Fatal(err) } if len(jobs) != 1 || jobs[0].ID != "1" || jobs[0].Priority != 1 { t.Errorf("Expected job with ID '1' and priority 1, got %+v", jobs) } } func TestReadYAMLFile(t *testing.T) { testContent := `- id: "1" priority: 1 ` tempFile, err := ioutil.TempFile("", "test.yaml") if err != nil { t.Fatal(err) } defer os.Remove(tempFile.Name()) if _, err := tempFile.Write([]byte(testContent)); err != nil { t.Fatal(err) } tempFile.Close() jobs, err := ReadYAMLFile(tempFile.Name()) if err != nil { t.Fatal(err) } if len(jobs) != 1 || jobs[0].ID != "1" || jobs[0].Priority != 1 { t.Errorf("Expected job with ID '1' and priority 1, got %+v", jobs) } }