-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmemory_test.go
75 lines (57 loc) · 1.53 KB
/
memory_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
package gormtestutil
import (
"testing"
"github.com/stretchr/testify/assert"
"gorm.io/gorm/clause"
)
type A struct {
ID int
}
type B struct {
ID int
A *A `gorm:"foreignKey:AID;"`
AID int
}
func TestNewMemoryDatabase_ReturnsDatabase(t *testing.T) {
t.Parallel()
// Act
db := NewMemoryDatabase(t)
// Assert
assert.Equal(t, "sqlite", db.Name())
}
func TestNewMemoryDatabase_DisablesPragmaOnTrue(t *testing.T) {
t.Parallel()
// Act
db := NewMemoryDatabase(t, WithoutForeignKeys())
// Assert
_ = db.AutoMigrate(A{}, B{})
// This A does not exist, but pragma is off, so we should not have an error
c := []B{{ID: 1, AID: 23}, {ID: 2, AID: 24}, {ID: 3, AID: 25}}
assert.Nil(t, db.Omit(clause.Associations).Create(&c).Error)
}
func TestNewMemoryDatabase_EnablesPragmaByDefault(t *testing.T) {
t.Parallel()
// Act
db := NewMemoryDatabase(t)
// Assert
_ = db.AutoMigrate(A{}, B{})
// This A does not exist, so we expect an error
c := []B{{ID: 1, AID: 23}, {ID: 2, AID: 24}, {ID: 3, AID: 25}}
resultError := db.Omit(clause.Associations).Create(&c).Error
if assert.NotNil(t, resultError) {
assert.Equal(t, "FOREIGN KEY constraint failed", resultError.Error())
}
}
func TestNewMemoryDatabase_ReturnsNamedDatabase(t *testing.T) {
t.Parallel()
// Act
db := NewMemoryDatabase(t, WithName("abc"))
// Assert
_ = db.AutoMigrate(A{})
db.Create(&A{ID: 1})
// Get another connection and check the item
checkDb := NewMemoryDatabase(t, WithName("abc"))
var result *A
checkDb.First(&result)
assert.Equal(t, &A{ID: 1}, result)
}