|
| 1 | +package structs |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +type Test struct { |
| 10 | + Name string `updateable:"name"` |
| 11 | + Age int `updateable:"age"` |
| 12 | + IsAdult bool `updateable:"is_adult"` |
| 13 | +} |
| 14 | + |
| 15 | +type ComplexTest struct { |
| 16 | + Data Test `updateable:"data"` |
| 17 | + UpdatedOn time.Time `updateable:"updated_on"` |
| 18 | + History []string `updateable:"history"` |
| 19 | +} |
| 20 | + |
| 21 | +func TestCompareStructs(t *testing.T) { |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + old interface{} |
| 25 | + new interface{} |
| 26 | + want []Result |
| 27 | + wantErr bool |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "success - compare two structs", |
| 31 | + old: Test{Name: "example", Age: 10, IsAdult: false}, |
| 32 | + new: Test{Name: "example - updated", Age: 18, IsAdult: true}, |
| 33 | + want: []Result{ |
| 34 | + { |
| 35 | + FieldName: "name", |
| 36 | + OldValue: "example", |
| 37 | + NewValue: "example - updated", |
| 38 | + }, |
| 39 | + { |
| 40 | + FieldName: "age", |
| 41 | + OldValue: 10, |
| 42 | + NewValue: 18, |
| 43 | + }, |
| 44 | + { |
| 45 | + FieldName: "is_adult", |
| 46 | + OldValue: false, |
| 47 | + NewValue: true, |
| 48 | + }, |
| 49 | + }, |
| 50 | + wantErr: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "success - compare two complex structs", |
| 54 | + old: ComplexTest{Data: Test{Name: "example1", Age: 25, IsAdult: true}, UpdatedOn: time.Date(2024, 10, 22, 00, 00, 00, 00, time.UTC), History: []string{"user1", "user2"}}, |
| 55 | + new: ComplexTest{Data: Test{Name: "example1", Age: 26, IsAdult: true}, UpdatedOn: time.Date(2024, 10, 22, 00, 01, 00, 00, time.UTC), History: []string{"user1", "user2", "user3"}}, |
| 56 | + want: []Result{ |
| 57 | + { |
| 58 | + FieldName: "data", |
| 59 | + OldValue: Test{Name: "example1", Age: 25, IsAdult: true}, |
| 60 | + NewValue: Test{Name: "example1", Age: 26, IsAdult: true}, |
| 61 | + }, |
| 62 | + { |
| 63 | + FieldName: "updated_on", |
| 64 | + OldValue: time.Date(2024, 10, 22, 00, 00, 00, 00, time.UTC), |
| 65 | + NewValue: time.Date(2024, 10, 22, 00, 01, 00, 00, time.UTC), |
| 66 | + }, |
| 67 | + { |
| 68 | + FieldName: "history", |
| 69 | + OldValue: []string{"user1", "user2"}, |
| 70 | + NewValue: []string{"user1", "user2", "user3"}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + wantErr: false, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "fail - non struct parameters", |
| 77 | + old: map[string]string{"test": "example"}, |
| 78 | + new: map[string]string{"test": "example-updated"}, |
| 79 | + want: nil, |
| 80 | + wantErr: true, |
| 81 | + }, |
| 82 | + } |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + got, err := CompareStructs(tt.old, tt.new) |
| 86 | + if (err != nil) != tt.wantErr { |
| 87 | + t.Errorf("CompareStructs() error = %v, wantErr %v", err, tt.wantErr) |
| 88 | + return |
| 89 | + } |
| 90 | + if !reflect.DeepEqual(got, tt.want) { |
| 91 | + t.Errorf("CompareStructs() = %v, want %v", got, tt.want) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
0 commit comments