Skip to content

Commit 0f29f19

Browse files
committed
Added tw omore tests on makeComments function of pgComment.
1 parent 040b5cf commit 0f29f19

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

database/pgComment.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ func makeComments(ctx context.Context, conn PgGoConnection, model interface{}) e
1616
fieldType := modelType.Field(i)
1717

1818
if fieldType.Name == "tableName" {
19-
20-
readFromModelName := true
21-
tableName, readFromModelName = getPgName(fieldType)
22-
if readFromModelName {
19+
var ok bool
20+
tableName, ok = getPgName(fieldType)
21+
if !ok {
2322
tableName = pg.Safe(hasura.ToSnakeCase(modelType.Name()))
2423
}
2524

@@ -42,8 +41,8 @@ func makeComments(ctx context.Context, conn PgGoConnection, model interface{}) e
4241
continue
4342
}
4443

45-
columnName, readFromFieldName := getPgName(fieldType)
46-
if readFromFieldName {
44+
columnName, ok := getPgName(fieldType)
45+
if !ok {
4746
columnName = pg.Safe(hasura.ToSnakeCase(fieldType.Name))
4847
}
4948

database/pgComment_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,83 @@ func TestMakeCommentsWithTableNameWithoutPgComment(t *testing.T) {
7979
assert.Empty(t, err)
8080
}
8181

82+
func TestMakeCommentsFieldWithPgComment(t *testing.T) {
83+
type Ballot struct {
84+
//nolint
85+
tableName struct{} `pg:"ballots"`
86+
Ballot string `json:"ballot" pg-comment:"This is field comment"`
87+
}
88+
89+
mockCtrl, mockPgDB, pgGo, ctx := createPgDbMock(t)
90+
defer mockCtrl.Finish()
91+
92+
model := Ballot{}
93+
94+
// Assert prepare
95+
expectedParams := toInterfaceSlice([]pg.Safe{"ballots", "ballot", "This is field comment"})
96+
mockPgDB.
97+
EXPECT().
98+
ExecContext(ctx, "COMMENT ON COLUMN ?.? IS ?",
99+
gomock.Eq(expectedParams)).
100+
Times(1).
101+
Return(nil, nil)
102+
103+
// Act
104+
err := makeComments(ctx, pgGo, model)
105+
106+
// Assert
107+
assert.Empty(t, err)
108+
}
109+
110+
func TestMakeCommentsWithTableNameAndFieldsWithPgComment(t *testing.T) {
111+
type Ballot struct {
112+
//nolint
113+
tableName struct{} `pg:"ballots" pg-comment:"Ballot table"`
114+
CreatedAt int64 `json:"-" pg-comment:"This is field comment"`
115+
UpdatedAt int64 `json:"-" pg-comment:"This is field comment"`
116+
Network string `json:"network" pg:",pk" pg-comment:"This is field comment"`
117+
Hash string `json:"hash" pg:",pk" pg-comment:"This is field comment"`
118+
Branch string `json:"branch" pg-comment:"This is field comment"`
119+
Status string `json:"status" pg-comment:"This is field comment"`
120+
Kind string `json:"kind" pg-comment:"This is field comment"`
121+
Signature string `json:"signature" pg-comment:"This is field comment"`
122+
Protocol string `json:"protocol" pg-comment:"This is field comment"`
123+
Level uint64 `json:"level" pg-comment:"This is field comment"`
124+
Errors interface{} `json:"errors,omitempty" pg:"type:jsonb" pg-comment:"This is field comment"`
125+
ExpirationLevel *uint64 `json:"expiration_level" pg-comment:"This is field comment"`
126+
Raw interface{} `json:"raw,omitempty" pg:"type:jsonb" pg-comment:"This is field comment"`
127+
Ballot string `json:"ballot" pg-comment:"This is field comment"`
128+
Period int64 `json:"period" pg-comment:"This is field comment"`
129+
}
130+
131+
mockCtrl, mockPgDB, pgGo, ctx := createPgDbMock(t)
132+
defer mockCtrl.Finish()
133+
134+
model := Ballot{}
135+
136+
// Assert prepare
137+
expectedParams := toInterfaceSlice([]pg.Safe{"ballots", "Ballot table"})
138+
commentOnTableCall := mockPgDB.
139+
EXPECT().
140+
ExecContext(ctx, "COMMENT ON TABLE ? IS ?",
141+
gomock.Eq(expectedParams)).
142+
Times(1).
143+
Return(nil, nil)
144+
145+
mockPgDB.
146+
EXPECT().
147+
ExecContext(ctx, "COMMENT ON COLUMN ?.? IS ?", gomock.Any()).
148+
Times(15).
149+
After(commentOnTableCall).
150+
Return(nil, nil)
151+
152+
// Act
153+
err := makeComments(ctx, pgGo, model)
154+
155+
// Assert
156+
assert.Empty(t, err)
157+
}
158+
82159
func createPgDbMock(t *testing.T) (*gomock.Controller, *mocks.MockPgDB, *PgGoMock, context.Context) {
83160
mockCtrl := gomock.NewController(t)
84161
mockPgDB := mocks.NewMockPgDB(mockCtrl)

0 commit comments

Comments
 (0)