forked from patrickxb/fgosqlite
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdate_test.go
257 lines (210 loc) · 7.27 KB
/
date_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlite_test
import (
"testing"
"time"
"github.com/bmizerany/assert"
. "github.com/gwenn/gosqlite"
)
func TestJulianDay(t *testing.T) {
utc := JulianDayToUTC(2440587.5)
if utc.Unix() != 0 {
t.Errorf("got %d; want %d ", utc.Unix(), 0)
}
now := time.Now()
r := JulianDayToLocalTime(JulianDay(now))
if r.Unix() != now.Unix() { // FIXME Rounding problem?
t.Errorf("got %#v; want %#v", r, now)
}
}
func TestBindTime(t *testing.T) {
db := open(t)
defer checkClose(db, t)
var delta int
//err := db.OneValue("SELECT CAST(strftime('%s', 'now') AS NUMERIC) - ?", &delta, time.Now())
err := db.OneValue("SELECT datetime('now') - datetime(?)", &delta, time.Now())
checkNoError(t, err, "Error reading date: %#v")
if delta != 0 {
t.Errorf("Delta between Go and SQLite timestamps: %d", delta)
}
}
func TestScanTime(t *testing.T) {
db := open(t)
defer checkClose(db, t)
var dt time.Time
err := db.OneValue("SELECT date('now')", &dt)
checkNoError(t, err, "Error reading date: %#v")
if dt.IsZero() {
t.Error("Unexpected zero date")
}
var tm time.Time
err = db.OneValue("SELECT time('now')", &tm)
checkNoError(t, err, "Error reading date: %#v")
if tm.IsZero() {
t.Error("Unexpected zero time")
}
var dtm time.Time
err = db.OneValue("SELECT strftime('%Y-%m-%dT%H:%M:%f', 'now')", &dtm)
checkNoError(t, err, "Error reading date: %#v")
if dtm.IsZero() {
t.Error("Unexpected zero datetime")
}
var jd time.Time
err = db.OneValue("SELECT CAST(strftime('%J', 'now') AS NUMERIC)", &jd)
checkNoError(t, err, "Error reading date: %#v")
if jd.IsZero() {
t.Error("Unexpected zero julian day")
}
var unix time.Time
err = db.OneValue("SELECT CAST(strftime('%s', 'now') AS NUMERIC)", &unix)
checkNoError(t, err, "Error reading date: %#v")
if unix.IsZero() {
t.Error("Unexpected zero julian day")
}
}
func TestScanNullTime(t *testing.T) {
db := open(t)
defer checkClose(db, t)
var unix UnixTime
err := db.OneValue("SELECT NULL", &unix)
checkNoError(t, err, "Error scanning null time: %#v")
if !unix.IsZero() {
t.Error("Expected zero time")
}
}
func TestBindTimeAsString(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.Exec("CREATE TABLE test (time TEXT)")
checkNoError(t, err, "exec error: %s")
is, err := db.Prepare("INSERT INTO test (time) VALUES (?)")
checkNoError(t, err, "prepare error: %s")
defer checkFinalize(is, t)
now := time.Now()
//id1, err := is.Insert(YearMonthDay(now))
//checkNoError(t, err, "error inserting YearMonthDay: %s")
id2, err := is.Insert(TimeStamp{Time: now})
checkNoError(t, err, "error inserting TimeStamp: %s")
// The format used to persist has a max precision of 1ms.
now = now.Truncate(time.Millisecond)
var tim time.Time
//err = db.OneValue("SELECT /*date(*/time/*)*/ FROM test where ROWID = ?", &tim, id1)
//checkNoError(t, err, "error selecting YearMonthDay: %s")
//assert.Equal(t, now.Year(), tim.Year(), "year")
//assert.Equal(t, now.YearDay(), tim.YearDay(), "yearDay")
err = db.OneValue("SELECT /*datetime(*/time/*)*/ FROM test where ROWID = ?", &tim, id2)
checkNoError(t, err, "error selecting TimeStamp: %s")
if !now.Equal(tim) {
t.Errorf("got timeStamp: %s; want %s", tim, now)
}
}
func TestBindTimeAsNumeric(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.Exec("CREATE TABLE test (time NUMERIC)")
checkNoError(t, err, "exec error: %s")
is, err := db.Prepare("INSERT INTO test (time) VALUES (?)")
checkNoError(t, err, "prepare error: %s")
now := time.Now()
id1, err := is.Insert(UnixTime{Time: now})
checkNoError(t, err, "error inserting UnixTime: %s")
id2, err := is.Insert(JulianTime{Time: now})
checkNoError(t, err, "error inserting JulianTime: %s")
checkFinalize(is, t)
// the format used to persist has a max precision of 1s.
now = now.Truncate(time.Second)
var tim time.Time
err = db.OneValue("SELECT /*datetime(*/ time/*, 'unixepoch')*/ FROM test where ROWID = ?", &tim, id1)
checkNoError(t, err, "error selecting UnixTime: %s")
assert.Equal(t, now, tim)
err = db.OneValue("SELECT /*julianday(*/time/*)*/ FROM test where ROWID = ?", &tim, id2)
checkNoError(t, err, "error selecting JulianTime: %s")
assert.Equal(t, now, tim)
}
func TestJulianTime(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.Exec("CREATE TABLE test (time NUMERIC)")
checkNoError(t, err, "exec error: %s")
is, err := db.Prepare("INSERT INTO test (time) VALUES (?)")
checkNoError(t, err, "prepare error: %s")
now := time.Now()
id, err := is.Insert(JulianTime{Time: now})
checkNoError(t, err, "error inserting JulianTime: %s")
_, err = is.Insert(JulianTime{})
checkNoError(t, err, "error inserting JulianTime: %s")
checkFinalize(is, t)
// the format used to persist has a max precision of 1s.
now = now.Truncate(time.Second)
var jt JulianTime
err = db.OneValue("SELECT time FROM test where ROWID = ?", &jt, id)
checkNoError(t, err, "error selecting JulianTime: %s")
assert.Equal(t, now, jt.Time)
err = db.OneValue("SELECT null", &jt)
checkNoError(t, err, "%s")
assert.T(t, jt.IsZero())
err = db.OneValue("SELECT 0", &jt)
checkNoError(t, err, "%s")
err = db.OneValue("SELECT 'bim'", &jt)
assert.T(t, err != nil)
//println(err.Error())
}
func TestTimeStamp(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.Exec("CREATE TABLE test (time NUMERIC)")
checkNoError(t, err, "exec error: %s")
is, err := db.Prepare("INSERT INTO test (time) VALUES (?)")
checkNoError(t, err, "prepare error: %s")
now := time.Now()
id, err := is.Insert(TimeStamp{Time: now})
checkNoError(t, err, "error inserting TimeStamp: %s")
_, err = is.Insert(TimeStamp{})
checkNoError(t, err, "error inserting TimeStamp: %s")
checkFinalize(is, t)
// the format used to persist has a max precision of 1ms.
now = now.Truncate(time.Millisecond)
var ts TimeStamp
err = db.OneValue("SELECT time FROM test where ROWID = ?", &ts, id)
checkNoError(t, err, "error selecting TimeStamp: %s")
if !now.Equal(ts.Time) {
t.Errorf("got timeStamp: %s; want %s", ts, now)
}
err = db.OneValue("SELECT null", &ts)
checkNoError(t, err, "%s")
assert.T(t, ts.IsZero())
err = db.OneValue("SELECT 'bim'", &ts)
assert.T(t, err != nil)
//println(err.Error())
err = db.OneValue("SELECT 0", &ts)
assert.T(t, err != nil)
//println(err.Error())
}
func TestUnixTime(t *testing.T) {
db := open(t)
defer checkClose(db, t)
err := db.Exec("CREATE TABLE test (time NUMERIC)")
checkNoError(t, err, "exec error: %s")
is, err := db.Prepare("INSERT INTO test (time) VALUES (?)")
checkNoError(t, err, "prepare error: %s")
now := time.Now()
id, err := is.Insert(UnixTime{Time: now})
checkNoError(t, err, "error inserting UnixTime: %s")
_, err = is.Insert(UnixTime{})
checkNoError(t, err, "error inserting UnixTime: %s")
checkFinalize(is, t)
// the format used to persist has a max precision of 1s.
now = now.Truncate(time.Second)
var ut UnixTime
err = db.OneValue("SELECT time FROM test where ROWID = ?", &ut, id)
checkNoError(t, err, "error selecting UnixTime: %s")
assert.Equal(t, now, ut.Time)
err = db.OneValue("SELECT null", &ut)
checkNoError(t, err, "%s")
assert.T(t, ut.IsZero())
err = db.OneValue("SELECT 'bim'", &ut)
assert.T(t, err != nil)
//println(err.Error())
}