BVB Source Codes

parse-server Show ParseRelation.spec.js Source code

Return Download parse-server: download ParseRelation.spec.js Source code - Download parse-server Source code - Type:.js
  1. 'use strict';
  2. // This is a port of the test suite:
  3. // hungry/js/test/parse_relation_test.js
  4.  
  5. var ChildObject = Parse.Object.extend({className: "ChildObject"});
  6. var ParentObject = Parse.Object.extend({className: "ParentObject"});
  7.  
  8. describe('Parse.Relation testing', () => {
  9.   it("simple add and remove relation", (done) => {
  10.     var child = new ChildObject();
  11.     child.set("x", 2);
  12.     var parent = new ParentObject();
  13.     parent.set("x", 4);
  14.     var relation = parent.relation("child");
  15.  
  16.     child.save().then(() => {
  17.       relation.add(child);
  18.       return parent.save();
  19.     }, (e) => {
  20.       fail(e);
  21.     }).then(() => {
  22.       return relation.query().find();
  23.     }).then((list) => {
  24.       equal(list.length, 1,
  25.             "Should have gotten one element back");
  26.       equal(list[0].id, child.id,
  27.             "Should have gotten the right value");
  28.       ok(!parent.dirty("child"),
  29.          "The relation should not be dirty");
  30.  
  31.       relation.remove(child);
  32.       return parent.save();
  33.     }).then(() => {
  34.       return relation.query().find();
  35.     }).then((list) => {
  36.       equal(list.length, 0,
  37.             "Delete should have worked");
  38.       ok(!parent.dirty("child"),
  39.          "The relation should not be dirty");
  40.       done();
  41.     });
  42.   });
  43.  
  44.   it("query relation without schema", (done) => {
  45.     var ChildObject = Parse.Object.extend("ChildObject");
  46.     var childObjects = [];
  47.     for (var i = 0; i < 10; i++) {
  48.       childObjects.push(new ChildObject({x:i}));
  49.     }
  50.  
  51.     Parse.Object.saveAll(childObjects, expectSuccess({
  52.       success: function() {
  53.         var ParentObject = Parse.Object.extend("ParentObject");
  54.         var parent = new ParentObject();
  55.         parent.set("x", 4);
  56.         var relation = parent.relation("child");
  57.         relation.add(childObjects[0]);
  58.         parent.save(null, expectSuccess({
  59.           success: function() {
  60.             var parentAgain = new ParentObject();
  61.             parentAgain.id = parent.id;
  62.             var relation = parentAgain.relation("child");
  63.             relation.query().find(expectSuccess({
  64.               success: function(list) {
  65.                 equal(list.length, 1,
  66.                       "Should have gotten one element back");
  67.                 equal(list[0].id, childObjects[0].id,
  68.                       "Should have gotten the right value");
  69.                 done();
  70.               }
  71.             }));
  72.           }
  73.         }));
  74.       }
  75.     }));
  76.   });
  77.  
  78.   it("relations are constructed right from query", (done) => {
  79.  
  80.     var ChildObject = Parse.Object.extend("ChildObject");
  81.     var childObjects = [];
  82.     for (var i = 0; i < 10; i++) {
  83.       childObjects.push(new ChildObject({x: i}));
  84.     }
  85.  
  86.     Parse.Object.saveAll(childObjects, {
  87.       success: function() {
  88.         var ParentObject = Parse.Object.extend("ParentObject");
  89.         var parent = new ParentObject();
  90.         parent.set("x", 4);
  91.         var relation = parent.relation("child");
  92.         relation.add(childObjects[0]);
  93.         parent.save(null, {
  94.           success: function() {
  95.             var query = new Parse.Query(ParentObject);
  96.             query.get(parent.id, {
  97.               success: function(object) {
  98.                 var relationAgain = object.relation("child");
  99.                 relationAgain.query().find({
  100.                   success: function(list) {
  101.                     equal(list.length, 1,
  102.                           "Should have gotten one element back");
  103.                     equal(list[0].id, childObjects[0].id,
  104.                           "Should have gotten the right value");
  105.                     ok(!parent.dirty("child"),
  106.                        "The relation should not be dirty");
  107.                     done();
  108.                   },
  109.                   error: function() {
  110.                     ok(false, "This shouldn't have failed");
  111.                     done();
  112.                   }
  113.                 });
  114.  
  115.               }
  116.             });
  117.           }
  118.         });
  119.       }
  120.     });
  121.  
  122.   });
  123.  
  124.   it("compound add and remove relation", (done) => {
  125.     var ChildObject = Parse.Object.extend("ChildObject");
  126.     var childObjects = [];
  127.     for (var i = 0; i < 10; i++) {
  128.       childObjects.push(new ChildObject({x: i}));
  129.     }
  130.  
  131.     var parent;
  132.     var relation;
  133.  
  134.     Parse.Object.saveAll(childObjects).then(function() {
  135.       var ParentObject = Parse.Object.extend('ParentObject');
  136.       parent = new ParentObject();
  137.       parent.set('x', 4);
  138.       relation = parent.relation('child');
  139.       relation.add(childObjects[0]);
  140.       relation.add(childObjects[1]);
  141.       relation.remove(childObjects[0]);
  142.       relation.add(childObjects[2]);
  143.       return parent.save();
  144.     }).then(function() {
  145.       return relation.query().find();
  146.     }).then(function(list) {
  147.       equal(list.length, 2, 'Should have gotten two elements back');
  148.       ok(!parent.dirty('child'), 'The relation should not be dirty');
  149.       relation.remove(childObjects[1]);
  150.       relation.remove(childObjects[2]);
  151.       relation.add(childObjects[1]);
  152.       relation.add(childObjects[0]);
  153.       return parent.save();
  154.     }).then(function() {
  155.       return relation.query().find();
  156.     }).then(function(list) {
  157.       equal(list.length, 2, 'Deletes and then adds should have worked');
  158.       ok(!parent.dirty('child'), 'The relation should not be dirty');
  159.       done();
  160.     }, function(err) {
  161.       ok(false, err.message);
  162.       done();
  163.     });
  164.   });
  165.  
  166.  
  167.   it_exclude_dbs(['postgres'])("queries with relations", (done) => {
  168.  
  169.     var ChildObject = Parse.Object.extend("ChildObject");
  170.     var childObjects = [];
  171.     for (var i = 0; i < 10; i++) {
  172.       childObjects.push(new ChildObject({x: i}));
  173.     }
  174.  
  175.     Parse.Object.saveAll(childObjects, {
  176.       success: function() {
  177.         var ParentObject = Parse.Object.extend("ParentObject");
  178.         var parent = new ParentObject();
  179.         parent.set("x", 4);
  180.         var relation = parent.relation("child");
  181.         relation.add(childObjects[0]);
  182.         relation.add(childObjects[1]);
  183.         relation.add(childObjects[2]);
  184.         parent.save(null, {
  185.           success: function() {
  186.             var query = relation.query();
  187.             query.equalTo("x", 2);
  188.             query.find({
  189.               success: function(list) {
  190.                 equal(list.length, 1,
  191.                       "There should only be one element");
  192.                 ok(list[0] instanceof ChildObject,
  193.                    "Should be of type ChildObject");
  194.                 equal(list[0].id, childObjects[2].id,
  195.                       "We should have gotten back the right result");
  196.                 done();
  197.               }
  198.             });
  199.           }
  200.         });
  201.       }
  202.     });
  203.   });
  204.  
  205.   it("queries on relation fields", (done) => {
  206.     var ChildObject = Parse.Object.extend("ChildObject");
  207.     var childObjects = [];
  208.     for (var i = 0; i < 10; i++) {
  209.       childObjects.push(new ChildObject({x: i}));
  210.     }
  211.  
  212.     Parse.Object.saveAll(childObjects, {
  213.       success: function() {
  214.         var ParentObject = Parse.Object.extend("ParentObject");
  215.         var parent = new ParentObject();
  216.         parent.set("x", 4);
  217.         var relation = parent.relation("child");
  218.         relation.add(childObjects[0]);
  219.         relation.add(childObjects[1]);
  220.         relation.add(childObjects[2]);
  221.         var parent2 = new ParentObject();
  222.         parent2.set("x", 3);
  223.         var relation2 = parent2.relation("child");
  224.         relation2.add(childObjects[4]);
  225.         relation2.add(childObjects[5]);
  226.         relation2.add(childObjects[6]);
  227.         var parents = [];
  228.         parents.push(parent);
  229.         parents.push(parent2);
  230.         Parse.Object.saveAll(parents, {
  231.           success: function() {
  232.             var query = new Parse.Query(ParentObject);
  233.             var objects = [];
  234.             objects.push(childObjects[4]);
  235.             objects.push(childObjects[9]);
  236.             query.containedIn("child", objects);
  237.             query.find({
  238.               success: function(list) {
  239.                 equal(list.length, 1, "There should be only one result");
  240.                 equal(list[0].id, parent2.id,
  241.                       "Should have gotten back the right result");
  242.                 done();
  243.               }
  244.             });
  245.           }
  246.         });
  247.       }
  248.     });
  249.   });
  250.  
  251.   it("queries on relation fields with multiple containedIn (regression test for #1271)", (done) => {
  252.     const ChildObject = Parse.Object.extend("ChildObject");
  253.     const childObjects = [];
  254.     for (let i = 0; i < 10; i++) {
  255.       childObjects.push(new ChildObject({x: i}));
  256.     }
  257.  
  258.     Parse.Object.saveAll(childObjects).then(() => {
  259.       const ParentObject = Parse.Object.extend("ParentObject");
  260.       const parent = new ParentObject();
  261.       parent.set("x", 4);
  262.       const parent1Children = parent.relation("child");
  263.       parent1Children.add(childObjects[0]);
  264.       parent1Children.add(childObjects[1]);
  265.       parent1Children.add(childObjects[2]);
  266.       const parent2 = new ParentObject();
  267.       parent2.set("x", 3);
  268.       const parent2Children = parent2.relation("child");
  269.       parent2Children.add(childObjects[4]);
  270.       parent2Children.add(childObjects[5]);
  271.       parent2Children.add(childObjects[6]);
  272.  
  273.       const parent2OtherChildren = parent2.relation("otherChild");
  274.       parent2OtherChildren.add(childObjects[0]);
  275.       parent2OtherChildren.add(childObjects[1]);
  276.       parent2OtherChildren.add(childObjects[2]);
  277.  
  278.       return Parse.Object.saveAll([parent, parent2]);
  279.     }).then(() => {
  280.       const objectsWithChild0InBothChildren = new Parse.Query(ParentObject);
  281.       objectsWithChild0InBothChildren.containedIn("child", [childObjects[0]]);
  282.       objectsWithChild0InBothChildren.containedIn("otherChild", [childObjects[0]]);
  283.       return objectsWithChild0InBothChildren.find();
  284.     }).then(objectsWithChild0InBothChildren => {
  285.       //No parent has child 0 in both it's "child" and "otherChild" field;
  286.       expect(objectsWithChild0InBothChildren.length).toEqual(0);
  287.     }).then(() => {
  288.       const objectsWithChild4andOtherChild1 = new Parse.Query(ParentObject);
  289.       objectsWithChild4andOtherChild1.containedIn("child", [childObjects[4]]);
  290.       objectsWithChild4andOtherChild1.containedIn("otherChild", [childObjects[1]]);
  291.       return objectsWithChild4andOtherChild1.find();
  292.     }).then(objects => {
  293.       // parent2 has child 4 and otherChild 1
  294.       expect(objects.length).toEqual(1);
  295.       done();
  296.     });
  297.   });
  298.  
  299.   it_exclude_dbs(['postgres'])("query on pointer and relation fields with equal", (done) => {
  300.     var ChildObject = Parse.Object.extend("ChildObject");
  301.     var childObjects = [];
  302.     for (var i = 0; i < 10; i++) {
  303.       childObjects.push(new ChildObject({x: i}));
  304.     }
  305.  
  306.     Parse.Object.saveAll(childObjects).then(() => {
  307.       var ParentObject = Parse.Object.extend("ParentObject");
  308.       var parent = new ParentObject();
  309.       parent.set("x", 4);
  310.       var relation = parent.relation("toChilds");
  311.       relation.add(childObjects[0]);
  312.       relation.add(childObjects[1]);
  313.       relation.add(childObjects[2]);
  314.  
  315.       var parent2 = new ParentObject();
  316.       parent2.set("x", 3);
  317.       parent2.set("toChild", childObjects[2]);
  318.  
  319.       var parents = [];
  320.       parents.push(parent);
  321.       parents.push(parent2);
  322.       parents.push(new ParentObject());
  323.  
  324.       return Parse.Object.saveAll(parents).then(() => {
  325.         var query = new Parse.Query(ParentObject);
  326.         query.equalTo("objectId", parent.id);
  327.         query.equalTo("toChilds", childObjects[2]);
  328.  
  329.         return query.find().then((list) => {
  330.           equal(list.length, 1, "There should be 1 result");
  331.           done();
  332.         });
  333.       });
  334.     }).catch(err => {
  335.       jfail(err);
  336.       done();
  337.     });
  338.   });
  339.  
  340.   it("query on pointer and relation fields with equal bis", (done) => {
  341.     var ChildObject = Parse.Object.extend("ChildObject");
  342.     var childObjects = [];
  343.     for (var i = 0; i < 10; i++) {
  344.       childObjects.push(new ChildObject({x: i}));
  345.     }
  346.  
  347.     Parse.Object.saveAll(childObjects).then(() => {
  348.       var ParentObject = Parse.Object.extend("ParentObject");
  349.       var parent = new ParentObject();
  350.       parent.set("x", 4);
  351.       var relation = parent.relation("toChilds");
  352.       relation.add(childObjects[0]);
  353.       relation.add(childObjects[1]);
  354.       relation.add(childObjects[2]);
  355.  
  356.       var parent2 = new ParentObject();
  357.       parent2.set("x", 3);
  358.       parent2.relation("toChilds").add(childObjects[2]);
  359.  
  360.       var parents = [];
  361.       parents.push(parent);
  362.       parents.push(parent2);
  363.       parents.push(new ParentObject());
  364.  
  365.       return Parse.Object.saveAll(parents).then(() => {
  366.         var query = new Parse.Query(ParentObject);
  367.         query.equalTo("objectId", parent2.id);
  368.           // childObjects[2] is in 2 relations
  369.           // before the fix, that woul yield 2 results
  370.         query.equalTo("toChilds", childObjects[2]);
  371.  
  372.         return query.find().then((list) => {
  373.           equal(list.length, 1, "There should be 1 result");
  374.           done();
  375.         });
  376.       });
  377.     });
  378.   });
  379.  
  380.   it_exclude_dbs(['postgres'])("or queries on pointer and relation fields", (done) => {
  381.     var ChildObject = Parse.Object.extend("ChildObject");
  382.     var childObjects = [];
  383.     for (var i = 0; i < 10; i++) {
  384.       childObjects.push(new ChildObject({x: i}));
  385.     }
  386.  
  387.     Parse.Object.saveAll(childObjects).then(() => {
  388.       var ParentObject = Parse.Object.extend("ParentObject");
  389.       var parent = new ParentObject();
  390.       parent.set("x", 4);
  391.       var relation = parent.relation("toChilds");
  392.       relation.add(childObjects[0]);
  393.       relation.add(childObjects[1]);
  394.       relation.add(childObjects[2]);
  395.  
  396.       var parent2 = new ParentObject();
  397.       parent2.set("x", 3);
  398.       parent2.set("toChild", childObjects[2]);
  399.  
  400.       var parents = [];
  401.       parents.push(parent);
  402.       parents.push(parent2);
  403.       parents.push(new ParentObject());
  404.  
  405.       return Parse.Object.saveAll(parents).then(() => {
  406.         var query1 = new Parse.Query(ParentObject);
  407.         query1.containedIn("toChilds", [childObjects[2]]);
  408.         var query2 = new Parse.Query(ParentObject);
  409.         query2.equalTo("toChild", childObjects[2]);
  410.         var query = Parse.Query.or(query1, query2);
  411.         return query.find().then((list) => {
  412.           var objectIds = list.map(function(item){
  413.             return item.id;
  414.           });
  415.           expect(objectIds.indexOf(parent.id)).not.toBe(-1);
  416.           expect(objectIds.indexOf(parent2.id)).not.toBe(-1);
  417.           equal(list.length, 2, "There should be 2 results");
  418.           done();
  419.         });
  420.       });
  421.     });
  422.   });
  423.  
  424.  
  425.   it("Get query on relation using un-fetched parent object", (done) => {
  426.     // Setup data model
  427.     var Wheel = Parse.Object.extend('Wheel');
  428.     var Car = Parse.Object.extend('Car');
  429.     var origWheel = new Wheel();
  430.     origWheel.save().then(function() {
  431.       var car = new Car();
  432.       var relation = car.relation('wheels');
  433.       relation.add(origWheel);
  434.       return car.save();
  435.     }).then(function(car) {
  436.       // Test starts here.
  437.       // Create an un-fetched shell car object
  438.       var unfetchedCar = new Car();
  439.       unfetchedCar.id = car.id;
  440.       var relation = unfetchedCar.relation('wheels');
  441.       var query = relation.query();
  442.  
  443.       // Parent object is un-fetched, so this will call /1/classes/Car instead
  444.       // of /1/classes/Wheel and pass { "redirectClassNameForKey":"wheels" }.
  445.       return query.get(origWheel.id);
  446.     }).then(function(wheel) {
  447.       // Make sure this is Wheel and not Car.
  448.       strictEqual(wheel.className, 'Wheel');
  449.       strictEqual(wheel.id, origWheel.id);
  450.     }).then(function() {
  451.       done();
  452.     },function(err) {
  453.       ok(false, 'unexpected error: ' + JSON.stringify(err));
  454.       done();
  455.     });
  456.   });
  457.  
  458.   it("Find query on relation using un-fetched parent object", (done) => {
  459.     // Setup data model
  460.     var Wheel = Parse.Object.extend('Wheel');
  461.     var Car = Parse.Object.extend('Car');
  462.     var origWheel = new Wheel();
  463.     origWheel.save().then(function() {
  464.       var car = new Car();
  465.       var relation = car.relation('wheels');
  466.       relation.add(origWheel);
  467.       return car.save();
  468.     }).then(function(car) {
  469.       // Test starts here.
  470.       // Create an un-fetched shell car object
  471.       var unfetchedCar = new Car();
  472.       unfetchedCar.id = car.id;
  473.       var relation = unfetchedCar.relation('wheels');
  474.       var query = relation.query();
  475.  
  476.       // Parent object is un-fetched, so this will call /1/classes/Car instead
  477.       // of /1/classes/Wheel and pass { "redirectClassNameForKey":"wheels" }.
  478.       return query.find(origWheel.id);
  479.     }).then(function(results) {
  480.       // Make sure this is Wheel and not Car.
  481.       var wheel = results[0];
  482.       strictEqual(wheel.className, 'Wheel');
  483.       strictEqual(wheel.id, origWheel.id);
  484.     }).then(function() {
  485.       done();
  486.     },function(err) {
  487.       ok(false, 'unexpected error: ' + JSON.stringify(err));
  488.       done();
  489.     });
  490.   });
  491.  
  492.   it('Find objects with a related object using equalTo', (done) => {
  493.     // Setup the objects
  494.     var Card = Parse.Object.extend('Card');
  495.     var House = Parse.Object.extend('House');
  496.     var card = new Card();
  497.     card.save().then(() => {
  498.       var house = new House();
  499.       var relation = house.relation('cards');
  500.       relation.add(card);
  501.       return house.save();
  502.     }).then(() => {
  503.       var query = new Parse.Query('House');
  504.       query.equalTo('cards', card);
  505.       return query.find();
  506.     }).then((results) => {
  507.       expect(results.length).toEqual(1);
  508.       done();
  509.     });
  510.   });
  511.  
  512.   it('should properly get related objects with unfetched queries', (done) => {
  513.     const objects = [];
  514.     const owners = [];
  515.     const allObjects = [];
  516.     // Build 10 Objects and 10 owners
  517.     while (objects.length != 10) {
  518.       const object = new Parse.Object('AnObject');
  519.       object.set({
  520.         index: objects.length,
  521.         even: objects.length % 2 == 0
  522.       });
  523.       objects.push(object);
  524.       const owner = new Parse.Object('AnOwner');
  525.       owners.push(owner);
  526.       allObjects.push(object);
  527.       allObjects.push(owner);
  528.     }
  529.  
  530.     const anotherOwner = new Parse.Object('AnotherOwner');
  531.  
  532.     return Parse.Object.saveAll(allObjects.concat([anotherOwner])).then(() => {
  533.       // put all the AnObject into the anotherOwner relationKey
  534.       anotherOwner.relation('relationKey').add(objects);
  535.       // Set each object[i] into owner[i];
  536.       owners.forEach((owner,i) => {
  537.         owner.set('key', objects[i]);
  538.       });
  539.       return Parse.Object.saveAll(owners.concat([anotherOwner]));
  540.     }).then(() => {
  541.       // Query on the relation of another owner
  542.       const object = new Parse.Object('AnotherOwner');
  543.       object.id = anotherOwner.id;
  544.       const relationQuery = object.relation('relationKey').query();
  545.       // Just get the even ones
  546.       relationQuery.equalTo('even', true);
  547.       // Make the query on anOwner
  548.       const query = new Parse.Query('AnOwner');
  549.       // where key match the relation query.
  550.       query.matchesQuery('key', relationQuery);
  551.       query.include('key');
  552.       return query.find();
  553.     }).then((results) => {
  554.       expect(results.length).toBe(5);
  555.       results.forEach((result) => {
  556.         expect(result.get('key').get('even')).toBe(true);
  557.       });
  558.       return Promise.resolve();
  559.     }).then(() => {
  560.       // Query on the relation of another owner
  561.       const object = new Parse.Object('AnotherOwner');
  562.       object.id = anotherOwner.id;
  563.       const relationQuery = object.relation('relationKey').query();
  564.       // Just get the even ones
  565.       relationQuery.equalTo('even', true);
  566.       // Make the query on anOwner
  567.       const query = new Parse.Query('AnOwner');
  568.       // where key match the relation query.
  569.       query.doesNotMatchQuery('key', relationQuery);
  570.       query.include('key');
  571.       return query.find();
  572.     }).then((results) => {
  573.       expect(results.length).toBe(5);
  574.       results.forEach((result) => {
  575.         expect(result.get('key').get('even')).toBe(false);
  576.       });
  577.       done();
  578.     }, (e) => {
  579.       fail(JSON.stringify(e));
  580.       done();
  581.     })
  582.   });
  583.  
  584.   it("select query", function(done) {
  585.     var RestaurantObject = Parse.Object.extend("Restaurant");
  586.     var PersonObject = Parse.Object.extend("Person");
  587.     var OwnerObject = Parse.Object.extend('Owner');
  588.     var restaurants = [
  589.       new RestaurantObject({ ratings: 5, location: "Djibouti" }),
  590.       new RestaurantObject({ ratings: 3, location: "Ouagadougou" }),
  591.     ];
  592.     const persons = [
  593.       new PersonObject({ name: "Bob", hometown: "Djibouti" }),
  594.       new PersonObject({ name: "Tom", hometown: "Ouagadougou" }),
  595.       new PersonObject({ name: "Billy", hometown: "Detroit" }),
  596.     ];
  597.     const owner = new OwnerObject({name: 'Joe'});
  598.     const allObjects = [owner].concat(restaurants).concat(persons);
  599.     expect(allObjects.length).toEqual(6);
  600.     Parse.Object.saveAll([owner].concat(restaurants).concat(persons)).then(function() {
  601.       owner.relation('restaurants').add(restaurants);
  602.       return owner.save()
  603.     }).then(() => {
  604.       const unfetchedOwner = new OwnerObject();
  605.       unfetchedOwner.id = owner.id;
  606.       var query = unfetchedOwner.relation('restaurants').query();
  607.       query.greaterThan("ratings", 4);
  608.       var mainQuery = new Parse.Query(PersonObject);
  609.       mainQuery.matchesKeyInQuery("hometown", "location", query);
  610.       mainQuery.find(expectSuccess({
  611.         success: function(results) {
  612.           equal(results.length, 1);
  613.           if (results.length > 0) {
  614.             equal(results[0].get('name'), 'Bob');
  615.           }
  616.           done();
  617.         }
  618.       }));
  619.     }, (e) => {
  620.       fail(JSON.stringify(e));
  621.       done();
  622.     });
  623.   });
  624.  
  625.   it("dontSelect query", function(done) {
  626.     var RestaurantObject = Parse.Object.extend("Restaurant");
  627.     var PersonObject = Parse.Object.extend("Person");
  628.     var OwnerObject = Parse.Object.extend('Owner');
  629.     var restaurants = [
  630.       new RestaurantObject({ ratings: 5, location: "Djibouti" }),
  631.       new RestaurantObject({ ratings: 3, location: "Ouagadougou" }),
  632.     ];
  633.     const persons = [
  634.       new PersonObject({ name: "Bob", hometown: "Djibouti" }),
  635.       new PersonObject({ name: "Tom", hometown: "Ouagadougou" }),
  636.       new PersonObject({ name: "Billy", hometown: "Detroit" }),
  637.     ];
  638.     const owner = new OwnerObject({name: 'Joe'});
  639.     const allObjects = [owner].concat(restaurants).concat(persons);
  640.     expect(allObjects.length).toEqual(6);
  641.     Parse.Object.saveAll([owner].concat(restaurants).concat(persons)).then(function() {
  642.       owner.relation('restaurants').add(restaurants);
  643.       return owner.save()
  644.     }).then(() => {
  645.       const unfetchedOwner = new OwnerObject();
  646.       unfetchedOwner.id = owner.id;
  647.       var query = unfetchedOwner.relation('restaurants').query();
  648.       query.greaterThan("ratings", 4);
  649.       var mainQuery = new Parse.Query(PersonObject);
  650.       mainQuery.doesNotMatchKeyInQuery("hometown", "location", query);
  651.       mainQuery.ascending('name');
  652.       mainQuery.find(expectSuccess({
  653.         success: function(results) {
  654.           equal(results.length, 2);
  655.           if (results.length > 0) {
  656.             equal(results[0].get('name'), 'Billy');
  657.             equal(results[1].get('name'), 'Tom');
  658.           }
  659.           done();
  660.         }
  661.       }));
  662.     }, (e) => {
  663.       fail(JSON.stringify(e));
  664.       done();
  665.     });
  666.   });
  667.  
  668.   it('relations are not bidirectional (regression test for #871)', done => {
  669.     const PersonObject = Parse.Object.extend("Person");
  670.     const p1 = new PersonObject();
  671.     const p2 = new PersonObject();
  672.     Parse.Object.saveAll([p1, p2]).then(results => {
  673.       const p1 = results[0];
  674.       const p2 = results[1];
  675.       const relation = p1.relation('relation');
  676.       relation.add(p2);
  677.       p1.save().then(() => {
  678.         const query = new Parse.Query(PersonObject);
  679.         query.equalTo('relation', p1);
  680.         query.find().then(results => {
  681.           expect(results.length).toEqual(0);
  682.  
  683.           const query = new Parse.Query(PersonObject);
  684.           query.equalTo('relation', p2);
  685.           query.find().then(results => {
  686.             expect(results.length).toEqual(1);
  687.             expect(results[0].objectId).toEqual(p1.objectId);
  688.             done();
  689.           });
  690.         });
  691.       })
  692.     });
  693.   });
  694.  
  695.   it('can query roles in Cloud Code (regession test #1489)', done => {
  696.     Parse.Cloud.define('isAdmin', (request, response) => {
  697.       const query = new Parse.Query(Parse.Role);
  698.       query.equalTo('name', 'admin');
  699.       query.first({ useMasterKey: true })
  700.       .then(role => {
  701.         const relation = new Parse.Relation(role, 'users');
  702.         const admins = relation.query();
  703.         admins.equalTo('username', request.user.get('username'));
  704.         admins.first({ useMasterKey: true })
  705.         .then(user => {
  706.           if (user) {
  707.             response.success(user);
  708.             done();
  709.           } else {
  710.             fail('Should have found admin user, found nothing instead');
  711.             done();
  712.           }
  713.         }, () => {
  714.           fail('User not admin');
  715.           done();
  716.         })
  717.       }, error => {
  718.         fail('Should have found admin user, errored instead');
  719.         fail(error);
  720.         done();
  721.       });
  722.     });
  723.  
  724.     const adminUser = new Parse.User();
  725.     adminUser.set('username', 'name');
  726.     adminUser.set('password', 'pass');
  727.     adminUser.signUp()
  728.     .then(adminUser => {
  729.       const adminACL = new Parse.ACL();
  730.       adminACL.setPublicReadAccess(true);
  731.  
  732.       // Create admin role
  733.       const adminRole = new Parse.Role('admin', adminACL);
  734.       adminRole.getUsers().add(adminUser);
  735.       adminRole.save()
  736.       .then(() => {
  737.         Parse.Cloud.run('isAdmin');
  738.       }, error => {
  739.         fail('failed to save role');
  740.         fail(error);
  741.         done()
  742.       });
  743.     }, error => {
  744.       fail('failed to sign up');
  745.       fail(error);
  746.       done();
  747.     });
  748.   });
  749.  
  750.   it('can be saved without error', done => {
  751.     const obj1 = new Parse.Object('PPAP');
  752.     obj1.save()
  753.     .then(() => {
  754.       const newRelation = obj1.relation('aRelation');
  755.       newRelation.add(obj1);
  756.       obj1.save().then(() => {
  757.         const relation = obj1.get('aRelation');
  758.         obj1.set('aRelation', relation);
  759.         obj1.save().then(() => {
  760.           done();
  761.         }, error => {
  762.           fail('failed to save ParseRelation object');
  763.           fail(error);
  764.           done();
  765.         });
  766.       }, error => {
  767.         fail('failed to create relation field');
  768.         fail(error);
  769.         done();
  770.       });
  771.     }, error => {
  772.       fail('failed to save obj');
  773.       fail(error);
  774.       done();
  775.     });
  776.   });
  777. });
  778.  
downloadParseRelation.spec.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top