statement_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. "testing"
  12. )
  13. func TestConvertDerivedString(t *testing.T) {
  14. type derived string
  15. output, err := converter{}.ConvertValue(derived("value"))
  16. if err != nil {
  17. t.Fatal("Derived string type not convertible", err)
  18. }
  19. if output != "value" {
  20. t.Fatalf("Derived string type not converted, got %#v %T", output, output)
  21. }
  22. }
  23. func TestConvertDerivedByteSlice(t *testing.T) {
  24. type derived []uint8
  25. output, err := converter{}.ConvertValue(derived("value"))
  26. if err != nil {
  27. t.Fatal("Byte slice not convertible", err)
  28. }
  29. if bytes.Compare(output.([]byte), []byte("value")) != 0 {
  30. t.Fatalf("Byte slice not converted, got %#v %T", output, output)
  31. }
  32. }
  33. func TestConvertDerivedUnsupportedSlice(t *testing.T) {
  34. type derived []int
  35. _, err := converter{}.ConvertValue(derived{1})
  36. if err == nil || err.Error() != "unsupported type mysql.derived, a slice of int" {
  37. t.Fatal("Unexpected error", err)
  38. }
  39. }
  40. func TestConvertDerivedBool(t *testing.T) {
  41. type derived bool
  42. output, err := converter{}.ConvertValue(derived(true))
  43. if err != nil {
  44. t.Fatal("Derived bool type not convertible", err)
  45. }
  46. if output != true {
  47. t.Fatalf("Derived bool type not converted, got %#v %T", output, output)
  48. }
  49. }
  50. func TestConvertPointer(t *testing.T) {
  51. str := "value"
  52. output, err := converter{}.ConvertValue(&str)
  53. if err != nil {
  54. t.Fatal("Pointer type not convertible", err)
  55. }
  56. if output != "value" {
  57. t.Fatalf("Pointer type not converted, got %#v %T", output, output)
  58. }
  59. }
  60. func TestConvertSignedIntegers(t *testing.T) {
  61. values := []interface{}{
  62. int8(-42),
  63. int16(-42),
  64. int32(-42),
  65. int64(-42),
  66. int(-42),
  67. }
  68. for _, value := range values {
  69. output, err := converter{}.ConvertValue(value)
  70. if err != nil {
  71. t.Fatalf("%T type not convertible %s", value, err)
  72. }
  73. if output != int64(-42) {
  74. t.Fatalf("%T type not converted, got %#v %T", value, output, output)
  75. }
  76. }
  77. }
  78. func TestConvertUnsignedIntegers(t *testing.T) {
  79. values := []interface{}{
  80. uint8(42),
  81. uint16(42),
  82. uint32(42),
  83. uint64(42),
  84. uint(42),
  85. }
  86. for _, value := range values {
  87. output, err := converter{}.ConvertValue(value)
  88. if err != nil {
  89. t.Fatalf("%T type not convertible %s", value, err)
  90. }
  91. if output != uint64(42) {
  92. t.Fatalf("%T type not converted, got %#v %T", value, output, output)
  93. }
  94. }
  95. output, err := converter{}.ConvertValue(^uint64(0))
  96. if err != nil {
  97. t.Fatal("uint64 high-bit not convertible", err)
  98. }
  99. if output != ^uint64(0) {
  100. t.Fatalf("uint64 high-bit converted, got %#v %T", output, output)
  101. }
  102. }