-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat4_test.go
More file actions
64 lines (57 loc) · 2.19 KB
/
Copy pathfloat4_test.go
File metadata and controls
64 lines (57 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package pgtype_test
import (
"context"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxtest"
)
func TestFloat4Codec(t *testing.T) {
pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "float4", []pgxtest.ValueRoundTripTest{
{Param: pgtype.Float4{Float32: -1, Valid: true}, Result: new(pgtype.Float4), Test: isExpectedEq(pgtype.Float4{Float32: -1, Valid: true})},
{Param: pgtype.Float4{Float32: 0, Valid: true}, Result: new(pgtype.Float4), Test: isExpectedEq(pgtype.Float4{Float32: 0, Valid: true})},
{Param: pgtype.Float4{Float32: 1, Valid: true}, Result: new(pgtype.Float4), Test: isExpectedEq(pgtype.Float4{Float32: 1, Valid: true})},
{Param: float32(0.00001), Result: new(float32), Test: isExpectedEq(float32(0.00001))},
{Param: float32(9999.99), Result: new(float32), Test: isExpectedEq(float32(9999.99))},
{Param: pgtype.Float4{}, Result: new(pgtype.Float4), Test: isExpectedEq(pgtype.Float4{})},
{Param: int64(1), Result: new(int64), Test: isExpectedEq(int64(1))},
{Param: "1.23", Result: new(string), Test: isExpectedEq("1.23")},
{Param: nil, Result: new(*float32), Test: isExpectedEq((*float32)(nil))},
})
}
func TestFloat4MarshalJSON(t *testing.T) {
successfulTests := []struct {
source pgtype.Float4
result string
}{
{source: pgtype.Float4{Float32: 0}, result: "null"},
{source: pgtype.Float4{Float32: 1.23, Valid: true}, result: "1.23"},
}
for i, tt := range successfulTests {
r, err := tt.source.MarshalJSON()
if err != nil {
t.Errorf("%d: %v", i, err)
}
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))
}
}
}
func TestFloat4UnmarshalJSON(t *testing.T) {
successfulTests := []struct {
source string
result pgtype.Float4
}{
{source: "null", result: pgtype.Float4{Float32: 0}},
{source: "1.23", result: pgtype.Float4{Float32: 1.23, Valid: true}},
}
for i, tt := range successfulTests {
var r pgtype.Float4
err := r.UnmarshalJSON([]byte(tt.source))
if err != nil {
t.Errorf("%d: %v", i, err)
}
if r != tt.result {
t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
}
}
}