@@ -2,10 +2,12 @@ package database
22
33import (
44 "context"
5+ "testing"
6+
57 "github.com/dipdup-net/go-lib/mocks"
68 "github.com/golang/mock/gomock"
7- "github.com/stretchr/testify/assert "
8- "testing "
9+ "github.com/stretchr/testify/require "
10+ "github.com/uptrace/bun "
911)
1012
1113func initMocks (t * testing.T ) (* gomock.Controller , * mocks.MockSchemeCommenter , context.Context ) {
@@ -39,7 +41,7 @@ func TestMakeCommentsWithTableName(t *testing.T) {
3941 err := MakeComments (ctx , mockSC , model )
4042
4143 // Assert
42- assert . Empty (t , err )
44+ require . NoError (t , err )
4345}
4446
4547func TestMakeCommentsWithoutPgComment (t * testing.T ) {
@@ -69,7 +71,7 @@ func TestMakeCommentsWithoutPgComment(t *testing.T) {
6971 err := MakeComments (ctx , mockSC , model )
7072
7173 // Assert
72- assert . Empty (t , err )
74+ require . NoError (t , err )
7375}
7476
7577func TestMakeCommentsFieldWithPgComment (t * testing.T ) {
@@ -95,7 +97,7 @@ func TestMakeCommentsFieldWithPgComment(t *testing.T) {
9597 err := MakeComments (ctx , mockSC , model )
9698
9799 // Assert
98- assert . Empty (t , err )
100+ require . NoError (t , err )
99101}
100102
101103func TestMakeCommentsWithTableNameAndFieldsWithPgComment (t * testing.T ) {
@@ -142,7 +144,7 @@ func TestMakeCommentsWithTableNameAndFieldsWithPgComment(t *testing.T) {
142144 err := MakeComments (ctx , mockSC , model )
143145
144146 // Assert
145- assert . Empty (t , err )
147+ require . NoError (t , err )
146148}
147149
148150func TestMakeCommentsWithMultipleModels (t * testing.T ) {
@@ -176,7 +178,7 @@ func TestMakeCommentsWithMultipleModels(t *testing.T) {
176178 err := MakeComments (ctx , mockSC , models ... )
177179
178180 // Assert
179- assert . Empty (t , err )
181+ require . NoError (t , err )
180182}
181183
182184func TestMakeCommentsWithMultipleModelsByPointers (t * testing.T ) {
@@ -210,7 +212,7 @@ func TestMakeCommentsWithMultipleModelsByPointers(t *testing.T) {
210212 err := MakeComments (ctx , mockSC , models ... )
211213
212214 // Assert
213- assert . Empty (t , err )
215+ require . NoError (t , err )
214216}
215217
216218func TestMakeCommentsIgnoreFieldWithPgHyphen (t * testing.T ) {
@@ -235,7 +237,7 @@ func TestMakeCommentsIgnoreFieldWithPgHyphen(t *testing.T) {
235237 err := MakeComments (ctx , mockSC , model )
236238
237239 // Assert
238- assert . Empty (t , err )
240+ require . NoError (t , err )
239241}
240242
241243func TestMakeCommentsIgnoreFieldsWithEmptyComment (t * testing.T ) {
@@ -260,7 +262,7 @@ func TestMakeCommentsIgnoreFieldsWithEmptyComment(t *testing.T) {
260262 err := MakeComments (ctx , mockSC , model )
261263
262264 // Assert
263- assert . Empty (t , err )
265+ require . NoError (t , err )
264266}
265267
266268func TestMakeCommentsIgnoreNilModel (t * testing.T ) {
@@ -277,7 +279,7 @@ func TestMakeCommentsIgnoreNilModel(t *testing.T) {
277279 err := MakeComments (ctx , mockSC , nil )
278280
279281 // Assert
280- assert . Empty (t , err )
282+ require . NoError (t , err )
281283}
282284
283285func TestMakeCommentsIgnoreNoModels (t * testing.T ) {
@@ -294,7 +296,7 @@ func TestMakeCommentsIgnoreNoModels(t *testing.T) {
294296 err := MakeComments (ctx , mockSC )
295297
296298 // Assert
297- assert . Empty (t , err )
299+ require . NoError (t , err )
298300}
299301
300302func TestMakeCommentsWithStructCompositionAndCorrectOrder (t * testing.T ) {
@@ -355,7 +357,7 @@ func TestMakeCommentsWithStructCompositionAndCorrectOrder(t *testing.T) {
355357 err := MakeComments (ctx , mockSC , model )
356358
357359 // Assert
358- assert . Empty (t , err )
360+ require . NoError (t , err )
359361}
360362
361363func TestMakeCommentsWithDeepStructComposition (t * testing.T ) {
@@ -424,7 +426,7 @@ func TestMakeCommentsWithDeepStructComposition(t *testing.T) {
424426 err := MakeComments (ctx , mockSC , model )
425427
426428 // Assert
427- assert . Empty (t , err )
429+ require . NoError (t , err )
428430}
429431
430432func TestMakeCommentsWithStructCompositionErrorOnEmbeddedTableName (t * testing.T ) {
@@ -459,5 +461,51 @@ func TestMakeCommentsWithStructCompositionErrorOnEmbeddedTableName(t *testing.T)
459461 err := MakeComments (ctx , mockSC , model )
460462
461463 // Assert
462- assert .Error (t , err , "Embedded type must not have tableName field." )
464+ require .Error (t , err , "Embedded type must not have tableName field." )
465+ }
466+
467+ func TestMakeCommentsWithBunBaseModel (t * testing.T ) {
468+ type Operation struct {
469+ bun.BaseModel `pg:"-" bun:"table:operation" comment:"This is bun comment."`
470+ CreatedAt int64 `json:"-" comment:"Date of creation in seconds since UNIX epoch."`
471+ UpdatedAt int64 `json:"-" comment:"Date of last update in seconds since UNIX epoch."`
472+ Network string `json:"network" bun:",pk" comment:"Identifies belonging network."`
473+ }
474+
475+ mockCtrl , mockSC , ctx := initMocks (t )
476+ defer mockCtrl .Finish ()
477+
478+ // Assert prepare
479+ tableNameCall := mockSC .
480+ EXPECT ().
481+ MakeTableComment (ctx , "operation" , "This is bun comment." ).
482+ Times (1 ).
483+ Return (nil )
484+
485+ createdAtCall := mockSC .
486+ EXPECT ().
487+ MakeColumnComment (ctx , "operation" , "created_at" , "Date of creation in seconds since UNIX epoch." ).
488+ After (tableNameCall ).
489+ Times (1 ).
490+ Return (nil )
491+
492+ updatedAtCall := mockSC .
493+ EXPECT ().
494+ MakeColumnComment (ctx , "operation" , "updated_at" , "Date of last update in seconds since UNIX epoch." ).
495+ After (createdAtCall ).
496+ Times (1 ).
497+ Return (nil )
498+
499+ mockSC .
500+ EXPECT ().
501+ MakeColumnComment (ctx , "operation" , "network" , "Identifies belonging network." ).
502+ After (updatedAtCall ).
503+ Times (1 ).
504+ Return (nil )
505+
506+ // Act
507+ err := MakeComments (ctx , mockSC , Operation {})
508+
509+ // Assert
510+ require .NoError (t , err , "Bun model comments was failed" )
463511}
0 commit comments