statement_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "bytes"
  11. "database/sql/driver"
  12. "encoding/json"
  13. "testing"
  14. )
  15. func TestConvertDerivedString(t *testing.T) {
  16. type derived string
  17. output, err := converter{}.ConvertValue(derived("value"))
  18. if err != nil {
  19. t.Fatal("Derived string type not convertible", err)
  20. }
  21. if output != "value" {
  22. t.Fatalf("Derived string type not converted, got %#v %T", output, output)
  23. }
  24. }
  25. func TestConvertDerivedByteSlice(t *testing.T) {
  26. type derived []uint8
  27. output, err := converter{}.ConvertValue(derived("value"))
  28. if err != nil {
  29. t.Fatal("Byte slice not convertible", err)
  30. }
  31. if bytes.Compare(output.([]byte), []byte("value")) != 0 {
  32. t.Fatalf("Byte slice not converted, got %#v %T", output, output)
  33. }
  34. }
  35. func TestConvertDerivedUnsupportedSlice(t *testing.T) {
  36. type derived []int
  37. _, err := converter{}.ConvertValue(derived{1})
  38. if err == nil || err.Error() != "unsupported type mysql.derived, a slice of int" {
  39. t.Fatal("Unexpected error", err)
  40. }
  41. }
  42. func TestConvertDerivedBool(t *testing.T) {
  43. type derived bool
  44. output, err := converter{}.ConvertValue(derived(true))
  45. if err != nil {
  46. t.Fatal("Derived bool type not convertible", err)
  47. }
  48. if output != true {
  49. t.Fatalf("Derived bool type not converted, got %#v %T", output, output)
  50. }
  51. }
  52. func TestConvertPointer(t *testing.T) {
  53. str := "value"
  54. output, err := converter{}.ConvertValue(&str)
  55. if err != nil {
  56. t.Fatal("Pointer type not convertible", err)
  57. }
  58. if output != "value" {
  59. t.Fatalf("Pointer type not converted, got %#v %T", output, output)
  60. }
  61. }
  62. func TestConvertSignedIntegers(t *testing.T) {
  63. values := []interface{}{
  64. int8(-42),
  65. int16(-42),
  66. int32(-42),
  67. int64(-42),
  68. int(-42),
  69. }
  70. for _, value := range values {
  71. output, err := converter{}.ConvertValue(value)
  72. if err != nil {
  73. t.Fatalf("%T type not convertible %s", value, err)
  74. }
  75. if output != int64(-42) {
  76. t.Fatalf("%T type not converted, got %#v %T", value, output, output)
  77. }
  78. }
  79. }
  80. type myUint64 struct {
  81. value uint64
  82. }
  83. func (u myUint64) Value() (driver.Value, error) {
  84. return u.value, nil
  85. }
  86. func TestConvertUnsignedIntegers(t *testing.T) {
  87. values := []interface{}{
  88. uint8(42),
  89. uint16(42),
  90. uint32(42),
  91. uint64(42),
  92. uint(42),
  93. myUint64{uint64(42)},
  94. }
  95. for _, value := range values {
  96. output, err := converter{}.ConvertValue(value)
  97. if err != nil {
  98. t.Fatalf("%T type not convertible %s", value, err)
  99. }
  100. if output != uint64(42) {
  101. t.Fatalf("%T type not converted, got %#v %T", value, output, output)
  102. }
  103. }
  104. output, err := converter{}.ConvertValue(^uint64(0))
  105. if err != nil {
  106. t.Fatal("uint64 high-bit not convertible", err)
  107. }
  108. if output != ^uint64(0) {
  109. t.Fatalf("uint64 high-bit converted, got %#v %T", output, output)
  110. }
  111. }
  112. func TestConvertJSON(t *testing.T) {
  113. raw := json.RawMessage("{}")
  114. out, err := converter{}.ConvertValue(raw)
  115. if err != nil {
  116. t.Fatal("json.RawMessage was failed in convert", err)
  117. }
  118. if _, ok := out.(json.RawMessage); !ok {
  119. t.Fatalf("json.RawMessage converted, got %#v %T", out, out)
  120. }
  121. }