@@ -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+
82159func createPgDbMock (t * testing.T ) (* gomock.Controller , * mocks.MockPgDB , * PgGoMock , context.Context ) {
83160 mockCtrl := gomock .NewController (t )
84161 mockPgDB := mocks .NewMockPgDB (mockCtrl )
0 commit comments