Skip to content

Commit 49f3cc8

Browse files
pqtgo.TypeMapOfStrings removed, more tests
1 parent a146193 commit 49f3cc8

File tree

8 files changed

+411
-10
lines changed

8 files changed

+411
-10
lines changed

example/app/internal/model/main_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"strconv"
1010
"strings"
1111
"testing"
12+
"time"
1213

14+
"github.com/lib/pq"
1315
"github.com/piotrkowalczuk/pqt/example/app/internal/model"
1416
)
1517

@@ -116,3 +118,29 @@ func populateNews(t testing.TB, r *model.NewsRepositoryBase, nb int) {
116118
}
117119
}
118120
}
121+
122+
func populateCategory(t testing.TB, r *model.CategoryRepositoryBase, nb int) {
123+
for i := 1; i <= nb; i++ {
124+
_, err := r.Insert(context.Background(), &model.CategoryEntity{
125+
Name: fmt.Sprintf("name-%d", i),
126+
Content: fmt.Sprintf("content-%d", i),
127+
CreatedAt: time.Now(),
128+
})
129+
if err != nil {
130+
t.Fatalf("unexpected error #%d: %s", i, err.Error())
131+
}
132+
133+
for j := 1; j <= nb; j++ {
134+
_, err := r.Insert(context.Background(), &model.CategoryEntity{
135+
ParentID: sql.NullInt64{Int64: int64(i), Valid: true},
136+
Name: fmt.Sprintf("name-%d-%d", i, j),
137+
Content: fmt.Sprintf("content-%d-%d", i, j),
138+
CreatedAt: time.Now(),
139+
UpdatedAt: pq.NullTime{Time: time.Now(), Valid: true},
140+
})
141+
if err != nil {
142+
t.Fatalf("unexpected error #%d: %s", i, err.Error())
143+
}
144+
}
145+
}
146+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package model_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
"testing"
8+
"time"
9+
10+
"database/sql"
11+
12+
"github.com/lib/pq"
13+
"github.com/piotrkowalczuk/pqt/example/app/internal/model"
14+
)
15+
16+
func TestCategoryRepositoryBase_FindIter(t *testing.T) {
17+
s := setup(t)
18+
defer s.teardown(t)
19+
20+
max := 10
21+
populateCategory(t, s.category, max)
22+
iter, err := s.category.FindIter(context.Background(), &model.CategoryFindExpr{})
23+
if err != nil {
24+
t.Fatalf("unexpected error: %s", err.Error())
25+
}
26+
defer iter.Close()
27+
28+
var got []*model.CategoryEntity
29+
for iter.Next() {
30+
ent, err := iter.Category()
31+
if err != nil {
32+
t.Fatalf("unexpected error: %s", err.Error())
33+
}
34+
got = append(got, ent)
35+
}
36+
if err = iter.Err(); err != nil {
37+
t.Fatalf("unexpected error: %s", err.Error())
38+
}
39+
exp := (max * max) + max
40+
if len(got) != exp {
41+
t.Errorf("wrong output, expected %d but got %d", exp, len(got))
42+
}
43+
}
44+
45+
func TestCategoryRepositoryBase_Count(t *testing.T) {
46+
s := setup(t)
47+
defer s.teardown(t)
48+
49+
max := 10
50+
populateCategory(t, s.category, max)
51+
got, err := s.category.Count(context.Background(), &model.CategoryCountExpr{
52+
Where: &model.CategoryCriteria{
53+
Content: sql.NullString{String: "content-5-1", Valid: true},
54+
Name: sql.NullString{String: "name-5-1", Valid: true},
55+
ParentID: sql.NullInt64{Int64: 5, Valid: true},
56+
},
57+
})
58+
if err != nil {
59+
t.Fatalf("unexpected error: %s", err.Error())
60+
}
61+
62+
if got != 1 {
63+
t.Errorf("wrong output, expected %d but got %d", 1, got)
64+
}
65+
}
66+
67+
func TestCategoryRepositoryBase_Find(t *testing.T) {
68+
s := setup(t)
69+
defer s.teardown(t)
70+
71+
max := 10
72+
populateCategory(t, s.category, max)
73+
got, err := s.category.Find(context.Background(), &model.CategoryFindExpr{
74+
Where: &model.CategoryCriteria{
75+
Content: sql.NullString{String: "content-5-1", Valid: true},
76+
Name: sql.NullString{String: "name-5-1", Valid: true},
77+
ParentID: sql.NullInt64{Int64: 5, Valid: true},
78+
},
79+
Limit: 10,
80+
Offset: 0,
81+
OrderBy: map[string]bool{
82+
model.TableCategoryColumnID: true,
83+
},
84+
})
85+
if err != nil {
86+
t.Fatalf("unexpected error: %s", err.Error())
87+
}
88+
89+
if len(got) != 1 {
90+
t.Errorf("wrong output, expected %d but got %d", 1, len(got))
91+
}
92+
}
93+
94+
func TestCategoryRepositoryBase_FindOneByID(t *testing.T) {
95+
s := setup(t)
96+
defer s.teardown(t)
97+
98+
expected := 5
99+
populateCategory(t, s.category, expected)
100+
101+
for i := 1; i <= expected; i++ {
102+
got, err := s.category.FindOneByID(context.Background(), int64(i))
103+
if err != nil {
104+
t.Fatalf("unexpected error: %s", err.Error())
105+
}
106+
if got.ID != int64(i) {
107+
t.Errorf("wrong id, expected %d but got %d", i, got.ID)
108+
}
109+
}
110+
}
111+
112+
func TestCategoryRepositoryBase_UpdateOneByID(t *testing.T) {
113+
s := setup(t)
114+
defer s.teardown(t)
115+
116+
expected := 10
117+
populateCategory(t, s.category, expected)
118+
119+
for i := 1; i <= expected; i++ {
120+
got, err := s.category.UpdateOneByID(context.Background(), int64(i), &model.CategoryPatch{
121+
Content: sql.NullString{
122+
Valid: true,
123+
String: fmt.Sprintf("content-updated-by-id-%d", i),
124+
},
125+
Name: sql.NullString{
126+
Valid: true,
127+
String: fmt.Sprintf("name-updated-by-id-%d", i),
128+
},
129+
CreatedAt: pq.NullTime{
130+
Valid: true,
131+
Time: time.Now(),
132+
},
133+
UpdatedAt: pq.NullTime{
134+
Valid: true,
135+
Time: time.Now(),
136+
},
137+
})
138+
if err != nil {
139+
t.Fatalf("unexpected error: %s", err.Error())
140+
}
141+
if got.ID != int64(i) {
142+
t.Errorf("wrong id, expected %d but got %d", i, got.ID)
143+
}
144+
if !strings.HasPrefix(got.Content, "content-updated-by-id") {
145+
t.Error("content was not updated properly")
146+
}
147+
}
148+
}

pqtgo/type.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,3 @@ func (ct CustomType) TypeOf(m int32) reflect.Type {
120120
return nil
121121
}
122122
}
123-
124-
// TypeMapOfStrings ....
125-
func TypeMapOfStrings() CustomType {
126-
return TypeCustom(
127-
map[string]string{},
128-
map[string]string{},
129-
nil,
130-
)
131-
}

pqtgo/type_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package pqtgo_test
2+
3+
import (
4+
"go/types"
5+
"testing"
6+
7+
"reflect"
8+
9+
"github.com/piotrkowalczuk/pqt/pqtgo"
10+
)
11+
12+
func TestBuiltinType_String(t *testing.T) {
13+
cases := map[string]types.BasicKind{
14+
"invalid": types.Invalid,
15+
"bool": types.Bool,
16+
"int": types.Int,
17+
"int8": types.Int8,
18+
"int16": types.Int16,
19+
"int32": types.Int32,
20+
"int64": types.Int64,
21+
"uint": types.Uint,
22+
"uint8": types.Uint8,
23+
"uint16": types.Uint16,
24+
"uint32": types.Uint32,
25+
"uint64": types.Uint64,
26+
"float32": types.Float32,
27+
"float64": types.Float64,
28+
"complex64": types.Complex64,
29+
"complex128": types.Complex128,
30+
"string": types.String,
31+
}
32+
for exp, kind := range cases {
33+
t.Run(exp, func(t *testing.T) {
34+
got := pqtgo.BuiltinType(kind).String()
35+
if got != exp {
36+
t.Errorf("wrong string representation of go builtin type: %s", got)
37+
}
38+
got = pqtgo.BuiltinType(kind).Fingerprint()
39+
if got != "gobuiltin: "+exp {
40+
t.Errorf("wrong fingerprint of go builtin type: %s", got)
41+
}
42+
})
43+
}
44+
}
45+
46+
func TestTypeCustom(t *testing.T) {
47+
type mandatory struct {
48+
X string
49+
}
50+
type optional struct {
51+
X string
52+
}
53+
type criteria struct {
54+
X string
55+
}
56+
57+
m := &mandatory{}
58+
o := &optional{}
59+
c := &criteria{}
60+
61+
got := pqtgo.TypeCustom(m, o, c)
62+
valueOfM := got.ValueOf(pqtgo.ModeMandatory)
63+
valueOfO := got.ValueOf(pqtgo.ModeOptional)
64+
valueOfC := got.ValueOf(pqtgo.ModeCriteria)
65+
valueOfD := got.ValueOf(pqtgo.ModeDefault)
66+
67+
if valueOfD != nil {
68+
t.Error("value of default should be nil")
69+
}
70+
if !reflect.DeepEqual(m, valueOfM) {
71+
t.Errorf("wrong mandatory value found: %v", valueOfM)
72+
}
73+
if !reflect.DeepEqual(o, valueOfO) {
74+
t.Errorf("wrong optional value found: %v", valueOfO)
75+
}
76+
if !reflect.DeepEqual(c, valueOfC) {
77+
t.Errorf("wrong criteria value found: %v", valueOfC)
78+
}
79+
80+
typeOfM := got.TypeOf(pqtgo.ModeMandatory)
81+
typeOfO := got.TypeOf(pqtgo.ModeOptional)
82+
typeOfC := got.TypeOf(pqtgo.ModeCriteria)
83+
typeOfD := got.TypeOf(pqtgo.ModeDefault)
84+
85+
if typeOfD != nil {
86+
t.Error("type of default should be nil")
87+
}
88+
if typeOfM.String() != "*pqtgo_test.mandatory" {
89+
t.Errorf("wrong mandatory type found: %s", typeOfM.String())
90+
}
91+
if typeOfO.String() != "*pqtgo_test.optional" {
92+
t.Errorf("wrong optional type found: %s", typeOfO.String())
93+
}
94+
if typeOfC.String() != "*pqtgo_test.criteria" {
95+
t.Errorf("wrong criteria type found: %s", typeOfC.String())
96+
}
97+
98+
gotS := got.String()
99+
if gotS != "*pqtgo_test.mandatory/*pqtgo_test.optional/*pqtgo_test.criteria" {
100+
t.Errorf("wrong string representation: %s", got.String())
101+
}
102+
103+
gotF := got.Fingerprint()
104+
if gotF != "gocustomtype: *pqtgo_test.mandatory/*pqtgo_test.optional/*pqtgo_test.criteria" {
105+
t.Errorf("wrong fingerprint: %s", got.Fingerprint())
106+
}
107+
}

relationship.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func WithOwnerForeignKey(columns, references Columns, opts ...ConstraintOption)
9797
return func(r *Relationship) {
9898
for _, c := range columns {
9999
if r.OwnerTable != c.Table {
100-
panic("colum tables inconsistency")
100+
panic("table columns inconsistency")
101101
}
102102
}
103103
r.OwnerForeignKey = ForeignKey(columns, references, opts...)

schema_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package pqt_test
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/piotrkowalczuk/pqt"
8+
)
9+
10+
func TestWithSchemaIfNotExists(t *testing.T) {
11+
sch := pqt.NewSchema("schema", pqt.WithSchemaIfNotExists())
12+
if !sch.IfNotExists {
13+
t.Error("expected if not exists to be true")
14+
}
15+
}
16+
17+
func TestSchema_AddTable(t *testing.T) {
18+
tbl := pqt.NewTable("table")
19+
sch := pqt.NewSchema("schema").AddTable(tbl).AddTable(tbl)
20+
if !reflect.DeepEqual(sch, tbl.Schema) {
21+
t.Error("wrong schema assigned to the table")
22+
}
23+
}
24+
25+
func TestSchema_AddFunction(t *testing.T) {
26+
fnc := pqt.FunctionNow()
27+
tbl := pqt.NewSchema("schema").AddFunction(fnc)
28+
if len(tbl.Functions) != 1 {
29+
t.Errorf("wrong number of functions: %d", len(tbl.Functions))
30+
}
31+
}

0 commit comments

Comments
 (0)