Skip to content
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');
});
Comment on lines +71 to +78
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Make row ordering explicit before index-based assertions.

These tests assert result[0..n] but the query has no explicit sort, so ordering is nondeterministic and may flake across DB engines.

Suggested fix
         return query
-            .querySQL(knex('posts'))
+            .querySQL(knex('posts').orderBy('id', 'asc'))
             .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');
             });
@@
         return query
-            .querySQL(knex('posts'))
+            .querySQL(knex('posts').orderBy('id', 'asc'))
             .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');
             });
@@
         return query
-            .querySQL(knex('posts'))
+            .querySQL(knex('posts').orderBy('id', 'asc'))
             .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');

Also applies to: 85-92, 100-109

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/nql/test/integration/nql_knex.test.js` around lines 71 - 78, Tests
assert result[0..n] without a deterministic sort; update the query chains (the
call starting with query.querySQL(knex('posts'))) to include an explicit
.orderBy('title') before .select() (and do the same for the other two test
blocks referenced) so the row order is deterministic across DB engines and the
index-based assertions remain stable.

});

// 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 () {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

fd -t f "nql_mingo.test.js" | head -5

Repository: TryGhost/NQL

Length of output: 103


🏁 Script executed:

# Check if the file exists and read the relevant section
wc -l packages/nql/test/integration/nql_mingo.test.js

Repository: TryGhost/NQL

Length of output: 107


🏁 Script executed:

# Read the specific lines around the describe.only
sed -n '410,460p' packages/nql/test/integration/nql_mingo.test.js

Repository: TryGhost/NQL

Length of output: 2589


Remove the exclusive Mocha suite before merge.

describe.only at line 417 will skip all other integration tests and mask regressions. Additionally, remove the console.log statements at lines 450–451 which violate lint rules.

Suggested fix
-    describe.only('Dates', function () {
+    describe('Dates', function () {
-            console.log(new Date().toISOString());
-            console.log(new Date());
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
describe.only('Dates', function () {
describe('Dates', function () {
🧰 Tools
🪛 ESLint

[error] 417-417: Unexpected exclusive mocha test.

(ghost/mocha/no-exclusive-tests)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/nql/test/integration/nql_mingo.test.js` at line 417, Replace the
exclusive Mocha suite call describe.only in the test file (the "Dates" suite)
with a normal describe so the suite doesn't block other tests, and remove the
stray console.log statements inside that "Dates" suite (the console.log calls
around the date tests) to satisfy lint rules and avoid noisy test output.

// 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());
Comment on lines +450 to +451
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Drop debug console output from tests.

Line 450 and Line 451 introduce console.log noise and fail linting (no-console).

Suggested fix
-            console.log(new Date().toISOString());
-            console.log(new Date());
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
console.log(new Date().toISOString());
console.log(new Date());
🧰 Tools
🪛 ESLint

[error] 450-450: Unexpected console statement.

(no-console)


[error] 451-451: Unexpected console statement.

(no-console)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/nql/test/integration/nql_mingo.test.js` around lines 450 - 451,
Remove the two debug statements causing lint errors: delete the lines containing
console.log(new Date().toISOString()) and console.log(new Date()) from the test
(or replace them with a proper test assertion or a test logger call that
complies with the project's linting), ensuring the test no longer uses console.*
so it passes the no-console rule.


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);
});
});
});
Loading