44 "context"
55 "errors"
66 "fmt"
7+ "os"
78 "strconv"
89 "time"
910
@@ -61,6 +62,10 @@ func (p Property) isZero() bool {
6162 return p .Key == "" && p .Value == ""
6263}
6364
65+ func (p Property ) String () string {
66+ return fmt .Sprintf ("{ %v : %v }" , p .Key , p .Value )
67+ }
68+
6469// ValidateCreateUpdateDelete is a comprehensive test scenario utilizing Read/Write/Delete connector operations.
6570//
6671// Flow:
@@ -69,7 +74,8 @@ func (p Property) isZero() bool {
6974// 3. Update the object using the "UP" payload.
7075// 4. Read again and verify updates took effect.
7176// 5. Delete the object at the end.
72- func ValidateCreateUpdateDelete [CP , UP any ](ctx context.Context , conn ConnectorCRUD , objectName string ,
77+ func ValidateCreateUpdateDelete [CP , UP any ](
78+ ctx context.Context , conn ConnectorCRUD , objectName string ,
7379 createPayload CP , updatePayload UP , suite CRUDTestSuite ,
7480) {
7581 fmt .Println ("> TEST Create/Update/Delete" , objectName )
@@ -114,7 +120,7 @@ func ValidateCreateUpdateDelete[CP, UP any](ctx context.Context, conn ConnectorC
114120
115121 // UPDATE
116122 fmt .Println ("Updating some object properties" )
117- err = updateObject (ctx , conn , objectName , objectID , & updatePayload )
123+ _ , err = updateObject (ctx , conn , objectName , objectID , & updatePayload )
118124 failOnError (err )
119125 fmt .Println ("Validate object has changed accordingly" )
120126
@@ -181,7 +187,7 @@ func getRecordIdentifierValue(object map[string]any, key string) string {
181187}
182188
183189func createObject [CP any ](
184- ctx context.Context , conn ConnectorCRUD , objectName string , payload * CP ,
190+ ctx context.Context , conn connectors. WriteConnector , objectName string , payload * CP ,
185191) (* common.WriteResult , error ) {
186192 res , err := conn .Write (ctx , common.WriteParams {
187193 ObjectName : objectName ,
@@ -199,8 +205,10 @@ func createObject[CP any](
199205 return res , nil
200206}
201207
202- func readObjects (ctx context.Context , conn ConnectorCRUD ,
203- objectName string , fields datautils.StringSet , since time.Time ) (* common.ReadResult , error ) {
208+ func readObjects (
209+ ctx context.Context , conn connectors.ReadConnector ,
210+ objectName string , fields datautils.StringSet , since time.Time ,
211+ ) (* common.ReadResult , error ) {
204212 res , err := conn .Read (ctx , common.ReadParams {
205213 ObjectName : objectName ,
206214 Fields : fields ,
@@ -247,22 +255,22 @@ func searchObjectRecord(res *common.ReadResult, key, value string) (*objectRecor
247255}
248256
249257func updateObject [UP any ](
250- ctx context.Context , conn ConnectorCRUD , objectName string , objectID string , payload * UP ,
251- ) error {
258+ ctx context.Context , conn connectors. WriteConnector , objectName string , objectID string , payload * UP ,
259+ ) ( * common. WriteResult , error ) {
252260 res , err := conn .Write (ctx , common.WriteParams {
253261 ObjectName : objectName ,
254262 RecordId : objectID ,
255263 RecordData : payload ,
256264 })
257265 if err != nil {
258- return fmt .Errorf ("error updating object: %w" , err )
266+ return nil , fmt .Errorf ("error updating object: %w" , err )
259267 }
260268
261269 if ! res .Success {
262- return errors .New ("failed to update an object" )
270+ return nil , errors .New ("failed to update an object" )
263271 }
264272
265- return nil
273+ return res , nil
266274}
267275
268276func removeObject (ctx context.Context , conn ConnectorCRUD , objectName string , objectID string ) error {
@@ -283,6 +291,21 @@ func removeObject(ctx context.Context, conn ConnectorCRUD, objectName string, ob
283291
284292func failOnError (err error ) {
285293 if err != nil {
286- utils .Fail ("fatal " , "error" , err )
294+ utils .Fail ("[test failed] " , "error" , err )
287295 }
288296}
297+
298+ // printError prints error and returns true if error is not nil.
299+ func printError (err error ) bool {
300+ if err == nil {
301+ return false
302+ }
303+
304+ fmt .Println ("[test failed]" , "error" , err .Error ())
305+ if httpError , ok := errors.AsType [* common.HTTPError ](err ); ok {
306+ utils .DumpJSON (httpError .Body , os .Stdout )
307+ return true
308+ }
309+
310+ return true
311+ }
0 commit comments