diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-09-19 19:49:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-19 19:49:59 +0800 |
commit | a4bfef265d9e512830350635a0489c2cdcd6508f (patch) | |
tree | 1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/webhook_test.go | |
parent | 462306e263db5a809dbe2cdf62e99307aeff28de (diff) | |
download | gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip |
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db
* Fix lint
* Fix lint
* Fix test
* Fix lint
* Fix lint
* revert unnecessary change
* Fix test
* Fix wrong replace string
* Use *Context
* Correct committer spelling and fix wrong replaced words
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/webhook_test.go')
-rw-r--r-- | models/webhook_test.go | 121 |
1 files changed, 61 insertions, 60 deletions
diff --git a/models/webhook_test.go b/models/webhook_test.go index 625d643856..d48fa365be 100644 --- a/models/webhook_test.go +++ b/models/webhook_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/json" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -28,23 +29,23 @@ func TestIsValidHookContentType(t *testing.T) { } func TestWebhook_History(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) - webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) + assert.NoError(t, db.PrepareTestDatabase()) + webhook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) tasks, err := webhook.History(0) assert.NoError(t, err) if assert.Len(t, tasks, 1) { assert.Equal(t, int64(1), tasks[0].ID) } - webhook = AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) + webhook = db.AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) tasks, err = webhook.History(0) assert.NoError(t, err) assert.Len(t, tasks, 0) } func TestWebhook_UpdateEvent(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) - webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) + assert.NoError(t, db.PrepareTestDatabase()) + webhook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook) hookEvent := &HookEvent{ PushOnly: true, SendEverything: false, @@ -90,35 +91,35 @@ func TestCreateWebhook(t *testing.T) { ContentType: ContentTypeJSON, Events: `{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}`, } - AssertNotExistsBean(t, hook) + db.AssertNotExistsBean(t, hook) assert.NoError(t, CreateWebhook(hook)) - AssertExistsAndLoadBean(t, hook) + db.AssertExistsAndLoadBean(t, hook) } func TestGetWebhookByRepoID(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hook, err := GetWebhookByRepoID(1, 1) assert.NoError(t, err) assert.Equal(t, int64(1), hook.ID) - _, err = GetWebhookByRepoID(NonexistentID, NonexistentID) + _, err = GetWebhookByRepoID(db.NonexistentID, db.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } func TestGetWebhookByOrgID(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hook, err := GetWebhookByOrgID(3, 3) assert.NoError(t, err) assert.Equal(t, int64(3), hook.ID) - _, err = GetWebhookByOrgID(NonexistentID, NonexistentID) + _, err = GetWebhookByOrgID(db.NonexistentID, db.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } func TestGetActiveWebhooksByRepoID(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hooks, err := ListWebhooksByOpts(&ListWebhookOptions{RepoID: 1, IsActive: util.OptionalBoolTrue}) assert.NoError(t, err) if assert.Len(t, hooks, 1) { @@ -128,7 +129,7 @@ func TestGetActiveWebhooksByRepoID(t *testing.T) { } func TestGetWebhooksByRepoID(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hooks, err := ListWebhooksByOpts(&ListWebhookOptions{RepoID: 1}) assert.NoError(t, err) if assert.Len(t, hooks, 2) { @@ -138,7 +139,7 @@ func TestGetWebhooksByRepoID(t *testing.T) { } func TestGetActiveWebhooksByOrgID(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hooks, err := ListWebhooksByOpts(&ListWebhookOptions{OrgID: 3, IsActive: util.OptionalBoolTrue}) assert.NoError(t, err) if assert.Len(t, hooks, 1) { @@ -148,7 +149,7 @@ func TestGetActiveWebhooksByOrgID(t *testing.T) { } func TestGetWebhooksByOrgID(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hooks, err := ListWebhooksByOpts(&ListWebhookOptions{OrgID: 3}) assert.NoError(t, err) if assert.Len(t, hooks, 1) { @@ -158,76 +159,76 @@ func TestGetWebhooksByOrgID(t *testing.T) { } func TestUpdateWebhook(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) - hook := AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) + assert.NoError(t, db.PrepareTestDatabase()) + hook := db.AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook) hook.IsActive = true hook.ContentType = ContentTypeForm - AssertNotExistsBean(t, hook) + db.AssertNotExistsBean(t, hook) assert.NoError(t, UpdateWebhook(hook)) - AssertExistsAndLoadBean(t, hook) + db.AssertExistsAndLoadBean(t, hook) } func TestDeleteWebhookByRepoID(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) - AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1}) + assert.NoError(t, db.PrepareTestDatabase()) + db.AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1}) assert.NoError(t, DeleteWebhookByRepoID(1, 2)) - AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1}) + db.AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1}) - err := DeleteWebhookByRepoID(NonexistentID, NonexistentID) + err := DeleteWebhookByRepoID(db.NonexistentID, db.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } func TestDeleteWebhookByOrgID(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) - AssertExistsAndLoadBean(t, &Webhook{ID: 3, OrgID: 3}) + assert.NoError(t, db.PrepareTestDatabase()) + db.AssertExistsAndLoadBean(t, &Webhook{ID: 3, OrgID: 3}) assert.NoError(t, DeleteWebhookByOrgID(3, 3)) - AssertNotExistsBean(t, &Webhook{ID: 3, OrgID: 3}) + db.AssertNotExistsBean(t, &Webhook{ID: 3, OrgID: 3}) - err := DeleteWebhookByOrgID(NonexistentID, NonexistentID) + err := DeleteWebhookByOrgID(db.NonexistentID, db.NonexistentID) assert.Error(t, err) assert.True(t, IsErrWebhookNotExist(err)) } func TestHookTasks(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hookTasks, err := HookTasks(1, 1) assert.NoError(t, err) if assert.Len(t, hookTasks, 1) { assert.Equal(t, int64(1), hookTasks[0].ID) } - hookTasks, err = HookTasks(NonexistentID, 1) + hookTasks, err = HookTasks(db.NonexistentID, 1) assert.NoError(t, err) assert.Len(t, hookTasks, 0) } func TestCreateHookTask(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 3, HookID: 3, Payloader: &api.PushPayload{}, } - AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) } func TestUpdateHookTask(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - hook := AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask) + hook := db.AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask) hook.PayloadContent = "new payload content" hook.DeliveredString = "new delivered string" hook.IsDelivered = true - AssertNotExistsBean(t, hook) + db.AssertNotExistsBean(t, hook) assert.NoError(t, UpdateHookTask(hook)) - AssertExistsAndLoadBean(t, hook) + db.AssertExistsAndLoadBean(t, hook) } func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 3, HookID: 3, @@ -235,32 +236,32 @@ func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) { IsDelivered: true, Delivered: time.Now().UnixNano(), } - AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0)) - AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) } func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 2, HookID: 4, Payloader: &api.PushPayload{}, IsDelivered: false, } - AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0)) - AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) } func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 2, HookID: 4, @@ -268,16 +269,16 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) { IsDelivered: true, Delivered: time.Now().UnixNano(), } - AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 1)) - AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) } func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 3, HookID: 3, @@ -285,32 +286,32 @@ func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) { IsDelivered: true, Delivered: time.Now().AddDate(0, 0, -8).UnixNano(), } - AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0)) - AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) } func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 2, HookID: 4, Payloader: &api.PushPayload{}, IsDelivered: false, } - AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0)) - AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) } func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *testing.T) { - assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) hookTask := &HookTask{ RepoID: 2, HookID: 4, @@ -318,10 +319,10 @@ func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *test IsDelivered: true, Delivered: time.Now().AddDate(0, 0, -6).UnixNano(), } - AssertNotExistsBean(t, hookTask) + db.AssertNotExistsBean(t, hookTask) assert.NoError(t, CreateHookTask(hookTask)) - AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0)) - AssertExistsAndLoadBean(t, hookTask) + db.AssertExistsAndLoadBean(t, hookTask) } |