mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Added new tests
This commit is contained in:
parent
91e5f4cabf
commit
396f7c1b8e
|
@ -1,116 +0,0 @@
|
||||||
import { handlePagination } from '../GenericFunctions';
|
|
||||||
|
|
||||||
describe('GenericFunctions - handlePagination', () => {
|
|
||||||
let mockContext: any;
|
|
||||||
let mockMakeRoutingRequest: jest.Mock;
|
|
||||||
let resultOptions: any;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
mockMakeRoutingRequest = jest.fn();
|
|
||||||
mockContext = {
|
|
||||||
makeRoutingRequest: mockMakeRoutingRequest,
|
|
||||||
getNodeParameter: jest.fn(),
|
|
||||||
};
|
|
||||||
|
|
||||||
resultOptions = {
|
|
||||||
maxResults: 60,
|
|
||||||
options: { body: {} },
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should aggregate results and handle pagination when returnAll is true', async () => {
|
|
||||||
mockMakeRoutingRequest
|
|
||||||
.mockResolvedValueOnce([
|
|
||||||
{ id: 1 },
|
|
||||||
{ id: 2 },
|
|
||||||
{ headers: { 'x-ms-continuation': 'token-1' } },
|
|
||||||
])
|
|
||||||
.mockResolvedValueOnce([{ id: 3 }, { id: 4 }, { headers: {} }]);
|
|
||||||
|
|
||||||
mockContext.getNodeParameter.mockImplementation((param: string) => {
|
|
||||||
if (param === 'returnAll') return true;
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await handlePagination.call(mockContext, resultOptions);
|
|
||||||
|
|
||||||
expect(result).toEqual([
|
|
||||||
{ json: { id: 1 } },
|
|
||||||
{ json: { id: 2 } },
|
|
||||||
{ json: { id: 3 } },
|
|
||||||
{ json: { id: 4 } },
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect(mockMakeRoutingRequest).toHaveBeenCalledTimes(2);
|
|
||||||
expect(resultOptions.options.headers).toEqual({
|
|
||||||
'x-ms-continuation': 'token-1',
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should stop pagination after reaching limit when returnAll is false', async () => {
|
|
||||||
mockMakeRoutingRequest
|
|
||||||
.mockResolvedValueOnce([
|
|
||||||
{ id: 1 },
|
|
||||||
{ id: 2 },
|
|
||||||
{ headers: { 'x-ms-continuation': 'token-1' } },
|
|
||||||
])
|
|
||||||
.mockResolvedValueOnce([{ id: 3 }, { id: 4 }, { headers: {} }]);
|
|
||||||
|
|
||||||
mockContext.getNodeParameter.mockImplementation((param: string) => {
|
|
||||||
if (param === 'returnAll') return false;
|
|
||||||
if (param === 'limit') return 3;
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await handlePagination.call(mockContext, resultOptions);
|
|
||||||
|
|
||||||
expect(result).toEqual([{ json: { id: 1 } }, { json: { id: 2 } }, { json: { id: 3 } }]);
|
|
||||||
|
|
||||||
expect(mockMakeRoutingRequest).toHaveBeenCalledTimes(2);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should handle cases with no continuation token gracefully', async () => {
|
|
||||||
mockMakeRoutingRequest.mockResolvedValueOnce([{ id: 1 }, { id: 2 }]);
|
|
||||||
|
|
||||||
mockContext.getNodeParameter.mockImplementation((param: string) => {
|
|
||||||
if (param === 'returnAll') return true;
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await handlePagination.call(mockContext, resultOptions);
|
|
||||||
|
|
||||||
expect(result).toEqual([{ json: { id: 1 } }, { json: { id: 2 } }]);
|
|
||||||
|
|
||||||
expect(mockMakeRoutingRequest).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should respect the limit even if fewer results are available', async () => {
|
|
||||||
mockMakeRoutingRequest.mockResolvedValueOnce([{ id: 1 }, { id: 2 }]);
|
|
||||||
|
|
||||||
mockContext.getNodeParameter.mockImplementation((param: string) => {
|
|
||||||
if (param === 'returnAll') return false;
|
|
||||||
if (param === 'limit') return 5;
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await handlePagination.call(mockContext, resultOptions);
|
|
||||||
|
|
||||||
expect(result).toEqual([{ json: { id: 1 } }, { json: { id: 2 } }]);
|
|
||||||
|
|
||||||
expect(mockMakeRoutingRequest).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('should break the loop if no results are returned', async () => {
|
|
||||||
mockMakeRoutingRequest.mockResolvedValueOnce([]);
|
|
||||||
|
|
||||||
mockContext.getNodeParameter.mockImplementation((param: string) => {
|
|
||||||
if (param === 'returnAll') return true;
|
|
||||||
return undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await handlePagination.call(mockContext, resultOptions);
|
|
||||||
|
|
||||||
expect(result).toEqual([]);
|
|
||||||
expect(mockMakeRoutingRequest).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -16,12 +16,11 @@ describe('GenericFunctions - microsoftCosmosDbRequest', () => {
|
||||||
|
|
||||||
test('should make a successful request with correct options', async () => {
|
test('should make a successful request with correct options', async () => {
|
||||||
mockRequestWithAuthentication.mockResolvedValueOnce({ success: true });
|
mockRequestWithAuthentication.mockResolvedValueOnce({ success: true });
|
||||||
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({ account: 'us-east-1' });
|
|
||||||
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({
|
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({
|
||||||
|
account: 'us-east-1',
|
||||||
database: 'first_database_1',
|
database: 'first_database_1',
|
||||||
});
|
baseUrl: 'https://us-east-1.documents.azure.com/dbs/first_database_1',
|
||||||
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({
|
|
||||||
baseUrl: 'https://us-east-1.documents.azure.com/dbs',
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
|
|
|
@ -18,7 +18,11 @@ describe('GenericFunctions - searchCollections', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should make a GET request to fetch collections and return results', async () => {
|
it('should make a GET request to fetch collections and return results', async () => {
|
||||||
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({ account: 'us-east-1' });
|
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({
|
||||||
|
account: 'us-east-1',
|
||||||
|
database: 'first_database_1',
|
||||||
|
baseUrl: 'https://us-east-1.documents.azure.com',
|
||||||
|
});
|
||||||
|
|
||||||
mockRequestWithAuthentication.mockResolvedValueOnce({
|
mockRequestWithAuthentication.mockResolvedValueOnce({
|
||||||
DocumentCollections: [{ id: 'Collection1' }, { id: 'Collection2' }],
|
DocumentCollections: [{ id: 'Collection1' }, { id: 'Collection2' }],
|
||||||
|
@ -27,7 +31,7 @@ describe('GenericFunctions - searchCollections', () => {
|
||||||
const response = await searchCollections.call(mockContext);
|
const response = await searchCollections.call(mockContext);
|
||||||
|
|
||||||
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||||
'azureCosmosDbSharedKeyApi',
|
'microsoftCosmosDbSharedKeyApi',
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
baseURL: 'https://us-east-1.documents.azure.com',
|
baseURL: 'https://us-east-1.documents.azure.com',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
@ -49,7 +53,11 @@ describe('GenericFunctions - searchCollections', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should filter collections by the provided filter string', async () => {
|
it('should filter collections by the provided filter string', async () => {
|
||||||
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({ account: 'us-east-1' });
|
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({
|
||||||
|
account: 'us-east-1',
|
||||||
|
database: 'first_database_1',
|
||||||
|
baseUrl: 'https://us-east-1.documents.azure.com',
|
||||||
|
});
|
||||||
|
|
||||||
mockRequestWithAuthentication.mockResolvedValueOnce({
|
mockRequestWithAuthentication.mockResolvedValueOnce({
|
||||||
DocumentCollections: [{ id: 'Test-Col-1' }, { id: 'Prod-Col-1' }],
|
DocumentCollections: [{ id: 'Test-Col-1' }, { id: 'Prod-Col-1' }],
|
||||||
|
@ -58,7 +66,7 @@ describe('GenericFunctions - searchCollections', () => {
|
||||||
const response = await searchCollections.call(mockContext, 'Test');
|
const response = await searchCollections.call(mockContext, 'Test');
|
||||||
|
|
||||||
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||||
'azureCosmosDbSharedKeyApi',
|
'microsoftCosmosDbSharedKeyApi',
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
baseURL: 'https://us-east-1.documents.azure.com',
|
baseURL: 'https://us-east-1.documents.azure.com',
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
|
|
|
@ -0,0 +1,124 @@
|
||||||
|
import type { ILoadOptionsFunctions } from 'n8n-workflow';
|
||||||
|
|
||||||
|
import { searchItems } from '../GenericFunctions';
|
||||||
|
|
||||||
|
describe('GenericFunctions - searchItems', () => {
|
||||||
|
const mockRequestWithAuthentication = jest.fn();
|
||||||
|
|
||||||
|
const mockContext = {
|
||||||
|
helpers: {
|
||||||
|
requestWithAuthentication: mockRequestWithAuthentication,
|
||||||
|
},
|
||||||
|
getNodeParameter: jest.fn(),
|
||||||
|
getCredentials: jest.fn(),
|
||||||
|
} as unknown as ILoadOptionsFunctions;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fetch documents and return formatted results', async () => {
|
||||||
|
(mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce({
|
||||||
|
mode: 'list',
|
||||||
|
value: 'coll-1',
|
||||||
|
});
|
||||||
|
|
||||||
|
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({
|
||||||
|
account: 'us-east-1',
|
||||||
|
database: 'first_database_1',
|
||||||
|
baseUrl: 'https://us-east-1.documents.azure.com',
|
||||||
|
});
|
||||||
|
|
||||||
|
mockRequestWithAuthentication.mockResolvedValueOnce({
|
||||||
|
Documents: [{ id: 'Item 1' }, { id: 'Item 2' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await searchItems.call(mockContext);
|
||||||
|
|
||||||
|
expect(mockRequestWithAuthentication).toHaveBeenCalledWith(
|
||||||
|
'microsoftCosmosDbSharedKeyApi',
|
||||||
|
expect.objectContaining({
|
||||||
|
method: 'GET',
|
||||||
|
url: '/colls/coll-1/docs',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(response).toEqual({
|
||||||
|
results: [
|
||||||
|
{ name: 'Item1', value: 'Item 1' }, // Space removed from 'Item 1'
|
||||||
|
{ name: 'Item2', value: 'Item 2' },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should filter results based on the provided filter string', async () => {
|
||||||
|
(mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce({
|
||||||
|
mode: 'list',
|
||||||
|
value: 'coll-1',
|
||||||
|
});
|
||||||
|
|
||||||
|
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({
|
||||||
|
account: 'us-east-1',
|
||||||
|
database: 'first_database_1',
|
||||||
|
baseUrl: 'https://us-east-1.documents.azure.com',
|
||||||
|
});
|
||||||
|
|
||||||
|
mockRequestWithAuthentication.mockResolvedValueOnce({
|
||||||
|
Documents: [{ id: 'TestItem' }, { id: 'ProdItem' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await searchItems.call(mockContext, 'Test');
|
||||||
|
|
||||||
|
expect(response).toEqual({
|
||||||
|
results: [{ name: 'TestItem', value: 'TestItem' }],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return an empty array if no documents are found', async () => {
|
||||||
|
(mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce({
|
||||||
|
mode: 'list',
|
||||||
|
value: 'coll-1',
|
||||||
|
});
|
||||||
|
|
||||||
|
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({
|
||||||
|
account: 'us-east-1',
|
||||||
|
database: 'first_database_1',
|
||||||
|
baseUrl: 'https://us-east-1.documents.azure.com',
|
||||||
|
});
|
||||||
|
|
||||||
|
mockRequestWithAuthentication.mockResolvedValueOnce({
|
||||||
|
Documents: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await searchItems.call(mockContext);
|
||||||
|
|
||||||
|
expect(response).toEqual({ results: [] });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle missing Documents property gracefully', async () => {
|
||||||
|
(mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce({
|
||||||
|
mode: 'list',
|
||||||
|
value: 'coll-1',
|
||||||
|
});
|
||||||
|
|
||||||
|
(mockContext.getCredentials as jest.Mock).mockResolvedValueOnce({
|
||||||
|
account: 'us-east-1',
|
||||||
|
database: 'first_database_1',
|
||||||
|
baseUrl: 'https://us-east-1.documents.azure.com',
|
||||||
|
});
|
||||||
|
|
||||||
|
mockRequestWithAuthentication.mockResolvedValueOnce({
|
||||||
|
unexpectedKey: 'value',
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await searchItems.call(mockContext);
|
||||||
|
|
||||||
|
expect(response).toEqual({ results: [] });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error when collection ID is missing', async () => {
|
||||||
|
(mockContext.getNodeParameter as jest.Mock).mockReturnValueOnce({ mode: 'list', value: '' });
|
||||||
|
|
||||||
|
await expect(searchItems.call(mockContext)).rejects.toThrow('Collection ID is required.');
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue