mock_gen.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "testing"
  7. )
  8. //go:generate go-mockgen -f gogs.io/gogs/internal/db -i AccessTokensStore -i LFSStore -i PermsStore -o mocks.go
  9. func SetMockAccessTokensStore(t *testing.T, mock AccessTokensStore) {
  10. before := AccessTokens
  11. AccessTokens = mock
  12. t.Cleanup(func() {
  13. AccessTokens = before
  14. })
  15. }
  16. func SetMockLFSStore(t *testing.T, mock LFSStore) {
  17. before := LFS
  18. LFS = mock
  19. t.Cleanup(func() {
  20. LFS = before
  21. })
  22. }
  23. var _ loginSourceFilesStore = (*mockLoginSourceFilesStore)(nil)
  24. type mockLoginSourceFilesStore struct {
  25. MockGetByID func(id int64) (*LoginSource, error)
  26. MockLen func() int
  27. MockList func(opts ListLoginSourceOpts) []*LoginSource
  28. MockUpdate func(source *LoginSource)
  29. }
  30. func (m *mockLoginSourceFilesStore) GetByID(id int64) (*LoginSource, error) {
  31. return m.MockGetByID(id)
  32. }
  33. func (m *mockLoginSourceFilesStore) Len() int {
  34. return m.MockLen()
  35. }
  36. func (m *mockLoginSourceFilesStore) List(opts ListLoginSourceOpts) []*LoginSource {
  37. return m.MockList(opts)
  38. }
  39. func (m *mockLoginSourceFilesStore) Update(source *LoginSource) {
  40. m.MockUpdate(source)
  41. }
  42. func setMockLoginSourceFilesStore(t *testing.T, db *loginSources, mock loginSourceFilesStore) {
  43. before := db.files
  44. db.files = mock
  45. t.Cleanup(func() {
  46. db.files = before
  47. })
  48. }
  49. var _ loginSourceFileStore = (*mockLoginSourceFileStore)(nil)
  50. type mockLoginSourceFileStore struct {
  51. MockSetGeneral func(name, value string)
  52. MockSetConfig func(cfg interface{}) error
  53. MockSave func() error
  54. }
  55. func (m *mockLoginSourceFileStore) SetGeneral(name, value string) {
  56. m.MockSetGeneral(name, value)
  57. }
  58. func (m *mockLoginSourceFileStore) SetConfig(cfg interface{}) error {
  59. return m.MockSetConfig(cfg)
  60. }
  61. func (m *mockLoginSourceFileStore) Save() error {
  62. return m.MockSave()
  63. }
  64. func SetMockPermsStore(t *testing.T, mock PermsStore) {
  65. before := Perms
  66. Perms = mock
  67. t.Cleanup(func() {
  68. Perms = before
  69. })
  70. }
  71. var _ ReposStore = (*MockReposStore)(nil)
  72. type MockReposStore struct {
  73. MockGetByName func(ownerID int64, name string) (*Repository, error)
  74. }
  75. func (m *MockReposStore) GetByName(ownerID int64, name string) (*Repository, error) {
  76. return m.MockGetByName(ownerID, name)
  77. }
  78. func SetMockReposStore(t *testing.T, mock ReposStore) {
  79. before := Repos
  80. Repos = mock
  81. t.Cleanup(func() {
  82. Repos = before
  83. })
  84. }
  85. var _ TwoFactorsStore = (*MockTwoFactorsStore)(nil)
  86. type MockTwoFactorsStore struct {
  87. MockCreate func(userID int64, key, secret string) error
  88. MockGetByUserID func(userID int64) (*TwoFactor, error)
  89. MockIsUserEnabled func(userID int64) bool
  90. }
  91. func (m *MockTwoFactorsStore) Create(userID int64, key, secret string) error {
  92. return m.MockCreate(userID, key, secret)
  93. }
  94. func (m *MockTwoFactorsStore) GetByUserID(userID int64) (*TwoFactor, error) {
  95. return m.MockGetByUserID(userID)
  96. }
  97. func (m *MockTwoFactorsStore) IsUserEnabled(userID int64) bool {
  98. return m.MockIsUserEnabled(userID)
  99. }
  100. func SetMockTwoFactorsStore(t *testing.T, mock TwoFactorsStore) {
  101. before := TwoFactors
  102. TwoFactors = mock
  103. t.Cleanup(func() {
  104. TwoFactors = before
  105. })
  106. }
  107. var _ UsersStore = (*MockUsersStore)(nil)
  108. type MockUsersStore struct {
  109. MockAuthenticate func(username, password string, loginSourceID int64) (*User, error)
  110. MockCreate func(username, email string, opts CreateUserOpts) (*User, error)
  111. MockGetByEmail func(email string) (*User, error)
  112. MockGetByID func(id int64) (*User, error)
  113. MockGetByUsername func(username string) (*User, error)
  114. }
  115. func (m *MockUsersStore) Authenticate(username, password string, loginSourceID int64) (*User, error) {
  116. return m.MockAuthenticate(username, password, loginSourceID)
  117. }
  118. func (m *MockUsersStore) Create(username, email string, opts CreateUserOpts) (*User, error) {
  119. return m.MockCreate(username, email, opts)
  120. }
  121. func (m *MockUsersStore) GetByEmail(email string) (*User, error) {
  122. return m.MockGetByEmail(email)
  123. }
  124. func (m *MockUsersStore) GetByID(id int64) (*User, error) {
  125. return m.MockGetByID(id)
  126. }
  127. func (m *MockUsersStore) GetByUsername(username string) (*User, error) {
  128. return m.MockGetByUsername(username)
  129. }
  130. func SetMockUsersStore(t *testing.T, mock UsersStore) {
  131. before := Users
  132. Users = mock
  133. t.Cleanup(func() {
  134. Users = before
  135. })
  136. }