Skip to content

Commit 1526312

Browse files
Add unit tests for updateTrainingData API handler and modify dataId to be optional for retrying all error data.
1 parent 9b24b35 commit 1526312

File tree

2 files changed

+165
-1
lines changed

2 files changed

+165
-1
lines changed

projects/app/src/pages/api/core/dataset/training/updateTrainingData.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { TrainingModeEnum } from '@fastgpt/global/core/dataset/constants';
88
export type updateTrainingDataBody = {
99
datasetId: string;
1010
collectionId: string;
11-
dataId: string;
11+
dataId?: string; // 改为可选,不传则重试所有错误数据
1212
q?: string;
1313
a?: string;
1414
chunkIndex?: number;
@@ -31,6 +31,25 @@ async function handler(
3131
per: WritePermissionVal
3232
});
3333

34+
// 如果没有传 dataId,则重试该集合下的所有错误数据
35+
if (!dataId) {
36+
await MongoDatasetTraining.updateMany(
37+
{
38+
teamId,
39+
datasetId,
40+
collectionId,
41+
errorMsg: { $exists: true, $ne: null }
42+
},
43+
{
44+
$unset: { errorMsg: '' },
45+
retryCount: 3,
46+
lockTime: new Date('2000')
47+
}
48+
);
49+
return {};
50+
}
51+
52+
// 单个数据重试逻辑
3453
const data = await MongoDatasetTraining.findOne({ teamId, datasetId, _id: dataId });
3554

3655
if (!data) {
@@ -77,3 +96,5 @@ async function handler(
7796
}
7897

7998
export default NextAPI(handler);
99+
100+
export { handler };
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import { describe, expect, it, vi } from 'vitest';
2+
import { handler } from '@/pages/api/core/dataset/training/updateTrainingData';
3+
import { MongoDatasetTraining } from '@fastgpt/service/core/dataset/training/schema';
4+
import { authDatasetCollection } from '@fastgpt/service/support/permission/dataset/auth';
5+
import { TrainingModeEnum } from '@fastgpt/global/core/dataset/constants';
6+
7+
vi.mock('@fastgpt/service/core/dataset/training/schema', () => ({
8+
MongoDatasetTraining: {
9+
findOne: vi.fn(),
10+
updateOne: vi.fn(),
11+
updateMany: vi.fn()
12+
}
13+
}));
14+
15+
vi.mock('@fastgpt/service/support/permission/dataset/auth', () => ({
16+
authDatasetCollection: vi.fn()
17+
}));
18+
19+
describe('updateTrainingData', () => {
20+
it('should retry all error data when dataId is not provided', async () => {
21+
vi.mocked(authDatasetCollection).mockResolvedValue({
22+
teamId: 'team1'
23+
});
24+
25+
const req = {
26+
body: {
27+
datasetId: 'dataset1',
28+
collectionId: 'collection1'
29+
}
30+
};
31+
32+
await handler(req as any);
33+
34+
expect(MongoDatasetTraining.updateMany).toHaveBeenCalledWith(
35+
{
36+
teamId: 'team1',
37+
datasetId: 'dataset1',
38+
collectionId: 'collection1',
39+
errorMsg: { $exists: true, $ne: null }
40+
},
41+
{
42+
$unset: { errorMsg: '' },
43+
retryCount: 3,
44+
lockTime: new Date('2000')
45+
}
46+
);
47+
});
48+
49+
it('should update single training data with image', async () => {
50+
vi.mocked(authDatasetCollection).mockResolvedValue({
51+
teamId: 'team1'
52+
});
53+
54+
vi.mocked(MongoDatasetTraining.findOne).mockResolvedValue({
55+
imageId: 'image1'
56+
});
57+
58+
const req = {
59+
body: {
60+
datasetId: 'dataset1',
61+
collectionId: 'collection1',
62+
dataId: 'data1',
63+
q: 'question',
64+
a: 'answer',
65+
chunkIndex: 1
66+
}
67+
};
68+
69+
await handler(req as any);
70+
71+
expect(MongoDatasetTraining.updateOne).toHaveBeenCalledWith(
72+
{
73+
teamId: 'team1',
74+
datasetId: 'dataset1',
75+
_id: 'data1'
76+
},
77+
{
78+
$unset: { errorMsg: '' },
79+
retryCount: 3,
80+
mode: TrainingModeEnum.chunk,
81+
q: 'question',
82+
a: 'answer',
83+
chunkIndex: 1,
84+
lockTime: new Date('2000')
85+
}
86+
);
87+
});
88+
89+
it('should update single training data without image', async () => {
90+
vi.mocked(authDatasetCollection).mockResolvedValue({
91+
teamId: 'team1'
92+
});
93+
94+
vi.mocked(MongoDatasetTraining.findOne).mockResolvedValue({});
95+
96+
const req = {
97+
body: {
98+
datasetId: 'dataset1',
99+
collectionId: 'collection1',
100+
dataId: 'data1',
101+
q: 'question',
102+
a: 'answer',
103+
chunkIndex: 1
104+
}
105+
};
106+
107+
await handler(req as any);
108+
109+
expect(MongoDatasetTraining.updateOne).toHaveBeenCalledWith(
110+
{
111+
teamId: 'team1',
112+
datasetId: 'dataset1',
113+
_id: 'data1'
114+
},
115+
{
116+
$unset: { errorMsg: '' },
117+
retryCount: 3,
118+
q: 'question',
119+
a: 'answer',
120+
chunkIndex: 1,
121+
lockTime: new Date('2000')
122+
}
123+
);
124+
});
125+
126+
it('should reject when data not found', async () => {
127+
vi.mocked(authDatasetCollection).mockResolvedValue({
128+
teamId: 'team1'
129+
});
130+
131+
vi.mocked(MongoDatasetTraining.findOne).mockResolvedValue(null);
132+
133+
const req = {
134+
body: {
135+
datasetId: 'dataset1',
136+
collectionId: 'collection1',
137+
dataId: 'data1'
138+
}
139+
};
140+
141+
await expect(handler(req as any)).rejects.toBe('data not found');
142+
});
143+
});

0 commit comments

Comments
 (0)