Skip to content

Commit 010b14d

Browse files
committed
tests: add SQL tests
Added SQL tests. Updated config.lua for creation the space for using SQL in tests. Added the check of Tarantool version to skip SQL tests if tarantool version < 2.0.0. Changed id of the test space with id=512, cause if using SQL there is no ability to set space id explicitly, so it gets created with id=512 by default and conflicts with already existing space with the same id. Follows up #62
1 parent c615c84 commit 010b14d

File tree

4 files changed

+329
-7
lines changed

4 files changed

+329
-7
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ import (
184184
)
185185

186186
func main() {
187-
spaceNo := uint32(512)
187+
spaceNo := uint32(515)
188188
indexNo := uint32(0)
189189

190190
server := "127.0.0.1:3013"

config.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ box.cfg{
66

77
box.once("init", function()
88
local s = box.schema.space.create('test', {
9-
id = 512,
9+
id = 515,
1010
if_not_exists = true,
1111
})
1212
s:create_index('primary', {type = 'tree', parts = {1, 'uint'}, if_not_exists = true})
@@ -40,7 +40,6 @@ box.once("init", function()
4040
})
4141
st:truncate()
4242

43-
--box.schema.user.grant('guest', 'read,write,execute', 'universe')
4443
box.schema.func.create('box.info')
4544
box.schema.func.create('simple_incr')
4645

@@ -49,6 +48,10 @@ box.once("init", function()
4948
box.schema.user.grant('test', 'execute', 'universe')
5049
box.schema.user.grant('test', 'read,write', 'space', 'test')
5150
box.schema.user.grant('test', 'read,write', 'space', 'schematest')
51+
52+
-- grants for sql tests
53+
box.schema.user.grant('test', 'create,read,write,drop,alter', 'space')
54+
box.schema.user.grant('test','create','sequence')
5255
end)
5356

5457
local function simple_incr(a)

example_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func ExampleConnection_Select() {
4242
return
4343
}
4444
defer conn.Close()
45-
resp, err := conn.Select(512, 0, 0, 100, tarantool.IterEq, []interface{}{uint(1111)})
45+
resp, err := conn.Select(515, 0, 0, 100, tarantool.IterEq, []interface{}{uint(1111)})
4646
if err != nil {
4747
fmt.Printf("error in select is %v", err)
4848
return
@@ -68,7 +68,7 @@ func ExampleConnection_SelectTyped() {
6868
}
6969
defer conn.Close()
7070
var res []Tuple
71-
err = conn.SelectTyped(512, 0, 0, 100, tarantool.IterEq, tarantool.IntKey{1111}, &res)
71+
err = conn.SelectTyped(515, 0, 0, 100, tarantool.IterEq, tarantool.IntKey{1111}, &res)
7272
if err != nil {
7373
fmt.Printf("error in select is %v", err)
7474
return
@@ -86,7 +86,7 @@ func ExampleConnection_SelectTyped() {
8686
}
8787

8888
func Example() {
89-
spaceNo := uint32(512)
89+
spaceNo := uint32(515)
9090
indexNo := uint32(0)
9191

9292
server := "127.0.0.1:3013"

tarantool_test.go

+320-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (c *Tuple2) DecodeMsgpack(d *msgpack.Decoder) error {
8585
}
8686

8787
var server = "127.0.0.1:3013"
88-
var spaceNo = uint32(512)
88+
var spaceNo = uint32(515)
8989
var spaceName = "test"
9090
var indexNo = uint32(0)
9191
var indexName = "primary"
@@ -693,6 +693,325 @@ func TestClient(t *testing.T) {
693693
}
694694
}
695695

696+
const (
697+
createTableQuery = "CREATE TABLE SQL_TEST (id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING COLLATE \"unicode\" DEFAULT NULL);"
698+
insertQuery = "INSERT INTO SQL_TEST VALUES (?, ?);"
699+
selectNamedQuery = "SELECT id, name FROM SQL_TEST WHERE id=:id AND name=:name;"
700+
selectPosQuery = "SELECT id, name FROM SQL_TEST WHERE id=? AND name=?;"
701+
updateQuery = "UPDATE SQL_TEST SET name=? WHERE id=?;"
702+
enableFullMetaData = "SET SESSION \"sql_full_metadata\" = true;"
703+
selectSpanDifQuery = "SELECT id*2, name, id FROM SQL_TEST WHERE name=?;"
704+
alterQueryIncrement = "ALTER TABLE SQL_TEST RENAME TO SQL_TEST2;"
705+
insertIncrQuery = "INSERT INTO SQL_TEST2 VALUES (?, ?);"
706+
deleteQuery = "DELETE FROM SQL_TEST2 WHERE name=?;"
707+
dropQuery = "DROP TABLE SQL_TEST2;"
708+
disableFullMetaData = "SET SESSION \"sql_full_metadata\" = false;"
709+
)
710+
711+
func TestSQL(t *testing.T) {
712+
// Data for test table
713+
testData := map[int]string{
714+
1: "test",
715+
2: "test",
716+
3: "test",
717+
4: "test",
718+
5: "test",
719+
}
720+
721+
// Check for skip SQL tests if tarantool version < 2.0.0
722+
isLess, err := test_helpers.IsTarantoolVersionLess(2, 0, 0)
723+
if err != nil {
724+
t.Errorf("Could not check the Tarantool version")
725+
return
726+
}
727+
if isLess {
728+
return
729+
}
730+
731+
var resp *Response
732+
var conn *Connection
733+
734+
conn, err = Connect(server, opts)
735+
if err != nil {
736+
t.Errorf("Failed to connect: %s", err.Error())
737+
return
738+
}
739+
if conn == nil {
740+
t.Errorf("conn is nil after Connect")
741+
return
742+
}
743+
defer conn.Close()
744+
745+
resp, err = conn.Execute(createTableQuery, []interface{}{})
746+
if err != nil {
747+
t.Errorf("Failed to Execute: %s", err.Error())
748+
}
749+
if resp == nil {
750+
t.Errorf("Response is nil after Execute")
751+
}
752+
if resp.Code != 0 {
753+
t.Errorf("Failed to Execute: %d", resp.Code)
754+
}
755+
if resp.SQLInfo.AffectedCount != 1 {
756+
t.Errorf("Incorrect count of created spaces: %d", resp.SQLInfo.AffectedCount)
757+
}
758+
759+
// Create a table with the same name
760+
resp, err = conn.Execute(createTableQuery, []interface{}{})
761+
if err == nil {
762+
t.Errorf("Must be a failure for creating space with the same name")
763+
}
764+
if resp.Code != ErSpaceExistsCode {
765+
t.Errorf("Wrong response code: %d", resp.Code)
766+
}
767+
768+
// Execute with nil sql bind
769+
resp, err = conn.Execute(createTableQuery, nil)
770+
if err == nil {
771+
t.Errorf("Must be a failure for creating space with the same name")
772+
}
773+
if resp.Code != IteratorCode {
774+
t.Errorf("Wrong response code: %d", resp.Code)
775+
}
776+
777+
// Execute with empty query
778+
resp, err = conn.Execute("", nil)
779+
if err == nil {
780+
t.Errorf("Must be a failure for creating space with the same name")
781+
}
782+
if resp.Code != IteratorCode {
783+
t.Errorf("Wrong response code: %d", resp.Code)
784+
}
785+
786+
for i := 1; i < 5; i++ {
787+
resp, err = conn.Execute(insertQuery, []interface{}{i, testData[i]})
788+
if err != nil {
789+
t.Errorf("Failed to Execute: %s", err.Error())
790+
}
791+
if resp == nil {
792+
t.Errorf("Response is nil after Execute")
793+
}
794+
if resp.Code != 0 {
795+
t.Errorf("Failed to Execute: %d", resp.Code)
796+
}
797+
if resp.SQLInfo.AffectedCount != 1 {
798+
t.Errorf("Incorrect count of affected rows: %d", resp.SQLInfo.AffectedCount)
799+
}
800+
}
801+
802+
// Test insert with positioned arguments
803+
resp, err = conn.Execute(insertQuery, 5, testData[5])
804+
if err != nil {
805+
t.Errorf("Failed to Execute: %s", err.Error())
806+
}
807+
if resp == nil {
808+
t.Errorf("Response is nil after Execute")
809+
}
810+
if resp.Code != 0 {
811+
t.Errorf("Failed to Execute: %d", resp.Code)
812+
}
813+
if resp.SQLInfo.AffectedCount != 1 {
814+
t.Errorf("Incorrect count of affected rows: %d", resp.SQLInfo.AffectedCount)
815+
}
816+
817+
// test all types of supported bindings
818+
// prepare named sql bind
819+
sqlBind := map[string]interface{}{
820+
"id": 1,
821+
"name": "test",
822+
}
823+
sqlBind2 := struct {
824+
Id int
825+
Name string
826+
}{2, "test"}
827+
828+
type kv struct {
829+
Key string
830+
Value interface{}
831+
}
832+
sqlBind3 := []struct {
833+
Key string
834+
Value interface{}
835+
}{
836+
kv{"id", 3},
837+
kv{"name", "test"},
838+
}
839+
840+
// positioned sql bind
841+
sqlBind4 := []interface{}{
842+
4, "test",
843+
}
844+
845+
namedSQLBinds := []interface{}{
846+
sqlBind,
847+
sqlBind2,
848+
sqlBind3,
849+
}
850+
851+
for i, bind := range namedSQLBinds {
852+
resp, err = conn.Execute(selectNamedQuery, bind)
853+
if err != nil {
854+
fmt.Println(bind)
855+
t.Errorf("Failed to Execute: %s", err.Error())
856+
}
857+
if resp == nil {
858+
t.Errorf("Response is nil after Execute")
859+
}
860+
if resp.Data[0] == i && resp.Data[1] == testData[i] {
861+
t.Errorf("Select with named arguments failed")
862+
}
863+
if resp.MetaData.ColumnsInfo[0].FieldType != "integer" ||
864+
resp.MetaData.ColumnsInfo[0].FieldName != "ID" ||
865+
resp.MetaData.ColumnsInfo[1].FieldType != "string" ||
866+
resp.MetaData.ColumnsInfo[1].FieldName != "NAME" {
867+
t.Errorf("Wrong metadata")
868+
}
869+
}
870+
871+
resp, err = conn.Execute(selectPosQuery, sqlBind4)
872+
if err != nil {
873+
t.Errorf("Failed to Execute: %s", err.Error())
874+
}
875+
if resp == nil {
876+
t.Errorf("Response is nil after Execute")
877+
}
878+
if resp.Data[0] == 4 && resp.Data[1] == testData[4] {
879+
t.Errorf("Select with positioned arguments failed")
880+
}
881+
if resp.MetaData.ColumnsInfo[0].FieldType != "integer" ||
882+
resp.MetaData.ColumnsInfo[0].FieldName != "ID" ||
883+
resp.MetaData.ColumnsInfo[1].FieldType != "string" ||
884+
resp.MetaData.ColumnsInfo[1].FieldName != "NAME" {
885+
t.Errorf("Wrong metadata")
886+
}
887+
888+
resp, err = conn.Execute(selectPosQuery, 5, testData[5])
889+
if err != nil {
890+
t.Errorf("Failed to Execute: %s", err.Error())
891+
}
892+
if resp == nil {
893+
t.Errorf("Response is nil after Execute")
894+
}
895+
if resp.Code != 0 {
896+
t.Errorf("Failed to Execute: %d", resp.Code)
897+
}
898+
if resp.Data[0] == 5 && resp.Data[1] == testData[5] {
899+
t.Errorf("Select with positioned arguments failed: wrong values received %v", resp.Data)
900+
}
901+
if resp.MetaData.ColumnsInfo[0].FieldType != "integer" ||
902+
resp.MetaData.ColumnsInfo[0].FieldName != "ID" ||
903+
resp.MetaData.ColumnsInfo[1].FieldType != "string" ||
904+
resp.MetaData.ColumnsInfo[1].FieldName != "NAME" {
905+
t.Errorf("Wrong metadata")
906+
}
907+
908+
sqlUpdateBind := []interface{}{"test2", 2}
909+
resp, err = conn.Execute(updateQuery, sqlUpdateBind)
910+
if err != nil {
911+
t.Errorf("Failed to Execute: %s", err.Error())
912+
}
913+
if resp == nil {
914+
t.Errorf("Response is nil after Execute")
915+
}
916+
if resp.SQLInfo.AffectedCount != 1 {
917+
t.Errorf("Incorrect count of affected rows: %d", resp.SQLInfo.AffectedCount)
918+
}
919+
920+
// Enable full metadata
921+
resp, err = conn.Execute(enableFullMetaData, []interface{}{})
922+
if err != nil {
923+
t.Errorf("Failed to Execute: %s", err.Error())
924+
}
925+
if resp == nil {
926+
t.Errorf("Response is nil after Execute")
927+
}
928+
if resp.Code != 0 {
929+
t.Errorf("Failed to Execute: %d", resp.Code)
930+
}
931+
932+
// Check all extended fields coming with metadata
933+
resp, err = conn.Execute(selectSpanDifQuery, []interface{}{"test2"})
934+
if err != nil {
935+
t.Errorf("Failed to Execute: %s", err.Error())
936+
}
937+
if resp == nil {
938+
t.Errorf("Response is nil after Execute")
939+
}
940+
if resp.Data[0] == 4 && resp.Data[1] == "test2" {
941+
t.Errorf("Select failed")
942+
}
943+
if resp.MetaData.ColumnsInfo[0].FieldSpan != "id*2" ||
944+
resp.MetaData.ColumnsInfo[1].FieldSpan != "name" ||
945+
resp.MetaData.ColumnsInfo[1].FieldIsNullable != true ||
946+
resp.MetaData.ColumnsInfo[1].FieldCollation != "unicode" ||
947+
resp.MetaData.ColumnsInfo[2].FieldIsAutoincrement != true {
948+
t.Errorf("Wrong metadata: %v", resp.MetaData)
949+
}
950+
951+
resp, err = conn.Execute(alterQueryIncrement, []interface{}{})
952+
if err != nil {
953+
t.Errorf("Failed to Execute: %s", err.Error())
954+
}
955+
if resp == nil {
956+
t.Errorf("Response is nil after Execute")
957+
}
958+
959+
sqlBind5 := []interface{}{nil, "test"}
960+
resp, err = conn.Execute(insertIncrQuery, sqlBind5)
961+
if err != nil {
962+
t.Errorf("Failed to Execute: %s", err.Error())
963+
}
964+
if resp == nil {
965+
t.Errorf("Response is nil after Execute")
966+
}
967+
if resp.SQLInfo.AffectedCount != 1 {
968+
t.Errorf("Incorrect count of affected rows: %d", resp.SQLInfo.AffectedCount)
969+
}
970+
if resp.SQLInfo.InfoAutoincrementIds[0] != 6 {
971+
t.Errorf("Incorrect autoincrement ids: %v", resp.SQLInfo.InfoAutoincrementIds)
972+
}
973+
974+
resp, err = conn.Execute(deleteQuery, []interface{}{"test"})
975+
if err != nil {
976+
t.Errorf("Failed to Execute: %s", err.Error())
977+
}
978+
if resp == nil {
979+
t.Errorf("Response is nil after Execute")
980+
}
981+
if resp.Code != 0 {
982+
t.Errorf("Failed to Execute: %d", resp.Code)
983+
}
984+
if resp.SQLInfo.AffectedCount != 5 {
985+
t.Errorf("Incorrect count of affected rows: %d", resp.SQLInfo.AffectedCount)
986+
}
987+
988+
resp, err = conn.Execute(dropQuery, []interface{}{})
989+
if err != nil {
990+
t.Errorf("Failed to Execute: %s", err.Error())
991+
}
992+
if resp == nil {
993+
t.Errorf("Response is nil after Execute")
994+
}
995+
if resp.Code != 0 {
996+
t.Errorf("Failed to Execute: %d", resp.Code)
997+
}
998+
if resp.SQLInfo.AffectedCount != 1 {
999+
t.Errorf("Incorrect count of dropped spaces: %d", resp.SQLInfo.AffectedCount)
1000+
}
1001+
1002+
// Disable full metadata
1003+
resp, err = conn.Execute(disableFullMetaData, []interface{}{})
1004+
if err != nil {
1005+
t.Errorf("Failed to Execute: %s", err.Error())
1006+
}
1007+
if resp == nil {
1008+
t.Errorf("Response is nil after Execute")
1009+
}
1010+
if resp.Code != 0 {
1011+
t.Errorf("Failed to Execute: %d", resp.Code)
1012+
}
1013+
}
1014+
6961015
func TestSchema(t *testing.T) {
6971016
var err error
6981017
var conn *Connection

0 commit comments

Comments
 (0)