github.com/argoproj/argo-events@v1.9.1/sensors/dependencies/transform_test.go (about) 1 package dependencies 2 3 import ( 4 "testing" 5 6 cloudevents "github.com/cloudevents/sdk-go/v2" 7 "github.com/cloudevents/sdk-go/v2/types" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func strptr(s string) *string { 12 return &s 13 } 14 15 func TestApplyJQTransform(t *testing.T) { 16 tests := []struct { 17 event *cloudevents.Event 18 result *cloudevents.Event 19 command string 20 hasError bool 21 }{ 22 { 23 event: &cloudevents.Event{ 24 Context: &cloudevents.EventContextV1{ 25 ID: "123", 26 Source: types.URIRef{}, 27 DataContentType: strptr(cloudevents.ApplicationJSON), 28 Subject: strptr("hello"), 29 Time: &types.Timestamp{}, 30 }, 31 DataEncoded: []byte(`{"a":1,"b":"2"}`), 32 }, 33 result: &cloudevents.Event{ 34 Context: &cloudevents.EventContextV1{ 35 ID: "123", 36 Source: types.URIRef{}, 37 DataContentType: strptr(cloudevents.ApplicationJSON), 38 Subject: strptr("hello"), 39 Time: &types.Timestamp{}, 40 }, 41 DataEncoded: []byte(`{"a":2,"b":"22"}`), 42 }, 43 hasError: false, 44 command: ".a += 1 | .b *= 2", 45 }, 46 } 47 for _, tt := range tests { 48 result, err := applyJQTransform(tt.event, tt.command) 49 if tt.hasError { 50 assert.NotNil(t, err) 51 } else { 52 assert.Nil(t, err) 53 } 54 assert.Equal(t, tt.result.Data(), result.Data()) 55 } 56 } 57 58 func TestApplyScriptTransform(t *testing.T) { 59 tests := []struct { 60 event *cloudevents.Event 61 result *cloudevents.Event 62 script string 63 hasError bool 64 }{ 65 { 66 event: &cloudevents.Event{ 67 Context: &cloudevents.EventContextV1{ 68 ID: "123", 69 Source: types.URIRef{}, 70 DataContentType: strptr(cloudevents.ApplicationJSON), 71 Subject: strptr("hello"), 72 Time: &types.Timestamp{}, 73 }, 74 DataEncoded: []byte(`{"a":1,"b":"2","c":{"d":[3]}}`), 75 }, 76 result: &cloudevents.Event{ 77 Context: &cloudevents.EventContextV1{ 78 ID: "123", 79 Source: types.URIRef{}, 80 DataContentType: strptr(cloudevents.ApplicationJSON), 81 Subject: strptr("hello"), 82 Time: &types.Timestamp{}, 83 }, 84 DataEncoded: []byte(`{"a":1,"b":"2","c":{"d":[4]}}`), 85 }, 86 hasError: false, 87 script: ` 88 event.c.d[1]=4 89 return event 90 `, 91 }, 92 { 93 event: &cloudevents.Event{ 94 Context: &cloudevents.EventContextV1{ 95 ID: "123", 96 Source: types.URIRef{}, 97 DataContentType: strptr(cloudevents.ApplicationJSON), 98 Subject: strptr("hello"), 99 Time: &types.Timestamp{}, 100 }, 101 DataEncoded: []byte(`{"a":1,"b":"2","c":{"d":[3]}}`), 102 }, 103 result: nil, 104 hasError: true, 105 script: ` 106 return "hello" 107 `, 108 }, 109 } 110 for _, tt := range tests { 111 result, err := applyScriptTransform(tt.event, tt.script) 112 if tt.hasError { 113 assert.NotNil(t, err) 114 } else { 115 assert.Nil(t, err) 116 assert.Equal(t, tt.result.Data(), result.Data()) 117 } 118 } 119 }