-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_integration_test.go
More file actions
83 lines (66 loc) · 1.91 KB
/
insert_integration_test.go
File metadata and controls
83 lines (66 loc) · 1.91 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
// +build integrate
package postgres
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/neuronlabs/neuron-extensions/repository/postgres/internal"
"github.com/neuronlabs/neuron-extensions/repository/postgres/tests"
"github.com/neuronlabs/neuron/database"
"github.com/neuronlabs/neuron/errors"
"github.com/neuronlabs/neuron/query"
)
func TestInsertSingleModel(t *testing.T) {
c := testingController(t, true, testModels...)
p := testingRepository(c)
ctx := context.Background()
mStruct, err := c.ModelStruct(&tests.SimpleModel{})
require.NoError(t, err)
defer func() {
_ = internal.DropTables(ctx, p.ConnPool, mStruct.DatabaseName, mStruct.DatabaseSchemaName)
}()
// No results should return no error.
db := database.New(c)
newModel := func() *tests.SimpleModel {
return &tests.SimpleModel{
Attr: "Something",
}
}
// Insert two models.
t.Run("AutoFieldset", func(t *testing.T) {
model1 := newModel()
err = db.Query(mStruct, model1).Insert()
require.NoError(t, err)
assert.NotZero(t, model1.ID)
})
t.Run("BatchModels", func(t *testing.T) {
model1 := newModel()
model2 := newModel()
err = db.Query(mStruct, model1, model2).Insert()
require.NoError(t, err)
assert.NotZero(t, model1.ID)
assert.NotZero(t, model2.ID)
assert.NotEqual(t, model1.ID, model2.ID)
})
t.Run("WithFieldset", func(t *testing.T) {
model1 := newModel()
model1.Attr = "something"
err = db.Query(mStruct, model1).
Select(mStruct.MustFieldByName("Attr")).
Insert()
require.NoError(t, err)
assert.NotZero(t, model1.ID)
})
t.Run("WithID", func(t *testing.T) {
model1 := newModel()
model1.ID = 1e8
err = db.Query(mStruct, model1).Insert()
require.NoError(t, err)
assert.NotZero(t, model1.ID)
err = db.Query(mStruct, model1).Insert()
if assert.Error(t, err) {
assert.True(t, errors.Is(err, query.ErrViolationUnique))
}
})
}