|
| 1 | +package leveledlogger |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/sirupsen/logrus" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestLogger_fields(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + keysAndValues []interface{} |
| 14 | + expectedFields map[string]interface{} |
| 15 | + description string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "empty slice", |
| 19 | + keysAndValues: []interface{}{}, |
| 20 | + expectedFields: nil, |
| 21 | + description: "should return original entry when no keys and values provided", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "nil slice", |
| 25 | + keysAndValues: nil, |
| 26 | + expectedFields: nil, |
| 27 | + description: "should return original entry when nil keys and values provided", |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "single key-value pair", |
| 31 | + keysAndValues: []interface{}{"key1", "value1"}, |
| 32 | + expectedFields: map[string]interface{}{ |
| 33 | + "key1": "value1", |
| 34 | + }, |
| 35 | + description: "should correctly handle single key-value pair", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "multiple key-value pairs", |
| 39 | + keysAndValues: []interface{}{"key1", "value1", "key2", "value2", "key3", "value3"}, |
| 40 | + expectedFields: map[string]interface{}{ |
| 41 | + "key1": "value1", |
| 42 | + "key2": "value2", |
| 43 | + "key3": "value3", |
| 44 | + }, |
| 45 | + description: "should correctly handle multiple key-value pairs", |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "odd number of elements - trailing key", |
| 49 | + keysAndValues: []interface{}{"key1", "value1", "key2"}, |
| 50 | + expectedFields: map[string]interface{}{ |
| 51 | + "key1": "value1", |
| 52 | + "key2": "(MISSING)", |
| 53 | + }, |
| 54 | + description: "should add (MISSING) for trailing key without value", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "single trailing key", |
| 58 | + keysAndValues: []interface{}{"lonely_key"}, |
| 59 | + expectedFields: map[string]interface{}{ |
| 60 | + "lonely_key": "(MISSING)", |
| 61 | + }, |
| 62 | + description: "should handle single trailing key", |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "non-string key at start", |
| 66 | + keysAndValues: []interface{}{123, "value1", "key2", "value2"}, |
| 67 | + expectedFields: map[string]interface{}{ |
| 68 | + "key2": "value2", |
| 69 | + }, |
| 70 | + description: "should skip non-string keys", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "non-string key in middle", |
| 74 | + keysAndValues: []interface{}{"key1", "value1", 456, "value2", "key3", "value3"}, |
| 75 | + expectedFields: map[string]interface{}{ |
| 76 | + "key1": "value1", |
| 77 | + "key3": "value3", |
| 78 | + }, |
| 79 | + description: "should skip non-string keys in middle", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "all non-string keys", |
| 83 | + keysAndValues: []interface{}{123, "value1", 456, "value2"}, |
| 84 | + expectedFields: nil, |
| 85 | + description: "should return original entry when all keys are non-string", |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "non-string trailing key", |
| 89 | + keysAndValues: []interface{}{"key1", "value1", 789}, |
| 90 | + expectedFields: map[string]interface{}{ |
| 91 | + "key1": "value1", |
| 92 | + }, |
| 93 | + description: "should skip non-string trailing key", |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "various value types", |
| 97 | + keysAndValues: []interface{}{"string", "value", "int", 42, "bool", true, "float", 3.14, "nil", nil}, |
| 98 | + expectedFields: map[string]interface{}{ |
| 99 | + "string": "value", |
| 100 | + "int": 42, |
| 101 | + "bool": true, |
| 102 | + "float": 3.14, |
| 103 | + "nil": nil, |
| 104 | + }, |
| 105 | + description: "should handle various value types", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "empty string key", |
| 109 | + keysAndValues: []interface{}{"", "value1", "key2", "value2"}, |
| 110 | + expectedFields: map[string]interface{}{ |
| 111 | + "": "value1", |
| 112 | + "key2": "value2", |
| 113 | + }, |
| 114 | + description: "should accept empty string as key", |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "mixed valid and invalid keys", |
| 118 | + keysAndValues: []interface{}{"valid1", "value1", 123, "skipped", "valid2", "value2", true, "alsoSkipped", "valid3", "value3"}, |
| 119 | + expectedFields: map[string]interface{}{ |
| 120 | + "valid1": "value1", |
| 121 | + "valid2": "value2", |
| 122 | + "valid3": "value3", |
| 123 | + }, |
| 124 | + description: "should only process valid string keys", |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "duplicate keys - last wins", |
| 128 | + keysAndValues: []interface{}{"key", "first", "key", "second", "key", "third"}, |
| 129 | + expectedFields: map[string]interface{}{ |
| 130 | + "key": "third", |
| 131 | + }, |
| 132 | + description: "should use last value when key is duplicated", |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "struct value", |
| 136 | + keysAndValues: []interface{}{"struct", struct{ Name string }{"test"}}, |
| 137 | + expectedFields: map[string]interface{}{ |
| 138 | + "struct": struct{ Name string }{"test"}, |
| 139 | + }, |
| 140 | + description: "should handle struct values", |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "slice value", |
| 144 | + keysAndValues: []interface{}{"slice", []string{"a", "b", "c"}}, |
| 145 | + expectedFields: map[string]interface{}{ |
| 146 | + "slice": []string{"a", "b", "c"}, |
| 147 | + }, |
| 148 | + description: "should handle slice values", |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "map value", |
| 152 | + keysAndValues: []interface{}{"map", map[string]int{"a": 1, "b": 2}}, |
| 153 | + expectedFields: map[string]interface{}{ |
| 154 | + "map": map[string]int{"a": 1, "b": 2}, |
| 155 | + }, |
| 156 | + description: "should handle map values", |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + for _, tt := range tests { |
| 161 | + t.Run(tt.name, func(t *testing.T) { |
| 162 | + // Create base logger |
| 163 | + baseLogger := logrus.New() |
| 164 | + baseEntry := logrus.NewEntry(baseLogger) |
| 165 | + |
| 166 | + logger := &Logger{ |
| 167 | + Entry: baseEntry, |
| 168 | + } |
| 169 | + |
| 170 | + // Call fields method |
| 171 | + result := logger.fields(tt.keysAndValues) |
| 172 | + |
| 173 | + // If no fields expected, should return original entry |
| 174 | + if tt.expectedFields == nil { |
| 175 | + assert.Equal(t, baseEntry, result, tt.description) |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + // Check that returned entry has expected fields |
| 180 | + assert.NotEqual(t, baseEntry, result, "should return new entry with fields") |
| 181 | + |
| 182 | + // Verify all expected fields are present |
| 183 | + for key, expectedValue := range tt.expectedFields { |
| 184 | + actualValue, exists := result.Data[key] |
| 185 | + assert.True(t, exists, "field %s should exist", key) |
| 186 | + assert.Equal(t, expectedValue, actualValue, "field %s should have correct value", key) |
| 187 | + } |
| 188 | + |
| 189 | + // Verify no unexpected fields |
| 190 | + assert.Equal(t, len(tt.expectedFields), len(result.Data), "should have exact number of expected fields") |
| 191 | + }) |
| 192 | + } |
| 193 | +} |
0 commit comments