Skip to content

Conversation

@wuzhixiang0827
Copy link

notification开发过程中几个问题.docx
@hwbrzzl Several questions during the development of notifications, please provide answers.

@wuzhixiang0827 wuzhixiang0827 requested a review from a team as a code owner November 18, 2025 01:43
@hwbrzzl
Copy link
Contributor

hwbrzzl commented Nov 18, 2025

Thanks, will check it.

mockDB := mocksdb.NewDB(s.T())
s.mockConfig.EXPECT().GetString("DB_CONNECTION").Return("mysql").Once()
mockDB.EXPECT().Connection("mysql").Return(mockDB).Once()
mockDB.EXPECT().Table("notifications").Return(nil).Once()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mockQuery should be returned here, then mockQuery.EXPECT().Insert().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood.

mockDB.EXPECT().Table("notifications").Return(mockQuery).Once()

var notificationModel models.Notification
notificationModel.ID = uuid.New().String()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hwbrzzl
Random UUIDs causing test failures, how to handle?

=== RUN   TestApplicationTestSuite
=== RUN   TestApplicationTestSuite/TestDatabaseNotification
    mock.go:361: 
        
        mock: Unexpected Method Call
        -----------------------------
        
        Insert(*models.Notification)
        		0: &models.Notification{ID:"f6ddac72-b09c-47b4-baf4-f0c351530f5f", Type:"notification.LoginSuccessNotification", NotifiableType:"notification.User", NotifiableId:"1", Data:"{\"content\":\"Congratulations, your login is successful!\",\"title\":\"Login success\"}", ReadAt:<nil>, Timestamps:orm.Timestamps{CreatedAt:<nil>, UpdatedAt:<nil>}}
        
        The closest call I have is: 
        
        Insert(*models.Notification)
        		0: &models.Notification{ID:"bb39e579-17f2-4299-a91f-fc2eb45ce293", Type:"notification.LoginSuccessNotification", NotifiableType:"notification.User", NotifiableId:"1", Data:"{\"content\":\"Congratulations, your login is successful!\",\"title\":\"Login success\"}", ReadAt:<nil>, Timestamps:orm.Timestamps{CreatedAt:<nil>, UpdatedAt:<nil>}}
        
        Difference found in argument 0:
        
        --- Expected
        +++ Actual
        @@ -1,3 +1,3 @@
         (*models.Notification)({
        - ID: (string) (len=36) "bb39e579-17f2-4299-a91f-fc2eb45ce293",
        + ID: (string) (len=36) "f6ddac72-b09c-47b4-baf4-f0c351530f5f",
          Type: (string) (len=37) "notification.LoginSuccessNotification",
        
        Diff: 0: FAIL:  (*models.Notification=&{f6ddac72-b09c-47b4-baf4-f0c351530f5f notification.LoginSuccessNotification notification.User 1 {"content":"Congratulations, your login is successful!","title":"Login success"}  { }}) != (*models.Notification=&{bb39e579-17f2-4299-a91f-fc2eb45ce293 notification.LoginSuccessNotification notification.User 1 {"content":"Congratulations, your login is successful!","title":"Login success"}  { }})
        at: [D:/work/study/goravel/framework/mocks/database/db/Query.go:1142 D:/work/study/goravel/framework/notification/channels/database.go:34 D:/work/study/goravel/framework/notification/application.go:51 D:/work/study/goravel/framework/notification/application_test.go:146]
    Query.go:4259: FAIL:	Insert(*models.Notification)
        		at: [D:/work/study/goravel/framework/mocks/database/db/Query.go:1178 D:/work/study/goravel/framework/notification/application_test.go:139]
    Query.go:4259: FAIL: 0 out of 1 expectation(s) were met.
        	The code you are testing needs to make 1 more call(s).
        	at: [D:/work/study/goravel/framework/mocks/database/db/Query.go:4259 D:/work/program/GoEnvironment/g/gopath/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1308 D:/work/program/GoEnvironment/g/gopath/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1572 D:/work/program/GoEnvironment/g/gopath/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1928 D:/work/program/GoEnvironment/g/gopath/pkg/mod/golang.org/[email protected]/src/runtime/panic.go:615 D:/work/program/GoEnvironment/g/gopath/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1013 D:/work/study/goravel/framework/mocks/database/db/Query.go:1142 D:/work/study/goravel/framework/notification/channels/database.go:34 D:/work/study/goravel/framework/notification/application.go:51 D:/work/study/goravel/framework/notification/application_test.go:146]
--- FAIL: TestApplicationTestSuite/TestDatabaseNotification (0.00s)

--- FAIL: TestApplicationTestSuite (0.00s)

FAIL

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use mock.MatchedBy to assert the model but except ID.

mockQuery.EXPECT().Insert(mock.MatchedBy(func(model *models.Notification) bool {
  model.Data =="{\"content\":\"Congratulations, your login is successful!\",\"title\":\"Login success\"}" && etc.
})).Return(nil, nil).Once()
Image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfectly resolved

@wuzhixiang0827
Copy link
Author

@hwbrzzl
Two questions, please reply:

  1. How to pass notifiables []notification.Notifiable and notif notification.Notif to a job.
  2. How to call the sendnow method in the job.

@hwbrzzl
Copy link
Contributor

hwbrzzl commented Nov 18, 2025

@hwbrzzl Two questions, please reply:

  1. How to pass notifiables []notification.Notifiable and notif notification.Notif to a job.
  2. How to call the sendnow method in the job.

Could you relate your questions to the specific code?

@wuzhixiang0827
Copy link
Author

@hwbrzzl I have solved these two problems. I will submit my code later.

@codecov
Copy link

codecov bot commented Nov 19, 2025

Codecov Report

❌ Patch coverage is 3.98406% with 241 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.42%. Comparing base (c51a5fe) to head (15ba236).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
notification/notification_sender.go 0.00% 43 Missing ⚠️
notification/service_provider.go 0.00% 41 Missing ⚠️
notification/job.go 20.83% 38 Missing ⚠️
notification/utils/notification_method_caller.go 0.00% 37 Missing ⚠️
notification/channels/mail.go 0.00% 33 Missing ⚠️
notification/channels/database.go 0.00% 20 Missing ⚠️
notification/channel_manager.go 0.00% 12 Missing ⚠️
notification/application.go 0.00% 11 Missing ⚠️
foundation/container.go 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1271      +/-   ##
==========================================
- Coverage   68.47%   67.42%   -1.05%     
==========================================
  Files         264      271       +7     
  Lines       15493    15606     +113     
==========================================
- Hits        10609    10523      -86     
- Misses       4406     4626     +220     
+ Partials      478      457      -21     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hwbrzzl
Copy link
Contributor

hwbrzzl commented Nov 20, 2025

Hey @wuzhixiang0827 Could you add the use cases in the PR description? Let's confirm the basic logic first before a deeper optimization.

@wuzhixiang0827
Copy link
Author

Hey @wuzhixiang0827 Could you add the use cases in the PR description? Let's confirm the basic logic first before a deeper optimization.

Sure, I will add it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants