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))
| 134 | // Mocked runs a subtest with a mocked database. Removing the overhead of a real |
| 135 | // postgres database resulting in much faster tests. |
| 136 | func (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. |
no test coverage detected