|
| 1 | +package datetime_test |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "bytes" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + . "github.com/tarantool/go-tarantool" |
| 12 | + "github.com/tarantool/go-tarantool/datetime" |
| 13 | + "github.com/tarantool/go-tarantool/test_helpers" |
| 14 | + "gopkg.in/vmihailenco/msgpack.v2" |
| 15 | +) |
| 16 | + |
| 17 | +// There is no way to skip tests in testing.M, |
| 18 | +// so we use this variable to pass info |
| 19 | +// to each testing.T that it should skip. |
| 20 | +var isDatetimeSupported = false |
| 21 | + |
| 22 | +var server = "127.0.0.1:3013" |
| 23 | +var opts = Opts{ |
| 24 | + Timeout: 500 * time.Millisecond, |
| 25 | + User: "test", |
| 26 | + Pass: "test", |
| 27 | +} |
| 28 | + |
| 29 | +var space = "testDatetime" |
| 30 | +var index = "primary" |
| 31 | + |
| 32 | +type TupleDatetime struct { |
| 33 | + tm datetime.EventTime |
| 34 | +} |
| 35 | + |
| 36 | +func (t *TupleDatetime) DecodeMsgpack(d *msgpack.Decoder) error { |
| 37 | + var err error |
| 38 | + var l int |
| 39 | + if l, err = d.DecodeSliceLen(); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + if l != 1 { |
| 43 | + return fmt.Errorf("array len doesn't match: %d", l) |
| 44 | + } |
| 45 | + |
| 46 | + res, err := d.DecodeInterface() |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + t.tm = res.(datetime.EventTime) |
| 51 | + |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func connectWithValidation(t *testing.T) *Connection { |
| 56 | + conn, err := Connect(server, opts) |
| 57 | + if err != nil { |
| 58 | + t.Errorf("Failed to connect: %s", err.Error()) |
| 59 | + } |
| 60 | + if conn == nil { |
| 61 | + t.Errorf("conn is nil after Connect") |
| 62 | + } |
| 63 | + return conn |
| 64 | +} |
| 65 | + |
| 66 | +func tupleValueIsDatetime(t *testing.T, tuples []interface{}, tm datetime.EventTime) { |
| 67 | + if tpl, ok := tuples[0].([]interface{}); !ok { |
| 68 | + t.Errorf("Unexpected return value body") |
| 69 | + } else { |
| 70 | + if len(tpl) != 1 { |
| 71 | + t.Errorf("Unexpected return value body (tuple len)") |
| 72 | + } |
| 73 | + if val, ok := tpl[0].(datetime.EventTime); !ok || val != tm { |
| 74 | + t.Errorf("Unexpected return value body (tuple 0 field)") |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestEncodeDecode(t *testing.T) { |
| 80 | + t.Skip("Not imeplemented") |
| 81 | + if isDatetimeSupported == false { |
| 82 | + t.Skip("Skipping test for Tarantool without datetime support in msgpack") |
| 83 | + } |
| 84 | + |
| 85 | + //e := msgpack.NewEncoder(w io.Writer) |
| 86 | + /* |
| 87 | + r := bytes.NewReader(byteData) |
| 88 | + d := msgpack.NewDecoder(r) |
| 89 | + var tm datetime.EventTime |
| 90 | + //func (t *TupleDatetime) DecodeMsgpack(d *msgpack.Decoder) error { |
| 91 | + var err error |
| 92 | + var l int |
| 93 | + if l, err = d.DecodeSliceLen(); err != nil { |
| 94 | + t.Errorf("Datetime decode failed") |
| 95 | + } |
| 96 | + if l != 1 { |
| 97 | + t.Errorf("array len doesn't match: %d", l) |
| 98 | + } |
| 99 | +
|
| 100 | + res, err := d.DecodeInterface() |
| 101 | + if err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + t.tm = res.(datetime.EventTime) |
| 105 | + */ |
| 106 | +} |
| 107 | + |
| 108 | +func TestSelect(t *testing.T) { |
| 109 | + if isDatetimeSupported == false { |
| 110 | + t.Skip("Skipping test for Tarantool without datetime support in msgpack") |
| 111 | + } |
| 112 | + |
| 113 | + conn := connectWithValidation(t) |
| 114 | + defer conn.Close() |
| 115 | + |
| 116 | + tm := datetime.EventTime{0, 0, 0, 0} |
| 117 | + |
| 118 | + var offset uint32 = 0 |
| 119 | + var limit uint32 = 1 |
| 120 | + resp, errSel := conn.Select(space, index, offset, limit, IterEq, []interface{}{tm}) |
| 121 | + if errSel != nil { |
| 122 | + t.Errorf("Datetime select failed: %s", errSel.Error()) |
| 123 | + } |
| 124 | + if resp == nil { |
| 125 | + t.Errorf("Response is nil after Select") |
| 126 | + } |
| 127 | + tupleValueIsDatetime(t, resp.Data, tm) |
| 128 | + |
| 129 | + var tuples []TupleDatetime |
| 130 | + errTyp := conn.SelectTyped(space, index, 0, 1, IterEq, []interface{}{tm}, &tuples) |
| 131 | + if errTyp != nil { |
| 132 | + t.Errorf("Failed to SelectTyped: %s", errTyp.Error()) |
| 133 | + } |
| 134 | + if len(tuples) != 1 { |
| 135 | + t.Errorf("Result len of SelectTyped != 1") |
| 136 | + } |
| 137 | + if tuples[0].tm != tm { |
| 138 | + t.Errorf("Bad value loaded from SelectTyped: %d", tuples[0].tm.Seconds) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestReplace(t *testing.T) { |
| 143 | + if isDatetimeSupported == false { |
| 144 | + t.Skip("Skipping test for Tarantool without datetime support in msgpack") |
| 145 | + } |
| 146 | + |
| 147 | + conn := connectWithValidation(t) |
| 148 | + defer conn.Close() |
| 149 | + |
| 150 | + tm := datetime.EventTime{15, 0, 0, 0} |
| 151 | + |
| 152 | + respRep, errRep := conn.Replace(space, []interface{}{tm}) |
| 153 | + if errRep != nil { |
| 154 | + t.Errorf("Datetime replace failed: %s", errRep) |
| 155 | + } |
| 156 | + if respRep == nil { |
| 157 | + t.Errorf("Response is nil after Replace") |
| 158 | + } |
| 159 | + tupleValueIsDatetime(t, respRep.Data, tm) |
| 160 | + |
| 161 | + respSel, errSel := conn.Select(space, index, 0, 1, IterEq, []interface{}{tm}) |
| 162 | + if errSel != nil { |
| 163 | + t.Errorf("Datetime select failed: %s", errSel) |
| 164 | + } |
| 165 | + if respSel == nil { |
| 166 | + t.Errorf("Response is nil after Select") |
| 167 | + } |
| 168 | + tupleValueIsDatetime(t, respSel.Data, tm) |
| 169 | +} |
| 170 | + |
| 171 | +// runTestMain is a body of TestMain function |
| 172 | +// (see https://pkg.go.dev/testing#hdr-Main). |
| 173 | +// Using defer + os.Exit is not works so TestMain body |
| 174 | +// is a separate function, see |
| 175 | +// https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls |
| 176 | +func runTestMain(m *testing.M) int { |
| 177 | + isLess, err := test_helpers.IsTarantoolVersionLess(2, 2, 0) |
| 178 | + if err != nil { |
| 179 | + log.Fatalf("Failed to extract Tarantool version: %s", err) |
| 180 | + } |
| 181 | + |
| 182 | + if isLess { |
| 183 | + log.Println("Skipping datetime tests...") |
| 184 | + isDatetimeSupported = false |
| 185 | + return m.Run() |
| 186 | + } else { |
| 187 | + isDatetimeSupported = true |
| 188 | + } |
| 189 | + |
| 190 | + instance, err := test_helpers.StartTarantool(test_helpers.StartOpts{ |
| 191 | + InitScript: "config.lua", |
| 192 | + Listen: server, |
| 193 | + WorkDir: "work_dir", |
| 194 | + User: opts.User, |
| 195 | + Pass: opts.Pass, |
| 196 | + WaitStart: 100 * time.Millisecond, |
| 197 | + ConnectRetry: 3, |
| 198 | + RetryTimeout: 500 * time.Millisecond, |
| 199 | + }) |
| 200 | + defer test_helpers.StopTarantoolWithCleanup(instance) |
| 201 | + |
| 202 | + if err != nil { |
| 203 | + log.Fatalf("Failed to prepare test Tarantool: %s", err) |
| 204 | + } |
| 205 | + |
| 206 | + return m.Run() |
| 207 | +} |
| 208 | + |
| 209 | +func TestMain(m *testing.M) { |
| 210 | + code := runTestMain(m) |
| 211 | + os.Exit(code) |
| 212 | +} |
0 commit comments