Skip to content

Commit 64ba792

Browse files
Add bool access tests (#15)
* feat: add unit tests for access_bool.go Adds unit tests for the booleanAccess struct in access_bool.go. Creates a new test file access_bool_test.go and adds tests for various methods of booleanAccess like NewBooleanAccess, ValueToInsert, Equals, And, Get, AppendScannable, FieldValueToScan, and TableAlias. Also adds a helper SQL() method to booleanAccess to facilitate testing. * feat: add unit tests for access_bool.go and access_float.go This commit adds unit tests for the `booleanAccess` and `float64Access` structs. - Creates new test files `access_bool_test.go` and `access_float_test.go`. - Adds tests for various methods of `booleanAccess` and `float64Access`. - Adds a helper `SQL()` method to both `booleanAccess` and `float64Access` to facilitate testing. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 43524f8 commit 64ba792

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

access_bool.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package pgtalk
22

3+
import "bytes"
4+
35
type booleanAccess struct {
46
unimplementedBooleanExpression
57
ColumnInfo
@@ -21,6 +23,13 @@ func (a booleanAccess) ValueToInsert() any {
2123
return a.valueToInsert
2224
}
2325

26+
func (a booleanAccess) SQL() string {
27+
buf := new(bytes.Buffer)
28+
w := NewWriteContext(buf)
29+
a.SQLOn(w)
30+
return buf.String()
31+
}
32+
2433
func (a booleanAccess) And(e SQLExpression) SQLExpression {
2534
return makeBinaryOperator(a, "AND", e)
2635
}

access_bool_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package pgtalk
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestNewBooleanAccess(t *testing.T) {
8+
info := MakeColumnInfo(boolTable, "fbool", NotPrimary, Nullable, 1)
9+
a := NewBooleanAccess(info, func(dest any) any { return &dest.(*poly).FBool })
10+
if got, want := a.Column().columnName, "fbool"; got != want {
11+
t.Errorf("got [%v] want [%v]", got, want)
12+
}
13+
}
14+
15+
var boolTable = TableInfo{Name: "boolies", Schema: "public", Alias: "b1"}
16+
17+
var polyFBool = NewBooleanAccess(MakeColumnInfo(boolTable, "fbool", NotPrimary, Nullable, 1),
18+
func(dest any) any { return &dest.(*poly).FBool })
19+
20+
func TestBooleanAccess_ValueToInsert(t *testing.T) {
21+
a := polyFBool.Set(true)
22+
if got, want := a.ValueToInsert(), true; got != want {
23+
t.Errorf("got [%v] want [%v]", got, want)
24+
}
25+
}
26+
27+
func TestBooleanAccess_Equals(t *testing.T) {
28+
a := polyFBool.Equals(true)
29+
sql := SQL(a)
30+
if got, want := sql, `(b1.fbool = true)`; got != want {
31+
t.Errorf("got [%v] want [%v]", got, want)
32+
}
33+
}
34+
35+
func TestBooleanAccess_And(t *testing.T) {
36+
a := polyFBool.Equals(true)
37+
b := polyFBool.Equals(false)
38+
c := a.And(b)
39+
sql := SQL(c)
40+
if got, want := sql, `((b1.fbool = true) AND (b1.fbool = false))`; got != want {
41+
t.Errorf("got [%v] want [%v]", got, want)
42+
}
43+
}
44+
45+
func TestBooleanAccess_Get(t *testing.T) {
46+
a := polyFBool
47+
values := map[string]any{
48+
"fbool": true,
49+
}
50+
if got, want := a.Get(values), true; got != want {
51+
t.Errorf("got [%v] want [%v]", got, want)
52+
}
53+
}
54+
55+
func TestBooleanAccess_AppendScannable(t *testing.T) {
56+
a := polyFBool
57+
list := []any{}
58+
list = a.AppendScannable(list)
59+
if got, want := len(list), 1; got != want {
60+
t.Errorf("got [%v] want [%v]", got, want)
61+
}
62+
}
63+
64+
func TestBooleanAccess_FieldValueToScan(t *testing.T) {
65+
p := new(poly)
66+
a := polyFBool
67+
field := a.FieldValueToScan(p)
68+
if _, ok := field.(*bool); !ok {
69+
t.Errorf("got [%T] want [*bool]", field)
70+
}
71+
}
72+
73+
func TestBooleanAccess_TableAlias(t *testing.T) {
74+
a := polyFBool.TableAlias("p")
75+
if got, want := a.SQL(), "p.fbool"; got != want {
76+
t.Errorf("got [%v] want [%v]", got, want)
77+
}
78+
}

access_float.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pgtalk
22

33
import (
4+
"bytes"
45
"strings"
56
)
67

@@ -27,6 +28,13 @@ func (a float64Access) Set(v float64) float64Access {
2728

2829
func (a float64Access) Column() ColumnInfo { return a.ColumnInfo }
2930

31+
func (a float64Access) SQL() string {
32+
buf := new(bytes.Buffer)
33+
w := NewWriteContext(buf)
34+
a.SQLOn(w)
35+
return buf.String()
36+
}
37+
3038
func (a float64Access) Equals(isFloatLike any) SQLExpression {
3139
return a.Compare("=", isFloatLike)
3240
}

access_float_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,71 @@
11
package pgtalk
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestNewFloat64Access(t *testing.T) {
8+
info := MakeColumnInfo(polyTable, "ffloat", NotPrimary, Nullable, 1)
9+
a := NewFloat64Access(info, func(dest any) any { return &dest.(*poly).FFloat })
10+
if got, want := a.Column().columnName, "ffloat"; got != want {
11+
t.Errorf("got [%v] want [%v]", got, want)
12+
}
13+
}
14+
15+
func TestFloat64Access_Set(t *testing.T) {
16+
a := polyFFloat.Set(1.23)
17+
if got, want := a.valueToInsert, 1.23; got != want {
18+
t.Errorf("got [%v] want [%v]", got, want)
19+
}
20+
}
21+
22+
func TestFloat64Access_Equals(t *testing.T) {
23+
a := polyFFloat.Equals(1.23)
24+
sql := SQL(a)
25+
if got, want := sql, `(p1.ffloat = 1.23)`; got != want {
26+
t.Errorf("got [%v] want [%v]", got, want)
27+
}
28+
}
29+
30+
func TestFloat64Access_Compare(t *testing.T) {
31+
a := polyFFloat.Compare(">", 1.23)
32+
sql := SQL(a)
33+
if got, want := sql, `(p1.ffloat > 1.23)`; got != want {
34+
t.Errorf("got [%v] want [%v]", got, want)
35+
}
36+
}
37+
38+
func TestFloat64Access_FieldValueToScan(t *testing.T) {
39+
p := new(poly)
40+
a := polyFFloat
41+
field := a.FieldValueToScan(p)
42+
if _, ok := field.(*float64); !ok {
43+
t.Errorf("got [%T] want [*float64]", field)
44+
}
45+
}
46+
47+
func TestFloat64Access_TableAlias(t *testing.T) {
48+
a := polyFFloat.TableAlias("p")
49+
if got, want := a.SQL(), "p.ffloat"; got != want {
50+
t.Errorf("got [%v] want [%v]", got, want)
51+
}
52+
}
53+
54+
func TestFloat64Access_AppendScannable(t *testing.T) {
55+
a := polyFFloat
56+
list := []any{}
57+
list = a.AppendScannable(list)
58+
if got, want := len(list), 1; got != want {
59+
t.Errorf("got [%v] want [%v]", got, want)
60+
}
61+
}
62+
63+
func TestFloat64Access_Get(t *testing.T) {
64+
a := polyFFloat
65+
values := map[string]any{
66+
"ffloat": 1.23,
67+
}
68+
if got, want := a.Get(values), 1.23; got != want {
69+
t.Errorf("got [%v] want [%v]", got, want)
70+
}
71+
}

0 commit comments

Comments
 (0)