@@ -53,136 +53,145 @@ type TaskParams struct {
5353
5454func Test_CreateNewTask (t * testing.T ) {
5555 Wrap (t , func (t * testing.T , client arangodb.Client ) {
56- dbName := "_system"
57- testCases := map [string ]* arangodb.TaskOptions {
58- "taskWithParams" : {
59- Name : utils .NewType ("taskWithParams" ),
60- Command : utils .NewType ("(function(params) { require('@arangodb').print(params); })(params)" ),
61- Period : utils .NewType (int64 (2 )),
62- Params : map [string ]interface {}{
63- "test" : "hello" ,
64- },
65- },
66- "taskWithoutParams" : {
67- Name : utils .NewType ("taskWithoutParams" ),
68- Command : utils .NewType ("(function() { require('@arangodb').print('Hello'); })()" ),
69- Period : utils .NewType (int64 (2 )),
70- },
71- }
72-
73- for name , options := range testCases {
56+ WithDatabase (t , client , nil , func (db arangodb.Database ) {
7457 withContextT (t , defaultTestTimeout , func (ctx context.Context , tb testing.TB ) {
75- createdTask , err := client .CreateTask (ctx , dbName , * options )
76- require .NoError (t , err )
77- require .NotNil (t , createdTask )
78- require .Equal (t , name , * createdTask .Name ())
79- t .Logf ("Params: %v" , options .Params )
80- // Proper params comparison
81- // Check parameters
82- if options .Params != nil {
83- var params map [string ]interface {}
84- err = createdTask .Params (& params )
85-
86- if err != nil {
87- t .Logf ("WARNING: Could not fetch task params (unsupported feature?): %v" , err )
88- } else if len (params ) == 0 {
89- t .Logf ("WARNING: Task params exist but returned empty (ArangoDB limitation?)" )
90- } else {
91- // Only check if params are actually returned
92- require .Equal (t , options .Params , params )
93- }
58+ requireV8Enabled (client , ctx , tb )
59+ testCases := map [string ]* arangodb.TaskOptions {
60+ "taskWithParams" : {
61+ Name : utils .NewType ("taskWithParams" ),
62+ Command : utils .NewType ("(function(params) { require('@arangodb').print(params); })(params)" ),
63+ Period : utils .NewType (int64 (2 )),
64+ Params : map [string ]interface {}{
65+ "test" : "hello" ,
66+ },
67+ },
68+ "taskWithoutParams" : {
69+ Name : utils .NewType ("taskWithoutParams" ),
70+ Command : utils .NewType ("(function() { require('@arangodb').print('Hello'); })()" ),
71+ Period : utils .NewType (int64 (2 )),
72+ },
9473 }
9574
96- taskInfo , err := client .Task (ctx , dbName , * createdTask .ID ())
97- require .NoError (t , err )
98- require .NotNil (t , taskInfo )
99- require .Equal (t , name , * taskInfo .Name ())
100-
101- tasks , err := client .Tasks (ctx , dbName )
102- require .NoError (t , err )
103- require .NotNil (t , tasks )
104- require .Greater (t , len (tasks ), 0 , "Expected at least one task to be present" )
105- t .Logf ("Found tasks: %v" , tasks )
106- if len (tasks ) > 0 && tasks [0 ].ID () != nil {
107- t .Logf ("Task Id to be removed: %s\n " , * tasks [0 ].ID ())
108- } else {
109- t .Logf ("Task Id to be removed: <nil>" )
110- }
111- if id := createdTask .ID (); id != nil {
112- require .NoError (t , client .RemoveTask (ctx , dbName , * id ))
113- t .Logf ("Task %s removed successfully" , * id )
114- } else {
115- t .Logf ("Task ID is nil" )
75+ for name , options := range testCases {
76+ withContextT (t , defaultTestTimeout , func (ctx context.Context , tb testing.TB ) {
77+ createdTask , err := client .CreateTask (ctx , db .Name (), * options )
78+ require .NoError (t , err )
79+ require .NotNil (t , createdTask )
80+ require .Equal (t , name , * createdTask .Name ())
81+ t .Logf ("Params: %v" , options .Params )
82+ // Proper params comparison
83+ // Check parameters
84+ if options .Params != nil {
85+ var params map [string ]interface {}
86+ err = createdTask .Params (& params )
87+
88+ if err != nil {
89+ t .Logf ("WARNING: Could not fetch task params (unsupported feature?): %v" , err )
90+ } else if len (params ) == 0 {
91+ t .Logf ("WARNING: Task params exist but returned empty (ArangoDB limitation?)" )
92+ } else {
93+ // Only check if params are actually returned
94+ require .Equal (t , options .Params , params )
95+ }
96+ }
97+
98+ taskInfo , err := client .Task (ctx , db .Name (), * createdTask .ID ())
99+ require .NoError (t , err )
100+ require .NotNil (t , taskInfo )
101+ require .Equal (t , name , * taskInfo .Name ())
102+
103+ tasks , err := client .Tasks (ctx , db .Name ())
104+ require .NoError (t , err )
105+ require .NotNil (t , tasks )
106+ require .Greater (t , len (tasks ), 0 , "Expected at least one task to be present" )
107+ t .Logf ("Found tasks: %v" , tasks )
108+ if len (tasks ) > 0 && tasks [0 ].ID () != nil {
109+ t .Logf ("Task Id to be removed: %s\n " , * tasks [0 ].ID ())
110+ } else {
111+ t .Logf ("Task Id to be removed: <nil>" )
112+ }
113+ if id := createdTask .ID (); id != nil {
114+ require .NoError (t , client .RemoveTask (ctx , db .Name (), * id ))
115+ t .Logf ("Task %s removed successfully" , * id )
116+ } else {
117+ t .Logf ("Task ID is nil" )
118+ }
119+ })
116120 }
117121 })
118- }
122+ })
119123 }, WrapOptions {
120124 Parallel : utils .NewType (false ),
121125 })
122126}
123127
124128func Test_ValidationsForCreateNewTask (t * testing.T ) {
125129 Wrap (t , func (t * testing.T , client arangodb.Client ) {
126- dbName := "_system"
127- testCases := map [string ]* arangodb.TaskOptions {
128- "taskWithoutCommand" : {
129- Name : utils .NewType ("taskWithoutCommand" ),
130- Period : utils .NewType (int64 (2 )),
131- },
132- "taskWithoutPeriod" : nil ,
133- }
134-
135- for name , options := range testCases {
130+ WithDatabase (t , client , nil , func (db arangodb.Database ) {
136131 withContextT (t , defaultTestTimeout , func (ctx context.Context , tb testing.TB ) {
137- var err error
138- if options == nil {
139- _ , err = client .CreateTask (ctx , dbName , arangodb.TaskOptions {})
140- } else {
141- _ , err = client .CreateTask (ctx , dbName , * options )
132+ requireV8Enabled (client , ctx , tb )
133+ testCases := map [string ]* arangodb.TaskOptions {
134+ "taskWithoutCommand" : {
135+ Name : utils .NewType ("taskWithoutCommand" ),
136+ Period : utils .NewType (int64 (2 )),
137+ },
138+ "taskWithoutPeriod" : nil ,
142139 }
143140
144- require .Error (t , err )
145- t .Logf ("Expected error for task '%s': %v" , name , err )
141+ for name , options := range testCases {
142+ withContextT (t , defaultTestTimeout , func (ctx context.Context , tb testing.TB ) {
143+ var err error
144+ if options == nil {
145+ _ , err = client .CreateTask (ctx , db .Name (), arangodb.TaskOptions {})
146+ } else {
147+ _ , err = client .CreateTask (ctx , db .Name (), * options )
148+ }
149+
150+ require .Error (t , err )
151+ t .Logf ("Expected error for task '%s': %v" , name , err )
152+ })
153+ }
146154 })
147- }
148-
155+ })
149156 }, WrapOptions {
150157 Parallel : utils .NewType (false ),
151158 })
152159}
153160
154161func Test_TaskCreationWithId (t * testing.T ) {
155162 Wrap (t , func (t * testing.T , client arangodb.Client ) {
156- withContextT (t , defaultTestTimeout , func (ctx context.Context , tb testing.TB ) {
157- dbName := "_system"
158- taskID := "test-task-id" + StringWithCharset (16 , charset )
159- options := & arangodb.TaskOptions {
160- ID : & taskID , // Optional if CreateTaskWithID sets it, but safe to keep
161- Name : utils .NewType ("TestTaskWithID" ),
162- Command : utils .NewType ("console.log('This is a test task with ID');" ),
163- Period : utils .NewType (int64 (5 )),
164- }
165-
166- // Create the task with explicit ID
167- task , err := client .CreateTaskWithID (ctx , dbName , taskID , * options )
168- require .NoError (t , err , "Expected task creation to succeed" )
169- require .NotNil (t , task , "Expected task to be non-nil" )
170- require .Equal (t , taskID , * task .ID (), "Task ID mismatch" )
171- require .Equal (t , * options .Name , * task .Name (), "Task Name mismatch" )
172-
173- // Retrieve and validate
174- retrievedTask , err := client .Task (ctx , dbName , taskID )
175- require .NoError (t , err , "Expected task retrieval to succeed" )
176- require .NotNil (t , retrievedTask , "Expected retrieved task to be non-nil" )
177- require .Equal (t , taskID , * retrievedTask .ID (), "Retrieved task ID mismatch" )
178- require .Equal (t , * options .Name , * retrievedTask .Name (), "Retrieved task Name mismatch" )
179-
180- _ , err = client .CreateTaskWithID (ctx , dbName , taskID , * options )
181- require .Error (t , err , "Creating a duplicate task should fail" )
182-
183- // Clean up
184- err = client .RemoveTask (ctx , dbName , taskID )
185- require .NoError (t , err , "Expected task removal to succeed" )
163+ WithDatabase (t , client , nil , func (db arangodb.Database ) {
164+ withContextT (t , defaultTestTimeout , func (ctx context.Context , tb testing.TB ) {
165+ requireV8Enabled (client , ctx , tb )
166+ taskID := "test-task-id" + StringWithCharset (16 , charset )
167+ options := & arangodb.TaskOptions {
168+ ID : & taskID , // Optional if CreateTaskWithID sets it, but safe to keep
169+ Name : utils .NewType ("TestTaskWithID" ),
170+ Command : utils .NewType ("console.log('This is a test task with ID');" ),
171+ Period : utils .NewType (int64 (5 )),
172+ }
173+
174+ // Create the task with explicit ID
175+ task , err := client .CreateTaskWithID (ctx , db .Name (), taskID , * options )
176+ require .NoError (t , err , "Expected task creation to succeed" )
177+ require .NotNil (t , task , "Expected task to be non-nil" )
178+ require .Equal (t , taskID , * task .ID (), "Task ID mismatch" )
179+ require .Equal (t , * options .Name , * task .Name (), "Task Name mismatch" )
180+
181+ // Retrieve and validate
182+ retrievedTask , err := client .Task (ctx , db .Name (), taskID )
183+ require .NoError (t , err , "Expected task retrieval to succeed" )
184+ require .NotNil (t , retrievedTask , "Expected retrieved task to be non-nil" )
185+ require .Equal (t , taskID , * retrievedTask .ID (), "Retrieved task ID mismatch" )
186+ require .Equal (t , * options .Name , * retrievedTask .Name (), "Retrieved task Name mismatch" )
187+
188+ _ , err = client .CreateTaskWithID (ctx , db .Name (), taskID , * options )
189+ require .Error (t , err , "Creating a duplicate task should fail" )
190+
191+ // Clean up
192+ err = client .RemoveTask (ctx , db .Name (), taskID )
193+ require .NoError (t , err , "Expected task removal to succeed" )
194+ })
186195 })
187196 })
188197}
0 commit comments