@@ -41,6 +41,8 @@ package tests
41
41
import (
42
42
"fmt"
43
43
"testing"
44
+ "time"
45
+ "strings"
44
46
45
47
. "github.com/oracle-samples/gorm-oracle/tests/utils"
46
48
)
@@ -90,3 +92,65 @@ func TestSetAndGet(t *testing.T) {
90
92
t .Errorf ("Get non existing key should return error" )
91
93
}
92
94
}
95
+
96
+ func TestUserInsertScenarios (t * testing.T ) {
97
+ type UserWithAge struct {
98
+ ID uint `gorm:"column:ID;primaryKey"`
99
+ Name string `gorm:"column:NAME;not null;size:100"`
100
+ Age int `gorm:"column:AGE"`
101
+ }
102
+
103
+ if err := DB .AutoMigrate (& UserWithAge {}); err != nil {
104
+ t .Fatalf ("Failed to migrate table: %v" , err )
105
+ }
106
+
107
+ user1 := UserWithAge {Name : "Alice" , Age : 30 }
108
+ if err := DB .Create (& user1 ).Error ; err != nil {
109
+ t .Errorf ("Basic insert failed: %v" , err )
110
+ }
111
+
112
+ user2 := UserWithAge {Name : "Bob" }
113
+ if err := DB .Create (& user2 ).Error ; err != nil {
114
+ t .Errorf ("Insert with NULL failed: %v" , err )
115
+ }
116
+
117
+ user3 := UserWithAge {Name : "O'Reilly" , Age : 45 }
118
+ if err := DB .Create (& user3 ).Error ; err != nil {
119
+ t .Errorf ("Insert with special characters failed: %v" , err )
120
+ }
121
+
122
+ type UserWithTime struct {
123
+ ID uint `gorm:"column:ID;primaryKey"`
124
+ Name string `gorm:"column:NAME;not null;size:100"`
125
+ CreatedAt time.Time `gorm:"column:CREATED_AT"`
126
+ }
127
+
128
+ if err := DB .AutoMigrate (& UserWithTime {}); err != nil {
129
+ t .Fatalf ("Failed to migrate UserWithTime table: %v" , err )
130
+ }
131
+
132
+ user4 := UserWithTime {Name : "Charlie" }
133
+ if err := DB .Create (& user4 ).Error ; err != nil {
134
+ t .Errorf ("Insert with default timestamp failed: %v" , err )
135
+ }
136
+
137
+ invalidUser1 := UserWithAge {Age : 50 }
138
+ if err := DB .Create (& invalidUser1 ).Error ; err == nil {
139
+ t .Errorf ("Expected NOT NULL constraint failure, got no error" )
140
+ }
141
+
142
+ invalidUser2 := UserWithAge {ID : user1 .ID , Name : "Duplicate" , Age : 40 }
143
+ if err := DB .Create (& invalidUser2 ).Error ; err == nil {
144
+ t .Errorf ("Expected duplicate primary key error, got no error" )
145
+ }
146
+
147
+ invalidUser3 := UserWithAge {Name : "InvalidAge" , Age : - 10 }
148
+ if err := DB .Create (& invalidUser3 ).Error ; err != nil {
149
+ t .Logf ("Insert with negative age failed as expected: %v" , err )
150
+ }
151
+
152
+ invalidUser4 := UserWithAge {Name : strings .Repeat ("A" , 300 ), Age : 20 }
153
+ if err := DB .Create (& invalidUser4 ).Error ; err == nil {
154
+ t .Errorf ("Expected value too large error for oversized string, got no error" )
155
+ }
156
+ }
0 commit comments