Skip to content

Commit 45271e2

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 7e642f8 commit 45271e2

File tree

4 files changed

+328
-7
lines changed

4 files changed

+328
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 319 additions & 1 deletion
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,324 @@ 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+
t.Errorf("Failed to Execute: %s", err.Error())
855+
}
856+
if resp == nil {
857+
t.Errorf("Response is nil after Execute")
858+
}
859+
if resp.Data[0] == i && resp.Data[1] == testData[i] {
860+
t.Errorf("Select with named arguments failed")
861+
}
862+
if resp.MetaData.ColumnsInfo[0].FieldType != "integer" ||
863+
resp.MetaData.ColumnsInfo[0].FieldName != "ID" ||
864+
resp.MetaData.ColumnsInfo[1].FieldType != "string" ||
865+
resp.MetaData.ColumnsInfo[1].FieldName != "NAME" {
866+
t.Errorf("Wrong metadata")
867+
}
868+
}
869+
870+
resp, err = conn.Execute(selectPosQuery, sqlBind4)
871+
if err != nil {
872+
t.Errorf("Failed to Execute: %s", err.Error())
873+
}
874+
if resp == nil {
875+
t.Errorf("Response is nil after Execute")
876+
}
877+
if resp.Data[0] == 4 && resp.Data[1] == testData[4] {
878+
t.Errorf("Select with positioned arguments failed")
879+
}
880+
if resp.MetaData.ColumnsInfo[0].FieldType != "integer" ||
881+
resp.MetaData.ColumnsInfo[0].FieldName != "ID" ||
882+
resp.MetaData.ColumnsInfo[1].FieldType != "string" ||
883+
resp.MetaData.ColumnsInfo[1].FieldName != "NAME" {
884+
t.Errorf("Wrong metadata")
885+
}
886+
887+
resp, err = conn.Execute(selectPosQuery, 5, testData[5])
888+
if err != nil {
889+
t.Errorf("Failed to Execute: %s", err.Error())
890+
}
891+
if resp == nil {
892+
t.Errorf("Response is nil after Execute")
893+
}
894+
if resp.Code != 0 {
895+
t.Errorf("Failed to Execute: %d", resp.Code)
896+
}
897+
if resp.Data[0] == 5 && resp.Data[1] == testData[5] {
898+
t.Errorf("Select with positioned arguments failed: wrong values received %v", resp.Data)
899+
}
900+
if resp.MetaData.ColumnsInfo[0].FieldType != "integer" ||
901+
resp.MetaData.ColumnsInfo[0].FieldName != "ID" ||
902+
resp.MetaData.ColumnsInfo[1].FieldType != "string" ||
903+
resp.MetaData.ColumnsInfo[1].FieldName != "NAME" {
904+
t.Errorf("Wrong metadata")
905+
}
906+
907+
sqlUpdateBind := []interface{}{"test2", 2}
908+
resp, err = conn.Execute(updateQuery, sqlUpdateBind)
909+
if err != nil {
910+
t.Errorf("Failed to Execute: %s", err.Error())
911+
}
912+
if resp == nil {
913+
t.Errorf("Response is nil after Execute")
914+
}
915+
if resp.SQLInfo.AffectedCount != 1 {
916+
t.Errorf("Incorrect count of affected rows: %d", resp.SQLInfo.AffectedCount)
917+
}
918+
919+
// Enable full metadata
920+
resp, err = conn.Execute(enableFullMetaData, []interface{}{})
921+
if err != nil {
922+
t.Errorf("Failed to Execute: %s", err.Error())
923+
}
924+
if resp == nil {
925+
t.Errorf("Response is nil after Execute")
926+
}
927+
if resp.Code != 0 {
928+
t.Errorf("Failed to Execute: %d", resp.Code)
929+
}
930+
931+
// Check all extended fields coming with metadata
932+
resp, err = conn.Execute(selectSpanDifQuery, []interface{}{"test2"})
933+
if err != nil {
934+
t.Errorf("Failed to Execute: %s", err.Error())
935+
}
936+
if resp == nil {
937+
t.Errorf("Response is nil after Execute")
938+
}
939+
if resp.Data[0] == 4 && resp.Data[1] == "test2" {
940+
t.Errorf("Select failed")
941+
}
942+
if resp.MetaData.ColumnsInfo[0].FieldSpan != "id*2" ||
943+
resp.MetaData.ColumnsInfo[1].FieldSpan != "name" ||
944+
resp.MetaData.ColumnsInfo[1].FieldIsNullable != true ||
945+
resp.MetaData.ColumnsInfo[1].FieldCollation != "unicode" ||
946+
resp.MetaData.ColumnsInfo[2].FieldIsAutoincrement != true {
947+
t.Errorf("Wrong metadata: %v", resp.MetaData)
948+
}
949+
950+
resp, err = conn.Execute(alterQueryIncrement, []interface{}{})
951+
if err != nil {
952+
t.Errorf("Failed to Execute: %s", err.Error())
953+
}
954+
if resp == nil {
955+
t.Errorf("Response is nil after Execute")
956+
}
957+
958+
sqlBind5 := []interface{}{nil, "test"}
959+
resp, err = conn.Execute(insertIncrQuery, sqlBind5)
960+
if err != nil {
961+
t.Errorf("Failed to Execute: %s", err.Error())
962+
}
963+
if resp == nil {
964+
t.Errorf("Response is nil after Execute")
965+
}
966+
if resp.SQLInfo.AffectedCount != 1 {
967+
t.Errorf("Incorrect count of affected rows: %d", resp.SQLInfo.AffectedCount)
968+
}
969+
if resp.SQLInfo.InfoAutoincrementIds[0] != 6 {
970+
t.Errorf("Incorrect autoincrement ids: %v", resp.SQLInfo.InfoAutoincrementIds)
971+
}
972+
973+
resp, err = conn.Execute(deleteQuery, []interface{}{"test"})
974+
if err != nil {
975+
t.Errorf("Failed to Execute: %s", err.Error())
976+
}
977+
if resp == nil {
978+
t.Errorf("Response is nil after Execute")
979+
}
980+
if resp.Code != 0 {
981+
t.Errorf("Failed to Execute: %d", resp.Code)
982+
}
983+
if resp.SQLInfo.AffectedCount != 5 {
984+
t.Errorf("Incorrect count of affected rows: %d", resp.SQLInfo.AffectedCount)
985+
}
986+
987+
resp, err = conn.Execute(dropQuery, []interface{}{})
988+
if err != nil {
989+
t.Errorf("Failed to Execute: %s", err.Error())
990+
}
991+
if resp == nil {
992+
t.Errorf("Response is nil after Execute")
993+
}
994+
if resp.Code != 0 {
995+
t.Errorf("Failed to Execute: %d", resp.Code)
996+
}
997+
if resp.SQLInfo.AffectedCount != 1 {
998+
t.Errorf("Incorrect count of dropped spaces: %d", resp.SQLInfo.AffectedCount)
999+
}
1000+
1001+
// Disable full metadata
1002+
resp, err = conn.Execute(disableFullMetaData, []interface{}{})
1003+
if err != nil {
1004+
t.Errorf("Failed to Execute: %s", err.Error())
1005+
}
1006+
if resp == nil {
1007+
t.Errorf("Response is nil after Execute")
1008+
}
1009+
if resp.Code != 0 {
1010+
t.Errorf("Failed to Execute: %d", resp.Code)
1011+
}
1012+
}
1013+
6961014
func TestSchema(t *testing.T) {
6971015
var err error
6981016
var conn *Connection

0 commit comments

Comments
 (0)