@@ -85,7 +85,7 @@ func (c *Tuple2) DecodeMsgpack(d *msgpack.Decoder) error {
85
85
}
86
86
87
87
var server = "127.0.0.1:3013"
88
- var spaceNo = uint32 (512 )
88
+ var spaceNo = uint32 (515 )
89
89
var spaceName = "test"
90
90
var indexNo = uint32 (0 )
91
91
var indexName = "primary"
@@ -693,6 +693,325 @@ func TestClient(t *testing.T) {
693
693
}
694
694
}
695
695
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
+
696
1015
func TestSchema (t * testing.T ) {
697
1016
var err error
698
1017
var conn * Connection
0 commit comments