-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
feature: run query via livequery #9864
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
dblythy
wants to merge
8
commits into
parse-community:alpha
Choose a base branch
from
dblythy:feature/live-query-query
base: alpha
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.
+387
−1
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f0630e1
feature: run query via livequery
dblythy aa91cd5
Update ParseLiveQueryServer.ts
dblythy 1140484
add subscription check
dblythy 7f31285
fix tests
dblythy e3a169c
fix tests
dblythy 4266ab6
Update ParseLiveQueryQuery.spec.js
dblythy 2a7ad48
Update ParseLiveQueryQuery.spec.js
dblythy 466d789
Update ParseLiveQueryQuery.spec.js
dblythy 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| 'use strict'; | ||
|
|
||
| const Parse = require('parse/node'); | ||
|
|
||
| describe('ParseLiveQuery query operation', function () { | ||
| beforeEach(function (done) { | ||
| Parse.CoreManager.getLiveQueryController().setDefaultLiveQueryClient(null); | ||
| // Mock ParseWebSocketServer | ||
| const mockParseWebSocketServer = jasmine.createSpy('ParseWebSocketServer'); | ||
| jasmine.mockLibrary( | ||
| '../lib/LiveQuery/ParseWebSocketServer', | ||
| 'ParseWebSocketServer', | ||
| mockParseWebSocketServer | ||
| ); | ||
| // Mock Client pushError | ||
| const Client = require('../lib/LiveQuery/Client').Client; | ||
| Client.pushError = jasmine.createSpy('pushError'); | ||
| done(); | ||
| }); | ||
|
|
||
| afterEach(async function () { | ||
| const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient(); | ||
| if (client) { | ||
| await client.close(); | ||
| } | ||
| jasmine.restoreLibrary('../lib/LiveQuery/ParseWebSocketServer', 'ParseWebSocketServer'); | ||
| }); | ||
|
|
||
| function addMockClient(parseLiveQueryServer, clientId) { | ||
| const Client = require('../lib/LiveQuery/Client').Client; | ||
| const client = new Client(clientId, {}); | ||
| client.pushResult = jasmine.createSpy('pushResult'); | ||
| parseLiveQueryServer.clients.set(clientId, client); | ||
| return client; | ||
| } | ||
|
|
||
| function addMockSubscription(parseLiveQueryServer, clientId, requestId, parseWebSocket, query = {}) { | ||
| const Subscription = require('../lib/LiveQuery/Subscription').Subscription; | ||
| const subscription = new Subscription( | ||
| query.className || 'TestObject', | ||
| query.where || {}, | ||
| 'hash' | ||
| ); | ||
|
|
||
| // Add to server subscriptions | ||
| if (!parseLiveQueryServer.subscriptions.has(subscription.className)) { | ||
| parseLiveQueryServer.subscriptions.set(subscription.className, new Map()); | ||
| } | ||
| const classSubscriptions = parseLiveQueryServer.subscriptions.get(subscription.className); | ||
| classSubscriptions.set('hash', subscription); | ||
|
|
||
| // Add to client | ||
| const client = parseLiveQueryServer.clients.get(clientId); | ||
| const subscriptionInfo = { | ||
| subscription: subscription, | ||
| keys: query.keys, | ||
| }; | ||
| if (parseWebSocket.sessionToken) { | ||
| subscriptionInfo.sessionToken = parseWebSocket.sessionToken; | ||
| } | ||
| client.subscriptionInfos.set(requestId, subscriptionInfo); | ||
|
|
||
| return subscription; | ||
| } | ||
|
|
||
| it('can handle query command with existing subscription', async () => { | ||
| await reconfigureServer({ | ||
| liveQuery: { | ||
| classNames: ['TestObject'], | ||
| }, | ||
| startLiveQueryServer: true, | ||
| verbose: false, | ||
| silent: true, | ||
| }); | ||
|
|
||
| const { ParseLiveQueryServer } = require('../lib/LiveQuery/ParseLiveQueryServer'); | ||
| const parseLiveQueryServer = new ParseLiveQueryServer({ | ||
| appId: 'test', | ||
| masterKey: 'test', | ||
| serverURL: 'http://localhost:1337/parse' | ||
| }); | ||
dblythy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Create test objects | ||
| const TestObject = Parse.Object.extend('TestObject'); | ||
| const obj1 = new TestObject(); | ||
| obj1.set('name', 'object1'); | ||
| await obj1.save(); | ||
|
|
||
| const obj2 = new TestObject(); | ||
| obj2.set('name', 'object2'); | ||
| await obj2.save(); | ||
|
|
||
| // Add mock client | ||
| const clientId = 1; | ||
| const client = addMockClient(parseLiveQueryServer, clientId); | ||
| client.hasMasterKey = true; | ||
|
|
||
| // Add mock subscription | ||
| const parseWebSocket = { clientId: 1 }; | ||
| const requestId = 2; | ||
| const query = { | ||
| className: 'TestObject', | ||
| where: {}, | ||
| }; | ||
| addMockSubscription(parseLiveQueryServer, clientId, requestId, parseWebSocket, query); | ||
|
|
||
| // Handle query command | ||
| const request = { | ||
| op: 'query', | ||
| requestId: requestId, | ||
| }; | ||
|
|
||
| await parseLiveQueryServer._handleQuery(parseWebSocket, request); | ||
|
|
||
| // Verify pushResult was called | ||
| expect(client.pushResult).toHaveBeenCalled(); | ||
| const results = client.pushResult.calls.mostRecent().args[1]; | ||
| expect(Array.isArray(results)).toBe(true); | ||
| expect(results.length).toBe(2); | ||
| expect(results.some(r => r.name === 'object1')).toBe(true); | ||
| expect(results.some(r => r.name === 'object2')).toBe(true); | ||
| }); | ||
|
|
||
| it('can handle query command without clientId', async () => { | ||
| const { ParseLiveQueryServer } = require('../lib/LiveQuery/ParseLiveQueryServer'); | ||
| const parseLiveQueryServer = new ParseLiveQueryServer({}); | ||
| const incompleteParseConn = {}; | ||
| await parseLiveQueryServer._handleQuery(incompleteParseConn, {}); | ||
|
|
||
| const Client = require('../lib/LiveQuery/Client').Client; | ||
| expect(Client.pushError).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('can handle query command without subscription', async () => { | ||
| const { ParseLiveQueryServer } = require('../lib/LiveQuery/ParseLiveQueryServer'); | ||
| const parseLiveQueryServer = new ParseLiveQueryServer({}); | ||
| const clientId = 1; | ||
| addMockClient(parseLiveQueryServer, clientId); | ||
|
|
||
| const parseWebSocket = { clientId: 1 }; | ||
| const request = { | ||
| op: 'query', | ||
| requestId: 999, // Non-existent subscription | ||
| }; | ||
|
|
||
| await parseLiveQueryServer._handleQuery(parseWebSocket, request); | ||
|
|
||
| const Client = require('../lib/LiveQuery/Client').Client; | ||
| expect(Client.pushError).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('respects field filtering (keys) when executing query', async () => { | ||
| await reconfigureServer({ | ||
| liveQuery: { | ||
| classNames: ['TestObject'], | ||
| }, | ||
| startLiveQueryServer: true, | ||
| verbose: false, | ||
| silent: true, | ||
| }); | ||
|
|
||
| const { ParseLiveQueryServer } = require('../lib/LiveQuery/ParseLiveQueryServer'); | ||
| const parseLiveQueryServer = new ParseLiveQueryServer({ | ||
| appId: 'test', | ||
| masterKey: 'test', | ||
| serverURL: 'http://localhost:1337/parse' | ||
| }); | ||
|
|
||
| // Create test object with multiple fields | ||
| const TestObject = Parse.Object.extend('TestObject'); | ||
| const obj = new TestObject(); | ||
| obj.set('name', 'test'); | ||
| obj.set('color', 'blue'); | ||
| obj.set('size', 'large'); | ||
| await obj.save(); | ||
|
|
||
| // Add mock client | ||
| const clientId = 1; | ||
| const client = addMockClient(parseLiveQueryServer, clientId); | ||
| client.hasMasterKey = true; | ||
|
|
||
| // Add mock subscription with keys | ||
| const parseWebSocket = { clientId: 1 }; | ||
| const requestId = 2; | ||
| const query = { | ||
| className: 'TestObject', | ||
| where: {}, | ||
| keys: ['name', 'color'], // Only these fields | ||
| }; | ||
| addMockSubscription(parseLiveQueryServer, clientId, requestId, parseWebSocket, query); | ||
|
|
||
| // Handle query command | ||
| const request = { | ||
| op: 'query', | ||
| requestId: requestId, | ||
| }; | ||
|
|
||
| await parseLiveQueryServer._handleQuery(parseWebSocket, request); | ||
|
|
||
| // Verify results | ||
| expect(client.pushResult).toHaveBeenCalled(); | ||
| const results = client.pushResult.calls.mostRecent().args[1]; | ||
| expect(results.length).toBe(1); | ||
|
|
||
| // Results should include selected fields | ||
| expect(results[0].name).toBe('test'); | ||
| expect(results[0].color).toBe('blue'); | ||
|
|
||
| // Results should NOT include size | ||
| expect(results[0].size).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('handles query with where constraints', async () => { | ||
| await reconfigureServer({ | ||
| liveQuery: { | ||
| classNames: ['TestObject'], | ||
| }, | ||
| startLiveQueryServer: true, | ||
| verbose: false, | ||
| silent: true, | ||
| }); | ||
|
|
||
| const { ParseLiveQueryServer } = require('../lib/LiveQuery/ParseLiveQueryServer'); | ||
| const parseLiveQueryServer = new ParseLiveQueryServer({ | ||
| appId: 'test', | ||
| masterKey: 'test', | ||
| serverURL: 'http://localhost:1337/parse' | ||
| }); | ||
|
|
||
| // Create test objects | ||
| const TestObject = Parse.Object.extend('TestObject'); | ||
| const obj1 = new TestObject(); | ||
| obj1.set('name', 'match'); | ||
| obj1.set('status', 'active'); | ||
| await obj1.save(); | ||
|
|
||
| const obj2 = new TestObject(); | ||
| obj2.set('name', 'nomatch'); | ||
| obj2.set('status', 'inactive'); | ||
| await obj2.save(); | ||
|
|
||
| // Add mock client | ||
| const clientId = 1; | ||
| const client = addMockClient(parseLiveQueryServer, clientId); | ||
| client.hasMasterKey = true; | ||
|
|
||
| // Add mock subscription with where clause | ||
| const parseWebSocket = { clientId: 1 }; | ||
| const requestId = 2; | ||
| const query = { | ||
| className: 'TestObject', | ||
| where: { status: 'active' }, // Only active objects | ||
| }; | ||
| addMockSubscription(parseLiveQueryServer, clientId, requestId, parseWebSocket, query); | ||
|
|
||
| // Handle query command | ||
| const request = { | ||
| op: 'query', | ||
| requestId: requestId, | ||
| }; | ||
|
|
||
| await parseLiveQueryServer._handleQuery(parseWebSocket, request); | ||
|
|
||
| // Verify results | ||
| expect(client.pushResult).toHaveBeenCalled(); | ||
| const results = client.pushResult.calls.mostRecent().args[1]; | ||
| expect(results.length).toBe(1); | ||
| expect(results[0].name).toBe('match'); | ||
| expect(results[0].status).toBe('active'); | ||
| }); | ||
| }); | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.