-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_test.go
113 lines (80 loc) · 2.83 KB
/
time_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
package sqle
import (
"database/sql"
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestTimeInSQL(t *testing.T) {
now := time.Now()
d, err := sql.Open("sqlite3", "file::memory:")
require.NoError(t, err)
_, err = d.Exec("CREATE TABLE `times` (`id` id NOT NULL,`created_at` datetime, PRIMARY KEY (`id`))")
require.NoError(t, err)
result, err := d.Exec("INSERT INTO `times`(`id`) VALUES(?)", 10)
require.NoError(t, err)
rows, err := result.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rows)
result, err = d.Exec("INSERT INTO `times`(`id`, `created_at`) VALUES(?, ?)", 20, now)
require.NoError(t, err)
rows, err = result.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rows)
var t10 Time
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 10).Scan(&t10)
require.NoError(t, err)
require.EqualValues(t, false, t10.Valid)
var t20 Time
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 20).Scan(&t20)
require.NoError(t, err)
require.EqualValues(t, true, t20.Valid)
require.EqualValues(t, now.UTC(), t20.Time().UTC())
result, err = d.Exec("INSERT INTO `times`(`id`,`created_at`) VALUES(?, ?)", 11, t10)
require.NoError(t, err)
rows, err = result.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rows)
result, err = d.Exec("INSERT INTO `times`(`id`, `created_at`) VALUES(?, ?)", 21, t20)
require.NoError(t, err)
rows, err = result.RowsAffected()
require.NoError(t, err)
require.Equal(t, int64(1), rows)
var t11 Time
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 11).Scan(&t11)
require.NoError(t, err)
require.EqualValues(t, false, t11.Valid)
var t21 Time
err = d.QueryRow("SELECT `created_at` FROM `times` WHERE id=?", 21).Scan(&t21)
require.NoError(t, err)
require.EqualValues(t, true, t21.Valid)
require.EqualValues(t, now.UTC(), t21.Time().UTC())
}
func TestTimeInJSON(t *testing.T) {
sysTime := time.Now()
bufSysTime, err := json.Marshal(sysTime)
require.NoError(t, err)
sqleTime := NewTime(sysTime, true)
bufSqleTime, err := json.Marshal(sqleTime)
require.NoError(t, err)
require.Equal(t, bufSysTime, bufSqleTime)
var jsSqleTime Time
// Unmarshal sqle.Time from time.Time json bytes
err = json.Unmarshal(bufSysTime, &jsSqleTime)
require.NoError(t, err)
require.True(t, sysTime.Equal(jsSqleTime.Time()))
require.Equal(t, true, jsSqleTime.Valid)
var jsSysTime time.Time
// Unmarshal time.Time from sqle.Time json bytes
err = json.Unmarshal(bufSqleTime, &jsSysTime)
require.NoError(t, err)
require.True(t, sysTime.Equal(jsSysTime))
var nullTime Time
err = json.Unmarshal([]byte("null"), &nullTime)
require.NoError(t, err)
require.Equal(t, false, nullTime.Valid)
bufNull, err := json.Marshal(nullTime)
require.NoError(t, err)
require.Equal(t, []byte("null"), bufNull)
}