-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrator_test.go
90 lines (71 loc) · 2.25 KB
/
migrator_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
87
88
89
90
package duckdb_test
import (
"testing"
"time"
"github.com/alifiroozi80/duckdb"
_ "github.com/marcboeker/go-duckdb"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)
// Define test structs
type User struct {
ID uint `gorm:"primaryKey"`
Name string
Email string `gorm:"unique"`
}
type Product struct {
ID uint `gorm:"primaryKey"`
Name string
Price float64
}
type Post struct {
ID uint `gorm:"primaryKey"`
Content string
CreatedAt time.Time
}
// TestMigratorBasicSchema verifies basic schema creation.
func TestMigratorBasicSchema(t *testing.T) {
db, err := gorm.Open(duckdb.Open("test.db"), &gorm.Config{})
assert.NoError(t, err)
// Migrate User table
err = db.AutoMigrate(&Product{})
assert.NoError(t, err)
// Check if table exists
assert.True(t, db.Migrator().HasTable(&Product{}))
assert.True(t, db.Migrator().HasColumn(&Product{}, "Price"))
}
// TestMigratorDropTable verifies dropping a table.
func TestMigratorDropTable(t *testing.T) {
db, err := gorm.Open(duckdb.Open("test.db"), &gorm.Config{})
assert.NoError(t, err)
db.AutoMigrate(&User{})
assert.True(t, db.Migrator().HasTable(&User{}))
// Drop table and verify
db.Migrator().DropTable(&User{})
assert.False(t, db.Migrator().HasTable(&User{}))
}
// TestUniqueConstraint tests that unique constraints are enforced.
func TestUniqueConstraint(t *testing.T) {
db, err := gorm.Open(duckdb.Open("test.db"), &gorm.Config{})
assert.NoError(t, err)
db.AutoMigrate(&User{})
assert.True(t, db.Migrator().HasColumn(&User{}, "Email"))
// Create first user
user1 := User{Name: "User1", Email: "[email protected]"}
db.Create(&user1)
// Attempt to create a second user with the same email
user2 := User{Name: "User2", Email: "[email protected]"}
result := db.Create(&user2)
assert.Error(t, result.Error, "Expected unique constraint violation")
}
// TestDefaultValues verifies that default values are set correctly.
func TestDefaultValues(t *testing.T) {
db, err := gorm.Open(duckdb.Open("test.db"), &gorm.Config{})
assert.NoError(t, err)
db.AutoMigrate(&Post{})
// Insert a new post without specifying CreatedAt
post := Post{Content: "Hello, World!"}
db.Create(&post)
// Verify CreatedAt has a value (defaulted to the current timestamp)
assert.NotZero(t, post.CreatedAt)
}