forked from go-xorm/xorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession_get_test.go
91 lines (77 loc) · 2.66 KB
/
session_get_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
91
// Copyright 2017 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xorm
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestGetVar(t *testing.T) {
assert.NoError(t, prepareEngine())
type GetVar struct {
Id int64 `xorm:"autoincr pk"`
Msg string `xorm:"varchar(255)"`
Age int
Money float32
Created time.Time `xorm:"created"`
}
assert.NoError(t, testEngine.Sync2(new(GetVar)))
var data = GetVar{
Msg: "hi",
Age: 28,
Money: 1.5,
}
_, err := testEngine.InsertOne(data)
assert.NoError(t, err)
var msg string
has, err := testEngine.Table("get_var").Cols("msg").Get(&msg)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, "hi", msg)
var age int
has, err = testEngine.Table("get_var").Cols("age").Get(&age)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, 28, age)
var money float64
has, err = testEngine.Table("get_var").Cols("money").Get(&money)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, "1.5", fmt.Sprintf("%.1f", money))
var valuesString = make(map[string]string)
has, err = testEngine.Table("get_var").Get(&valuesString)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, 5, len(valuesString))
assert.Equal(t, "1", valuesString["id"])
assert.Equal(t, "hi", valuesString["msg"])
assert.Equal(t, "28", valuesString["age"])
assert.Equal(t, "1.5", valuesString["money"])
var valuesInter = make(map[string]interface{})
has, err = testEngine.Table("get_var").Where("id = ?", 1).Select("*").Get(&valuesInter)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, 5, len(valuesInter))
assert.EqualValues(t, 1, valuesInter["id"])
assert.Equal(t, "hi", fmt.Sprintf("%s", valuesInter["msg"]))
assert.EqualValues(t, 28, valuesInter["age"])
assert.Equal(t, "1.5", fmt.Sprintf("%v", valuesInter["money"]))
var valuesSliceString = make([]string, 5)
has, err = testEngine.Table("get_var").Get(&valuesSliceString)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.Equal(t, "1", valuesSliceString[0])
assert.Equal(t, "hi", valuesSliceString[1])
assert.Equal(t, "28", valuesSliceString[2])
assert.Equal(t, "1.5", valuesSliceString[3])
var valuesSliceInter = make([]interface{}, 5)
has, err = testEngine.Table("get_var").Get(&valuesSliceInter)
assert.NoError(t, err)
assert.Equal(t, true, has)
assert.EqualValues(t, 1, valuesSliceInter[0])
assert.Equal(t, "hi", fmt.Sprintf("%s", valuesSliceInter[1]))
assert.EqualValues(t, 28, valuesSliceInter[2])
assert.Equal(t, "1.5", fmt.Sprintf("%v", valuesSliceInter[3]))
}