|
9 | 9 |
|
10 | 10 | . "github.com/tarantool/go-tarantool"
|
11 | 11 | "gopkg.in/vmihailenco/msgpack.v2"
|
| 12 | + "github.com/google/uuid" |
12 | 13 | )
|
13 | 14 |
|
14 | 15 | type Member struct {
|
@@ -1005,3 +1006,119 @@ func TestComplexStructs(t *testing.T) {
|
1005 | 1006 | return
|
1006 | 1007 | }
|
1007 | 1008 | }
|
| 1009 | + |
| 1010 | + |
| 1011 | +var uuidSpace = "testUUID" |
| 1012 | +var uuidIndex = "primary" |
| 1013 | + |
| 1014 | +type TupleUUID struct { |
| 1015 | + id uuid.UUID |
| 1016 | +} |
| 1017 | + |
| 1018 | +func (t *TupleUUID) DecodeMsgpack(d *msgpack.Decoder) error { |
| 1019 | + var err error |
| 1020 | + var l int |
| 1021 | + if l, err = d.DecodeSliceLen(); err != nil { |
| 1022 | + return err |
| 1023 | + } |
| 1024 | + if l != 1 { |
| 1025 | + return fmt.Errorf("array len doesn't match: %d", l) |
| 1026 | + } |
| 1027 | + res, err := d.DecodeInterface() |
| 1028 | + if err != nil { |
| 1029 | + return err |
| 1030 | + } |
| 1031 | + t.id = res.(uuid.UUID) |
| 1032 | + return nil |
| 1033 | +} |
| 1034 | + |
| 1035 | +func connectWithValidation(t *testing.T) *Connection { |
| 1036 | + conn, err := Connect(server, opts) |
| 1037 | + if err != nil { |
| 1038 | + t.Errorf("Failed to connect: %s", err.Error()) |
| 1039 | + } |
| 1040 | + if conn == nil { |
| 1041 | + t.Errorf("conn is nil after Connect") |
| 1042 | + } |
| 1043 | + return conn |
| 1044 | +} |
| 1045 | + |
| 1046 | +func skipIfUUIDUnsupported(t *testing.T, conn *Connection) { |
| 1047 | + resp, err := conn.Eval("return pcall(require('msgpack').encode, require('uuid').new())", []interface{}{}) |
| 1048 | + if err != nil { |
| 1049 | + t.Errorf("Failed to Eval: %s", err.Error()) |
| 1050 | + } |
| 1051 | + if resp == nil { |
| 1052 | + t.Errorf("Response is nil after Eval") |
| 1053 | + } |
| 1054 | + if len(resp.Data) < 1 { |
| 1055 | + t.Errorf("Response.Data is empty after Eval") |
| 1056 | + } |
| 1057 | + val := resp.Data[0].(bool) |
| 1058 | + if val != true { |
| 1059 | + t.Skip("Skipping test for Tarantool without UUID support in msgpack") |
| 1060 | + } |
| 1061 | +} |
| 1062 | + |
| 1063 | +func TestUUIDselect(t *testing.T) { |
| 1064 | + conn := connectWithValidation(t) |
| 1065 | + defer conn.Close() |
| 1066 | + |
| 1067 | + skipIfUUIDUnsupported(t, conn) |
| 1068 | + |
| 1069 | + id, uuidErr := uuid.Parse("c8f0fa1f-da29-438c-a040-393f1126ad39") |
| 1070 | + if uuidErr != nil { |
| 1071 | + t.Errorf("Failed to prepare test uuid: %s", uuidErr) |
| 1072 | + } |
| 1073 | + |
| 1074 | + resp, errSel := conn.Select(uuidSpace, uuidIndex, 0, 1, IterEq, []interface{}{ id }) |
| 1075 | + if errSel != nil { |
| 1076 | + t.Errorf("UUID select failed: %s", errSel.Error()) |
| 1077 | + } |
| 1078 | + if resp == nil { |
| 1079 | + t.Errorf("Response is nil after Select") |
| 1080 | + } |
| 1081 | + if len(resp.Data) != 1 { |
| 1082 | + t.Errorf("Response Data len != 1") |
| 1083 | + } |
| 1084 | + |
| 1085 | + var tuples []TupleUUID |
| 1086 | + errTyp := conn.SelectTyped(uuidSpace, uuidIndex, 0, 1, IterEq, []interface{}{ id }, &tuples) |
| 1087 | + if errTyp != nil { |
| 1088 | + t.Errorf("Failed to SelectTyped: %s", errTyp.Error()) |
| 1089 | + } |
| 1090 | + if len(tuples) != 1 { |
| 1091 | + t.Errorf("Result len of SelectTyped != 1") |
| 1092 | + } |
| 1093 | + if tuples[0].id != id { |
| 1094 | + t.Errorf("Bad value loaded from SelectTyped: %s", tuples[0].id) |
| 1095 | + } |
| 1096 | +} |
| 1097 | + |
| 1098 | +func TestUUIDreplace(t *testing.T) { |
| 1099 | + conn := connectWithValidation(t) |
| 1100 | + defer conn.Close() |
| 1101 | + |
| 1102 | + skipIfUUIDUnsupported(t, conn) |
| 1103 | + |
| 1104 | + id, uuidErr := uuid.Parse("64d22e4d-ac92-4a23-899a-e59f34af5479") |
| 1105 | + if uuidErr != nil { |
| 1106 | + t.Errorf("Failed to prepare test uuid: %s", uuidErr) |
| 1107 | + } |
| 1108 | + |
| 1109 | + _, errRep := conn.Replace(uuidSpace, []interface{}{ id }) |
| 1110 | + if errRep != nil { |
| 1111 | + t.Errorf("UUID replace failed: %s", errRep) |
| 1112 | + } |
| 1113 | + |
| 1114 | + resp, errSel := conn.Select(uuidSpace, uuidIndex, 0, 1, IterEq, []interface{}{ id }) |
| 1115 | + if errSel != nil { |
| 1116 | + t.Errorf("UUID select failed: %s", errSel) |
| 1117 | + } |
| 1118 | + if resp == nil { |
| 1119 | + t.Errorf("Response is nil after Select") |
| 1120 | + } |
| 1121 | + if len(resp.Data) != 1 { |
| 1122 | + t.Errorf("Response Data len != 1") |
| 1123 | + } |
| 1124 | +} |
0 commit comments