mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2026-10-14 03:50:46 +02:00
test(core): Add tests for the remaining data-store endpoints (no-changelog) (#18367)
This commit is contained in:
1 parent
1965029236
commit
b6aa0bbd6b
5 files changed
+1789
-24
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -1,10 +1,12 @@
|
||||
import { DataStoreCreateColumnSchema } from '@n8n/api-types';
|
||||
import { Service } from '@n8n/di';
|
||||
import { DataSource, EntityManager, Repository } from '@n8n/typeorm';
|
||||
import { UserError } from 'n8n-workflow';
|
||||
import { UnexpectedError, UserError } from 'n8n-workflow';
|
||||
|
||||
import { DataStoreColumn } from './data-store-column.entity';
|
||||
import { DataStoreRowsRepository } from './data-store-rows.repository';
|
||||
import { NotFoundError } from '@/errors/response-errors/not-found.error';
|
||||
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
||||
|
||||
@Service()
|
||||
export class DataStoreColumnRepository extends Repository<DataStoreColumn> {
|
||||
@@ -59,7 +61,7 @@ export class DataStoreColumnRepository extends Repository<DataStoreColumn> {
|
||||
|
||||
const queryRunner = em.queryRunner;
|
||||
if (!queryRunner) {
|
||||
throw new Error('QueryRunner is not available');
|
||||
throw new UnexpectedError('QueryRunner is not available');
|
||||
}
|
||||
|
||||
await this.dataStoreRowsRepository.ensureTableAndAddColumn(
|
||||
@@ -88,23 +90,25 @@ export class DataStoreColumnRepository extends Repository<DataStoreColumn> {
|
||||
|
||||
async moveColumn(rawDataStoreId: string, columnId: string, targetIndex: number) {
|
||||
await this.manager.transaction(async (em) => {
|
||||
const columnCount = await em.countBy(DataStoreColumn, { dataStoreId: rawDataStoreId });
|
||||
|
||||
if (targetIndex >= columnCount) {
|
||||
throw new UserError('tried to move column to index larger than column count');
|
||||
}
|
||||
|
||||
if (targetIndex < 0) {
|
||||
throw new UserError('tried to move column to negative index');
|
||||
}
|
||||
|
||||
const existingColumn = await em.findOneBy(DataStoreColumn, {
|
||||
id: columnId,
|
||||
dataStoreId: rawDataStoreId,
|
||||
});
|
||||
|
||||
if (existingColumn === null) {
|
||||
throw new UserError(`tried to move column not present in data store '${rawDataStoreId}'`);
|
||||
throw new NotFoundError(
|
||||
`tried to move column not present in data store '${rawDataStoreId}'`,
|
||||
);
|
||||
}
|
||||
|
||||
const columnCount = await em.countBy(DataStoreColumn, { dataStoreId: rawDataStoreId });
|
||||
|
||||
if (targetIndex >= columnCount) {
|
||||
throw new BadRequestError('tried to move column to index larger than column count');
|
||||
}
|
||||
|
||||
if (targetIndex < 0) {
|
||||
throw new BadRequestError('tried to move column to negative index');
|
||||
}
|
||||
|
||||
await this.shiftColumns(rawDataStoreId, existingColumn.index, -1, em);
|
||||
|
||||
@@ -37,7 +37,7 @@ function getConditionAndParams(
|
||||
dbType: DataSourceOptions['type'],
|
||||
): [string, Record<string, unknown>] {
|
||||
const paramName = `filter_${index}`;
|
||||
const column = `dataStore.${quoteIdentifier(filter.columnName, dbType)}`;
|
||||
const column = `${quoteIdentifier('dataStore', dbType)}.${quoteIdentifier(filter.columnName, dbType)}`;
|
||||
|
||||
switch (filter.condition) {
|
||||
case 'eq':
|
||||
@@ -185,7 +185,6 @@ export class DataStoreRowsRepository {
|
||||
|
||||
private applySorting(query: QueryBuilder, dto: ListDataStoreContentQueryDto): void {
|
||||
if (!dto.sortBy) {
|
||||
// query.orderBy('dataStore.', 'DESC');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -195,7 +194,7 @@ export class DataStoreRowsRepository {
|
||||
|
||||
private applySortingByField(query: QueryBuilder, field: string, direction: 'DESC' | 'ASC'): void {
|
||||
const dbType = this.dataSource.options.type;
|
||||
const quotedField = `dataStore.${quoteIdentifier(field, dbType)}`;
|
||||
const quotedField = `${quoteIdentifier('dataStore', dbType)}.${quoteIdentifier(field, dbType)}`;
|
||||
query.orderBy(quotedField, direction);
|
||||
}
|
||||
|
||||
|
||||
@@ -110,7 +110,7 @@ export class DataStoreService {
|
||||
});
|
||||
|
||||
if (existingColumnMatch === null) {
|
||||
throw new UserError(
|
||||
throw new NotFoundError(
|
||||
`Tried to delete column with name not present in data store '${dataStoreId}'`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
import type { CreateDataStoreColumnDto } from '@n8n/api-types';
|
||||
import type { CreateDataStoreColumnDto, DataStoreRows } from '@n8n/api-types';
|
||||
import { randomName } from '@n8n/backend-test-utils';
|
||||
import type { Project } from '@n8n/db';
|
||||
import { Container } from '@n8n/di';
|
||||
|
||||
import { DataStoreColumnRepository } from '@/modules/data-store/data-store-column.repository';
|
||||
import { DataStoreRowsRepository } from '@/modules/data-store/data-store-rows.repository';
|
||||
import { DataStoreRepository } from '@/modules/data-store/data-store.repository';
|
||||
import { toTableName } from '@/modules/data-store/utils/sql-utils';
|
||||
|
||||
export const createDataStore = async (
|
||||
project: Project,
|
||||
options: {
|
||||
name?: string;
|
||||
columns?: CreateDataStoreColumnDto[];
|
||||
data?: DataStoreRows;
|
||||
updatedAt?: Date;
|
||||
} = {},
|
||||
) => {
|
||||
@@ -27,5 +31,13 @@ export const createDataStore = async (
|
||||
dataStore.updatedAt = options.updatedAt;
|
||||
}
|
||||
|
||||
if (options.data) {
|
||||
const dataStoreColumnRepository = Container.get(DataStoreColumnRepository);
|
||||
const columns = await dataStoreColumnRepository.getColumns(dataStore.id);
|
||||
|
||||
const dataStoreRowsRepository = Container.get(DataStoreRowsRepository);
|
||||
await dataStoreRowsRepository.insertRows(toTableName(dataStore.id), options.data, columns);
|
||||
}
|
||||
|
||||
return dataStore;
|
||||
};
|
||||
Reference in new issue
Block a user