xorm/session_query_test.go
Lunny Xiao 9251a5165d
All checks were successful
continuous-integration/drone/push Build is passing
Add test for join subquery (#1528)
Fix test

Fix subquery with schema

Add test for join subquery

Add makefile (#1531)

Fix drone

Fix ci

Add deps

Improve drone

Fix envs

Add makefile

Reviewed-on: #1531

Add password for postgres drone image (#1530)

Add password for postgres drone image

Reviewed-on: #1530

format time when sqlTypeName is core.Varchar (#1026)

fix time test

add test for time format

sign codes according to contributing rules.

format time when sqlTypeName is core.Varchar. Same with core.DateTime or core.TimeStamp

Add test for second insert error (#1527)

Add test for second insert error

Reviewed-on: #1527

Add tests for table name (#1517)

add tests for table name

Fix test (#1526)

Fix test

Reviewed-on: #1526

Fix test (#1526)

Fix test

Reviewed-on: #1526

Fix wrong warning log on autoincrement column when sync table (#1525)

improve doc

Fix wrong warning log on autoincrement column when sync table

Reviewed-on: #1525

Fixed Join strings on func Exist (#1520)

fix test

fixed Join strings on func Exist

Co-authored-by: Tomofumi Kusana <tkusana@morisawa.co.jp>
Reviewed-on: #1520

For nullable columns, store nil values as NULL (#531)

Merge branch 'master' into jcsalem/fix/nil_ptr_is_nullable

fix bug when buffersize with iterate (#941)

Merge branch 'master' into lunny/fix_buffer_iterate

Exclude schema from index name (#1505)

Merge branch 'master' into fix-schema-idx

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: #1499

fix vet

fix drone lint

remove go1.10 test on drone

Exclude schema from the index name

Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: #1505

fix test

fix bug

fix bug when buffersize with iterate

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: #1499

fix vet

fix drone lint

remove go1.10 test on drone

Fix update with Alias (#1455)

Co-authored-by: Guillermo Prandi <guillep2k@noreply.gitea.io>
Reviewed-on: #941

fix update map with version (#1448)

fix test

fix update map with version

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: #1499

fix vet

fix drone lint

remove go1.10 test on drone

Fix update with Alias (#1455)

Reviewed-on: #1448

Exclude schema from index name (#1505)

Merge branch 'master' into fix-schema-idx

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: #1499

fix vet

fix drone lint

remove go1.10 test on drone

Exclude schema from the index name

Co-authored-by: Guillermo Prandi <guillep2k@users.noreply.github.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: #1505

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: #1499

For nullable columns, store nil values as NULL

fix vet

fix drone lint

remove go1.10 test on drone

Fix update with Alias (#1455)

Improve ci tests (#1477)

Rewrite Engine.QuoteTo() to accept multi-part identifiers (#1476)

Support local sql log (#1338)

Fix go mod and update version (#1460)

Move github.com/go-xorm/xorm to xorm.io/xorm (#1459)

add support custom type Nullfloat64 (#1450)

fix bug when query map condtion with no quote (#1449)

Don't warn when bool column default is 1 but not true (#1447)

* don't warn when bool column default is 1 but not true

* fix default case sensitive

Fix sync2 with custom table name (#1445)

* fix sync2 with custom table name

* fix bug on postgres

* fix bug on postgres

fix bug when update with setexpr (#1446)

add tidb tests on drone ci (#1444)

improve sync2 (#1443)

Fix wrong dbmetas (#1442)

* add tests for db metas

* add more tests

* fix bug on mssql

Fix default value parse bugs (#1437)

* fix default value

* fix default value tags

* fix postgres default

* fix default on postgres

* fix default on postgres

* fix mssql default

fix arg conversion (#1441)

* fix arg conversion

* fix bugs

* fix bug on postgres

* use traditional positional parameters on insert into select

* remove unnecessary tests

upgrade core (#1440)

add tests (#1439)

add go1.13 tests on drone (#1416)

Fix bug on insert where (#1436)

* fix bug on insert where

* fix bug

* fix lint

fix bug when insert multiple...
Reviewed-on: #1528
2020-02-21 02:43:58 +00:00

389 lines
10 KiB
Go

// 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"
"strconv"
"testing"
"time"
"xorm.io/builder"
"xorm.io/core"
"github.com/stretchr/testify/assert"
)
func TestQueryString(t *testing.T) {
assert.NoError(t, prepareEngine())
type GetVar2 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(GetVar2)))
var data = GetVar2{
Msg: "hi",
Age: 28,
Money: 1.5,
}
_, err := testEngine.InsertOne(data)
assert.NoError(t, err)
records, err := testEngine.QueryString("select * from " + testEngine.TableName("get_var2", true))
assert.NoError(t, err)
assert.Equal(t, 1, len(records))
assert.Equal(t, 5, len(records[0]))
assert.Equal(t, "1", records[0]["id"])
assert.Equal(t, "hi", records[0]["msg"])
assert.Equal(t, "28", records[0]["age"])
assert.Equal(t, "1.5", records[0]["money"])
}
func TestQueryString2(t *testing.T) {
assert.NoError(t, prepareEngine())
type GetVar3 struct {
Id int64 `xorm:"autoincr pk"`
Msg bool `xorm:"bit"`
}
assert.NoError(t, testEngine.Sync2(new(GetVar3)))
var data = GetVar3{
Msg: false,
}
_, err := testEngine.Insert(data)
assert.NoError(t, err)
records, err := testEngine.QueryString("select * from " + testEngine.TableName("get_var3", true))
assert.NoError(t, err)
assert.Equal(t, 1, len(records))
assert.Equal(t, 2, len(records[0]))
assert.Equal(t, "1", records[0]["id"])
assert.True(t, "0" == records[0]["msg"] || "false" == records[0]["msg"])
}
func toString(i interface{}) string {
switch i.(type) {
case []byte:
return string(i.([]byte))
case string:
return i.(string)
}
return fmt.Sprintf("%v", i)
}
func toInt64(i interface{}) int64 {
switch i.(type) {
case []byte:
n, _ := strconv.ParseInt(string(i.([]byte)), 10, 64)
return n
case int:
return int64(i.(int))
case int64:
return i.(int64)
}
return 0
}
func toFloat64(i interface{}) float64 {
switch i.(type) {
case []byte:
n, _ := strconv.ParseFloat(string(i.([]byte)), 64)
return n
case float64:
return i.(float64)
case float32:
return float64(i.(float32))
}
return 0
}
func TestQueryInterface(t *testing.T) {
assert.NoError(t, prepareEngine())
type GetVarInterface 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(GetVarInterface)))
var data = GetVarInterface{
Msg: "hi",
Age: 28,
Money: 1.5,
}
_, err := testEngine.InsertOne(data)
assert.NoError(t, err)
records, err := testEngine.QueryInterface("select * from " + testEngine.TableName("get_var_interface", true))
assert.NoError(t, err)
assert.Equal(t, 1, len(records))
assert.Equal(t, 5, len(records[0]))
assert.EqualValues(t, 1, toInt64(records[0]["id"]))
assert.Equal(t, "hi", toString(records[0]["msg"]))
assert.EqualValues(t, 28, toInt64(records[0]["age"]))
assert.EqualValues(t, 1.5, toFloat64(records[0]["money"]))
}
func TestQueryNoParams(t *testing.T) {
assert.NoError(t, prepareEngine())
type QueryNoParams struct {
Id int64 `xorm:"autoincr pk"`
Msg string `xorm:"varchar(255)"`
Age int
Money float32
Created time.Time `xorm:"created"`
}
testEngine.ShowSQL(true)
assert.NoError(t, testEngine.Sync2(new(QueryNoParams)))
var q = QueryNoParams{
Msg: "message",
Age: 20,
Money: 3000,
}
cnt, err := testEngine.Insert(&q)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
assertResult := func(t *testing.T, results []map[string][]byte) {
assert.EqualValues(t, 1, len(results))
id, err := strconv.ParseInt(string(results[0]["id"]), 10, 64)
assert.NoError(t, err)
assert.EqualValues(t, 1, id)
assert.Equal(t, "message", string(results[0]["msg"]))
age, err := strconv.Atoi(string(results[0]["age"]))
assert.NoError(t, err)
assert.EqualValues(t, 20, age)
money, err := strconv.ParseFloat(string(results[0]["money"]), 32)
assert.NoError(t, err)
assert.EqualValues(t, 3000, money)
}
results, err := testEngine.Table("query_no_params").Limit(10).Query()
assert.NoError(t, err)
assertResult(t, results)
results, err = testEngine.SQL("select * from " + testEngine.TableName("query_no_params", true)).Query()
assert.NoError(t, err)
assertResult(t, results)
}
func TestQueryStringNoParam(t *testing.T) {
assert.NoError(t, prepareEngine())
type GetVar4 struct {
Id int64 `xorm:"autoincr pk"`
Msg bool `xorm:"bit"`
}
assert.NoError(t, testEngine.Sync2(new(GetVar4)))
var data = GetVar4{
Msg: false,
}
_, err := testEngine.Insert(data)
assert.NoError(t, err)
records, err := testEngine.Table("get_var4").Limit(1).QueryString()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(records))
assert.EqualValues(t, "1", records[0]["id"])
if testEngine.Dialect().DBType() == core.POSTGRES || testEngine.Dialect().DBType() == core.MSSQL {
assert.EqualValues(t, "false", records[0]["msg"])
} else {
assert.EqualValues(t, "0", records[0]["msg"])
}
records, err = testEngine.Table("get_var4").Where(builder.Eq{"id": 1}).QueryString()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(records))
assert.EqualValues(t, "1", records[0]["id"])
if testEngine.Dialect().DBType() == core.POSTGRES || testEngine.Dialect().DBType() == core.MSSQL {
assert.EqualValues(t, "false", records[0]["msg"])
} else {
assert.EqualValues(t, "0", records[0]["msg"])
}
}
func TestQuerySliceStringNoParam(t *testing.T) {
assert.NoError(t, prepareEngine())
type GetVar6 struct {
Id int64 `xorm:"autoincr pk"`
Msg bool `xorm:"bit"`
}
assert.NoError(t, testEngine.Sync2(new(GetVar6)))
var data = GetVar6{
Msg: false,
}
_, err := testEngine.Insert(data)
assert.NoError(t, err)
records, err := testEngine.Table("get_var6").Limit(1).QuerySliceString()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(records))
assert.EqualValues(t, "1", records[0][0])
if testEngine.Dialect().DBType() == core.POSTGRES || testEngine.Dialect().DBType() == core.MSSQL {
assert.EqualValues(t, "false", records[0][1])
} else {
assert.EqualValues(t, "0", records[0][1])
}
records, err = testEngine.Table("get_var6").Where(builder.Eq{"id": 1}).QuerySliceString()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(records))
assert.EqualValues(t, "1", records[0][0])
if testEngine.Dialect().DBType() == core.POSTGRES || testEngine.Dialect().DBType() == core.MSSQL {
assert.EqualValues(t, "false", records[0][1])
} else {
assert.EqualValues(t, "0", records[0][1])
}
}
func TestQueryInterfaceNoParam(t *testing.T) {
assert.NoError(t, prepareEngine())
type GetVar5 struct {
Id int64 `xorm:"autoincr pk"`
Msg bool `xorm:"bit"`
}
assert.NoError(t, testEngine.Sync2(new(GetVar5)))
var data = GetVar5{
Msg: false,
}
_, err := testEngine.Insert(data)
assert.NoError(t, err)
records, err := testEngine.Table("get_var5").Limit(1).QueryInterface()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(records))
assert.EqualValues(t, 1, toInt64(records[0]["id"]))
assert.EqualValues(t, 0, toInt64(records[0]["msg"]))
records, err = testEngine.Table("get_var5").Where(builder.Eq{"id": 1}).QueryInterface()
assert.NoError(t, err)
assert.EqualValues(t, 1, len(records))
assert.EqualValues(t, 1, toInt64(records[0]["id"]))
assert.EqualValues(t, 0, toInt64(records[0]["msg"]))
}
func TestQueryWithBuilder(t *testing.T) {
assert.NoError(t, prepareEngine())
type QueryWithBuilder struct {
Id int64 `xorm:"autoincr pk"`
Msg string `xorm:"varchar(255)"`
Age int
Money float32
Created time.Time `xorm:"created"`
}
testEngine.ShowSQL(true)
assert.NoError(t, testEngine.Sync2(new(QueryWithBuilder)))
var q = QueryWithBuilder{
Msg: "message",
Age: 20,
Money: 3000,
}
cnt, err := testEngine.Insert(&q)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
assertResult := func(t *testing.T, results []map[string][]byte) {
assert.EqualValues(t, 1, len(results))
id, err := strconv.ParseInt(string(results[0]["id"]), 10, 64)
assert.NoError(t, err)
assert.EqualValues(t, 1, id)
assert.Equal(t, "message", string(results[0]["msg"]))
age, err := strconv.Atoi(string(results[0]["age"]))
assert.NoError(t, err)
assert.EqualValues(t, 20, age)
money, err := strconv.ParseFloat(string(results[0]["money"]), 32)
assert.NoError(t, err)
assert.EqualValues(t, 3000, money)
}
results, err := testEngine.Query(builder.Select("*").From(testEngine.TableName("query_with_builder", true)))
assert.NoError(t, err)
assertResult(t, results)
}
func TestJoinWithSubQuery(t *testing.T) {
assert.NoError(t, prepareEngine())
type JoinWithSubQuery1 struct {
Id int64 `xorm:"autoincr pk"`
Msg string `xorm:"varchar(255)"`
DepartId int64
Money float32
}
type JoinWithSubQueryDepart struct {
Id int64 `xorm:"autoincr pk"`
Name string
}
testEngine.ShowSQL(true)
assert.NoError(t, testEngine.Sync2(new(JoinWithSubQuery1), new(JoinWithSubQueryDepart)))
var depart = JoinWithSubQueryDepart{
Name: "depart1",
}
cnt, err := testEngine.Insert(&depart)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
var q = JoinWithSubQuery1{
Msg: "message",
DepartId: depart.Id,
Money: 3000,
}
cnt, err = testEngine.Insert(&q)
assert.NoError(t, err)
assert.EqualValues(t, 1, cnt)
tbName := testEngine.Quote(testEngine.TableName("join_with_sub_query_depart", true))
var querys []JoinWithSubQuery1
err = testEngine.Join("INNER", builder.Select("id").From(tbName),
"join_with_sub_query_depart.id = join_with_sub_query1.depart_id").Find(&querys)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(querys))
assert.EqualValues(t, q, querys[0])
querys = make([]JoinWithSubQuery1, 0, 1)
err = testEngine.Join("INNER", "(SELECT id FROM "+tbName+") join_with_sub_query_depart", "join_with_sub_query_depart.id = join_with_sub_query1.depart_id").
Find(&querys)
assert.NoError(t, err)
assert.EqualValues(t, 1, len(querys))
assert.EqualValues(t, q, querys[0])
}