forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8009d37
commit 8b2b26c
Showing
17 changed files
with
941 additions
and
674 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,35 +293,208 @@ func TestDaoBeforeHooksError(t *testing.T) { | |
testApp, _ := tests.NewTestApp() | ||
defer testApp.Cleanup() | ||
|
||
testApp.Dao().BeforeCreateFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
baseDao := testApp.Dao() | ||
|
||
baseDao.BeforeCreateFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
return errors.New("before_create") | ||
} | ||
testApp.Dao().BeforeUpdateFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
baseDao.BeforeUpdateFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
return errors.New("before_update") | ||
} | ||
testApp.Dao().BeforeDeleteFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
baseDao.BeforeDeleteFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
return errors.New("before_delete") | ||
} | ||
|
||
existingModel, _ := testApp.Dao().FindAdminByEmail("[email protected]") | ||
|
||
// try to create | ||
// test create error | ||
// --- | ||
newModel := &models.Admin{} | ||
newModel.Email = "[email protected]" | ||
if err := testApp.Dao().Save(newModel); err.Error() != "before_create" { | ||
if err := baseDao.Save(newModel); err.Error() != "before_create" { | ||
t.Fatalf("Expected before_create error, got %v", err) | ||
} | ||
|
||
// try to update | ||
// test update error | ||
// --- | ||
if err := testApp.Dao().Save(existingModel); err.Error() != "before_update" { | ||
if err := baseDao.Save(existingModel); err.Error() != "before_update" { | ||
t.Fatalf("Expected before_update error, got %v", err) | ||
} | ||
|
||
// try to delete | ||
// test delete error | ||
// --- | ||
if err := testApp.Dao().Delete(existingModel); err.Error() != "before_delete" { | ||
if err := baseDao.Delete(existingModel); err.Error() != "before_delete" { | ||
t.Fatalf("Expected before_delete error, got %v", err) | ||
} | ||
} | ||
|
||
func TestDaoTransactionHooksCallsOnFailure(t *testing.T) { | ||
testApp, _ := tests.NewTestApp() | ||
defer testApp.Cleanup() | ||
|
||
beforeCreateFuncCalls := 0 | ||
beforeUpdateFuncCalls := 0 | ||
beforeDeleteFuncCalls := 0 | ||
afterCreateFuncCalls := 0 | ||
afterUpdateFuncCalls := 0 | ||
afterDeleteFuncCalls := 0 | ||
|
||
baseDao := testApp.Dao() | ||
|
||
baseDao.BeforeCreateFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
beforeCreateFuncCalls++ | ||
return nil | ||
} | ||
baseDao.BeforeUpdateFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
beforeUpdateFuncCalls++ | ||
return nil | ||
} | ||
baseDao.BeforeDeleteFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
beforeDeleteFuncCalls++ | ||
return nil | ||
} | ||
|
||
baseDao.AfterCreateFunc = func(eventDao *daos.Dao, m models.Model) { | ||
afterCreateFuncCalls++ | ||
} | ||
baseDao.AfterUpdateFunc = func(eventDao *daos.Dao, m models.Model) { | ||
afterUpdateFuncCalls++ | ||
} | ||
baseDao.AfterDeleteFunc = func(eventDao *daos.Dao, m models.Model) { | ||
afterDeleteFuncCalls++ | ||
} | ||
|
||
existingModel, _ := testApp.Dao().FindAdminByEmail("[email protected]") | ||
|
||
baseDao.RunInTransaction(func(txDao *daos.Dao) error { | ||
// test create | ||
// --- | ||
newModel := &models.Admin{} | ||
newModel.Email = "[email protected]" | ||
newModel.SetPassword("123456") | ||
if err := txDao.Save(newModel); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// test update (twice) | ||
// --- | ||
if err := txDao.Save(existingModel); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := txDao.Save(existingModel); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// test delete | ||
// --- | ||
if err := txDao.Delete(existingModel); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return errors.New("test_tx_error") | ||
}) | ||
|
||
if beforeCreateFuncCalls != 1 { | ||
t.Fatalf("Expected beforeCreateFuncCalls to be called 1 times, got %d", beforeCreateFuncCalls) | ||
} | ||
if beforeUpdateFuncCalls != 2 { | ||
t.Fatalf("Expected beforeUpdateFuncCalls to be called 2 times, got %d", beforeUpdateFuncCalls) | ||
} | ||
if beforeDeleteFuncCalls != 1 { | ||
t.Fatalf("Expected beforeDeleteFuncCalls to be called 1 times, got %d", beforeDeleteFuncCalls) | ||
} | ||
if afterCreateFuncCalls != 0 { | ||
t.Fatalf("Expected afterCreateFuncCalls to be called 0 times, got %d", afterCreateFuncCalls) | ||
} | ||
if afterUpdateFuncCalls != 0 { | ||
t.Fatalf("Expected afterUpdateFuncCalls to be called 0 times, got %d", afterUpdateFuncCalls) | ||
} | ||
if afterDeleteFuncCalls != 0 { | ||
t.Fatalf("Expected afterDeleteFuncCalls to be called 0 times, got %d", afterDeleteFuncCalls) | ||
} | ||
} | ||
|
||
func TestDaoTransactionHooksCallsOnSuccess(t *testing.T) { | ||
testApp, _ := tests.NewTestApp() | ||
defer testApp.Cleanup() | ||
|
||
beforeCreateFuncCalls := 0 | ||
beforeUpdateFuncCalls := 0 | ||
beforeDeleteFuncCalls := 0 | ||
afterCreateFuncCalls := 0 | ||
afterUpdateFuncCalls := 0 | ||
afterDeleteFuncCalls := 0 | ||
|
||
baseDao := testApp.Dao() | ||
|
||
baseDao.BeforeCreateFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
beforeCreateFuncCalls++ | ||
return nil | ||
} | ||
baseDao.BeforeUpdateFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
beforeUpdateFuncCalls++ | ||
return nil | ||
} | ||
baseDao.BeforeDeleteFunc = func(eventDao *daos.Dao, m models.Model) error { | ||
beforeDeleteFuncCalls++ | ||
return nil | ||
} | ||
|
||
baseDao.AfterCreateFunc = func(eventDao *daos.Dao, m models.Model) { | ||
afterCreateFuncCalls++ | ||
} | ||
baseDao.AfterUpdateFunc = func(eventDao *daos.Dao, m models.Model) { | ||
afterUpdateFuncCalls++ | ||
} | ||
baseDao.AfterDeleteFunc = func(eventDao *daos.Dao, m models.Model) { | ||
afterDeleteFuncCalls++ | ||
} | ||
|
||
existingModel, _ := testApp.Dao().FindAdminByEmail("[email protected]") | ||
|
||
baseDao.RunInTransaction(func(txDao *daos.Dao) error { | ||
// test create | ||
// --- | ||
newModel := &models.Admin{} | ||
newModel.Email = "[email protected]" | ||
newModel.SetPassword("123456") | ||
if err := txDao.Save(newModel); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// test update (twice) | ||
// --- | ||
if err := txDao.Save(existingModel); err != nil { | ||
t.Fatal(err) | ||
} | ||
if err := txDao.Save(existingModel); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// test delete | ||
// --- | ||
if err := txDao.Delete(existingModel); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
if beforeCreateFuncCalls != 1 { | ||
t.Fatalf("Expected beforeCreateFuncCalls to be called 1 times, got %d", beforeCreateFuncCalls) | ||
} | ||
if beforeUpdateFuncCalls != 2 { | ||
t.Fatalf("Expected beforeUpdateFuncCalls to be called 2 times, got %d", beforeUpdateFuncCalls) | ||
} | ||
if beforeDeleteFuncCalls != 1 { | ||
t.Fatalf("Expected beforeDeleteFuncCalls to be called 1 times, got %d", beforeDeleteFuncCalls) | ||
} | ||
if afterCreateFuncCalls != 1 { | ||
t.Fatalf("Expected afterCreateFuncCalls to be called 1 times, got %d", afterCreateFuncCalls) | ||
} | ||
if afterUpdateFuncCalls != 2 { | ||
t.Fatalf("Expected afterUpdateFuncCalls to be called 2 times, got %d", afterUpdateFuncCalls) | ||
} | ||
if afterDeleteFuncCalls != 1 { | ||
t.Fatalf("Expected afterDeleteFuncCalls to be called 1 times, got %d", afterDeleteFuncCalls) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...PageAdminConfirmPasswordReset.e19b0bf2.js → ...PageAdminConfirmPasswordReset.f7f33bee.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...PageAdminRequestPasswordReset.a398d62b.js → ...PageAdminRequestPasswordReset.06a917cb.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.