Skip to content

Commit

Permalink
fix: Local datastore query with containedIn not working when field …
Browse files Browse the repository at this point in the history
…is an array (#1666)
  • Loading branch information
dplewis authored Jan 27, 2023
1 parent e9a2cc9 commit 2391bff
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
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

0 comments on commit 2391bff

Please sign in to comment.