0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-04 02:01:58 -05:00

Fixed non-deretministic post ordering in content gating test suite

refs https://github.com/TryGhost/Ghost/pull/17609

- The tests for content gating started to fail with introduction of the index on `published_at` data in for `posts` table. The reason for the failure was identical `published_at` date set during the fixture insertion, making the returned results change order non-deterministically. The problem is mostly in how the test is set up as it's quite unrealistic to have multiple posts in the system inserted at the same time down to millisecond. Maybe... by some coincidence, but thats not a problem we should care too much about imo.
This commit is contained in:
Naz 2023-08-08 12:54:19 +08:00 committed by naz
parent d068409fac
commit 92fb2a54ae
2 changed files with 11 additions and 6 deletions

View file

@ -1,4 +1,5 @@
const logging = require('@tryghost/logging');
const DatabaseInfo = require('@tryghost/database-info');
const {createNonTransactionalMigration} = require('../../utils');
const INDEX_NAME = 'posts_published_at_index';
@ -7,7 +8,7 @@ module.exports = createNonTransactionalMigration(
async function up(knex) {
let hasIndex = false;
if (knex.client.config.client === 'sqlite3') {
if (DatabaseInfo.isSQLite(knex)) {
const result = await knex.raw(`select * from sqlite_master where type = 'index' and tbl_name = 'posts' and name = '${INDEX_NAME}'`);
hasIndex = (result.length !== 0);
} else {
@ -29,7 +30,7 @@ module.exports = createNonTransactionalMigration(
async function down(knex) {
let missingIndex = false;
if (knex.client.config.client === 'sqlite3') {
if (DatabaseInfo.isSQLite(knex)) {
const result = await knex.raw(`select * from sqlite_master where type = 'index' and tbl_name = 'posts' and name = '${INDEX_NAME}'`);
missingIndex = (result.length === 0);
} else {

View file

@ -302,24 +302,28 @@ describe('api/endpoints/content/posts', function () {
before (function () {
publicPost = testUtils.DataGenerator.forKnex.createPost({
slug: 'free-to-see',
visibility: 'public'
visibility: 'public',
published_at: new Date('2023-07-15T04:20:30.000+00:00')
});
membersPost = testUtils.DataGenerator.forKnex.createPost({
slug: 'thou-shalt-not-be-seen',
visibility: 'members'
visibility: 'members',
published_at: new Date('2023-07-20T04:20:30.000+00:00')
});
paidPost = testUtils.DataGenerator.forKnex.createPost({
slug: 'thou-shalt-be-paid-for',
visibility: 'paid'
visibility: 'paid',
published_at: new Date('2023-07-25T04:20:30.000+00:00')
});
membersPostWithPaywallCard = testUtils.DataGenerator.forKnex.createPost({
slug: 'thou-shalt-have-a-taste',
visibility: 'members',
mobiledoc: '{"version":"0.3.1","markups":[],"atoms":[],"cards":[["paywall",{}]],"sections":[[1,"p",[[0,[],0,"Free content"]]],[10,0],[1,"p",[[0,[],0,"Members content"]]]]}',
html: '<p>Free content</p><!--members-only--><p>Members content</p>'
html: '<p>Free content</p><!--members-only--><p>Members content</p>',
published_at: new Date('2023-07-30T04:20:30.000+00:00')
});
return testUtils.fixtures.insertPosts([