mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 13:03:50 +00:00
test: Add unit test for projects/app/src/pages/api/core/dataset/paths.ts (#4349)
* Add unit tests for the getParents function in the dataset paths API. * Update paths.ts --------- Co-authored-by: gru-agent[bot] <185149714+gru-agent[bot]@users.noreply.github.com> Co-authored-by: Archer <545436317@qq.com>
This commit is contained in:
82
test/cases/pages/api/core/dataset/paths.test.ts
Normal file
82
test/cases/pages/api/core/dataset/paths.test.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { getParents } from '@/pages/api/core/dataset/paths';
|
||||
import { MongoDataset } from '@fastgpt/service/core/dataset/schema';
|
||||
|
||||
vi.mock('@fastgpt/service/core/dataset/schema', () => ({
|
||||
MongoDataset: {
|
||||
findById: vi.fn()
|
||||
},
|
||||
DatasetCollectionName: 'datasets'
|
||||
}));
|
||||
|
||||
describe('getParents', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should return empty array if parentId is undefined', async () => {
|
||||
const result = await getParents(undefined);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return empty array if parent not found', async () => {
|
||||
vi.mocked(MongoDataset.findById).mockResolvedValueOnce(null);
|
||||
|
||||
const result = await getParents('non-existent-id');
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return single parent path if no further parents', async () => {
|
||||
vi.mocked(MongoDataset.findById).mockResolvedValueOnce({
|
||||
name: 'Parent1',
|
||||
parentId: undefined
|
||||
});
|
||||
|
||||
const result = await getParents('parent1-id');
|
||||
|
||||
expect(result).toEqual([{ parentId: 'parent1-id', parentName: 'Parent1' }]);
|
||||
});
|
||||
|
||||
it('should return full parent path for nested parents', async () => {
|
||||
vi.mocked(MongoDataset.findById)
|
||||
.mockResolvedValueOnce({
|
||||
name: 'Child',
|
||||
parentId: 'parent1-id'
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
name: 'Parent1',
|
||||
parentId: 'parent2-id'
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
name: 'Parent2',
|
||||
parentId: undefined
|
||||
});
|
||||
|
||||
const result = await getParents('child-id');
|
||||
|
||||
expect(result).toEqual([
|
||||
{ parentId: 'parent2-id', parentName: 'Parent2' },
|
||||
{ parentId: 'parent1-id', parentName: 'Parent1' },
|
||||
{ parentId: 'child-id', parentName: 'Child' }
|
||||
]);
|
||||
});
|
||||
|
||||
it('should handle circular references gracefully', async () => {
|
||||
vi.mocked(MongoDataset.findById)
|
||||
.mockResolvedValueOnce({
|
||||
name: 'Node1',
|
||||
parentId: 'node2-id'
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
name: 'Node2',
|
||||
parentId: 'node1-id' // Circular reference
|
||||
});
|
||||
|
||||
const result = await getParents('node1-id');
|
||||
|
||||
expect(result).toEqual([
|
||||
{ parentId: 'node2-id', parentName: 'Node2' },
|
||||
{ parentId: 'node1-id', parentName: 'Node1' }
|
||||
]);
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user