connection_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2016 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. "context"
  11. "database/sql/driver"
  12. "testing"
  13. )
  14. func TestInterpolateParams(t *testing.T) {
  15. mc := &mysqlConn{
  16. buf: newBuffer(nil),
  17. maxAllowedPacket: maxPacketSize,
  18. cfg: &Config{
  19. InterpolateParams: true,
  20. },
  21. }
  22. q, err := mc.interpolateParams("SELECT ?+?", []driver.Value{int64(42), "gopher"})
  23. if err != nil {
  24. t.Errorf("Expected err=nil, got %#v", err)
  25. return
  26. }
  27. expected := `SELECT 42+'gopher'`
  28. if q != expected {
  29. t.Errorf("Expected: %q\nGot: %q", expected, q)
  30. }
  31. }
  32. func TestInterpolateParamsTooManyPlaceholders(t *testing.T) {
  33. mc := &mysqlConn{
  34. buf: newBuffer(nil),
  35. maxAllowedPacket: maxPacketSize,
  36. cfg: &Config{
  37. InterpolateParams: true,
  38. },
  39. }
  40. q, err := mc.interpolateParams("SELECT ?+?", []driver.Value{int64(42)})
  41. if err != driver.ErrSkip {
  42. t.Errorf("Expected err=driver.ErrSkip, got err=%#v, q=%#v", err, q)
  43. }
  44. }
  45. // We don't support placeholder in string literal for now.
  46. // https://github.com/go-sql-driver/mysql/pull/490
  47. func TestInterpolateParamsPlaceholderInString(t *testing.T) {
  48. mc := &mysqlConn{
  49. buf: newBuffer(nil),
  50. maxAllowedPacket: maxPacketSize,
  51. cfg: &Config{
  52. InterpolateParams: true,
  53. },
  54. }
  55. q, err := mc.interpolateParams("SELECT 'abc?xyz',?", []driver.Value{int64(42)})
  56. // When InterpolateParams support string literal, this should return `"SELECT 'abc?xyz', 42`
  57. if err != driver.ErrSkip {
  58. t.Errorf("Expected err=driver.ErrSkip, got err=%#v, q=%#v", err, q)
  59. }
  60. }
  61. func TestCheckNamedValue(t *testing.T) {
  62. value := driver.NamedValue{Value: ^uint64(0)}
  63. x := &mysqlConn{}
  64. err := x.CheckNamedValue(&value)
  65. if err != nil {
  66. t.Fatal("uint64 high-bit not convertible", err)
  67. }
  68. if value.Value != "18446744073709551615" {
  69. t.Fatalf("uint64 high-bit not converted, got %#v %T", value.Value, value.Value)
  70. }
  71. }
  72. // TestCleanCancel tests passed context is cancelled at start.
  73. // No packet should be sent. Connection should keep current status.
  74. func TestCleanCancel(t *testing.T) {
  75. mc := &mysqlConn{
  76. closech: make(chan struct{}),
  77. }
  78. mc.startWatcher()
  79. defer mc.cleanup()
  80. ctx, cancel := context.WithCancel(context.Background())
  81. cancel()
  82. for i := 0; i < 3; i++ { // Repeat same behavior
  83. err := mc.Ping(ctx)
  84. if err != context.Canceled {
  85. t.Errorf("expected context.Canceled, got %#v", err)
  86. }
  87. if mc.closed.IsSet() {
  88. t.Error("expected mc is not closed, closed actually")
  89. }
  90. if mc.watching {
  91. t.Error("expected watching is false, but true")
  92. }
  93. }
  94. }