-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_test.go
More file actions
107 lines (98 loc) · 3.15 KB
/
executor_test.go
File metadata and controls
107 lines (98 loc) · 3.15 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package ormshift_test
import (
"database/sql"
"testing"
"time"
"github.com/ordershift/ormshift"
"github.com/ordershift/ormshift/dialects/sqlite"
"github.com/ordershift/ormshift/internal/testutils"
"github.com/ordershift/ormshift/migrations"
)
type userRow struct {
ID sql.NullInt64
Name sql.NullString
Email sql.NullString
UpdatedAt sql.NullTime
Active sql.NullByte
Ammount sql.NullFloat64
Percent sql.NullFloat64
Photo *[]byte
}
func TestExecutor(t *testing.T) {
db, err := ormshift.OpenDatabase(sqlite.Driver(), ormshift.ConnectionParams{InMemory: true})
if !testutils.AssertNotNilResultAndNilError(t, db, err, "ormshift.OpenDatabase") {
return
}
defer func() { _ = db.Close() }()
migrationManager, err := migrations.Migrate(
db,
migrations.NewMigratorConfig(),
testutils.M001_Create_Table_User{},
testutils.M002_Alter_Table_User_Add_Column_UpdatedAt{},
)
if !testutils.AssertNotNilResultAndNilError(t, migrationManager, err, "migrations.Migrate") {
return
}
sqlExecutor := db.SQLExecutor()
sqlBuilder := db.SQLBuilder()
//INSERT
values := ormshift.ColumnsValues{
"name": "Jonh Doe",
"email": "jonh.doe@mail.com",
"updated_at": time.Date(2026, time.January, 9, 12, 15, 52, 100952002, time.UTC),
"active": true,
"ammount": 5000.00,
"percent": 25.54325,
}
sql, args := sqlBuilder.InsertWithValues("user", values)
result, err := sqlExecutor.Exec(sql, args...)
if !testutils.AssertNilError(t, err, "sqlExecutor.Exec") {
return
}
r, err := result.RowsAffected()
if !testutils.AssertNilError(t, err, "RowsAffected") {
return
}
testutils.AssertEqualWithLabel(t, 1, r, "RowsAffected")
i, err := result.LastInsertId()
if !testutils.AssertNilError(t, err, "LastInsertId") {
return
}
//SELECT
sql, args = sqlBuilder.SelectWithValues(
"user",
[]string{"id", "name", "email", "updated_at", "active", "ammount", "percent", "photo"},
ormshift.ColumnsValues{"id": i},
)
userRows, err := sqlExecutor.Query(sql, args...)
if !testutils.AssertNotNilResultAndNilError(t, userRows, err, "sqlExecutor.Query") {
return
}
defer func() { _ = userRows.Close() }()
if !testutils.AssertEqualWithLabel(t, true, userRows.Next(), "Next") {
return
}
//SCAN
var userRow userRow
err = userRows.Scan(
&userRow.ID,
&userRow.Name,
&userRow.Email,
&userRow.UpdatedAt,
&userRow.Active,
&userRow.Ammount,
&userRow.Percent,
&userRow.Photo,
)
if !testutils.AssertNilError(t, err, "Scan") {
return
}
testutils.AssertEqualWithLabel(t, i, userRow.ID.Int64, "user.id")
testutils.AssertEqualWithLabel(t, "Jonh Doe", userRow.Name.String, "user.name")
testutils.AssertEqualWithLabel(t, "jonh.doe@mail.com", userRow.Email.String, "user.email")
testutils.AssertEqualWithLabel(t, time.Date(2026, time.January, 9, 12, 15, 52, 100952002, time.UTC), userRow.UpdatedAt.Time, "user.updated_at")
testutils.AssertEqualWithLabel(t, 1, userRow.Active.Byte, "user.active")
testutils.AssertEqualWithLabel(t, 5000.00, userRow.Ammount.Float64, "user.ammount")
testutils.AssertEqualWithLabel(t, 25.54325, userRow.Percent.Float64, "user.percent")
testutils.AssertEqualWithLabel(t, nil, userRow.Photo, "user.photo")
}