-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_test.go
More file actions
118 lines (95 loc) · 3.01 KB
/
Copy pathsession_test.go
File metadata and controls
118 lines (95 loc) · 3.01 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package sqlok
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type TestUserBase struct {
Id int `sqlok:"pk"`
}
type TestUser struct {
TestUserBase
Name string
}
type TestPointerUser struct {
Id *int `sqlok:"pk"`
Name string
}
type TestCompositeUser struct {
OrgId int `sqlok:"pk"`
UserId int `sqlok:"pk"`
Name string
}
func TestSession_Add(t *testing.T) {
s := NewSession(nil)
t.Run("Should add new object without PK to pending", func(t *testing.T) {
user := &TestUser{Name: "New User"}
err := s.Add(user)
assert.NoError(t, err)
assert.Contains(t, s.pending, user)
assert.Empty(t, s.identityMap)
})
t.Run("Should add object with PK to identity map", func(t *testing.T) {
user := &TestUser{TestUserBase: TestUserBase{Id: 1}, Name: "Existing User"}
err := s.Add(user)
assert.NoError(t, err)
reflectType := reflect.TypeFor[TestUser]()
assert.NotNil(t, s.identityMap[reflectType])
assert.Equal(t, user, s.identityMap[reflectType][1])
})
t.Run("Should fail on identity conflict", func(t *testing.T) {
s = NewSession(nil)
user1 := &TestUser{TestUserBase: TestUserBase{Id: 10}, Name: "User 1"}
user2 := &TestUser{TestUserBase: TestUserBase{Id: 10}, Name: "User 2"}
err := s.Add(user1)
assert.NoError(t, err)
err = s.Add(user2)
assert.ErrorIs(t, err, ErrIdentityConflict)
})
t.Run("Should handle pointer PK correctly", func(t *testing.T) {
s = NewSession(nil)
id0 := 0
user0 := &TestPointerUser{Id: &id0, Name: "User with ID 0"}
err := s.Add(user0)
assert.NoError(t, err)
reflectType := reflect.TypeFor[TestPointerUser]()
assert.Equal(t, user0, s.identityMap[reflectType][0])
userNil := &TestPointerUser{Id: nil, Name: "User with Nil ID"}
err = s.Add(userNil)
assert.NoError(t, err)
assert.Contains(t, s.pending, userNil)
})
t.Run("Should handle composite PK correctly", func(t *testing.T) {
s = NewSession(nil)
user := &TestCompositeUser{OrgId: 1, UserId: 100, Name: "Joint User"}
err := s.Add(user)
assert.NoError(t, err)
reflectType := reflect.TypeOf(TestCompositeUser{})
compositeKey := "composite:1|100"
assert.Equal(t, user, s.identityMap[reflectType][compositeKey])
})
}
func TestSession_Load(t *testing.T) {
s := NewSession(nil)
user := &TestUser{TestUserBase: TestUserBase{Id: 50}, Name: "Database User"}
s.Add(user)
t.Run("Should load existing object from identity map", func(t *testing.T) {
loaded, err := Load[TestUser](s, 50)
assert.NoError(t, err)
assert.NotNil(t, loaded)
assert.Equal(t, user, loaded)
assert.Equal(t, "Database User", loaded.Name)
})
t.Run("Should load existing composite object from identity map", func(t *testing.T) {
comp := &TestCompositeUser{OrgId: 1, UserId: 200, Name: "Comp User"}
s.Add(comp)
loaded, err := Load[TestCompositeUser](s, "composite:1|200")
assert.NoError(t, err)
assert.Equal(t, comp, loaded)
})
t.Run("Should return nil when object not in session", func(t *testing.T) {
loaded, err := Load[TestUser](s, 999)
assert.NoError(t, err)
assert.Nil(t, loaded)
})
}