forked from rickb777/date
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_test.go
133 lines (118 loc) · 2.72 KB
/
sql_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2015 Rick Beton. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package date
import (
"database/sql/driver"
"testing"
)
func TestDate_Scan(t *testing.T) {
cases := []struct {
v interface{}
expected PeriodOfDays
}{
{int64(0), 0},
{int64(1000), 1000},
{int64(10000), 10000},
{int64(0), 0},
{int64(1000), 1000},
{int64(10000), 10000},
{"0", 0},
{"1000", 1000},
{"10000", 10000},
{"2018-12-31", 17896},
{"31/12/2018", 17896},
{[]byte("10000"), 10000},
{PeriodOfDays(10000).Date().Local(), 10000},
}
for i, c := range cases {
r := new(Date)
e := r.Scan(c.v)
if e != nil {
t.Errorf("%d: Got %v for %d", i, e, c.expected)
}
if r.DaysSinceEpoch() != c.expected {
t.Errorf("%d: Got %v, want %d", i, *r, c.expected)
}
var d driver.Valuer = *r
q, e := d.Value()
if e != nil {
t.Errorf("%d: Got %v for %d", i, e, c.expected)
}
if q.(int64) != int64(c.expected) {
t.Errorf("%d: Got %v, want %d", i, q, c.expected)
}
}
}
func TestDateString_Scan(t *testing.T) {
cases := []struct {
v interface{}
expected string
}{
{int64(0), "1970-01-01"},
{int64(15000), "2011-01-26"},
{"0", "1970-01-01"},
{"15000", "2011-01-26"},
{"2018-12-31", "2018-12-31"},
{"31/12/2018", "2018-12-31"},
//{[]byte("10000"), ""},
//{PeriodOfDays(10000).Date().Local(), ""},
}
for i, c := range cases {
r := new(DateString)
e := r.Scan(c.v)
if e != nil {
t.Errorf("%d: Got %v for %s", i, e, c.expected)
}
if r.Date().String() != c.expected {
t.Errorf("%d: Got %v, want %s", i, r.Date(), c.expected)
}
var d driver.Valuer = *r
q, e := d.Value()
if e != nil {
t.Errorf("%d: Got %v for %s", i, e, c.expected)
}
if q.(string) != c.expected {
t.Errorf("%d: Got %v, want %s", i, q, c.expected)
}
}
}
func TestDate_Scan_with_junk(t *testing.T) {
cases := []struct {
v interface{}
expected string
}{
{true, "bool true is not a meaningful date"},
{true, "bool true is not a meaningful date"},
}
for i, c := range cases {
r := new(Date)
e := r.Scan(c.v)
if e.Error() != c.expected {
t.Errorf("%d: Got %q, want %q", i, e.Error(), c.expected)
}
}
}
func TestDateString_Scan_with_junk(t *testing.T) {
cases := []struct {
v interface{}
expected string
}{
{true, "bool true is not a meaningful date"},
{true, "bool true is not a meaningful date"},
}
for i, c := range cases {
r := new(DateString)
e := r.Scan(c.v)
if e.Error() != c.expected {
t.Errorf("%d: Got %q, want %q", i, e.Error(), c.expected)
}
}
}
func TestDate_Scan_with_nil(t *testing.T) {
var r *Date
e := r.Scan(nil)
if e != nil {
t.Errorf("Got %v", e)
}
}