MCPcopy Index your code
hub / github.com/coder/coder / TestNullTime_MarshalJSON

Function TestNullTime_MarshalJSON

codersdk/time_test.go:15–59  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13)
14
15func TestNullTime_MarshalJSON(t *testing.T) {
16 t.Parallel()
17
18 t1, err := time.Parse(time.RFC3339, "2022-08-18T00:00:00Z")
19 require.NoError(t, err)
20 bt1, err := json.Marshal(t1)
21 require.NoError(t, err)
22
23 tests := []struct {
24 name string
25 input sql.NullTime
26 want string
27 }{
28 {
29 name: "valid zero",
30 input: sql.NullTime{Valid: true},
31 want: `"0001-01-01T00:00:00Z"`,
32 },
33 {
34 name: "invalid zero",
35 input: sql.NullTime{Valid: false},
36 want: "null",
37 },
38 {
39 name: "valid time",
40 input: sql.NullTime{Time: t1, Valid: true},
41 want: string(bt1),
42 },
43 {
44 name: "null time",
45 input: sql.NullTime{Time: t1, Valid: false},
46 want: "null",
47 },
48 }
49 for _, tt := range tests {
50 t.Run(tt.name, func(t *testing.T) {
51 t.Parallel()
52
53 tr := codersdk.NewNullTime(tt.input.Time, tt.input.Valid)
54 got, err := tr.MarshalJSON()
55 require.NoError(t, err)
56 require.Equal(t, tt.want, string(got))
57 })
58 }
59}
60
61func TestNullTime_UnmarshalJSON(t *testing.T) {
62 t.Parallel()

Callers

nothing calls this directly

Calls 6

MarshalJSONMethod · 0.95
NewNullTimeFunction · 0.92
ParseMethod · 0.65
RunMethod · 0.65
MarshalMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected