Skip to content

Commit 0a842ba

Browse files
Add tests for Insert query (#14)
* Add tests for Insert query * Fix test issues for TestInsertScenarios * Remove hard coded DB connection values * Refactor based on comments * Add Negative tests for insert query * Add Negative tests for insert query
1 parent e9ce36a commit 0a842ba

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

tests/main_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ package tests
4141
import (
4242
"fmt"
4343
"testing"
44+
"time"
45+
"strings"
4446

4547
. "github.com/oracle-samples/gorm-oracle/tests/utils"
4648
)
@@ -90,3 +92,65 @@ func TestSetAndGet(t *testing.T) {
9092
t.Errorf("Get non existing key should return error")
9193
}
9294
}
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+
}

tests/passed-tests.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,5 @@ BenchmarkDelete
382382
TestRawQueryInjection
383383
TestWhereClauseInjection
384384
TestUpdateInjection
385-
TestFirstOrCreateInjection
385+
TestFirstOrCreateInjection
386+
TestUserInsertScenarios

0 commit comments

Comments
 (0)