|
| 1 | +package decimal_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/shopspring/decimal" |
| 13 | + . "github.com/tarantool/go-tarantool" |
| 14 | + . "github.com/tarantool/go-tarantool/decimal" |
| 15 | + "github.com/tarantool/go-tarantool/test_helpers" |
| 16 | + "gopkg.in/vmihailenco/msgpack.v2" |
| 17 | +) |
| 18 | + |
| 19 | +// There is no way to skip tests in testing.M, |
| 20 | +// so we use this variable to pass info |
| 21 | +// to each testing.T that it should skip. |
| 22 | +var isDecimalSupported = false |
| 23 | + |
| 24 | +var server = "127.0.0.1:3013" |
| 25 | +var opts = Opts{ |
| 26 | + Timeout: 500 * time.Millisecond, |
| 27 | + User: "test", |
| 28 | + Pass: "test", |
| 29 | +} |
| 30 | + |
| 31 | +var space = "testDecimal" |
| 32 | +var index = "primary" |
| 33 | + |
| 34 | +type TupleDecimal struct { |
| 35 | + number decimal.Decimal |
| 36 | +} |
| 37 | + |
| 38 | +/* |
| 39 | +var decNumbers = []struct { |
| 40 | + num string |
| 41 | + hex []byte |
| 42 | +}{ |
| 43 | + {"-12.34", "d6010201234d"}, |
| 44 | + {"123.456789000000000", "c70b010f0123456789000000000c"}, |
| 45 | + {"0", "d501000c"}, |
| 46 | + {"-0", "d501000d"}, |
| 47 | + {"1", "d501001c"}, |
| 48 | + {"-1", "d501001d"}, |
| 49 | + {"0.1", "d501011c"}, |
| 50 | + {"-0.1", "d501011d"}, |
| 51 | + {"2.718281828459045", "c70a010f02718281828459045c"}, |
| 52 | + {"-2.718281828459045", "c70a010f02718281828459045d"}, |
| 53 | + {"3.141592653589793", "c70a010f03141592653589793c"}, |
| 54 | + {"-3.141592653589793", "c70a010f03141592653589793d"}, |
| 55 | + {"1234567891234567890.0987654321987654321", "c7150113012345678912345678900987654321987654321c"}, |
| 56 | + {"-1234567891234567890.0987654321987654321", "c7150113012345678912345678900987654321987654321d"}, |
| 57 | + {"0.0000000000000000000000000000000000001", "d501251c"}, |
| 58 | + {"-0.0000000000000000000000000000000000001", "d501251d"}, |
| 59 | + {"0.00000000000000000000000000000000000001", "d501261c"}, |
| 60 | + {"-0.00000000000000000000000000000000000001", "d501261d"}, |
| 61 | + {"99999999999999999999999999999999999999", "c7150100099999999999999999999999999999999999999c"}, |
| 62 | + {"-99999999999999999999999999999999999999", "c7150100099999999999999999999999999999999999999d"}, |
| 63 | +} |
| 64 | +*/ |
| 65 | + |
| 66 | +func (t *TupleDecimal) DecodeMsgpack(d *msgpack.Decoder) error { |
| 67 | + var err error |
| 68 | + var l int |
| 69 | + if l, err = d.DecodeSliceLen(); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + if l != 1 { |
| 73 | + return fmt.Errorf("Array length doesn't match: %d", l) |
| 74 | + } |
| 75 | + |
| 76 | + res, err := d.DecodeInterface() |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + t.number = res.(decimal.Decimal) |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func connectWithValidation(t *testing.T) *Connection { |
| 86 | + conn, err := Connect(server, opts) |
| 87 | + if err != nil { |
| 88 | + t.Fatalf("Failed to connect: %s", err.Error()) |
| 89 | + } |
| 90 | + if conn == nil { |
| 91 | + t.Fatalf("conn is nil after Connect") |
| 92 | + } |
| 93 | + return conn |
| 94 | +} |
| 95 | + |
| 96 | +func tupleValueIsDecimal(t *testing.T, tuples []interface{}, number decimal.Decimal) { |
| 97 | + if len(tuples) != 1 { |
| 98 | + t.Fatalf("Response Data len (%d) != 1", len(tuples)) |
| 99 | + } |
| 100 | + |
| 101 | + if tpl, ok := tuples[0].([]interface{}); !ok { |
| 102 | + t.Fatalf("Unexpected return value body") |
| 103 | + } else { |
| 104 | + if len(tpl) != 1 { |
| 105 | + t.Fatalf("Unexpected return value body (tuple len)") |
| 106 | + } |
| 107 | + if val, ok := tpl[0].(decimal.Decimal); !ok || !val.Equal(number) { |
| 108 | + t.Fatalf("Unexpected return value body (tuple 0 field)") |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +var decimalBCD = []struct { |
| 114 | + bcd []byte |
| 115 | + num string |
| 116 | +}{ |
| 117 | + {[]byte{0x02, 0x01, 0x23, 0x4b}, "-12.34"}, |
| 118 | + {[]byte{0x0, 0xa}, "0"}, // WRONG 00000a |
| 119 | + {[]byte{0x0, 0xb}, "-0"}, // WRONG 00000b |
| 120 | + {[]byte{0x0, 0x1a}, "1"}, // WRONG 00010a |
| 121 | + {[]byte{0x0, 0x1b}, "-1"}, // WRONG 00010b |
| 122 | + {[]byte{0x1, 0x1a}, "0.1"}, // WRONG 00010a |
| 123 | + {[]byte{0x1, 0x1b}, "-0.1"}, // WRONG 00010b |
| 124 | + {[]byte{0x25, 0x1a}, "0.0000000000000000000000000000000000001"}, // WRONG 25000000000000000000000000000000000000001a |
| 125 | + {[]byte{0x25, 0x1b}, "-0.0000000000000000000000000000000000001"}, // WRONG 25000000000000000000000000000000000000001b |
| 126 | + {[]byte{0x26, 0x1a}, "0.00000000000000000000000000000000000001"}, // WRONG 2600000000000000000000000000000000000000010a |
| 127 | + {[]byte{0x26, 0x1b}, "-0.00000000000000000000000000000000000001"}, // WRONG 2600000000000000000000000000000000000000010b |
| 128 | + {[]byte{0x03, 0x01, 0x24, 0x01, 0x0c}, "0.000000000000000000000000000000000010"}, |
| 129 | + // {[]byte{0xc70b010f0123456789000000000c}, "123.456789000000000"}, |
| 130 | + // {[]byte{0xc70a010f02718281828459045c}, "2.718281828459045"}, |
| 131 | + // {[]byte{0xc70a010f02718281828459045d}, "-2.718281828459045"}, |
| 132 | + // {[]byte{0xc70a010f03141592653589793c}, "3.141592653589793"}, |
| 133 | + // {[]byte{0xc70a010f03141592653589793d}, "-3.141592653589793"}, |
| 134 | + // {[]byte{0xc7150100099999999999999999999999999999999999999c}, "99999999999999999999999999999999999999"}, |
| 135 | + // {[]byte{0xc7150100099999999999999999999999999999999999999d}, "-99999999999999999999999999999999999999"}, |
| 136 | + // {[]byte{0xc7150113012345678912345678900987654321987654321c}, "1234567891234567890.0987654321987654321"}, |
| 137 | + // {[]byte{0xc7150113012345678912345678900987654321987654321d}, "-1234567891234567890.0987654321987654321"}, |
| 138 | +} |
| 139 | + |
| 140 | +func TestMPEncodeNumberToBCD(t *testing.T) { |
| 141 | + for _, testcase := range decimalBCD { |
| 142 | + t.Run(testcase.num, func(t *testing.T) { |
| 143 | + buf := MPEncodeNumberToBCD(testcase.num) |
| 144 | + if reflect.DeepEqual(buf, testcase.bcd) != true { |
| 145 | + t.Fatalf("Failed to encode decimal %s to BCD (actual: %x, expected '%x)'", testcase.num, buf, testcase.bcd) |
| 146 | + t.Fatalf("Failed to encode decimal %s to BCD (actual: %x, expected '%x)'", testcase.num, buf, testcase.bcd) |
| 147 | + } |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestMPDecodeNumberFromBCD(t *testing.T) { |
| 153 | + for _, tt := range decimalBCD { |
| 154 | + t.Run(tt.num, func(t *testing.T) { |
| 155 | + dec_expected, err := decimal.NewFromString(tt.num) |
| 156 | + if err != nil { |
| 157 | + t.Fatalf("Failed to encode string to decimal") |
| 158 | + } |
| 159 | + num, err := MPDecodeNumberFromBCD(tt.bcd) |
| 160 | + if err != nil { |
| 161 | + t.Fatalf("Failed to decode decimal (%s) from BCD (%x) - '%x'", tt.num, tt.bcd, num) |
| 162 | + } |
| 163 | + dec_actual, err := decimal.NewFromString(strings.Join(num, "")) |
| 164 | + if err != nil { |
| 165 | + t.Fatalf("Failed to encode string to decimal") |
| 166 | + } |
| 167 | + if !dec_expected.Equal(dec_actual) { |
| 168 | + t.Fatalf("Failed to decode decimal (%s) from BCD (%x) - '%x'", tt.num, tt.bcd, num) |
| 169 | + } |
| 170 | + }) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func BenchmarkMPEncodeNumberToBCD(b *testing.B) { |
| 175 | + for n := 0; n < b.N; n++ { |
| 176 | + MPEncodeNumberToBCD("-12.34") |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +func BenchmarkMPDecodeNumberFromBCD(b *testing.B) { |
| 181 | + buf := []byte{0x02, 0x01, 0x23, 0x4b} |
| 182 | + for n := 0; n < b.N; n++ { |
| 183 | + MPDecodeNumberFromBCD(buf) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func TestSelect(t *testing.T) { |
| 188 | + t.Skip("Broken") |
| 189 | + |
| 190 | + if isDecimalSupported == false { |
| 191 | + t.Skip("Skipping test for Tarantool without decimal support in msgpack") |
| 192 | + } |
| 193 | + |
| 194 | + conn := connectWithValidation(t) |
| 195 | + defer conn.Close() |
| 196 | + |
| 197 | + number, err := decimal.NewFromString("-12.34") |
| 198 | + if err != nil { |
| 199 | + t.Fatalf("Failed to prepare test decimal: %s", err) |
| 200 | + } |
| 201 | + |
| 202 | + var offset uint32 = 0 |
| 203 | + var limit uint32 = 1 |
| 204 | + resp, err := conn.Select(space, index, offset, limit, IterEq, []interface{}{number}) |
| 205 | + if err != nil { |
| 206 | + t.Fatalf("Decimal select failed: %s", err.Error()) |
| 207 | + } |
| 208 | + if resp == nil { |
| 209 | + t.Fatalf("Response is nil after Select") |
| 210 | + } |
| 211 | + fmt.Println(resp.Data) |
| 212 | + tupleValueIsDecimal(t, resp.Data, number) |
| 213 | +} |
| 214 | + |
| 215 | +func TestInsert(t *testing.T) { |
| 216 | + if isDecimalSupported == false { |
| 217 | + t.Skip("Skipping test for Tarantool without decimal support in msgpack") |
| 218 | + } |
| 219 | + |
| 220 | + conn := connectWithValidation(t) |
| 221 | + defer conn.Close() |
| 222 | + |
| 223 | + number, err := decimal.NewFromString("-12.34") |
| 224 | + if err != nil { |
| 225 | + t.Fatalf("Failed to prepare test decimal: %s", err) |
| 226 | + } |
| 227 | + |
| 228 | + resp, err := conn.Insert(space, []interface{}{number}) |
| 229 | + if err != nil { |
| 230 | + t.Fatalf("Decimal replace failed: %s", err) |
| 231 | + } |
| 232 | + if resp == nil { |
| 233 | + t.Fatalf("Response is nil after Replace") |
| 234 | + } |
| 235 | + tupleValueIsDecimal(t, resp.Data, number) |
| 236 | + |
| 237 | + resp, err = conn.Delete(space, index, []interface{}{number}) |
| 238 | + if err != nil { |
| 239 | + t.Fatalf("Decimal delete failed: %s", err) |
| 240 | + } |
| 241 | + tupleValueIsDecimal(t, resp.Data, number) |
| 242 | +} |
| 243 | + |
| 244 | +func TestReplace(t *testing.T) { |
| 245 | + if isDecimalSupported == false { |
| 246 | + t.Skip("Skipping test for Tarantool without decimal support in msgpack") |
| 247 | + } |
| 248 | + |
| 249 | + conn := connectWithValidation(t) |
| 250 | + defer conn.Close() |
| 251 | + |
| 252 | + number, err := decimal.NewFromString("-12.34") |
| 253 | + if err != nil { |
| 254 | + t.Fatalf("Failed to prepare test decimal: %s", err) |
| 255 | + } |
| 256 | + |
| 257 | + respRep, errRep := conn.Replace(space, []interface{}{number}) |
| 258 | + if errRep != nil { |
| 259 | + t.Fatalf("Decimal replace failed: %s", errRep) |
| 260 | + } |
| 261 | + if respRep == nil { |
| 262 | + t.Fatalf("Response is nil after Replace") |
| 263 | + } |
| 264 | + tupleValueIsDecimal(t, respRep.Data, number) |
| 265 | + |
| 266 | + respSel, errSel := conn.Select(space, index, 0, 1, IterEq, []interface{}{number}) |
| 267 | + if errSel != nil { |
| 268 | + t.Fatalf("Decimal select failed: %s", errSel) |
| 269 | + } |
| 270 | + if respSel == nil { |
| 271 | + t.Fatalf("Response is nil after Select") |
| 272 | + } |
| 273 | + tupleValueIsDecimal(t, respSel.Data, number) |
| 274 | +} |
| 275 | + |
| 276 | +// runTestMain is a body of TestMain function |
| 277 | +// (see https://pkg.go.dev/testing#hdr-Main). |
| 278 | +// Using defer + os.Exit is not works so TestMain body |
| 279 | +// is a separate function, see |
| 280 | +// https://stackoverflow.com/questions/27629380/how-to-exit-a-go-program-honoring-deferred-calls |
| 281 | +func runTestMain(m *testing.M) int { |
| 282 | + isLess, err := test_helpers.IsTarantoolVersionLess(2, 2, 0) |
| 283 | + if err != nil { |
| 284 | + log.Fatalf("Failed to extract Tarantool version: %s", err) |
| 285 | + } |
| 286 | + |
| 287 | + if isLess { |
| 288 | + log.Println("Skipping decimal tests...") |
| 289 | + isDecimalSupported = false |
| 290 | + return m.Run() |
| 291 | + } else { |
| 292 | + isDecimalSupported = true |
| 293 | + } |
| 294 | + |
| 295 | + instance, err := test_helpers.StartTarantool(test_helpers.StartOpts{ |
| 296 | + InitScript: "config.lua", |
| 297 | + Listen: server, |
| 298 | + WorkDir: "work_dir", |
| 299 | + User: opts.User, |
| 300 | + Pass: opts.Pass, |
| 301 | + WaitStart: 100 * time.Millisecond, |
| 302 | + ConnectRetry: 3, |
| 303 | + RetryTimeout: 500 * time.Millisecond, |
| 304 | + }) |
| 305 | + defer test_helpers.StopTarantoolWithCleanup(instance) |
| 306 | + |
| 307 | + if err != nil { |
| 308 | + log.Fatalf("Failed to prepare test Tarantool: %s", err) |
| 309 | + } |
| 310 | + |
| 311 | + return m.Run() |
| 312 | +} |
| 313 | + |
| 314 | +func TestMain(m *testing.M) { |
| 315 | + code := runTestMain(m) |
| 316 | + os.Exit(code) |
| 317 | +} |
0 commit comments