|
5 | 5 | "testing" |
6 | 6 |
|
7 | 7 | "github.com/go-rel/changeset/params" |
| 8 | + "github.com/go-rel/rel" |
8 | 9 | "github.com/stretchr/testify/assert" |
9 | 10 | ) |
10 | 11 |
|
@@ -40,26 +41,59 @@ func TestPutChange(t *testing.T) { |
40 | 41 | assert.Equal(t, 10, ch.Changes()["field1"]) |
41 | 42 | } |
42 | 43 |
|
| 44 | +func TestPutChange_ptr(t *testing.T) { |
| 45 | + var ( |
| 46 | + nullable = false |
| 47 | + a struct { |
| 48 | + ID int |
| 49 | + Nullable *bool |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + ch := Cast(a, params.Map{}, []string{}) |
| 54 | + PutChange(ch, "nullable", &nullable) |
| 55 | + |
| 56 | + assert.Nil(t, ch.Error()) |
| 57 | + assert.Equal(t, &nullable, ch.Changes()["nullable"]) |
| 58 | + |
| 59 | + assert.NotPanics(t, func() { |
| 60 | + rel.Apply(rel.NewDocument(&a), ch) |
| 61 | + assert.Equal(t, &nullable, a.Nullable) |
| 62 | + }) |
| 63 | +} |
| 64 | + |
43 | 65 | func TestPutChange_nil(t *testing.T) { |
44 | 66 | var a struct { |
| 67 | + ID int |
45 | 68 | Nullable *bool |
46 | 69 | } |
47 | 70 |
|
48 | 71 | ch := Cast(a, params.Map{}, []string{}) |
49 | 72 | PutChange(ch, "nullable", nil) |
50 | 73 |
|
51 | 74 | assert.Nil(t, ch.Error()) |
52 | | - assert.Equal(t, (*bool)(nil), ch.Changes()["nullable"]) |
| 75 | + assert.Equal(t, nil, ch.Changes()["nullable"]) |
| 76 | + |
| 77 | + assert.NotPanics(t, func() { |
| 78 | + rel.Apply(rel.NewDocument(&a), ch) |
| 79 | + assert.Nil(t, a.Nullable) |
| 80 | + }) |
53 | 81 | } |
54 | 82 |
|
55 | 83 | func TestPutChange_typedNil(t *testing.T) { |
56 | 84 | var a struct { |
| 85 | + ID int |
57 | 86 | Nullable *bool |
58 | 87 | } |
59 | 88 |
|
60 | 89 | ch := Cast(a, params.Map{}, []string{}) |
61 | 90 | PutChange(ch, "nullable", (*bool)(nil)) |
62 | 91 |
|
63 | 92 | assert.Nil(t, ch.Error()) |
64 | | - assert.Equal(t, (*bool)(nil), ch.Changes()["nullable"]) |
| 93 | + assert.Equal(t, nil, ch.Changes()["nullable"]) |
| 94 | + |
| 95 | + assert.NotPanics(t, func() { |
| 96 | + rel.Apply(rel.NewDocument(&a), ch) |
| 97 | + assert.Nil(t, a.Nullable) |
| 98 | + }) |
65 | 99 | } |
0 commit comments