mirror of https://github.com/jackc/pgx.git
Added unit test and fixed typo
parent
e352784fed
commit
52e2858629
|
@ -105,7 +105,7 @@ func (ts *Timestamp) UnmarshalJSON(b []byte) error {
|
||||||
*ts = Timestamp{Valid: true, InfinityModifier: -Infinity}
|
*ts = Timestamp{Valid: true, InfinityModifier: -Infinity}
|
||||||
default:
|
default:
|
||||||
tss := *s
|
tss := *s
|
||||||
// PostgreSQL uses ISO 8601 wihout timezone for to_json function and casting from a string to timestampt
|
// PostgreSQL uses ISO 8601 without timezone for to_json function and casting from a string to timestampt
|
||||||
if !strings.HasSuffix(tss, "Z") {
|
if !strings.HasSuffix(tss, "Z") {
|
||||||
tss = tss + "Z"
|
tss = tss + "Z"
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,14 @@ package pgtype_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
pgx "github.com/jackc/pgx/v5"
|
pgx "github.com/jackc/pgx/v5"
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
"github.com/jackc/pgx/v5/pgxtest"
|
"github.com/jackc/pgx/v5/pgxtest"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -100,12 +102,22 @@ func TestTimestampCodecDecodeTextInvalid(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTimestampMarshalJSON(t *testing.T) {
|
func TestTimestampMarshalJSON(t *testing.T) {
|
||||||
|
|
||||||
|
tsStruct := struct {
|
||||||
|
TS pgtype.Timestamp `json:"ts"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
tm := time.Date(2012, 3, 29, 10, 5, 45, 0, time.UTC)
|
||||||
|
var pgt pgtype.Timestamp
|
||||||
|
_ = pgt.Scan(tm)
|
||||||
|
|
||||||
successfulTests := []struct {
|
successfulTests := []struct {
|
||||||
source pgtype.Timestamp
|
source pgtype.Timestamp
|
||||||
result string
|
result string
|
||||||
}{
|
}{
|
||||||
{source: pgtype.Timestamp{}, result: "null"},
|
{source: pgtype.Timestamp{}, result: "null"},
|
||||||
{source: pgtype.Timestamp{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.UTC), Valid: true}, result: "\"2012-03-29T10:05:45Z\""},
|
{source: pgtype.Timestamp{Time: tm, Valid: true}, result: "\"2012-03-29T10:05:45Z\""},
|
||||||
|
{source: pgt, result: "\"2012-03-29T10:05:45Z\""},
|
||||||
{source: pgtype.Timestamp{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.UTC), Valid: true}, result: "\"2012-03-29T10:05:45.555Z\""},
|
{source: pgtype.Timestamp{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.UTC), Valid: true}, result: "\"2012-03-29T10:05:45.555Z\""},
|
||||||
{source: pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Valid: true}, result: "\"infinity\""},
|
{source: pgtype.Timestamp{InfinityModifier: pgtype.Infinity, Valid: true}, result: "\"infinity\""},
|
||||||
{source: pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, result: "\"-infinity\""},
|
{source: pgtype.Timestamp{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, result: "\"-infinity\""},
|
||||||
|
@ -119,6 +131,12 @@ func TestTimestampMarshalJSON(t *testing.T) {
|
||||||
if string(r) != tt.result {
|
if string(r) != tt.result {
|
||||||
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
|
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
|
||||||
}
|
}
|
||||||
|
tsStruct.TS = tt.source
|
||||||
|
b, err := json.Marshal(tsStruct)
|
||||||
|
assert.NoErrorf(t, err, "failed to marshal %v %s", tt.source, err)
|
||||||
|
t2 := tsStruct
|
||||||
|
err = json.Unmarshal(b, &t2)
|
||||||
|
assert.NoErrorf(t, err, "failed to unmarshal %v with %s", tt.source, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue