forked from mtusi2026/mtusi2026lab6
ЛР 6 — БПИ2302 — Нурлыгаянова — на 3
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTimestampAlwaysHasSixFractionalDigits(t *testing.T) {
|
||||
moscow := time.FixedZone("MSK", 3*60*60)
|
||||
cases := []struct {
|
||||
name string
|
||||
in time.Time
|
||||
want string
|
||||
}{
|
||||
{"нули в конце", time.Date(2026, 11, 4, 9, 47, 17, 951880000, time.UTC), `"2026-11-04T09:47:17.951880Z"`},
|
||||
{"без дробной части", time.Date(2026, 11, 4, 9, 47, 17, 0, time.UTC), `"2026-11-04T09:47:17.000000Z"`},
|
||||
{"шесть цифр", time.Date(2026, 11, 4, 9, 47, 17, 123456000, time.UTC), `"2026-11-04T09:47:17.123456Z"`},
|
||||
{"наносекунды отбрасываются", time.Date(2026, 11, 4, 9, 47, 17, 123456789, time.UTC), `"2026-11-04T09:47:17.123456Z"`},
|
||||
{"приводится к UTC", time.Date(2026, 11, 4, 12, 47, 17, 500000000, moscow), `"2026-11-04T09:47:17.500000Z"`},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
got, err := json.Marshal(NewTimestamp(c.in))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(got) != c.want {
|
||||
t.Fatalf("got %s, want %s", got, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimestampRoundTrip(t *testing.T) {
|
||||
want := time.Date(2026, 11, 4, 9, 47, 17, 951880000, time.UTC)
|
||||
data, err := json.Marshal(MessageResponse{CreatedAt: NewTimestamp(want)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got MessageResponse
|
||||
if err := json.Unmarshal(data, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !got.CreatedAt.Equal(want) {
|
||||
t.Fatalf("got %s, want %s", got.CreatedAt, want)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user