nulltime_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 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. "database/sql"
  11. "database/sql/driver"
  12. "testing"
  13. "time"
  14. )
  15. var (
  16. // Check implementation of interfaces
  17. _ driver.Valuer = NullTime{}
  18. _ sql.Scanner = (*NullTime)(nil)
  19. )
  20. func TestScanNullTime(t *testing.T) {
  21. var scanTests = []struct {
  22. in interface{}
  23. error bool
  24. valid bool
  25. time time.Time
  26. }{
  27. {tDate, false, true, tDate},
  28. {sDate, false, true, tDate},
  29. {[]byte(sDate), false, true, tDate},
  30. {tDateTime, false, true, tDateTime},
  31. {sDateTime, false, true, tDateTime},
  32. {[]byte(sDateTime), false, true, tDateTime},
  33. {tDate0, false, true, tDate0},
  34. {sDate0, false, true, tDate0},
  35. {[]byte(sDate0), false, true, tDate0},
  36. {sDateTime0, false, true, tDate0},
  37. {[]byte(sDateTime0), false, true, tDate0},
  38. {"", true, false, tDate0},
  39. {"1234", true, false, tDate0},
  40. {0, true, false, tDate0},
  41. }
  42. var nt = NullTime{}
  43. var err error
  44. for _, tst := range scanTests {
  45. err = nt.Scan(tst.in)
  46. if (err != nil) != tst.error {
  47. t.Errorf("%v: expected error status %t, got %t", tst.in, tst.error, (err != nil))
  48. }
  49. if nt.Valid != tst.valid {
  50. t.Errorf("%v: expected valid status %t, got %t", tst.in, tst.valid, nt.Valid)
  51. }
  52. if nt.Time != tst.time {
  53. t.Errorf("%v: expected time %v, got %v", tst.in, tst.time, nt.Time)
  54. }
  55. }
  56. }