Skip to content

Commit 957af7e

Browse files
BufferUnderflowerdplewis
authored andcommitted
Adds withCount query constraint (#868)
* adds withCount query constrain * tests withCount * doc * formatted * fix semicolons * integration * style * integration tests for default * default to true; withJson - why not
1 parent a807dae commit 957af7e

File tree

4 files changed

+323
-35
lines changed

4 files changed

+323
-35
lines changed

integration/test/ParseQueryTest.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('Parse Query', () => {
2323
.then(() => { done() }, () => { done() });
2424
});
2525

26+
2627
it('can do basic queries', (done) => {
2728
const baz = new TestObject({ foo: 'baz' });
2829
const qux = new TestObject({ foo: 'qux' });
@@ -51,6 +52,119 @@ describe('Parse Query', () => {
5152
}).catch(done.fail);
5253
});
5354

55+
it('can do query with count', async () => {
56+
const items = [];
57+
for (let i = 0; i < 4; i++) {
58+
items.push(new TestObject({ countMe: true }));
59+
}
60+
await Parse.Object.saveAll(items);
61+
62+
const query = new Parse.Query(TestObject);
63+
query.withCount(true);
64+
const {results,count} = await query.find();
65+
66+
assert(typeof count === 'number');
67+
assert.equal(results.length, 4);
68+
assert.equal(count, 4);
69+
for (let i = 0; i < 4; i++) {
70+
assert.equal(results[i].className,'TestObject');
71+
}
72+
});
73+
74+
it('can do query withCount set to false', async () => {
75+
const items = [];
76+
for (let i = 0; i < 4; i++) {
77+
items.push(new TestObject({ countMe: true }));
78+
}
79+
await Parse.Object.saveAll(items);
80+
81+
const query = new Parse.Query(TestObject);
82+
query.withCount(false);
83+
const results = await query.find();
84+
85+
assert.equal(results.length, 4);
86+
for (let i = 0; i < 4; i++) {
87+
assert.equal(results[i].className,'TestObject');
88+
}
89+
});
90+
91+
it('can do query with count on empty collection', async () => {
92+
const query = new Parse.Query(TestObject);
93+
query.withCount(true);
94+
const {results,count} = await query.find();
95+
96+
assert(typeof count == 'number');
97+
assert.equal(results.length, 0);
98+
assert.equal(count, 0);
99+
});
100+
101+
it('can do query with count and limit', async () => {
102+
const items = [];
103+
for (let i = 0; i < 4; i++) {
104+
items.push(new TestObject({ countMe: 2}));
105+
}
106+
await Parse.Object.saveAll(items);
107+
const query = new Parse.Query(TestObject);
108+
query.withCount(true);
109+
query.limit(2);
110+
111+
const {results,count} = await query.find();
112+
113+
assert(typeof count == 'number');
114+
assert.equal(results.length, 2);
115+
assert.equal(count, 4);
116+
});
117+
118+
it('can do query withCount and skip', async () => {
119+
const items = [];
120+
for (let i = 0; i < 4; i++) {
121+
items.push(new TestObject({ countMe: 2}));
122+
}
123+
await Parse.Object.saveAll(items);
124+
const query = new Parse.Query(TestObject);
125+
query.withCount(true);
126+
query.skip(3);
127+
128+
const {results,count} = await query.find();
129+
130+
assert(typeof count == 'number');
131+
assert.equal(results.length, 1);
132+
assert.equal(count, 4);
133+
});
134+
135+
it('can do query when withCount set without arguments', async () => {
136+
const items = [];
137+
for (let i = 0; i < 4; i++) {
138+
items.push(new TestObject({ countMe: 2}));
139+
}
140+
await Parse.Object.saveAll(items);
141+
const query = new Parse.Query(TestObject);
142+
query.withCount();
143+
144+
const {results,count} = await query.find();
145+
146+
assert(typeof count == 'number');
147+
assert.equal(results.length, 4);
148+
assert.equal(count, 4);
149+
});
150+
151+
it('can do query when withCount undefined', async () => {
152+
const items = [];
153+
for (let i = 0; i < 4; i++) {
154+
items.push(new TestObject({ countMe: 2}));
155+
}
156+
await Parse.Object.saveAll(items);
157+
const query = new Parse.Query(TestObject);
158+
let foo;
159+
query.withCount(foo);
160+
161+
const {results,count} = await query.find();
162+
163+
assert(typeof count == 'number');
164+
assert.equal(results.length, 4);
165+
assert.equal(count, 4);
166+
});
167+
54168
it('can do containedIn queries with arrays', (done) => {
55169
const messageList = [];
56170
for (let i = 0; i < 4; i++) {

package-lock.json

Lines changed: 12 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)