Skip to content

fix: Local datastore query with containedIn not working when field is an array #1666

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

Merged
merged 4 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions integration/test/ParseLocalDatastoreTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,66 @@ function runTest(controller) {
assert.equal(results[0].id, parent3.id);
});

it(`${controller.name} can handle containsAll query on array`, async () => {
const obj1 = new TestObject({ arrayField: [1, 2, 3, 4] });
const obj2 = new TestObject({ arrayField: [0, 2] });
const obj3 = new TestObject({ arrayField: [1, 2, 3] });
await Parse.Object.saveAll([obj1, obj2, obj3]);
await Parse.Object.pinAll([obj1, obj2, obj3]);

let query = new Parse.Query(TestObject);
query.containsAll('arrayField', [1, 2]);
query.fromPin();
let results = await query.find();
expect(results.length).toBe(2);

query = new Parse.Query(TestObject);
query.containsAll('arrayField', [5, 6]);
query.fromPin();
results = await query.find();
expect(results.length).toBe(0);
});

it(`${controller.name} can handle containedIn query on array`, async () => {
const obj1 = new TestObject({ arrayField: [1, 2, 3, 4] });
const obj2 = new TestObject({ arrayField: [0, 2] });
const obj3 = new TestObject({ arrayField: [1, 2, 3] });
await Parse.Object.saveAll([obj1, obj2, obj3]);
await Parse.Object.pinAll([obj1, obj2, obj3]);

let query = new Parse.Query(TestObject);
query.containedIn('arrayField', [3]);
query.fromPin();
let results = await query.find();
expect(results.length).toEqual(2);

query = new Parse.Query(TestObject);
query.containedIn('arrayField', [5]);
query.fromPin();
results = await query.find();
expect(results.length).toEqual(0);
});

it(`${controller.name} can handle notContainedIn query on array`, async () => {
const obj1 = new TestObject({ arrayField: [1, 2, 3, 4] });
const obj2 = new TestObject({ arrayField: [0, 2] });
const obj3 = new TestObject({ arrayField: [1, 2, 3] });
await Parse.Object.saveAll([obj1, obj2, obj3]);
await Parse.Object.pinAll([obj1, obj2, obj3]);

let query = new Parse.Query(TestObject);
query.notContainedIn('arrayField', [3]);
query.fromPin();
let results = await query.find();
expect(results.length).toEqual(1);

query = new Parse.Query(TestObject);
query.notContainedIn('arrayField', [5]);
query.fromPin();
results = await query.find();
expect(results.length).toEqual(3);
});

it(`${controller.name} can test equality with undefined`, async () => {
const query = new Parse.Query('BoxedNumber');
query.equalTo('number', undefined);
Expand Down
7 changes: 7 additions & 0 deletions src/OfflineQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ function contains(haystack, needle) {
}
return false;
}
if (Array.isArray(needle)) {
for (const need of needle) {
if (contains(haystack, need)) {
return true;
}
}
}
return haystack.indexOf(needle) > -1;
}

Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/OfflineQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,33 @@ describe('OfflineQuery', () => {
expect(matchesQuery(q.className, message, [], q)).toBe(false);
});

it('should support containedIn with array of pointers', () => {
const profile1 = new ParseObject('Profile');
profile1.id = 'yeahaw';
const profile2 = new ParseObject('Profile');
profile2.id = 'yes';

const message = new ParseObject('Message');
message.id = 'O2';
message.set('profiles', [profile1, profile2]);

let q = new ParseQuery('Message');
q.containedIn('profiles', [
ParseObject.fromJSON({ className: 'Profile', objectId: 'no' }),
ParseObject.fromJSON({ className: 'Profile', objectId: 'yes' }),
]);

expect(matchesQuery(q.className, message, [], q)).toBe(true);

q = new ParseQuery('Message');
q.containedIn('profiles', [
ParseObject.fromJSON({ className: 'Profile', objectId: 'no' }),
ParseObject.fromJSON({ className: 'Profile', objectId: 'nope' }),
]);

expect(matchesQuery(q.className, message, [], q)).toBe(false);
});

it('should support notContainedIn with pointers', () => {
const profile = new ParseObject('Profile');
profile.id = 'abc';
Expand Down