From ab17aa22037beed8b99422a95d8a9419066caf7c Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 8 May 2018 17:44:15 +0800 Subject: [PATCH] Add more test for query (#932) * add more test for query * fix tests --- session_query_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/session_query_test.go b/session_query_test.go index 8b4aefad1..2c1fb6179 100644 --- a/session_query_test.go +++ b/session_query_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/go-xorm/builder" + "github.com/go-xorm/core" "github.com/stretchr/testify/assert" ) @@ -186,6 +187,72 @@ func TestQueryNoParams(t *testing.T) { 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().URI().DbType == core.POSTGRES { + 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().URI().DbType == core.POSTGRES { + assert.EqualValues(t, "false", records[0]["msg"]) + } else { + assert.EqualValues(t, "0", records[0]["msg"]) + } +} + +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())