-
Notifications
You must be signed in to change notification settings - Fork 62
Fix ObjectFetcher internal BTQL cursor override during pagination #1370
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
colinbennettbrain
wants to merge
3
commits into
main
Choose a base branch
from
fix/js-internal-btql-limit-override
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
90566c4
Iternal_btql limit no longer overwritten by DEFAULT_FETCH_BATCH_SIZE
colinbennettbrain 5e3eaa1
Fix prettier formatting in object-fetcher test
colinbennettbrain a1588ed
Refactor ObjectFetcher to prevent internal BTQL cursor from overridin…
colinbennettbrain 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,132 @@ | ||
| import { describe, expect, test, vi } from "vitest"; | ||
| import { | ||
| DEFAULT_FETCH_BATCH_SIZE, | ||
| ObjectFetcher, | ||
| type BraintrustState, | ||
| } from "./logger"; | ||
| import { configureNode } from "./node"; | ||
|
|
||
| configureNode(); | ||
|
|
||
| type TestRecord = { id: string }; | ||
|
|
||
| type MockBtqlResponse = { | ||
| data: Array<Record<string, unknown>>; | ||
| cursor?: string | null; | ||
| }; | ||
|
|
||
| function createPostResponse(response: MockBtqlResponse) { | ||
| return { | ||
| json: vi.fn().mockResolvedValue(response), | ||
| }; | ||
| } | ||
|
|
||
| function createPostMock( | ||
| response: MockBtqlResponse = { data: [], cursor: null }, | ||
| ) { | ||
| return vi.fn().mockResolvedValue(createPostResponse(response)); | ||
| } | ||
|
|
||
| class TestObjectFetcher extends ObjectFetcher<TestRecord> { | ||
| constructor( | ||
| private readonly postMock: ReturnType<typeof createPostMock>, | ||
| internalBtql?: Record<string, unknown>, | ||
| ) { | ||
| super("dataset", undefined, undefined, internalBtql); | ||
| } | ||
|
|
||
| public get id(): Promise<string> { | ||
| return Promise.resolve("test-dataset-id"); | ||
| } | ||
|
|
||
| protected async getState(): Promise<BraintrustState> { | ||
| return { | ||
| apiConn: () => ({ | ||
| post: this.postMock, | ||
| }), | ||
| } as unknown as BraintrustState; | ||
| } | ||
| } | ||
|
|
||
| async function triggerFetch( | ||
| fetcher: TestObjectFetcher, | ||
| options?: { batchSize?: number }, | ||
| ) { | ||
| await fetcher.fetchedData(options); | ||
| } | ||
|
|
||
| function getBtqlQuery( | ||
| postMock: ReturnType<typeof createPostMock>, | ||
| callIndex = 0, | ||
| ) { | ||
| const call = postMock.mock.calls[callIndex]; | ||
| expect(call).toBeDefined(); | ||
| const requestBody = call[1] as { query: Record<string, unknown> }; | ||
| return requestBody.query; | ||
| } | ||
|
|
||
| describe("ObjectFetcher internal BTQL limit handling", () => { | ||
| test("preserves custom _internal_btql limit instead of default batch size", async () => { | ||
| const postMock = createPostMock(); | ||
| const fetcher = new TestObjectFetcher(postMock, { | ||
| limit: 50, | ||
| where: { op: "eq", left: "foo", right: "bar" }, | ||
| }); | ||
|
|
||
| await triggerFetch(fetcher); | ||
|
|
||
| expect(postMock).toHaveBeenCalledTimes(1); | ||
| const query = getBtqlQuery(postMock); | ||
| expect(query.limit).toBe(50); | ||
| expect(query.where).toEqual({ op: "eq", left: "foo", right: "bar" }); | ||
| }); | ||
|
|
||
| test("uses default batch size when no _internal_btql limit is provided", async () => { | ||
| const postMock = createPostMock(); | ||
| const fetcher = new TestObjectFetcher(postMock); | ||
|
|
||
| await triggerFetch(fetcher); | ||
|
|
||
| const query = getBtqlQuery(postMock); | ||
| expect(query.limit).toBe(DEFAULT_FETCH_BATCH_SIZE); | ||
| }); | ||
|
|
||
| test("uses explicit fetch batchSize when no _internal_btql limit is provided", async () => { | ||
| const postMock = createPostMock(); | ||
| const fetcher = new TestObjectFetcher(postMock); | ||
|
|
||
| await triggerFetch(fetcher, { batchSize: 17 }); | ||
|
|
||
| const query = getBtqlQuery(postMock); | ||
| expect(query.limit).toBe(17); | ||
| }); | ||
|
|
||
| test("does not allow _internal_btql cursor to override pagination cursor", async () => { | ||
| const postMock = vi | ||
| .fn() | ||
| .mockResolvedValueOnce( | ||
| createPostResponse({ | ||
| data: [{ id: "record-1" }], | ||
| cursor: "next-page-cursor", | ||
| }), | ||
| ) | ||
| .mockResolvedValueOnce( | ||
| createPostResponse({ | ||
| data: [{ id: "record-2" }], | ||
| cursor: null, | ||
| }), | ||
| ); | ||
| const fetcher = new TestObjectFetcher(postMock, { | ||
| cursor: "stale-cursor", | ||
| limit: 1, | ||
| }); | ||
|
|
||
| await triggerFetch(fetcher); | ||
|
|
||
| expect(postMock).toHaveBeenCalledTimes(2); | ||
| const firstQuery = getBtqlQuery(postMock, 0); | ||
| const secondQuery = getBtqlQuery(postMock, 1); | ||
| expect(firstQuery.cursor).toBeUndefined(); | ||
| expect(secondQuery.cursor).toBe("next-page-cursor"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm where is this used?