-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_with_id_test.go
86 lines (82 loc) · 2 KB
/
data_with_id_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package record
import (
"github.com/dal-go/dalgo/dal"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNewDataWithID(t *testing.T) {
type data struct {
Title string
}
type args[K comparable] struct {
id K
key *dal.Key
data *data
}
type testCase[K comparable] struct {
name string
args args[K]
want DataWithID[K, *data]
expectsPanic string
}
d1 := data{Title: "test"}
tests := []testCase[string]{
{
name: "should_pass",
args: args[string]{
id: "r1",
key: dal.NewKeyWithID("r1", "SomeCollection"),
data: &data{Title: "test"},
},
want: DataWithID[string, *data]{
WithID: WithID[string]{
ID: "r1",
Key: dal.NewKeyWithID("r1", "SomeCollection"),
Record: dal.NewRecordWithData(dal.NewKeyWithID("r1", "SomeCollection"), &d1),
},
Data: &d1,
},
},
{
name: "should_panic_on_nil_key",
args: args[string]{
id: "r1",
key: nil,
data: &data{Title: "test"},
},
expectsPanic: "key is nil for (id=r1)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.expectsPanic != "" {
assert.PanicsWithValue(t, tt.expectsPanic, func() {
NewDataWithID(tt.args.id, tt.args.key, tt.args.data)
})
} else {
got := NewDataWithID(tt.args.id, tt.args.key, tt.args.data)
assert.Equal(t, tt.want.ID, got.ID)
assert.Equal(t, tt.want.Key, got.Key)
assert.Equal(t, tt.want.Data, got.Data)
got.Record.SetError(nil)
assert.Equal(t, tt.want.Data, got.Record.Data())
}
})
}
t.Run("should_panic_on_pointer_to_a_pointer", func(t *testing.T) {
assert.Panics(t, func() {
d1 := data{Title: "test"}
d2 := &d1
d3 := &d2
NewDataWithID("r1", dal.NewKeyWithID("r1", "SomeCollection"), d3)
})
})
t.Run("should_panic_on_pointer_to_an_interface", func(t *testing.T) {
assert.Panics(t, func() {
d1 := data{Title: "test"}
var d2 any = &d1
var d3 = &d2
NewDataWithID("r1", dal.NewKeyWithID("r1", "SomeCollection"), d3)
})
})
}