|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestParseUpdateEntityBoolField(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + configMap map[string]any |
| 11 | + fieldName string |
| 12 | + mode FieldParsingMode |
| 13 | + wantNil bool |
| 14 | + wantValue bool // Only checked if wantNil is false |
| 15 | + }{ |
| 16 | + // FieldParsingKeyExistence mode tests |
| 17 | + { |
| 18 | + name: "key existence mode: key present with nil value", |
| 19 | + configMap: map[string]any{"title": nil}, |
| 20 | + fieldName: "title", |
| 21 | + mode: FieldParsingKeyExistence, |
| 22 | + wantNil: false, // Should return non-nil pointer |
| 23 | + wantValue: false, // Default bool value |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "key existence mode: key present with empty value", |
| 27 | + configMap: map[string]any{"title": ""}, |
| 28 | + fieldName: "title", |
| 29 | + mode: FieldParsingKeyExistence, |
| 30 | + wantNil: false, |
| 31 | + wantValue: false, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "key existence mode: key not present", |
| 35 | + configMap: map[string]any{"other": true}, |
| 36 | + fieldName: "title", |
| 37 | + mode: FieldParsingKeyExistence, |
| 38 | + wantNil: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "key existence mode: nil config map", |
| 42 | + configMap: nil, |
| 43 | + fieldName: "title", |
| 44 | + mode: FieldParsingKeyExistence, |
| 45 | + wantNil: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "key existence mode: empty config map", |
| 49 | + configMap: map[string]any{}, |
| 50 | + fieldName: "title", |
| 51 | + mode: FieldParsingKeyExistence, |
| 52 | + wantNil: true, |
| 53 | + }, |
| 54 | + |
| 55 | + // FieldParsingBoolValue mode tests |
| 56 | + { |
| 57 | + name: "bool value mode: true value", |
| 58 | + configMap: map[string]any{"title": true}, |
| 59 | + fieldName: "title", |
| 60 | + mode: FieldParsingBoolValue, |
| 61 | + wantNil: false, |
| 62 | + wantValue: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "bool value mode: false value", |
| 66 | + configMap: map[string]any{"title": false}, |
| 67 | + fieldName: "title", |
| 68 | + mode: FieldParsingBoolValue, |
| 69 | + wantNil: false, |
| 70 | + wantValue: false, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "bool value mode: nil value (not a bool)", |
| 74 | + configMap: map[string]any{"title": nil}, |
| 75 | + fieldName: "title", |
| 76 | + mode: FieldParsingBoolValue, |
| 77 | + wantNil: true, // Non-bool values return nil |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "bool value mode: string value (not a bool)", |
| 81 | + configMap: map[string]any{"title": "true"}, |
| 82 | + fieldName: "title", |
| 83 | + mode: FieldParsingBoolValue, |
| 84 | + wantNil: true, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "bool value mode: key not present", |
| 88 | + configMap: map[string]any{"other": true}, |
| 89 | + fieldName: "title", |
| 90 | + mode: FieldParsingBoolValue, |
| 91 | + wantNil: true, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "bool value mode: nil config map", |
| 95 | + configMap: nil, |
| 96 | + fieldName: "title", |
| 97 | + mode: FieldParsingBoolValue, |
| 98 | + wantNil: true, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + for _, tt := range tests { |
| 103 | + t.Run(tt.name, func(t *testing.T) { |
| 104 | + result := parseUpdateEntityBoolField(tt.configMap, tt.fieldName, tt.mode) |
| 105 | + |
| 106 | + if tt.wantNil { |
| 107 | + if result != nil { |
| 108 | + t.Errorf("Expected nil result, got %v", result) |
| 109 | + } |
| 110 | + } else { |
| 111 | + if result == nil { |
| 112 | + t.Errorf("Expected non-nil result, got nil") |
| 113 | + } else if *result != tt.wantValue { |
| 114 | + t.Errorf("Expected value %v, got %v", tt.wantValue, *result) |
| 115 | + } |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestParseUpdateEntityBoolFieldFieldNames(t *testing.T) { |
| 122 | + // Test that different field names work correctly |
| 123 | + configMap := map[string]any{ |
| 124 | + "title": nil, |
| 125 | + "body": nil, |
| 126 | + "status": nil, |
| 127 | + "labels": nil, |
| 128 | + } |
| 129 | + |
| 130 | + fieldNames := []string{"title", "body", "status", "labels"} |
| 131 | + for _, fieldName := range fieldNames { |
| 132 | + t.Run(fieldName, func(t *testing.T) { |
| 133 | + result := parseUpdateEntityBoolField(configMap, fieldName, FieldParsingKeyExistence) |
| 134 | + if result == nil { |
| 135 | + t.Errorf("Expected non-nil result for field %s", fieldName) |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
0 commit comments