Merge pull request #1046 from ParsePlatform/flovilmart.fix1014

Fixes bug related to subqueries on unfetched objects
This commit is contained in:
Florent Vilmart
2016-03-15 19:26:13 -04:00
2 changed files with 191 additions and 28 deletions

View File

@@ -1,3 +1,4 @@
'use strict';
// This is a port of the test suite:
// hungry/js/test/parse_relation_test.js
@@ -501,5 +502,154 @@ describe('Parse.Relation testing', () => {
});
});
it('should properly get related objects with unfetched queries', (done) => {
let objects = [];
let owners = [];
let allObjects = [];
// Build 10 Objects and 10 owners
while (objects.length != 10) {
let object = new Parse.Object('AnObject');
object.set({
index: objects.length,
even: objects.length % 2 == 0
});
objects.push(object);
let owner = new Parse.Object('AnOwner');
owners.push(owner);
allObjects.push(object);
allObjects.push(owner);
}
let anotherOwner = new Parse.Object('AnotherOwner');
return Parse.Object.saveAll(allObjects.concat([anotherOwner])).then(() => {
// put all the AnObject into the anotherOwner relationKey
anotherOwner.relation('relationKey').add(objects);
// Set each object[i] into owner[i];
owners.forEach((owner,i) => {
owner.set('key', objects[i]);
});
return Parse.Object.saveAll(owners.concat([anotherOwner]));
}).then(() => {
// Query on the relation of another owner
let object = new Parse.Object('AnotherOwner');
object.id = anotherOwner.id;
let relationQuery = object.relation('relationKey').query();
// Just get the even ones
relationQuery.equalTo('even', true);
// Make the query on anOwner
let query = new Parse.Query('AnOwner');
// where key match the relation query.
query.matchesQuery('key', relationQuery);
query.include('key');
return query.find();
}).then((results) => {
expect(results.length).toBe(5);
results.forEach((result) => {
expect(result.get('key').get('even')).toBe(true);
});
return Promise.resolve();
}).then(() => {
// Query on the relation of another owner
let object = new Parse.Object('AnotherOwner');
object.id = anotherOwner.id;
let relationQuery = object.relation('relationKey').query();
// Just get the even ones
relationQuery.equalTo('even', true);
// Make the query on anOwner
let query = new Parse.Query('AnOwner');
// where key match the relation query.
query.doesNotMatchQuery('key', relationQuery);
query.include('key');
return query.find();
}).then((results) => {
expect(results.length).toBe(5);
results.forEach((result) => {
expect(result.get('key').get('even')).toBe(false);
});
done();
})
});
it("select query", function(done) {
var RestaurantObject = Parse.Object.extend("Restaurant");
var PersonObject = Parse.Object.extend("Person");
var OwnerObject = Parse.Object.extend('Owner');
var restaurants = [
new RestaurantObject({ ratings: 5, location: "Djibouti" }),
new RestaurantObject({ ratings: 3, location: "Ouagadougou" }),
];
let persons = [
new PersonObject({ name: "Bob", hometown: "Djibouti" }),
new PersonObject({ name: "Tom", hometown: "Ouagadougou" }),
new PersonObject({ name: "Billy", hometown: "Detroit" }),
];
let owner = new OwnerObject({name: 'Joe'});
let ownerId;
let allObjects = [owner].concat(restaurants).concat(persons);
expect(allObjects.length).toEqual(6);
Parse.Object.saveAll([owner].concat(restaurants).concat(persons)).then(function() {
ownerId = owner.id;
owner.relation('restaurants').add(restaurants);
return owner.save()
}).then(() => {
let unfetchedOwner = new OwnerObject();
unfetchedOwner.id = owner.id;
var query = unfetchedOwner.relation('restaurants').query();
query.greaterThan("ratings", 4);
var mainQuery = new Parse.Query(PersonObject);
mainQuery.matchesKeyInQuery("hometown", "location", query);
mainQuery.find(expectSuccess({
success: function(results) {
equal(results.length, 1);
if (results.length > 0) {
equal(results[0].get('name'), 'Bob');
}
done();
}
}));
});
});
it("dontSelect query", function(done) {
var RestaurantObject = Parse.Object.extend("Restaurant");
var PersonObject = Parse.Object.extend("Person");
var OwnerObject = Parse.Object.extend('Owner');
var restaurants = [
new RestaurantObject({ ratings: 5, location: "Djibouti" }),
new RestaurantObject({ ratings: 3, location: "Ouagadougou" }),
];
let persons = [
new PersonObject({ name: "Bob", hometown: "Djibouti" }),
new PersonObject({ name: "Tom", hometown: "Ouagadougou" }),
new PersonObject({ name: "Billy", hometown: "Detroit" }),
];
let owner = new OwnerObject({name: 'Joe'});
let ownerId;
let allObjects = [owner].concat(restaurants).concat(persons);
expect(allObjects.length).toEqual(6);
Parse.Object.saveAll([owner].concat(restaurants).concat(persons)).then(function() {
ownerId = owner.id;
owner.relation('restaurants').add(restaurants);
return owner.save()
}).then(() => {
let unfetchedOwner = new OwnerObject();
unfetchedOwner.id = owner.id;
var query = unfetchedOwner.relation('restaurants').query();
query.greaterThan("ratings", 4);
var mainQuery = new Parse.Query(PersonObject);
mainQuery.doesNotMatchKeyInQuery("hometown", "location", query);
mainQuery.ascending('name');
mainQuery.find(expectSuccess({
success: function(results) {
equal(results.length, 2);
if (results.length > 0) {
equal(results[0].get('name'), 'Billy');
equal(results[1].get('name'), 'Tom');
}
done();
}
}));
});
});
});

View File

@@ -20,7 +20,6 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}) {
this.className = className;
this.restWhere = restWhere;
this.response = null;
this.findOptions = {};
if (!this.auth.isMaster) {
this.findOptions.acl = this.auth.user ? [this.auth.user.id] : null;
@@ -205,15 +204,19 @@ RestQuery.prototype.replaceInQuery = function() {
'improper usage of $inQuery');
}
let additionalOptions = {
redirectClassNameForKey: inQueryValue.redirectClassNameForKey
};
var subquery = new RestQuery(
this.config, this.auth, inQueryValue.className,
inQueryValue.where);
inQueryValue.where, additionalOptions);
return subquery.execute().then((response) => {
var values = [];
for (var result of response.results) {
values.push({
__type: 'Pointer',
className: inQueryValue.className,
className: subquery.className,
objectId: result.objectId
});
}
@@ -223,7 +226,6 @@ RestQuery.prototype.replaceInQuery = function() {
} else {
inQueryObject['$in'] = values;
}
// Recurse to repeat
return this.replaceInQuery();
});
@@ -246,15 +248,19 @@ RestQuery.prototype.replaceNotInQuery = function() {
'improper usage of $notInQuery');
}
let additionalOptions = {
redirectClassNameForKey: notInQueryValue.redirectClassNameForKey
};
var subquery = new RestQuery(
this.config, this.auth, notInQueryValue.className,
notInQueryValue.where);
notInQueryValue.where, additionalOptions);
return subquery.execute().then((response) => {
var values = [];
for (var result of response.results) {
values.push({
__type: 'Pointer',
className: notInQueryValue.className,
className: subquery.className,
objectId: result.objectId
});
}
@@ -293,9 +299,13 @@ RestQuery.prototype.replaceSelect = function() {
'improper usage of $select');
}
let additionalOptions = {
redirectClassNameForKey: selectValue.query.redirectClassNameForKey
};
var subquery = new RestQuery(
this.config, this.auth, selectValue.query.className,
selectValue.query.where);
selectValue.query.where, additionalOptions);
return subquery.execute().then((response) => {
var values = [];
for (var result of response.results) {
@@ -334,9 +344,13 @@ RestQuery.prototype.replaceDontSelect = function() {
throw new Parse.Error(Parse.Error.INVALID_QUERY,
'improper usage of $dontSelect');
}
let additionalOptions = {
redirectClassNameForKey: dontSelectValue.query.redirectClassNameForKey
};
var subquery = new RestQuery(
this.config, this.auth, dontSelectValue.query.className,
dontSelectValue.query.where);
dontSelectValue.query.where, additionalOptions);
return subquery.execute().then((response) => {
var values = [];
for (var result of response.results) {
@@ -385,7 +399,6 @@ RestQuery.prototype.runFind = function() {
r.className = this.redirectClassName;
}
}
this.response = {results: results};
});
};