|
| 1 | +package uuid_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + . "github.com/tarantool/go-tarantool" |
| 9 | + _ "github.com/tarantool/go-tarantool/uuid" |
| 10 | + "gopkg.in/vmihailenco/msgpack.v2" |
| 11 | + "github.com/google/uuid" |
| 12 | +) |
| 13 | + |
| 14 | +var server = "127.0.0.1:3013" |
| 15 | +var opts = Opts{ |
| 16 | + Timeout: 500 * time.Millisecond, |
| 17 | + User: "test", |
| 18 | + Pass: "test", |
| 19 | +} |
| 20 | + |
| 21 | +var space = "testUUID" |
| 22 | +var index = "primary" |
| 23 | + |
| 24 | +type TupleUUID struct { |
| 25 | + id uuid.UUID |
| 26 | +} |
| 27 | + |
| 28 | +func (t *TupleUUID) DecodeMsgpack(d *msgpack.Decoder) error { |
| 29 | + var err error |
| 30 | + var l int |
| 31 | + if l, err = d.DecodeSliceLen(); err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + if l != 1 { |
| 35 | + return fmt.Errorf("array len doesn't match: %d", l) |
| 36 | + } |
| 37 | + res, err := d.DecodeInterface() |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + t.id = res.(uuid.UUID) |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +func connectWithValidation(t *testing.T) *Connection { |
| 46 | + conn, err := Connect(server, opts) |
| 47 | + if err != nil { |
| 48 | + t.Errorf("Failed to connect: %s", err.Error()) |
| 49 | + } |
| 50 | + if conn == nil { |
| 51 | + t.Errorf("conn is nil after Connect") |
| 52 | + } |
| 53 | + return conn |
| 54 | +} |
| 55 | + |
| 56 | +func skipIfUUIDUnsupported(t *testing.T, conn *Connection) { |
| 57 | + resp, err := conn.Eval("return pcall(require('msgpack').encode, require('uuid').new())", []interface{}{}) |
| 58 | + if err != nil { |
| 59 | + t.Errorf("Failed to Eval: %s", err.Error()) |
| 60 | + } |
| 61 | + if resp == nil { |
| 62 | + t.Errorf("Response is nil after Eval") |
| 63 | + } |
| 64 | + if len(resp.Data) < 1 { |
| 65 | + t.Errorf("Response.Data is empty after Eval") |
| 66 | + } |
| 67 | + val := resp.Data[0].(bool) |
| 68 | + if val != true { |
| 69 | + t.Skip("Skipping test for Tarantool without UUID support in msgpack") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func tupleValueIsId(t *testing.T, tuples []interface{}, id uuid.UUID) { |
| 74 | + if len(tuples) != 1 { |
| 75 | + t.Errorf("Response Data len != 1") |
| 76 | + } |
| 77 | + |
| 78 | + if tpl, ok := tuples[0].([]interface{}); !ok { |
| 79 | + t.Errorf("Unexpected return value body") |
| 80 | + } else { |
| 81 | + if len(tpl) != 1 { |
| 82 | + t.Errorf("Unexpected return value body (tuple len)") |
| 83 | + } |
| 84 | + if val, ok := tpl[0].(uuid.UUID); !ok || val != id { |
| 85 | + t.Errorf("Unexpected return value body (tuple 0 field)") |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestSelect(t *testing.T) { |
| 91 | + conn := connectWithValidation(t) |
| 92 | + defer conn.Close() |
| 93 | + |
| 94 | + skipIfUUIDUnsupported(t, conn) |
| 95 | + |
| 96 | + id, uuidErr := uuid.Parse("c8f0fa1f-da29-438c-a040-393f1126ad39") |
| 97 | + if uuidErr != nil { |
| 98 | + t.Errorf("Failed to prepare test uuid: %s", uuidErr) |
| 99 | + } |
| 100 | + |
| 101 | + resp, errSel := conn.Select(space, index, 0, 1, IterEq, []interface{}{ id }) |
| 102 | + if errSel != nil { |
| 103 | + t.Errorf("UUID select failed: %s", errSel.Error()) |
| 104 | + } |
| 105 | + if resp == nil { |
| 106 | + t.Errorf("Response is nil after Select") |
| 107 | + } |
| 108 | + tupleValueIsId(t, resp.Data, id) |
| 109 | + |
| 110 | + var tuples []TupleUUID |
| 111 | + errTyp := conn.SelectTyped(space, index, 0, 1, IterEq, []interface{}{ id }, &tuples) |
| 112 | + if errTyp != nil { |
| 113 | + t.Errorf("Failed to SelectTyped: %s", errTyp.Error()) |
| 114 | + } |
| 115 | + if len(tuples) != 1 { |
| 116 | + t.Errorf("Result len of SelectTyped != 1") |
| 117 | + } |
| 118 | + if tuples[0].id != id { |
| 119 | + t.Errorf("Bad value loaded from SelectTyped: %s", tuples[0].id) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestReplace(t *testing.T) { |
| 124 | + conn := connectWithValidation(t) |
| 125 | + defer conn.Close() |
| 126 | + |
| 127 | + skipIfUUIDUnsupported(t, conn) |
| 128 | + |
| 129 | + id, uuidErr := uuid.Parse("64d22e4d-ac92-4a23-899a-e59f34af5479") |
| 130 | + if uuidErr != nil { |
| 131 | + t.Errorf("Failed to prepare test uuid: %s", uuidErr) |
| 132 | + } |
| 133 | + |
| 134 | + respRep, errRep := conn.Replace(space, []interface{}{ id }) |
| 135 | + if errRep != nil { |
| 136 | + t.Errorf("UUID replace failed: %s", errRep) |
| 137 | + } |
| 138 | + if respRep == nil { |
| 139 | + t.Errorf("Response is nil after Replace") |
| 140 | + } |
| 141 | + tupleValueIsId(t, respRep.Data, id) |
| 142 | + |
| 143 | + respSel, errSel := conn.Select(space, index, 0, 1, IterEq, []interface{}{ id }) |
| 144 | + if errSel != nil { |
| 145 | + t.Errorf("UUID select failed: %s", errSel) |
| 146 | + } |
| 147 | + if respSel == nil { |
| 148 | + t.Errorf("Response is nil after Select") |
| 149 | + } |
| 150 | + tupleValueIsId(t, respSel.Data, id) |
| 151 | +} |
0 commit comments