-
Notifications
You must be signed in to change notification settings - Fork 45
fix: use injected project client for programmatic table push #259
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
suhasdeshpande
wants to merge
1
commit into
master
Choose a base branch
from
suhas/fix-programmatic-push-tables-project-context
base: master
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
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
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,109 @@ | ||
| import { describe, expect, test, mock } from "bun:test"; | ||
|
|
||
| describe("Push.pushTables programmatic mode", () => { | ||
| test("uses injected project client for table columns and indexes", async () => { | ||
| const projectClient = { _tag: "project-client" } as any; | ||
| const consoleClient = { _tag: "console-client" } as any; | ||
|
|
||
| const databaseServiceClientArgs: any[] = []; | ||
| const tableServiceClientArgs: any[] = []; | ||
|
|
||
| mock.module("../lib/services.js", () => { | ||
| const databasesService = { | ||
| createStringAttribute: async () => ({}), | ||
| createIndex: async () => ({}), | ||
| listAttributes: async () => ({ | ||
| total: 1, | ||
| attributes: [{ key: "title", status: "available" }], | ||
| }), | ||
| listIndexes: async () => ({ | ||
| total: 1, | ||
| indexes: [{ key: "idx_title", status: "available" }], | ||
| }), | ||
| }; | ||
|
|
||
| const tablesService = { | ||
| getTable: async () => { | ||
| throw { code: 404 }; | ||
| }, | ||
| createTable: async () => ({}), | ||
| }; | ||
|
|
||
| const throwIfNoProjectClient = (sdk?: any) => { | ||
| if (!sdk) { | ||
| throw new Error( | ||
| "Project is not set. Please run `appwrite init project`.", | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| getProxyService: async () => ({}), | ||
| getConsoleService: async () => ({}), | ||
| getFunctionsService: async () => ({}), | ||
| getSitesService: async () => ({}), | ||
| getStorageService: async () => ({}), | ||
| getMessagingService: async () => ({}), | ||
| getOrganizationsService: async () => ({}), | ||
| getTeamsService: async () => ({}), | ||
| getProjectsService: async () => ({}), | ||
| getDatabasesService: async (sdk?: any) => { | ||
| databaseServiceClientArgs.push(sdk); | ||
| throwIfNoProjectClient(sdk); | ||
| return databasesService as any; | ||
| }, | ||
| getTablesDBService: async (sdk?: any) => { | ||
| tableServiceClientArgs.push(sdk); | ||
| throwIfNoProjectClient(sdk); | ||
| return tablesService as any; | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| const { Push } = await import("../lib/commands/push.ts"); | ||
| const push = new Push(projectClient, consoleClient, true); | ||
|
|
||
| const result = await push.pushTables( | ||
| [ | ||
| { | ||
| $id: "tbl_1", | ||
| databaseId: "db_1", | ||
| name: "Regression Table", | ||
| rowSecurity: true, | ||
| enabled: true, | ||
| columns: [ | ||
| { | ||
| key: "title", | ||
| type: "string", | ||
| size: 255, | ||
| required: true, | ||
| default: null, | ||
| array: false, | ||
| encrypt: false, | ||
| }, | ||
| ], | ||
| indexes: [ | ||
| { | ||
| key: "idx_title", | ||
| type: "key", | ||
| columns: ["title"], | ||
| orders: ["ASC"], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| { attempts: 1, skipConfirmation: true }, | ||
| ); | ||
|
|
||
| expect(result.errors).toHaveLength(0); | ||
| expect(result.successfullyPushed).toBe(1); | ||
| expect(databaseServiceClientArgs.length).toBeGreaterThan(0); | ||
| expect( | ||
| databaseServiceClientArgs.every((sdk) => sdk === projectClient), | ||
| ).toBe(true); | ||
| expect(tableServiceClientArgs.length).toBeGreaterThan(0); | ||
| expect(tableServiceClientArgs.every((sdk) => sdk === projectClient)).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
| }); | ||
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.
mock.moduleinside atest()body can leak to other test files.As of Bun v1.0.19, Bun resolves the specifier passed to
mock.module()as though it were an import, using the resolved path as the module-cache key. So"../lib/services.js"fromtests/correctly resolves tolib/services.js, matching the same module imported bypush.ts,attributes.ts, andpools.ts. The path and themock.module→ dynamicimport()ordering are both valid.However, there is a known Bun behavior:
mock.modulealso mocks the functions in other test files where they aren't wanted to be mocked, and this is the case even when the function is defined inside the test block. If additional test files are added later that import (directly or transitively) fromlib/services.js, this mock may interfere with them.Consider moving the
mock.modulecall to a--preloadfile or callingmock.restore()at the end of the test to limit scope.🛡️ Suggested cleanup to prevent leakage
🤖 Prompt for AI Agents