Skip to content

Commit

Permalink
finish range date test
Browse files Browse the repository at this point in the history
  • Loading branch information
acim committed Dec 1, 2022
1 parent d4c1c4f commit 6d8d76e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
4 changes: 2 additions & 2 deletions pgtype/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (dst *Range[T]) UnmarshalJSON(b []byte) error {
case "[":
dst.LowerType = Inclusive
default:
panic("not implemented")
return fmt.Errorf("lower bound %q not implemented", lowerBound)
}

switch upperBound {
Expand All @@ -417,7 +417,7 @@ func (dst *Range[T]) UnmarshalJSON(b []byte) error {
case "]":
dst.UpperType = Inclusive
default:
panic(upperBound)
return fmt.Errorf("upper bound %q not implemented", upperBound)
}

return nil
Expand Down
60 changes: 51 additions & 9 deletions pgtype/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,25 +251,67 @@ func TestRangeDateUnmarshalJSON(t *testing.T) {
{src: "null", result: Range[Date]{}},
{src: `"(2022-12-01,2022-12-31)"`, result: Range[Date]{
Lower: Date{Time: time.Date(2022, 12, 1, 0, 0, 0, 0, time.UTC), Valid: true},
Upper: Date{Time: time.Date(2022, 12, 1, 0, 0, 0, 0, time.UTC), Valid: true},
Upper: Date{Time: time.Date(2022, 12, 31, 0, 0, 0, 0, time.UTC), Valid: true},
LowerType: Exclusive,
UpperType: Exclusive,
Valid: true,
}},
{src: `"(2022-12-01,2022-12-31]"`, result: Range[Date]{
Lower: Date{Time: time.Date(2022, 12, 1, 0, 0, 0, 0, time.UTC), Valid: true},
Upper: Date{Time: time.Date(2022, 12, 31, 0, 0, 0, 0, time.UTC), Valid: true},
LowerType: Exclusive,
UpperType: Inclusive,
Valid: true,
}},
{src: `"[2022-12-01,2022-12-31)"`, result: Range[Date]{
Lower: Date{Time: time.Date(2022, 12, 1, 0, 0, 0, 0, time.UTC), Valid: true},
Upper: Date{Time: time.Date(2022, 12, 31, 0, 0, 0, 0, time.UTC), Valid: true},
LowerType: Inclusive,
UpperType: Exclusive,
Valid: true,
}},
{src: `"[2022-12-01,2022-12-31]"`, result: Range[Date]{
Lower: Date{Time: time.Date(2022, 12, 1, 0, 0, 0, 0, time.UTC), Valid: true},
Upper: Date{Time: time.Date(2022, 12, 31, 0, 0, 0, 0, time.UTC), Valid: true},
LowerType: Inclusive,
UpperType: Inclusive,
Valid: true,
}},
{src: `"(-infinity,2022-12-31)"`, result: Range[Date]{
Lower: Date{InfinityModifier: NegativeInfinity, Valid: true},
Upper: Date{Time: time.Date(2022, 12, 31, 0, 0, 0, 0, time.UTC), Valid: true},
LowerType: Exclusive,
UpperType: Exclusive,
Valid: true,
}},
// {source: "\"2012-03-29\"", result: pgtype.Date{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.FixedZone("", -6*60*60)), Valid: true}},
// {source: "\"2012-03-29\"", result: pgtype.Date{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.FixedZone("", -6*60*60)), Valid: true}},
// {source: "\"infinity\"", result: pgtype.Date{InfinityModifier: pgtype.Infinity, Valid: true}},
// {source: "\"-infinity\"", result: pgtype.Date{InfinityModifier: pgtype.NegativeInfinity, Valid: true}},
{src: `"[2022-12-31,infinity)"`, result: Range[Date]{
Lower: Date{Time: time.Date(2022, 12, 31, 0, 0, 0, 0, time.UTC), Valid: true},
Upper: Date{InfinityModifier: Infinity, Valid: true},
LowerType: Inclusive,
UpperType: Exclusive,
Valid: true,
}},
}

for i, tt := range tests {
var r Range[Date]
err := r.UnmarshalJSON([]byte(tt.src))
if err != nil {
t.Errorf("%d: %v", i, err)
t.Fatalf("%d: %v", i, err)
}

// if r.Time.Year() != tt.result.Time.Year() || r.Time.Month() != tt.result.Time.Month() || r.Time.Day() != tt.result.Time.Day() || r.Valid != tt.result.Valid || r.InfinityModifier != tt.result.InfinityModifier {
// t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.src, tt.result, r)
// }
if r.Lower.Time.Year() != tt.result.Lower.Time.Year() ||
r.Lower.Time.Month() != tt.result.Lower.Time.Month() ||
r.Lower.Time.Day() != tt.result.Lower.Time.Day() ||
r.Lower.InfinityModifier != tt.result.Lower.InfinityModifier ||
r.LowerType != tt.result.LowerType ||
r.Upper.Time.Year() != tt.result.Upper.Time.Year() ||
r.Upper.Time.Month() != tt.result.Upper.Time.Month() ||
r.Upper.Time.Day() != tt.result.Upper.Time.Day() ||
r.Upper.InfinityModifier != tt.result.Upper.InfinityModifier ||
r.UpperType != tt.result.UpperType ||
r.Valid != tt.result.Valid {
t.Errorf("%d: expected %v to decode to %v, got %v", i, tt.src, tt.result, r)
}
}
}

0 comments on commit 6d8d76e

Please sign in to comment.