feat: support relativeTime query constraint on Postgres (#7747)
This commit is contained in:
@@ -4766,7 +4766,7 @@ describe('Parse.Query testing', () => {
|
||||
.catch(done.fail);
|
||||
});
|
||||
|
||||
it_only_db('mongo')('should handle relative times correctly', function (done) {
|
||||
it('should handle relative times correctly', async () => {
|
||||
const now = Date.now();
|
||||
const obj1 = new Parse.Object('MyCustomObject', {
|
||||
name: 'obj1',
|
||||
@@ -4777,94 +4777,75 @@ describe('Parse.Query testing', () => {
|
||||
ttl: new Date(now - 2 * 24 * 60 * 60 * 1000), // 2 days ago
|
||||
});
|
||||
|
||||
Parse.Object.saveAll([obj1, obj2])
|
||||
.then(() => {
|
||||
const q = new Parse.Query('MyCustomObject');
|
||||
q.greaterThan('ttl', { $relativeTime: 'in 1 day' });
|
||||
return q.find({ useMasterKey: true });
|
||||
})
|
||||
.then(results => {
|
||||
expect(results.length).toBe(1);
|
||||
})
|
||||
.then(() => {
|
||||
const q = new Parse.Query('MyCustomObject');
|
||||
q.greaterThan('ttl', { $relativeTime: '1 day ago' });
|
||||
return q.find({ useMasterKey: true });
|
||||
})
|
||||
.then(results => {
|
||||
expect(results.length).toBe(1);
|
||||
})
|
||||
.then(() => {
|
||||
const q = new Parse.Query('MyCustomObject');
|
||||
q.lessThan('ttl', { $relativeTime: '5 days ago' });
|
||||
return q.find({ useMasterKey: true });
|
||||
})
|
||||
.then(results => {
|
||||
expect(results.length).toBe(0);
|
||||
})
|
||||
.then(() => {
|
||||
const q = new Parse.Query('MyCustomObject');
|
||||
q.greaterThan('ttl', { $relativeTime: '3 days ago' });
|
||||
return q.find({ useMasterKey: true });
|
||||
})
|
||||
.then(results => {
|
||||
expect(results.length).toBe(2);
|
||||
})
|
||||
.then(() => {
|
||||
const q = new Parse.Query('MyCustomObject');
|
||||
q.greaterThan('ttl', { $relativeTime: 'now' });
|
||||
return q.find({ useMasterKey: true });
|
||||
})
|
||||
.then(results => {
|
||||
expect(results.length).toBe(1);
|
||||
})
|
||||
.then(() => {
|
||||
const q = new Parse.Query('MyCustomObject');
|
||||
q.greaterThan('ttl', { $relativeTime: 'now' });
|
||||
q.lessThan('ttl', { $relativeTime: 'in 1 day' });
|
||||
return q.find({ useMasterKey: true });
|
||||
})
|
||||
.then(results => {
|
||||
expect(results.length).toBe(0);
|
||||
})
|
||||
.then(() => {
|
||||
const q = new Parse.Query('MyCustomObject');
|
||||
q.greaterThan('ttl', { $relativeTime: '1 year 3 weeks ago' });
|
||||
return q.find({ useMasterKey: true });
|
||||
})
|
||||
.then(results => {
|
||||
expect(results.length).toBe(2);
|
||||
})
|
||||
.then(done, done.fail);
|
||||
await Parse.Object.saveAll([obj1, obj2])
|
||||
const q1 = new Parse.Query('MyCustomObject');
|
||||
q1.greaterThan('ttl', { $relativeTime: 'in 1 day' });
|
||||
const results1 = await q1.find({ useMasterKey: true });
|
||||
expect(results1.length).toBe(1);
|
||||
|
||||
const q2 = new Parse.Query('MyCustomObject');
|
||||
q2.greaterThan('ttl', { $relativeTime: '1 day ago' });
|
||||
const results2 = await q2.find({ useMasterKey: true });
|
||||
expect(results2.length).toBe(1);
|
||||
|
||||
const q3 = new Parse.Query('MyCustomObject');
|
||||
q3.lessThan('ttl', { $relativeTime: '5 days ago' });
|
||||
const results3 = await q3.find({ useMasterKey: true });
|
||||
expect(results3.length).toBe(0);
|
||||
|
||||
const q4 = new Parse.Query('MyCustomObject');
|
||||
q4.greaterThan('ttl', { $relativeTime: '3 days ago' });
|
||||
const results4 = await q4.find({ useMasterKey: true });
|
||||
expect(results4.length).toBe(2);
|
||||
|
||||
const q5 = new Parse.Query('MyCustomObject');
|
||||
q5.greaterThan('ttl', { $relativeTime: 'now' });
|
||||
const results5 = await q5.find({ useMasterKey: true });
|
||||
expect(results5.length).toBe(1);
|
||||
|
||||
const q6 = new Parse.Query('MyCustomObject');
|
||||
q6.greaterThan('ttl', { $relativeTime: 'now' });
|
||||
q6.lessThan('ttl', { $relativeTime: 'in 1 day' });
|
||||
const results6 = await q6.find({ useMasterKey: true });
|
||||
expect(results6.length).toBe(0);
|
||||
|
||||
const q7 = new Parse.Query('MyCustomObject');
|
||||
q7.greaterThan('ttl', { $relativeTime: '1 year 3 weeks ago' });
|
||||
const results7 = await q7.find({ useMasterKey: true });
|
||||
expect(results7.length).toBe(2);
|
||||
});
|
||||
|
||||
it_only_db('mongo')('should error on invalid relative time', function (done) {
|
||||
it('should error on invalid relative time', async () => {
|
||||
const obj1 = new Parse.Object('MyCustomObject', {
|
||||
name: 'obj1',
|
||||
ttl: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000), // 2 days from now
|
||||
});
|
||||
|
||||
await obj1.save({ useMasterKey: true });
|
||||
const q = new Parse.Query('MyCustomObject');
|
||||
q.greaterThan('ttl', { $relativeTime: '-12 bananas ago' });
|
||||
obj1
|
||||
.save({ useMasterKey: true })
|
||||
.then(() => q.find({ useMasterKey: true }))
|
||||
.then(done.fail, () => done());
|
||||
try {
|
||||
await q.find({ useMasterKey: true });
|
||||
fail("Should have thrown error");
|
||||
} catch(error) {
|
||||
expect(error.code).toBe(Parse.Error.INVALID_JSON);
|
||||
}
|
||||
});
|
||||
|
||||
it_only_db('mongo')('should error when using $relativeTime on non-Date field', function (done) {
|
||||
it('should error when using $relativeTime on non-Date field', async () => {
|
||||
const obj1 = new Parse.Object('MyCustomObject', {
|
||||
name: 'obj1',
|
||||
nonDateField: 'abcd',
|
||||
ttl: new Date(Date.now() + 2 * 24 * 60 * 60 * 1000), // 2 days from now
|
||||
});
|
||||
|
||||
await obj1.save({ useMasterKey: true });
|
||||
const q = new Parse.Query('MyCustomObject');
|
||||
q.greaterThan('nonDateField', { $relativeTime: '1 day ago' });
|
||||
obj1
|
||||
.save({ useMasterKey: true })
|
||||
.then(() => q.find({ useMasterKey: true }))
|
||||
.then(done.fail, () => done());
|
||||
try {
|
||||
await q.find({ useMasterKey: true });
|
||||
fail("Should have thrown error");
|
||||
} catch(error) {
|
||||
expect(error.code).toBe(Parse.Error.INVALID_JSON);
|
||||
}
|
||||
});
|
||||
|
||||
it('should match complex structure with dot notation when using matchesKeyInQuery', function (done) {
|
||||
|
||||
Reference in New Issue
Block a user