github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/lists/sum_test.go (about) 1 package lists_test 2 3 import ( 4 "testing" 5 6 "github.com/lmorg/murex/test/count" 7 "github.com/lmorg/murex/utils/json" 8 "github.com/lmorg/murex/utils/lists" 9 ) 10 11 func TestSumInt(t *testing.T) { 12 type testSumIntT struct { 13 Source map[string]int 14 Destination map[string]int 15 Output map[string]int 16 } 17 18 tests := []testSumIntT{ 19 { 20 Source: map[string]int{ 21 "a": 1, 22 "b": 2, 23 "c": 3, 24 }, 25 Destination: map[string]int{ 26 "a": 9, 27 "b": 7, 28 "c": 5, 29 }, 30 Output: map[string]int{ 31 "a": 10, 32 "b": 9, 33 "c": 8, 34 }, 35 }, 36 { 37 Source: map[string]int{ 38 "a": 1, 39 }, 40 Destination: map[string]int{}, 41 Output: map[string]int{ 42 "a": 1, 43 }, 44 }, 45 { 46 Source: map[string]int{}, 47 Destination: map[string]int{ 48 "c": 5, 49 }, 50 Output: map[string]int{ 51 "c": 5, 52 }, 53 }, 54 } 55 56 count.Tests(t, len(tests)) 57 58 for i, test := range tests { 59 jExp := json.LazyLogging(test.Output) 60 61 actual := make(map[string]int) 62 for k, v := range test.Destination { 63 actual[k] = v 64 } 65 lists.SumInt(actual, test.Source) 66 jAct := json.LazyLogging(actual) 67 68 if jExp != jAct { 69 t.Errorf("Test %d failed", i) 70 t.Logf(" Source: %s", json.LazyLogging(test.Source)) 71 t.Logf(" Destination: %s", json.LazyLogging(test.Destination)) 72 t.Logf(" Expected: %s", jExp) 73 t.Logf(" Actual: %s", jAct) 74 } 75 } 76 } 77 78 func TestSumFloat64(t *testing.T) { 79 type testSumFloat64T struct { 80 Source map[string]float64 81 Destination map[string]float64 82 Output map[string]float64 83 } 84 85 tests := []testSumFloat64T{ 86 { 87 Source: map[string]float64{ 88 "a": 1, 89 "b": 2, 90 "c": 3, 91 }, 92 Destination: map[string]float64{ 93 "a": 9, 94 "b": 7, 95 "c": 5, 96 }, 97 Output: map[string]float64{ 98 "a": 10, 99 "b": 9, 100 "c": 8, 101 }, 102 }, 103 { 104 Source: map[string]float64{ 105 "a": 1, 106 }, 107 Destination: map[string]float64{}, 108 Output: map[string]float64{ 109 "a": 1, 110 }, 111 }, 112 { 113 Source: map[string]float64{}, 114 Destination: map[string]float64{ 115 "c": 5, 116 }, 117 Output: map[string]float64{ 118 "c": 5, 119 }, 120 }, 121 { 122 Source: map[string]float64{ 123 "a": 1.1, 124 "b": 2.5, 125 "c": 3.9, 126 }, 127 Destination: map[string]float64{ 128 "a": 9.1, 129 "b": 7.5, 130 "c": 5.9, 131 }, 132 Output: map[string]float64{ 133 "a": 10.2, 134 "b": 10, 135 "c": 9.8, 136 }, 137 }, 138 } 139 140 count.Tests(t, len(tests)) 141 142 for i, test := range tests { 143 jExp := json.LazyLogging(test.Output) 144 145 actual := make(map[string]float64) 146 for k, v := range test.Destination { 147 actual[k] = v 148 } 149 lists.SumFloat64(actual, test.Source) 150 jAct := json.LazyLogging(actual) 151 152 if jExp != jAct { 153 t.Errorf("Test %d failed", i) 154 t.Logf(" Source: %s", json.LazyLogging(test.Source)) 155 t.Logf(" Destination: %s", json.LazyLogging(test.Destination)) 156 t.Logf(" Expected: %s", jExp) 157 t.Logf(" Actual: %s", jAct) 158 } 159 } 160 } 161 162 func TestSumInterface(t *testing.T) { 163 type testSumInterfaceT struct { 164 Source map[string]interface{} 165 Destination map[string]interface{} 166 Output map[string]interface{} 167 Error bool 168 } 169 170 tests := []testSumInterfaceT{ 171 { 172 Source: map[string]interface{}{ 173 "a": 1, 174 "b": 2, 175 "c": 3, 176 }, 177 Destination: map[string]interface{}{ 178 "a": 9, 179 "b": 7, 180 "c": 5, 181 }, 182 Output: map[string]interface{}{ 183 "a": 10, 184 "b": 9, 185 "c": 8, 186 }, 187 }, 188 { 189 Source: map[string]interface{}{ 190 "a": "1", 191 "b": "2", 192 "c": "3", 193 }, 194 Destination: map[string]interface{}{ 195 "a": "9", 196 "b": "7", 197 "c": "5", 198 }, 199 Output: map[string]interface{}{ 200 "a": 10, 201 "b": 9, 202 "c": 8, 203 }, 204 }, 205 { 206 Source: map[string]interface{}{ 207 "a": 1, 208 }, 209 Destination: map[string]interface{}{}, 210 Output: map[string]interface{}{ 211 "a": 1, 212 }, 213 }, 214 { 215 Source: map[string]interface{}{}, 216 Destination: map[string]interface{}{ 217 "c": 5, 218 }, 219 Output: map[string]interface{}{ 220 "c": 5, 221 }, 222 }, 223 { 224 Source: map[string]interface{}{ 225 "a": "1", 226 }, 227 Destination: map[string]interface{}{ 228 "c": 5, 229 }, 230 Output: map[string]interface{}{ 231 "a": 1, 232 "c": 5, 233 }, 234 }, 235 { 236 Source: map[string]interface{}{ 237 "a": 1, 238 }, 239 Destination: map[string]interface{}{ 240 "c": "5", 241 }, 242 Output: map[string]interface{}{ 243 "a": 1, 244 "c": "5", 245 }, 246 }, 247 { 248 Source: map[string]interface{}{ 249 "a": 1.1, 250 "b": 2.5, 251 "c": 3.9, 252 }, 253 Destination: map[string]interface{}{ 254 "a": 9.1, 255 "b": 7.5, 256 "c": 5.9, 257 }, 258 Output: map[string]interface{}{ 259 "a": 10.2, 260 "b": 10, 261 "c": 9.8, 262 }, 263 }, 264 { 265 Source: map[string]interface{}{ 266 "a": 1.1, 267 "b": "2.5", 268 "c": 3.9, 269 }, 270 Destination: map[string]interface{}{ 271 "a": "9.1", 272 "b": 7.5, 273 "c": "5.9", 274 }, 275 Output: map[string]interface{}{ 276 "a": 10.2, 277 "b": 10, 278 "c": 9.8, 279 }, 280 }, 281 { 282 Source: map[string]interface{}{ 283 "a": 1.1, 284 "b": "2.5", 285 "c": 3.9, 286 "foo": "bar", 287 }, 288 Destination: map[string]interface{}{ 289 "a": "9.1", 290 "b": 7.5, 291 "c": "5.9", 292 "bar": "foo", 293 }, 294 Output: map[string]interface{}{ 295 "a": 10.2, 296 "b": 10, 297 "c": 9.8, 298 "bar": "foo", 299 }, 300 Error: true, 301 }, 302 } 303 304 count.Tests(t, len(tests)) 305 306 for i, test := range tests { 307 jExp := json.LazyLogging(test.Output) 308 309 actual := make(map[string]interface{}) 310 for k, v := range test.Destination { 311 actual[k] = v 312 } 313 err := lists.SumInterface(actual, test.Source) 314 jAct := json.LazyLogging(actual) 315 316 var errStr string 317 318 if jExp != jAct && !test.Error { 319 errStr += ". Expected != Actual" 320 } 321 322 if (err == nil) == test.Error { 323 errStr += ". Error mismatch" 324 } 325 326 if errStr != "" { 327 t.Errorf("Test %d failed%s", i, errStr) 328 t.Logf(" Source: %s", json.LazyLogging(test.Source)) 329 t.Logf(" Destination: %s", json.LazyLogging(test.Destination)) 330 t.Logf(" Expected: %s", jExp) 331 t.Logf(" Actual: %s", jAct) 332 t.Logf(" err exp: %v", test.Error) 333 t.Logf(" err message: %s", err) 334 } 335 } 336 }