MCPcopy Index your code
hub / github.com/coder/coder / Mocked

Method Mocked

coderd/database/dbauthz/setup_test.go:136–175  ·  view source on GitHub ↗

Mocked runs a subtest with a mocked database. Removing the overhead of a real postgres database resulting in much faster tests.

(testCaseF func(dmb *dbmock.MockStore, faker *gofakeit.Faker, check *expects))

Source from the content-addressed store, hash-verified

134// Mocked runs a subtest with a mocked database. Removing the overhead of a real
135// postgres database resulting in much faster tests.
136func (s *MethodTestSuite) Mocked(testCaseF func(dmb *dbmock.MockStore, faker *gofakeit.Faker, check *expects)) func() {
137 t := s.T()
138 mDB := dbmock.NewMockStore(gomock.NewController(t))
139 mDB.EXPECT().Wrappers().Return([]string{}).AnyTimes()
140 // dbauthz now expands DB-backed system roles (e.g. organization-member)
141 // during role-assignment validation, which triggers a CustomRoles lookup
142 // with IncludeSystemRoles=true.
143 mDB.EXPECT().CustomRoles(gomock.Any(), includeSystemRolesMatcher{}).DoAndReturn(func(_ context.Context, arg database.CustomRolesParams) ([]database.CustomRole, error) {
144 if len(arg.LookupRoles) == 0 {
145 return []database.CustomRole{}, nil
146 }
147
148 out := make([]database.CustomRole, 0, len(arg.LookupRoles))
149
150 for _, pair := range arg.LookupRoles {
151 // Minimal set of fields that the tested code uses.
152 out = append(out, database.CustomRole{
153 Name: pair.Name,
154 OrganizationID: uuid.NullUUID{
155 UUID: pair.OrganizationID,
156 Valid: pair.OrganizationID != uuid.Nil,
157 },
158 IsSystem: rolestore.IsSystemRoleName(pair.Name),
159 ID: uuid.New(),
160 })
161 }
162
163 return out, nil
164 }).AnyTimes()
165
166 // Use a constant seed to prevent flakes from random data generation.
167 faker := gofakeit.New(0)
168
169 // The usual Subtest assumes the test setup will use a real database to populate
170 // with data. In this mocked case, we want to pass the underlying mocked database
171 // to the test case instead.
172 return s.SubtestWithDB(mDB, func(_ database.Store, check *expects) {
173 testCaseF(mDB, faker, check)
174 })
175}
176
177// Subtest starts up a real postgres database for each test case.
178// Deprecated: Use 'Mocked' instead for much faster tests.

Callers 15

TestAPIKeyMethod · 0.95
TestAuditLogsMethod · 0.95
TestBoundaryLogsMethod · 0.95
TestConnectionLogsMethod · 0.95
TestChatsMethod · 0.95
TestFileMethod · 0.95
TestGroupMethod · 0.95
TestProvisionerJobMethod · 0.95
TestAISeatMethod · 0.95
TestLicenseMethod · 0.95
TestOrganizationMethod · 0.95
TestWorkspaceProxyMethod · 0.95

Calls 7

EXPECTMethod · 0.95
SubtestWithDBMethod · 0.95
NewMockStoreFunction · 0.92
IsSystemRoleNameFunction · 0.92
WrappersMethod · 0.65
CustomRolesMethod · 0.65
NewMethod · 0.65

Tested by

no test coverage detected