Fix tests

This commit is contained in:
Alexandre Bodin 2023-10-05 13:30:56 +02:00
parent 290f9f1269
commit c8c571e5f7
7 changed files with 24 additions and 57 deletions

View File

@ -1,7 +1,7 @@
'use strict';
const { createStrapiInstance } = require('api-tests/strapi');
const { isKnexQuery } = require('../../../packages/core/database/lib/utils/knex');
const { isKnexQuery } = require('../../../packages/core/database/dist/utils/knex');
let strapi;

View File

@ -31,7 +31,7 @@
"scripts": {
"build": "run -T tsc -p tsconfig.build.json",
"build:ts": "run build",
"watch": "run -T tsc -w --preserveWatchOutput",
"watch": "run build -w --preserveWatchOutput",
"clean": "run -T rimraf ./dist",
"prepublishOnly": "yarn clean && yarn build",
"test:unit": "run -T jest",

View File

@ -1,4 +1,4 @@
import { padCharsEnd } from 'lodash/fp';
import { padCharsEnd, isString, toString } from 'lodash/fp';
import * as dateFns from 'date-fns';
import { InvalidDateTimeError, InvalidDateError, InvalidTimeError } from '../../errors';
@ -16,17 +16,13 @@ export const parseDateTimeOrTimestamp = (value: unknown): Date => {
return value;
}
if (typeof value !== 'string') {
throw new InvalidDateTimeError(`Expected a string, got a ${typeof value}`);
}
try {
const date = dateFns.parseISO(value);
const date = dateFns.parseISO(toString(value));
if (dateFns.isValid(date)) {
return date;
}
const milliUnixDate = dateFns.parse(value, 'T', new Date());
const milliUnixDate = dateFns.parse(toString(value), 'T', new Date());
if (dateFns.isValid(milliUnixDate)) {
return milliUnixDate;
}
@ -42,14 +38,10 @@ export const parseDate = (value: unknown) => {
return dateFns.format(value, 'yyyy-MM-dd');
}
if (typeof value !== 'string') {
throw new InvalidDateError(`Expected a string, got a ${typeof value}`);
}
const found = value.match(PARTIAL_DATE_REGEX) || [];
const found = isString(value) ? value.match(PARTIAL_DATE_REGEX) || [] : [];
const extractedValue = found[0];
if (extractedValue && !DATE_REGEX.test(value)) {
if (extractedValue && !DATE_REGEX.test(toString(value))) {
// TODO V5: throw an error when format yyyy-MM-dd is not respected
// throw new InvalidDateError(`Invalid format, expected yyyy-MM-dd`);
process.emitWarning(
@ -77,6 +69,7 @@ export const parseTime = (value: unknown) => {
if (typeof value !== 'string') {
throw new InvalidTimeError(`Expected a string, got a ${typeof value}`);
}
const result = value.match(TIME_REGEX);
if (result === null) {

View File

@ -55,7 +55,7 @@ const isOwner = (
!isBidirectional(attribute) || hasInversedBy(attribute);
const shouldUseJoinTable = (attribute: RelationalAttribute) =>
'useJoinTable' in attribute && attribute.useJoinTable !== false;
!('useJoinTable' in attribute) || attribute.useJoinTable !== false;
export const getJoinTableName = (tableName: string, attributeName: string) =>
_.snakeCase(`${tableName}_${attributeName}_links`);

View File

@ -45,12 +45,7 @@ const XtoOne = async (
const { attribute, attributeName, results, populateValue, targetMeta, isCount } = input;
const { db, qb } = ctx;
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) => {
if (!rowOrRows) {
return rowOrRows;
}
return fromRow(targetMeta, rowOrRows);
};
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) => fromRow(targetMeta, rowOrRows);
if ('joinColumn' in attribute && attribute.joinColumn) {
const { name: joinColumnName, referencedColumn: referencedColumnName } = attribute.joinColumn;
@ -167,13 +162,7 @@ const oneToMany = async (input: InputWithTarget<Relation.OneToMany>, ctx: Contex
const { attribute, attributeName, results, populateValue, targetMeta, isCount } = input;
const { db, qb } = ctx;
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) => {
if (!rowOrRows) {
return rowOrRows;
}
return fromRow(targetMeta, rowOrRows);
};
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) => fromRow(targetMeta, rowOrRows);
if ('joinColumn' in attribute && attribute.joinColumn) {
const { name: joinColumnName, referencedColumn: referencedColumnName } = attribute.joinColumn;
@ -288,13 +277,7 @@ const manyToMany = async (input: InputWithTarget<Relation.ManyToMany>, ctx: Cont
const { attribute, attributeName, results, populateValue, targetMeta, isCount } = input;
const { db } = ctx;
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) => {
if (!rowOrRows) {
return rowOrRows;
}
return fromRow(targetMeta, rowOrRows);
};
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) => fromRow(targetMeta, rowOrRows);
const { joinTable } = attribute;
@ -379,13 +362,7 @@ const morphX = async (
const { attribute, attributeName, results, populateValue, targetMeta } = input;
const { db, uid } = ctx;
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) => {
if (!rowOrRows) {
return rowOrRows;
}
return fromRow(targetMeta, rowOrRows);
};
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) => fromRow(targetMeta, rowOrRows);
const { target, morphBy } = attribute;
@ -562,12 +539,9 @@ const morphToMany = async (input: Input<Relation.MorphToMany>, ctx: Context) =>
const id = joinResult[idColumn.name] as ID;
const type = joinResult[typeColumn.name] as string;
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) => {
if (!rowOrRows) {
return rowOrRows;
}
return fromRow(db.metadata.get(type), rowOrRows);
};
const targetMeta = db.metadata.get(type);
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) => fromRow(targetMeta, rowOrRows);
return (map[type][id] || []).map((row) => {
return {
@ -642,13 +616,8 @@ const morphToOne = async (input: Input<Relation.MorphToOne>, ctx: Context) => {
const matchingRows = map[type][id];
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) => {
if (!rowOrRows) {
return rowOrRows;
}
return fromRow(db.metadata.get(type), rowOrRows);
};
const fromTargetRow = (rowOrRows: Row | Row[] | undefined) =>
fromRow(db.metadata.get(type), rowOrRows);
result[attributeName] = fromTargetRow(_.first(matchingRows));
});

View File

@ -41,7 +41,11 @@ const fromSingleRow = (meta: Meta, row: Row): Rec => {
return obj;
};
const fromRow = (meta: Meta, row: Row | Row[]) => {
const fromRow = (meta: Meta, row: Row | Row[] | undefined) => {
if (_.isNil(row)) {
return null;
}
if (Array.isArray(row)) {
return row.map((singleRow) => fromSingleRow(meta, singleRow));
}

View File

@ -398,6 +398,7 @@ const createQueryBuilder = (
state.where = helpers.processWhere(state.where, { qb: this, uid, db });
state.populate = helpers.processPopulate(state.populate, { qb: this, uid, db });
state.data = helpers.toRow(meta, state.data);
this.processSelect();