diff --git a/.gitignore b/.gitignore index adf6091..77da274 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode cmd/pgtalk-gen/pgtalk-gen test/gen.sh +coverage.out diff --git a/convert/convert_test.go b/convert/convert_test.go index ae7f4e0..3ea3320 100644 --- a/convert/convert_test.go +++ b/convert/convert_test.go @@ -2,10 +2,51 @@ package convert import ( "testing" + "time" + "github.com/google/uuid" "github.com/jackc/pgx/v5/pgtype" ) +func TestStringToUUID(t *testing.T) { + s := "123e4567-e89b-12d3-a456-426614174000" + u := StringToUUID(s) + if !u.Valid { + t.Fatal("valid uuid expected") + } + if got, want := u.Bytes, [16]byte{0x12, 0x3e, 0x45, 0x67, 0xe8, 0x9b, 0x12, 0xd3, 0xa4, 0x56, 0x42, 0x66, 0x14, 0x17, 0x40, 0x00}; got != want { + t.Errorf("got %v want %v", got, want) + } + if want := StringToUUID("123"); want.Valid { + t.Error("invalid uuid expected") + } + if want := StringToUUID("123e4567-e89b-12d3-a456-42661417400"); want.Valid { + t.Error("invalid uuid expected") + } + s = "123e4567e89b12d3a456426614174000" + u = StringToUUID(s) + if !u.Valid { + t.Fatal("valid uuid expected") + } +} + +func TestParseUUID(t *testing.T) { + if _, err := parseUUID("123"); err == nil { + t.Error("error expected") + } + if _, err := parseUUID("123e4567-e89b-12d3-a456-42661417400k"); err == nil { + t.Error("error expected") + } +} + +func TestUUID(t *testing.T) { + u := uuid.New() + pu := UUID(u) + if !pu.Valid { + t.Error("valid pgtype.UUID expected") + } +} + func TestUUIDToString(t *testing.T) { i := pgtype.UUID{ Bytes: [16]byte{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}, @@ -20,6 +61,67 @@ func TestUUIDToString(t *testing.T) { } } +func TestTime(t *testing.T) { + now := time.Now() + if got, want := TimeToTimestamptz(now).Time, now; !got.Equal(want) { + t.Errorf("got [%v] want [%v]", got, want) + } + if got, want := TimeToTimestamp(now).Time, now.UTC(); !got.Equal(want) { + t.Errorf("got [%v] want [%v]", got, want) + } + if got, want := TimeToDate(now).Time, now; !got.Equal(want) { + t.Errorf("got [%v] want [%v]", got, want) + } + if got, want := TimeToTime(now).Microseconds, now.UnixMicro(); got != want { + t.Errorf("got [%v] want [%v]", got, want) + } +} + +func TestInts(t *testing.T) { + if got, want := Int16ToInt2(1).Int16, int16(1); got != want { + t.Errorf("got [%v] want [%v]", got, want) + } + if got, want := Int64ToInt8(1).Int64, int64(1); got != want { + t.Errorf("got [%v] want [%v]", got, want) + } + if got, want := Int4ToInt8(pgtype.Int4{Int32: 1, Valid: true}).Int64, int64(1); got != want { + t.Errorf("got [%v] want [%v]", got, want) + } + if got, want := Int32ToInt4(1).Int32, int32(1); got != want { + t.Errorf("got [%v] want [%v]", got, want) + } + if got, want := Int8(1).Int64, int64(1); got != want { + t.Errorf("got [%v] want [%v]", got, want) + } + if got, want := Int4(1).Int32, int32(1); got != want { + t.Errorf("got [%v] want [%v]", got, want) + } +} + +func TestOthers(t *testing.T) { + if got, want := StringToText("a").String, "a"; got != want { + t.Errorf("got [%v] want [%v]", got, want) + } + if got, want := Bool(true).Bool, true; got != want { + t.Errorf("got [%v] want [%v]", got, want) + } + d := pgtype.Date{Time: time.Now(), Valid: true} + if got := DateToTimePtr(d); got == nil { + t.Error("got nil want time.Time") + } + d.Valid = false + if got := DateToTimePtr(d); got != nil { + t.Error("got time.Time want nil") + } + if got, want := Float64ToFloat8(1.0).Float64, 1.0; got != want { + t.Errorf("got [%v] want [%v]", got, want) + } + sa := StringsToTextArray([]string{"a", "b"}) + if got, want := len(sa), 2; got != want { + t.Errorf("got [%v] want [%v]", got, want) + } +} + func TestTextArrayToStrings(t *testing.T) { a := pgtype.FlatArray[pgtype.Text]{} a = append(a, pgtype.Text{String: "a", Valid: true}) diff --git a/coverage.out b/coverage.out new file mode 100644 index 0000000..fabe9df --- /dev/null +++ b/coverage.out @@ -0,0 +1,37 @@ +mode: set +github.com/emicklei/pgtalk/convert/convert.go:12.36,17.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:21.41,23.16 2 0 +github.com/emicklei/pgtalk/convert/convert.go:23.16,28.3 1 0 +github.com/emicklei/pgtalk/convert/convert.go:29.2,32.3 1 0 +github.com/emicklei/pgtalk/convert/convert.go:36.54,37.18 1 0 +github.com/emicklei/pgtalk/convert/convert.go:38.10,39.66 1 0 +github.com/emicklei/pgtalk/convert/convert.go:40.10,40.10 0 0 +github.com/emicklei/pgtalk/convert/convert.go:42.10,44.54 1 0 +github.com/emicklei/pgtalk/convert/convert.go:47.2,48.16 2 0 +github.com/emicklei/pgtalk/convert/convert.go:48.16,50.3 1 0 +github.com/emicklei/pgtalk/convert/convert.go:52.2,53.17 2 0 +github.com/emicklei/pgtalk/convert/convert.go:57.41,58.14 1 1 +github.com/emicklei/pgtalk/convert/convert.go:58.14,60.3 1 1 +github.com/emicklei/pgtalk/convert/convert.go:61.2,62.91 2 1 +github.com/emicklei/pgtalk/convert/convert.go:65.56,67.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:69.52,71.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:73.42,75.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:77.42,79.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:81.39,83.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:85.41,87.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:89.39,91.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:93.44,95.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:97.39,99.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:101.30,103.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:105.30,107.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:113.31,115.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:117.46,118.13 1 0 +github.com/emicklei/pgtalk/convert/convert.go:118.13,121.3 2 0 +github.com/emicklei/pgtalk/convert/convert.go:122.2,122.12 1 0 +github.com/emicklei/pgtalk/convert/convert.go:125.47,130.2 1 0 +github.com/emicklei/pgtalk/convert/convert.go:132.70,134.28 2 0 +github.com/emicklei/pgtalk/convert/convert.go:134.28,136.3 1 0 +github.com/emicklei/pgtalk/convert/convert.go:137.2,137.10 1 0 +github.com/emicklei/pgtalk/convert/convert.go:140.71,142.29 2 1 +github.com/emicklei/pgtalk/convert/convert.go:142.29,144.3 1 1 +github.com/emicklei/pgtalk/convert/convert.go:145.2,145.13 1 1