BVB Source Codes

parse-server Show QueryTools.spec.js Source code

Return Download parse-server: download QueryTools.spec.js Source code - Download parse-server Source code - Type:.js
  1. var Parse = require('parse/node');
  2.  
  3. var Id = require('../src/LiveQuery/Id');
  4. var QueryTools = require('../src/LiveQuery/QueryTools');
  5. var queryHash = QueryTools.queryHash;
  6. var matchesQuery = QueryTools.matchesQuery;
  7.  
  8. var Item = Parse.Object.extend('Item');
  9.  
  10. describe('queryHash', function() {
  11.  
  12.   it('should always hash a query to the same string', function() {
  13.     var q = new Parse.Query(Item);
  14.     q.equalTo('field', 'value');
  15.     q.exists('name');
  16.     q.ascending('createdAt');
  17.     q.limit(10);
  18.     var firstHash = queryHash(q);
  19.     var secondHash = queryHash(q);
  20.     expect(firstHash).toBe(secondHash);
  21.   });
  22.  
  23.   it('should return equivalent hashes for equivalent queries', function() {
  24.     var q1 = new Parse.Query(Item);
  25.     q1.equalTo('field', 'value');
  26.     q1.exists('name');
  27.     q1.lessThan('age', 30);
  28.     q1.greaterThan('age', 3);
  29.     q1.ascending('createdAt');
  30.     q1.include(['name', 'age']);
  31.     q1.limit(10);
  32.  
  33.     var q2 = new Parse.Query(Item);
  34.     q2.limit(10);
  35.     q2.greaterThan('age', 3);
  36.     q2.lessThan('age', 30);
  37.     q2.include(['name', 'age']);
  38.     q2.ascending('createdAt');
  39.     q2.exists('name');
  40.     q2.equalTo('field', 'value');
  41.  
  42.     var firstHash = queryHash(q1);
  43.     var secondHash = queryHash(q2);
  44.     expect(firstHash).toBe(secondHash);
  45.  
  46.     q1.containedIn('fruit', ['apple', 'banana', 'cherry']);
  47.     firstHash = queryHash(q1);
  48.     expect(firstHash).not.toBe(secondHash);
  49.  
  50.     q2.containedIn('fruit', ['banana', 'cherry', 'apple']);
  51.     secondHash = queryHash(q2);
  52.     expect(secondHash).toBe(firstHash);
  53.  
  54.     q1.containedIn('fruit', ['coconut']);
  55.     firstHash = queryHash(q1);
  56.     expect(firstHash).not.toBe(secondHash);
  57.  
  58.     q1 = new Parse.Query(Item);
  59.     q1.equalTo('field', 'value');
  60.     q1.lessThan('age', 30);
  61.     q1.exists('name');
  62.  
  63.     q2 = new Parse.Query(Item);
  64.     q2.equalTo('name', 'person');
  65.     q2.equalTo('field', 'other');
  66.  
  67.     firstHash = queryHash(Parse.Query.or(q1, q2));
  68.     secondHash = queryHash(Parse.Query.or(q2, q1));
  69.     expect(firstHash).toBe(secondHash);
  70.   });
  71.  
  72.   it('should not let fields of different types appear similar', function() {
  73.     var q1 = new Parse.Query(Item);
  74.     q1.lessThan('age', 30);
  75.  
  76.     var q2 = new Parse.Query(Item);
  77.     q2.equalTo('age', '{$lt:30}');
  78.  
  79.     expect(queryHash(q1)).not.toBe(queryHash(q2));
  80.  
  81.     q1 = new Parse.Query(Item);
  82.     q1.equalTo('age', 15);
  83.  
  84.     q2.equalTo('age', '15');
  85.  
  86.     expect(queryHash(q1)).not.toBe(queryHash(q2));
  87.   });
  88. });
  89.  
  90. describe('matchesQuery', function() {
  91.   it('matches blanket queries', function() {
  92.     var obj = {
  93.       id: new Id('Klass', 'O1'),
  94.       value: 12
  95.     };
  96.     var q = new Parse.Query('Klass');
  97.     expect(matchesQuery(obj, q)).toBe(true);
  98.  
  99.     obj.id = new Id('Other', 'O1');
  100.     expect(matchesQuery(obj, q)).toBe(false);
  101.   });
  102.  
  103.   it('matches existence queries', function() {
  104.     var obj = {
  105.       id: new Id('Item', 'O1'),
  106.       count: 15
  107.     };
  108.     var q = new Parse.Query('Item');
  109.     q.exists('count');
  110.     expect(matchesQuery(obj, q)).toBe(true);
  111.     q.exists('name');
  112.     expect(matchesQuery(obj, q)).toBe(false);
  113.   });
  114.  
  115.   it('matches queries with doesNotExist constraint', function() {
  116.     var obj = {
  117.       id: new Id('Item', 'O1'),
  118.       count: 15
  119.     };
  120.     var q = new Parse.Query('Item');
  121.     q.doesNotExist('name');
  122.     expect(matchesQuery(obj, q)).toBe(true);
  123.  
  124.     q = new Parse.Query('Item');
  125.     q.doesNotExist('count');
  126.     expect(matchesQuery(obj, q)).toBe(false);
  127.   });
  128.  
  129.   it('matches on equality queries', function() {
  130.     var day = new Date();
  131.     var location = new Parse.GeoPoint({
  132.       latitude: 37.484815,
  133.       longitude: -122.148377
  134.     });
  135.     var obj = {
  136.       id: new Id('Person', 'O1'),
  137.       score: 12,
  138.       name: 'Bill',
  139.       birthday: day,
  140.       lastLocation: location
  141.     };
  142.  
  143.     var q = new Parse.Query('Person');
  144.     q.equalTo('score', 12);
  145.     expect(matchesQuery(obj, q)).toBe(true);
  146.  
  147.     q = new Parse.Query('Person');
  148.     q.equalTo('name', 'Bill');
  149.     expect(matchesQuery(obj, q)).toBe(true);
  150.     q.equalTo('name', 'Jeff');
  151.     expect(matchesQuery(obj, q)).toBe(false);
  152.  
  153.     q = new Parse.Query('Person');
  154.     q.containedIn('name', ['Adam', 'Ben', 'Charles']);
  155.     expect(matchesQuery(obj, q)).toBe(false);
  156.     q.containedIn('name', ['Adam', 'Bill', 'Charles']);
  157.     expect(matchesQuery(obj, q)).toBe(true);
  158.  
  159.     q = new Parse.Query('Person');
  160.     q.notContainedIn('name', ['Adam', 'Bill', 'Charles']);
  161.     expect(matchesQuery(obj, q)).toBe(false);
  162.     q.notContainedIn('name', ['Adam', 'Ben', 'Charles']);
  163.     expect(matchesQuery(obj, q)).toBe(true);
  164.  
  165.     q = new Parse.Query('Person');
  166.     q.equalTo('birthday', day);
  167.     expect(matchesQuery(obj, q)).toBe(true);
  168.     q.equalTo('birthday', new Date(1990, 1));
  169.     expect(matchesQuery(obj, q)).toBe(false);
  170.  
  171.     q = new Parse.Query('Person');
  172.     q.equalTo('lastLocation', new Parse.GeoPoint({
  173.       latitude: 37.484815,
  174.       longitude: -122.148377
  175.     }));
  176.     expect(matchesQuery(obj, q)).toBe(true);
  177.     q.equalTo('lastLocation', new Parse.GeoPoint({
  178.       latitude: 37.4848,
  179.       longitude: -122.1483
  180.     }));
  181.     expect(matchesQuery(obj, q)).toBe(false);
  182.  
  183.     q.equalTo('lastLocation', new Parse.GeoPoint({
  184.       latitude: 37.484815,
  185.       longitude: -122.148377
  186.     }));
  187.     q.equalTo('score', 12);
  188.     q.equalTo('name', 'Bill');
  189.     q.equalTo('birthday', day);
  190.     expect(matchesQuery(obj, q)).toBe(true);
  191.  
  192.     q.equalTo('name', 'bill');
  193.     expect(matchesQuery(obj, q)).toBe(false);
  194.  
  195.     var img = {
  196.       id: new Id('Image', 'I1'),
  197.       tags: ['nofilter', 'latergram', 'tbt']
  198.     };
  199.  
  200.     q = new Parse.Query('Image');
  201.     q.equalTo('tags', 'selfie');
  202.     expect(matchesQuery(img, q)).toBe(false);
  203.     q.equalTo('tags', 'tbt');
  204.     expect(matchesQuery(img, q)).toBe(true);
  205.  
  206.     var q2 = new Parse.Query('Image');
  207.     q2.containsAll('tags', ['latergram', 'nofilter']);
  208.     expect(matchesQuery(img, q2)).toBe(true);
  209.     q2.containsAll('tags', ['latergram', 'selfie']);
  210.     expect(matchesQuery(img, q2)).toBe(false);
  211.  
  212.     var u = new Parse.User();
  213.     u.id = 'U2';
  214.     q = new Parse.Query('Image');
  215.     q.equalTo('owner', u);
  216.  
  217.     img = {
  218.       className: 'Image',
  219.       objectId: 'I1',
  220.       owner: {
  221.         className: '_User',
  222.         objectId: 'U2'
  223.       }
  224.     };
  225.     expect(matchesQuery(img, q)).toBe(true);
  226.  
  227.     img.owner.objectId = 'U3';
  228.     expect(matchesQuery(img, q)).toBe(false);
  229.  
  230.     // pointers in arrays
  231.     q = new Parse.Query('Image');
  232.     q.equalTo('owners', u);
  233.  
  234.     img = {
  235.       className: 'Image',
  236.       objectId: 'I1',
  237.       owners: [{
  238.         className: '_User',
  239.         objectId: 'U2'
  240.       }]
  241.     };
  242.     expect(matchesQuery(img, q)).toBe(true);
  243.  
  244.     img.owners[0].objectId = 'U3';
  245.     expect(matchesQuery(img, q)).toBe(false);
  246.   });
  247.  
  248.   it('matches on inequalities', function() {
  249.     var player = {
  250.       id: new Id('Person', 'O1'),
  251.       score: 12,
  252.       name: 'Bill',
  253.       birthday: new Date(1980, 2, 4),
  254.     };
  255.     var q = new Parse.Query('Person');
  256.     q.lessThan('score', 15);
  257.     expect(matchesQuery(player, q)).toBe(true);
  258.     q.lessThan('score', 10);
  259.     expect(matchesQuery(player, q)).toBe(false);
  260.  
  261.     q = new Parse.Query('Person');
  262.     q.lessThanOrEqualTo('score', 15);
  263.     expect(matchesQuery(player, q)).toBe(true);
  264.     q.lessThanOrEqualTo('score', 12);
  265.     expect(matchesQuery(player, q)).toBe(true);
  266.     q.lessThanOrEqualTo('score', 10);
  267.     expect(matchesQuery(player, q)).toBe(false);
  268.  
  269.     q = new Parse.Query('Person');
  270.     q.greaterThan('score', 15);
  271.     expect(matchesQuery(player, q)).toBe(false);
  272.     q.greaterThan('score', 10);
  273.     expect(matchesQuery(player, q)).toBe(true);
  274.  
  275.     q = new Parse.Query('Person');
  276.     q.greaterThanOrEqualTo('score', 15);
  277.     expect(matchesQuery(player, q)).toBe(false);
  278.     q.greaterThanOrEqualTo('score', 12);
  279.     expect(matchesQuery(player, q)).toBe(true);
  280.     q.greaterThanOrEqualTo('score', 10);
  281.     expect(matchesQuery(player, q)).toBe(true);
  282.  
  283.     q = new Parse.Query('Person');
  284.     q.notEqualTo('score', 12);
  285.     expect(matchesQuery(player, q)).toBe(false);
  286.     q.notEqualTo('score', 40);
  287.     expect(matchesQuery(player, q)).toBe(true);
  288.   });
  289.  
  290.   it('matches an $or query', function() {
  291.     var player = {
  292.       id: new Id('Player', 'P1'),
  293.       name: 'Player 1',
  294.       score: 12
  295.     };
  296.     var q = new Parse.Query('Player');
  297.     q.equalTo('name', 'Player 1');
  298.     var q2 = new Parse.Query('Player');
  299.     q2.equalTo('name', 'Player 2');
  300.     var orQuery = Parse.Query.or(q, q2);
  301.     expect(matchesQuery(player, q)).toBe(true);
  302.     expect(matchesQuery(player, q2)).toBe(false);
  303.     expect(matchesQuery(player, orQuery)).toBe(true);
  304.   });
  305.  
  306.   it('matches $regex queries', function() {
  307.     var player = {
  308.       id: new Id('Player', 'P1'),
  309.       name: 'Player 1',
  310.       score: 12
  311.     };
  312.  
  313.     var q = new Parse.Query('Player');
  314.     q.startsWith('name', 'Play');
  315.     expect(matchesQuery(player, q)).toBe(true);
  316.     q.startsWith('name', 'Ploy');
  317.     expect(matchesQuery(player, q)).toBe(false);
  318.  
  319.     q = new Parse.Query('Player');
  320.     q.endsWith('name', ' 1');
  321.     expect(matchesQuery(player, q)).toBe(true);
  322.     q.endsWith('name', ' 2');
  323.     expect(matchesQuery(player, q)).toBe(false);
  324.  
  325.     // Check that special characters are escaped
  326.     player.name = 'Android-7';
  327.     q = new Parse.Query('Player');
  328.     q.contains('name', 'd-7');
  329.     expect(matchesQuery(player, q)).toBe(true);
  330.  
  331.     q = new Parse.Query('Player');
  332.     q.matches('name', /A.d/);
  333.     expect(matchesQuery(player, q)).toBe(true);
  334.  
  335.     q.matches('name', /A[^n]d/);
  336.     expect(matchesQuery(player, q)).toBe(false);
  337.  
  338.     // Check that the string \\E is returned to normal
  339.     player.name = 'Slash \\E';
  340.     q = new Parse.Query('Player');
  341.     q.endsWith('name', 'h \\E');
  342.     expect(matchesQuery(player, q)).toBe(true);
  343.  
  344.     q.endsWith('name', 'h \\Ee');
  345.     expect(matchesQuery(player, q)).toBe(false);
  346.  
  347.     player.name = 'Slash \\Q and more';
  348.     q = new Parse.Query('Player');
  349.     q.contains('name', 'h \\Q and');
  350.     expect(matchesQuery(player, q)).toBe(true);
  351.     q.contains('name', 'h \\Q or');
  352.     expect(matchesQuery(player, q)).toBe(false);
  353.   });
  354.  
  355.   it('matches $nearSphere queries', function() {
  356.     var q = new Parse.Query('Checkin');
  357.     q.near('location', new Parse.GeoPoint(20, 20));
  358.     // With no max distance, any GeoPoint is 'near'
  359.     var pt = {
  360.       id: new Id('Checkin', 'C1'),
  361.       location: new Parse.GeoPoint(40, 40)
  362.     };
  363.     expect(matchesQuery(pt, q)).toBe(true);
  364.  
  365.     q = new Parse.Query('Checkin');
  366.     pt.location = new Parse.GeoPoint(40, 40);
  367.     q.withinRadians('location', new Parse.GeoPoint(30, 30), 0.3);
  368.     expect(matchesQuery(pt, q)).toBe(true);
  369.  
  370.     q.withinRadians('location', new Parse.GeoPoint(30, 30), 0.2);
  371.     expect(matchesQuery(pt, q)).toBe(false);
  372.   });
  373.  
  374.   it('matches $within queries', function() {
  375.     var caltrainStation = {
  376.       id: new Id('Checkin', 'C1'),
  377.       location: new Parse.GeoPoint(37.776346, -122.394218),
  378.       name: 'Caltrain'
  379.     };
  380.  
  381.     var santaClara = {
  382.       id: new Id('Checkin', 'C2'),
  383.       location: new Parse.GeoPoint(37.325635, -121.945753),
  384.       name: 'Santa Clara'
  385.     };
  386.  
  387.     var q = new Parse.Query('Checkin').withinGeoBox(
  388.       'location',
  389.       new Parse.GeoPoint(37.708813, -122.526398),
  390.       new Parse.GeoPoint(37.822802, -122.373962)
  391.     );
  392.  
  393.     expect(matchesQuery(caltrainStation, q)).toBe(true);
  394.     expect(matchesQuery(santaClara, q)).toBe(false);
  395.  
  396.     // Invalid rectangles
  397.     q = new Parse.Query('Checkin').withinGeoBox(
  398.       'location',
  399.       new Parse.GeoPoint(37.822802, -122.373962),
  400.       new Parse.GeoPoint(37.708813, -122.526398)
  401.     );
  402.  
  403.     expect(matchesQuery(caltrainStation, q)).toBe(false);
  404.     expect(matchesQuery(santaClara, q)).toBe(false);
  405.  
  406.     q = new Parse.Query('Checkin').withinGeoBox(
  407.       'location',
  408.       new Parse.GeoPoint(37.708813, -122.373962),
  409.       new Parse.GeoPoint(37.822802, -122.526398)
  410.     );
  411.  
  412.     expect(matchesQuery(caltrainStation, q)).toBe(false);
  413.     expect(matchesQuery(santaClara, q)).toBe(false);
  414.   });
  415.  
  416.   it('matches on subobjects with dot notation', function() {
  417.     var message = {
  418.       id: new Id('Message', 'O1'),
  419.       text: "content",
  420.       status: {x: "read", y: "delivered"}
  421.     };
  422.  
  423.     var q = new Parse.Query('Message');
  424.     q.equalTo("status.x", "read");
  425.     expect(matchesQuery(message, q)).toBe(true);
  426.  
  427.     q = new Parse.Query('Message');
  428.     q.equalTo("status.z", "read");
  429.     expect(matchesQuery(message, q)).toBe(false);
  430.  
  431.     q = new Parse.Query('Message');
  432.     q.equalTo("status.x", "delivered");
  433.     expect(matchesQuery(message, q)).toBe(false);
  434.  
  435.     q = new Parse.Query('Message');
  436.     q.notEqualTo("status.x", "read");
  437.     expect(matchesQuery(message, q)).toBe(false);
  438.  
  439.     q = new Parse.Query('Message');
  440.     q.notEqualTo("status.z", "read");
  441.     expect(matchesQuery(message, q)).toBe(true);
  442.  
  443.     q = new Parse.Query('Message');
  444.     q.notEqualTo("status.x", "delivered");
  445.     expect(matchesQuery(message, q)).toBe(true);
  446.  
  447.     q = new Parse.Query('Message');
  448.     q.exists("status.x");
  449.     expect(matchesQuery(message, q)).toBe(true);
  450.  
  451.     q = new Parse.Query('Message');
  452.     q.exists("status.z");
  453.     expect(matchesQuery(message, q)).toBe(false);
  454.  
  455.     q = new Parse.Query('Message');
  456.     q.exists("nonexistent.x");
  457.     expect(matchesQuery(message, q)).toBe(false);
  458.  
  459.     q = new Parse.Query('Message');
  460.     q.doesNotExist("status.x");
  461.     expect(matchesQuery(message, q)).toBe(false);
  462.  
  463.     q = new Parse.Query('Message');
  464.     q.doesNotExist("status.z");
  465.     expect(matchesQuery(message, q)).toBe(true);
  466.  
  467.     q = new Parse.Query('Message');
  468.     q.doesNotExist("nonexistent.z");
  469.     expect(matchesQuery(message, q)).toBe(true);
  470.  
  471.     q = new Parse.Query('Message');
  472.     q.equalTo("status.x", "read");
  473.     q.doesNotExist("status.y");
  474.     expect(matchesQuery(message, q)).toBe(false);
  475.  
  476.   });
  477. });
  478.  
downloadQueryTools.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