package requests import ( "encoding/json" "testing" "time" ) func TestFlexibleDate_UnmarshalJSON_DateOnly(t *testing.T) { var fd FlexibleDate err := fd.UnmarshalJSON([]byte(`"2025-11-27"`)) if err != nil { t.Fatalf("unexpected error: %v", err) } want := time.Date(2025, 11, 27, 0, 0, 0, 0, time.UTC) if !fd.Time.Equal(want) { t.Errorf("got %v, want %v", fd.Time, want) } } func TestFlexibleDate_UnmarshalJSON_RFC3339(t *testing.T) { var fd FlexibleDate err := fd.UnmarshalJSON([]byte(`"2025-11-27T15:30:00Z"`)) if err != nil { t.Fatalf("unexpected error: %v", err) } want := time.Date(2025, 11, 27, 15, 30, 0, 0, time.UTC) if !fd.Time.Equal(want) { t.Errorf("got %v, want %v", fd.Time, want) } } func TestFlexibleDate_UnmarshalJSON_Null(t *testing.T) { var fd FlexibleDate err := fd.UnmarshalJSON([]byte(`null`)) if err != nil { t.Fatalf("unexpected error: %v", err) } if !fd.Time.IsZero() { t.Errorf("expected zero time, got %v", fd.Time) } } func TestFlexibleDate_UnmarshalJSON_EmptyString(t *testing.T) { var fd FlexibleDate err := fd.UnmarshalJSON([]byte(`""`)) if err != nil { t.Fatalf("unexpected error: %v", err) } if !fd.Time.IsZero() { t.Errorf("expected zero time, got %v", fd.Time) } } func TestFlexibleDate_UnmarshalJSON_Invalid(t *testing.T) { var fd FlexibleDate err := fd.UnmarshalJSON([]byte(`"not-a-date"`)) if err == nil { t.Fatal("expected error for invalid date, got nil") } } func TestFlexibleDate_MarshalJSON_Valid(t *testing.T) { fd := FlexibleDate{Time: time.Date(2025, 11, 27, 15, 30, 0, 0, time.UTC)} data, err := fd.MarshalJSON() if err != nil { t.Fatalf("unexpected error: %v", err) } var s string if err := json.Unmarshal(data, &s); err != nil { t.Fatalf("result is not a JSON string: %v", err) } want := "2025-11-27T15:30:00Z" if s != want { t.Errorf("got %q, want %q", s, want) } } func TestFlexibleDate_MarshalJSON_Zero(t *testing.T) { fd := FlexibleDate{} data, err := fd.MarshalJSON() if err != nil { t.Fatalf("unexpected error: %v", err) } if string(data) != "null" { t.Errorf("got %s, want null", string(data)) } } func TestFlexibleDate_ToTimePtr_Valid(t *testing.T) { fd := &FlexibleDate{Time: time.Date(2025, 11, 27, 0, 0, 0, 0, time.UTC)} ptr := fd.ToTimePtr() if ptr == nil { t.Fatal("expected non-nil pointer") } if !ptr.Equal(fd.Time) { t.Errorf("got %v, want %v", *ptr, fd.Time) } } func TestFlexibleDate_ToTimePtr_Zero(t *testing.T) { fd := &FlexibleDate{} ptr := fd.ToTimePtr() if ptr != nil { t.Errorf("expected nil, got %v", *ptr) } } func TestFlexibleDate_ToTimePtr_NilReceiver(t *testing.T) { var fd *FlexibleDate ptr := fd.ToTimePtr() if ptr != nil { t.Errorf("expected nil for nil receiver, got %v", *ptr) } } func TestFlexibleDate_RoundTrip(t *testing.T) { original := FlexibleDate{Time: time.Date(2025, 6, 15, 10, 0, 0, 0, time.UTC)} data, err := original.MarshalJSON() if err != nil { t.Fatalf("marshal error: %v", err) } var restored FlexibleDate if err := restored.UnmarshalJSON(data); err != nil { t.Fatalf("unmarshal error: %v", err) } if !original.Time.Equal(restored.Time) { t.Errorf("round-trip mismatch: original %v, restored %v", original.Time, restored.Time) } }