Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added knex integration tests for dates inc relative #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/nql/test/integration/knex/fixtures/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,50 @@
"title": "A Whole New World",
"featured": false,
"image": null,
"status": "published"
"status": "published",
"created_at": "2020-01-05 18:16:49",
"updated_at": "2020-01-10 12:05:08"
},
{
"id": 2,
"title": "The Bare Necessities",
"featured": false,
"image": "myimage.jpg",
"status": "draft"
"status": "draft",
"created_at": "2020-01-18 08:23:46",
"updated_at": "2020-01-18 12:05:08"
},
{
"id": 3,
"title": "When She Loved Me",
"featured": true,
"image": null,
"status": "published"
"status": "published",
"created_at": "2022-02-02 10:05:49"
},
{
"id": 4,
"title": "Circle of Life",
"featured": true,
"image": null,
"status": "published"
"status": "published",
"created_at": "2022-03-01 10:12:23"
},
{
"id": 5,
"title": "Be Our Guest",
"featured": true,
"image": null,
"status": "published"
"status": "published",
"created_at": "2022-03-02 11:06:49"
},
{
"id": 6,
"title": "He's a Tramp",
"featured": true,
"image": null,
"status": "published"
"status": "published",
"created_at": "2022-03-02 11:06:50"
}
]
}
1 change: 1 addition & 0 deletions packages/nql/test/integration/knex/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports.up = function (knex) {
table.boolean('featured').defaultsTo(false);
table.string('image', 191).nullable();
table.string('status', 191).nullable();
table.timestamps(true, true);
}));
};

Expand Down
6 changes: 4 additions & 2 deletions packages/nql/test/integration/mingo/simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"featured": false,
"image": null,
"status": "published",
"tags": ["photo"]
"tags": ["photo"],
"created_at": "2022-03-02T10:14:23.000Z"
},
{
"id": 2,
Expand All @@ -18,7 +19,8 @@
"featured": false,
"image": "myimage.jpg",
"status": "draft",
"tags": ["video"]
"tags": ["video"],
"created_at": "2022-03-02 10:14:23"
},
{
"id": 3,
Expand Down
46 changes: 46 additions & 0 deletions packages/nql/test/integration/nql_knex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,50 @@ describe('Integration with Knex', function () {
});
});
});

describe('Dates', function () {
it('can match based on dates - this is the format that works across MySQL and SQLite3', function () {
const query = nql('created_at:>=\'2022-03-02 11:06:49\'');

return query
.querySQL(knex('posts'))
.select()
.then((result) => {
result.should.be.an.Array().with.lengthOf(2);
result[0].title.should.eql('Be Our Guest');
result[1].title.should.eql('He\'s a Tramp');
});
});

// NOTE: I believe this will work in MySQL but not SQLite3
it('can match based on dates in iso format ISO', function () {
const query = nql('created_at:>=\'2022-03-02T11:06:49.000Z\'');

return query
.querySQL(knex('posts'))
.select()
.then((result) => {
result.should.be.an.Array().with.lengthOf(2);
result[0].title.should.eql('Be Our Guest');
result[1].title.should.eql('He\'s a Tramp');
});
});

it('can match based on relative dates', function () {
// This test relies on the fact that knex inserts an updated_at of now for all fixtures that are blank
// Only 2 tests have explicit updated_at dates, these should not be returned
const query = nql('updated_at:>now-1d');

return query
.querySQL(knex('posts'))
.select()
.then((result) => {
result.should.be.an.Array().with.lengthOf(4);
result[0].title.should.eql('When She Loved Me');
result[1].title.should.eql('Circle of Life');
result[2].title.should.eql('Be Our Guest');
result[3].title.should.eql('He\'s a Tramp');
});
});
});
});
42 changes: 42 additions & 0 deletions packages/nql/test/integration/nql_mingo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,46 @@ describe('Integration with Mingo', function () {
query.queryJSON(advancedJSON.posts[4]).should.eql(false);
});
});

describe.only('Dates', function () {
// Dates are a nightmare because SQLite3 and MySQL work differently, and knex doesn't help here
// The date format that goes into a database has to be YYYY-MM-DD HH:mm:ss, but what comes out is normalised to ISO YYYY-MM-DDTHH:mm:ss.000Z

it('can query JSON by date', function () {
const query = makeQuery('created_at:>=\'2022-03-02 10:14:23\'');

query.queryJSON({created_at: '2022-03-02T10:14:23.000Z'}).should.eql(true);
query.queryJSON({created_at: '2022-03-02 10:14:23'}).should.eql(true);
query.queryJSON({created_at: '2022-03-02T10:14:24.000Z'}).should.eql(true);
query.queryJSON({created_at: '2022-03-02 10:14:24'}).should.eql(true);
// FAIL query.queryJSON({created_at: '2022-03-02T10:14:22.000Z'}).should.eql(false);
query.queryJSON({created_at: '2022-03-02 10:14:22'}).should.eql(false);

query.queryJSON({}).should.eql(false);
});

it('can query JSON by ISO date', function () {
const query = makeQuery('created_at:>=\'2022-03-02T10:14:23.000Z\'');

query.queryJSON({created_at: '2022-03-02T10:14:23.000Z'}).should.eql(true);
// FAIL query.queryJSON({created_at: '2022-03-02 10:14:23'}).should.eql(true);
query.queryJSON({created_at: '2022-03-02T10:14:24.000Z'}).should.eql(true);
// FAIL query.queryJSON({created_at: '2022-03-02 10:14:24'}).should.eql(true);
query.queryJSON({created_at: '2022-03-02T10:14:22.000Z'}).should.eql(false);
query.queryJSON({created_at: '2022-03-02 10:14:22'}).should.eql(false);

query.queryJSON({}).should.eql(false);
});

it('can query JSON by rel date', function () {
const query = makeQuery('created_at:>=now-1d');

console.log(new Date().toISOString());
console.log(new Date());

query.queryJSON({created_at: new Date().toISOString()}).should.eql(true);
query.queryJSON({created_at: '2022-01-00T00:00:00.000Z'}).should.eql(false);
query.queryJSON({created_at: '2022-01-00 00:00:00'}).should.eql(false);
});
});
});