-
Notifications
You must be signed in to change notification settings - Fork 5
Document mock adapter usage #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rafael81
wants to merge
2
commits into
prest:main
Choose a base branch
from
rafael81:docs/mock-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # Mock Adapter | ||
|
|
||
| The `github.com/prest/prest/v2/adapters/mock` package provides a lightweight | ||
| implementation of the `adapters.Adapter` interface for tests. It avoids a real | ||
| PostgreSQL connection and returns queued scanner responses from memory. | ||
|
|
||
| Use it when a controller, middleware, router, or other adapter-backed test needs | ||
| pREST behavior but does not need database-specific behavior. | ||
|
|
||
| ### Basic usage | ||
|
|
||
| Create the adapter with the current test, then assign it to the global pREST | ||
| configuration used by the code under test: | ||
|
|
||
| ```go | ||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/prest/prest/v2/adapters/mock" | ||
| "github.com/prest/prest/v2/config" | ||
| ) | ||
|
|
||
| func TestHandler(t *testing.T) { | ||
| adapter := mock.New(t) | ||
| config.PrestConf.Adapter = adapter | ||
|
|
||
| adapter.AddItem([]byte(`[{"id":1,"name":"prest"}]`), nil, false) | ||
|
|
||
| // Run code that calls config.PrestConf.Adapter.Query(...) | ||
| } | ||
| ``` | ||
|
|
||
| `mock.New(t)` also registers a `database/sql` driver named `mock`. The | ||
| transaction helpers use a mock DSN named `prest`, so tests can exercise | ||
| transaction paths without opening a network connection. | ||
|
|
||
| ### Queued responses | ||
|
|
||
| The mock adapter stores responses in `Mock.Items`. Each query-like or | ||
| mutation-like method consumes the next queued item and returns it as a | ||
| `scanner.PrestScanner`. | ||
|
|
||
| Queue items with `AddItem`: | ||
|
|
||
| ```go | ||
| adapter.AddItem(responseBody, responseError, isCount) | ||
| ``` | ||
|
|
||
| The arguments map to `mock.Item` fields: | ||
|
|
||
| * `responseBody`: JSON bytes returned by the scanner. | ||
| * `responseError`: error exposed by the scanner. | ||
| * `isCount`: count metadata returned by clause helpers such as `DatabaseClause` | ||
| and `SchemaClause`. | ||
|
|
||
| Add one item for each adapter operation the test expects. If code calls a | ||
| mocked operation that consumes a response and no items are queued, the mock | ||
| fails the test with `do not have any operations to perform`. | ||
|
|
||
| ### Permissions | ||
|
|
||
| `TablePermissions` reads `config.PrestConf.AccessConf`. When access | ||
| restrictions are disabled, it returns `true`. When restrictions are enabled, it | ||
| checks table-level permissions first and then user-specific table permissions | ||
| when a user name is provided. | ||
|
|
||
| This lets tests cover authorization behavior without a database fixture: | ||
|
|
||
| ```go | ||
| config.PrestConf.AccessConf.Restrict = true | ||
| config.PrestConf.AccessConf.Tables = []config.TablesConf{ | ||
| {Name: "books", Permissions: []string{"read"}}, | ||
| } | ||
|
|
||
| if !adapter.TablePermissions("books", "read", "") { | ||
| t.Fatal("expected read access") | ||
| } | ||
| ``` | ||
|
|
||
| ### Generated mocks | ||
|
|
||
| This package is different from `adapters/mockgen`. The `mockgen` package is | ||
| generated by GoMock from the adapter interfaces and is useful when a test needs | ||
| strict call expectations. The `mock` package is hand-written and is better for | ||
| controller-style tests that need scanner responses and simple permission | ||
| behavior. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.