|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/keboola/keboola-sdk-go/v2/pkg/keboola" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestValidateNotificationFilters_ValidFields(t *testing.T) { |
| 11 | + t.Parallel() |
| 12 | + |
| 13 | + validFilters := []keboola.NotificationFilter{ |
| 14 | + {Field: "branch.id", Operator: keboola.NotificationFilterOperatorEquals, Value: "123"}, |
| 15 | + {Field: "job.id", Operator: keboola.NotificationFilterOperatorEquals, Value: "456"}, |
| 16 | + {Field: "job.component.id", Operator: keboola.NotificationFilterOperatorEquals, Value: "ex-generic-v2"}, |
| 17 | + {Field: "job.configuration.id", Operator: keboola.NotificationFilterOperatorEquals, Value: "789"}, |
| 18 | + {Field: "job.token.id", Operator: keboola.NotificationFilterOperatorEquals, Value: "token-123"}, |
| 19 | + {Field: "project.id", Operator: keboola.NotificationFilterOperatorEquals, Value: "proj-456"}, |
| 20 | + {Field: "eventType", Operator: keboola.NotificationFilterOperatorEquals, Value: "job-failed"}, |
| 21 | + } |
| 22 | + |
| 23 | + err := ValidateNotificationFilters(validFilters) |
| 24 | + assert.NoError(t, err, "valid filter fields should not return error") |
| 25 | +} |
| 26 | + |
| 27 | +func TestValidateNotificationFilters_DeprecatedFields(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + field string |
| 33 | + correctField string |
| 34 | + }{ |
| 35 | + {"configId", "configId", "job.configuration.id"}, |
| 36 | + {"componentId", "componentId", "job.component.id"}, |
| 37 | + {"branchId", "branchId", "branch.id"}, |
| 38 | + {"jobId", "jobId", "job.id"}, |
| 39 | + {"tokenId", "tokenId", "job.token.id"}, |
| 40 | + {"projectId", "projectId", "project.id"}, |
| 41 | + } |
| 42 | + |
| 43 | + for _, tt := range tests { |
| 44 | + t.Run(tt.name, func(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + |
| 47 | + filters := []keboola.NotificationFilter{ |
| 48 | + {Field: tt.field, Operator: keboola.NotificationFilterOperatorEquals, Value: "123"}, |
| 49 | + } |
| 50 | + |
| 51 | + err := ValidateNotificationFilters(filters) |
| 52 | + assert.Error(t, err, "deprecated field should return error") |
| 53 | + assert.Contains(t, err.Error(), "deprecated field name") |
| 54 | + assert.Contains(t, err.Error(), tt.field) |
| 55 | + assert.Contains(t, err.Error(), tt.correctField) |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestValidateNotificationFilters_InvalidFields(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + tests := []struct { |
| 64 | + name string |
| 65 | + field string |
| 66 | + }{ |
| 67 | + {"completely_invalid", "foobar"}, |
| 68 | + {"another_invalid", "xyz123"}, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tt := range tests { |
| 72 | + t.Run(tt.name, func(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + |
| 75 | + filters := []keboola.NotificationFilter{ |
| 76 | + {Field: tt.field, Operator: keboola.NotificationFilterOperatorEquals, Value: "123"}, |
| 77 | + } |
| 78 | + |
| 79 | + err := ValidateNotificationFilters(filters) |
| 80 | + assert.Error(t, err, "invalid field should return error") |
| 81 | + assert.Contains(t, err.Error(), "invalid field name") |
| 82 | + assert.Contains(t, err.Error(), tt.field) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestValidateNotificationFilters_EmptyFilters(t *testing.T) { |
| 88 | + t.Parallel() |
| 89 | + |
| 90 | + err := ValidateNotificationFilters([]keboola.NotificationFilter{}) |
| 91 | + assert.NoError(t, err, "empty filters should not return error") |
| 92 | +} |
| 93 | + |
| 94 | +func TestValidateNotificationFilters_MultipleFilters(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + |
| 97 | + filters := []keboola.NotificationFilter{ |
| 98 | + {Field: "branch.id", Operator: keboola.NotificationFilterOperatorEquals, Value: "123"}, |
| 99 | + {Field: "configId", Operator: keboola.NotificationFilterOperatorEquals, Value: "456"}, // deprecated |
| 100 | + {Field: "job.token.id", Operator: keboola.NotificationFilterOperatorEquals, Value: "789"}, |
| 101 | + } |
| 102 | + |
| 103 | + err := ValidateNotificationFilters(filters) |
| 104 | + assert.Error(t, err, "should fail on first invalid filter") |
| 105 | + assert.Contains(t, err.Error(), "filter[1]") |
| 106 | + assert.Contains(t, err.Error(), "configId") |
| 107 | +} |
| 108 | + |
| 109 | +func TestFindSimilarFieldNames(t *testing.T) { |
| 110 | + t.Parallel() |
| 111 | + |
| 112 | + tests := []struct { |
| 113 | + name string |
| 114 | + input string |
| 115 | + expectedSuggestions []string |
| 116 | + }{ |
| 117 | + { |
| 118 | + name: "config", |
| 119 | + input: "config", |
| 120 | + expectedSuggestions: []string{"job.configuration.id"}, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "job", |
| 124 | + input: "job", |
| 125 | + expectedSuggestions: []string{"job.id", "job.component.id", "job.configuration.id", "job.token.id"}, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "branch", |
| 129 | + input: "branch", |
| 130 | + expectedSuggestions: []string{"branch.id"}, |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "completely_invalid", |
| 134 | + input: "xyz", |
| 135 | + expectedSuggestions: []string{}, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + for _, tt := range tests { |
| 140 | + t.Run(tt.name, func(t *testing.T) { |
| 141 | + t.Parallel() |
| 142 | + |
| 143 | + suggestions := findSimilarFieldNames(tt.input) |
| 144 | + |
| 145 | + // Check that all expected suggestions are present |
| 146 | + for _, expected := range tt.expectedSuggestions { |
| 147 | + assert.Contains(t, suggestions, expected, "should contain suggested field") |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | +} |
0 commit comments