|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestParseIntValue(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + value any |
| 11 | + expected int |
| 12 | + ok bool |
| 13 | + }{ |
| 14 | + { |
| 15 | + name: "int value", |
| 16 | + value: 42, |
| 17 | + expected: 42, |
| 18 | + ok: true, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "int64 value", |
| 22 | + value: int64(100), |
| 23 | + expected: 100, |
| 24 | + ok: true, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "uint64 value", |
| 28 | + value: uint64(200), |
| 29 | + expected: 200, |
| 30 | + ok: true, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "float64 value", |
| 34 | + value: float64(3.14), |
| 35 | + expected: 3, |
| 36 | + ok: true, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "string value (not supported)", |
| 40 | + value: "42", |
| 41 | + expected: 0, |
| 42 | + ok: false, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "nil value", |
| 46 | + value: nil, |
| 47 | + expected: 0, |
| 48 | + ok: false, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "bool value (not supported)", |
| 52 | + value: true, |
| 53 | + expected: 0, |
| 54 | + ok: false, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + for _, tt := range tests { |
| 59 | + t.Run(tt.name, func(t *testing.T) { |
| 60 | + result, ok := parseIntValue(tt.value) |
| 61 | + if ok != tt.ok { |
| 62 | + t.Errorf("parseIntValue() ok = %v, want %v", ok, tt.ok) |
| 63 | + } |
| 64 | + if result != tt.expected { |
| 65 | + t.Errorf("parseIntValue() result = %v, want %v", result, tt.expected) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestFilterMapKeys(t *testing.T) { |
| 72 | + tests := []struct { |
| 73 | + name string |
| 74 | + original map[string]any |
| 75 | + excludeKeys []string |
| 76 | + expected map[string]any |
| 77 | + }{ |
| 78 | + { |
| 79 | + name: "filter single key", |
| 80 | + original: map[string]any{ |
| 81 | + "key1": "value1", |
| 82 | + "key2": "value2", |
| 83 | + "key3": "value3", |
| 84 | + }, |
| 85 | + excludeKeys: []string{"key2"}, |
| 86 | + expected: map[string]any{ |
| 87 | + "key1": "value1", |
| 88 | + "key3": "value3", |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "filter multiple keys", |
| 93 | + original: map[string]any{ |
| 94 | + "key1": "value1", |
| 95 | + "key2": "value2", |
| 96 | + "key3": "value3", |
| 97 | + "key4": "value4", |
| 98 | + }, |
| 99 | + excludeKeys: []string{"key1", "key3"}, |
| 100 | + expected: map[string]any{ |
| 101 | + "key2": "value2", |
| 102 | + "key4": "value4", |
| 103 | + }, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "filter no keys", |
| 107 | + original: map[string]any{ |
| 108 | + "key1": "value1", |
| 109 | + "key2": "value2", |
| 110 | + }, |
| 111 | + excludeKeys: []string{}, |
| 112 | + expected: map[string]any{ |
| 113 | + "key1": "value1", |
| 114 | + "key2": "value2", |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "filter non-existent key", |
| 119 | + original: map[string]any{ |
| 120 | + "key1": "value1", |
| 121 | + "key2": "value2", |
| 122 | + }, |
| 123 | + excludeKeys: []string{"key3"}, |
| 124 | + expected: map[string]any{ |
| 125 | + "key1": "value1", |
| 126 | + "key2": "value2", |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "empty original map", |
| 131 | + original: map[string]any{}, |
| 132 | + excludeKeys: []string{"key1"}, |
| 133 | + expected: map[string]any{}, |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "filter all keys", |
| 137 | + original: map[string]any{ |
| 138 | + "key1": "value1", |
| 139 | + "key2": "value2", |
| 140 | + }, |
| 141 | + excludeKeys: []string{"key1", "key2"}, |
| 142 | + expected: map[string]any{}, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + for _, tt := range tests { |
| 147 | + t.Run(tt.name, func(t *testing.T) { |
| 148 | + result := filterMapKeys(tt.original, tt.excludeKeys...) |
| 149 | + |
| 150 | + // Check length |
| 151 | + if len(result) != len(tt.expected) { |
| 152 | + t.Errorf("filterMapKeys() length = %v, want %v", len(result), len(tt.expected)) |
| 153 | + } |
| 154 | + |
| 155 | + // Check each key-value pair |
| 156 | + for key, expectedValue := range tt.expected { |
| 157 | + resultValue, exists := result[key] |
| 158 | + if !exists { |
| 159 | + t.Errorf("filterMapKeys() missing key %v", key) |
| 160 | + } |
| 161 | + if resultValue != expectedValue { |
| 162 | + t.Errorf("filterMapKeys() value for key %v = %v, want %v", key, resultValue, expectedValue) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // Check for unexpected keys |
| 167 | + for key := range result { |
| 168 | + if _, exists := tt.expected[key]; !exists { |
| 169 | + t.Errorf("filterMapKeys() unexpected key %v", key) |
| 170 | + } |
| 171 | + } |
| 172 | + }) |
| 173 | + } |
| 174 | +} |
0 commit comments