feat: update export automation scripts
This commit is contained in:
@@ -44,14 +44,24 @@ function extractReportId(reportInfo) {
|
||||
);
|
||||
}
|
||||
|
||||
function extractReportInfo(input) {
|
||||
if (!input.payload) {
|
||||
return input;
|
||||
}
|
||||
|
||||
return ensureObject(input.payload, 'payload');
|
||||
}
|
||||
|
||||
function validateAndNormalizeReportInput(input) {
|
||||
const reportInfo = ensureObject(input, 'body');
|
||||
const reportId = extractReportId(reportInfo);
|
||||
const body = ensureObject(input, 'body');
|
||||
const reportId = extractReportId(body);
|
||||
|
||||
if (!reportId) {
|
||||
throw new ValidationError('report_id is required in response info');
|
||||
}
|
||||
|
||||
const reportInfo = extractReportInfo(body);
|
||||
|
||||
return {
|
||||
reportId,
|
||||
reportInfo,
|
||||
@@ -68,6 +78,7 @@ function toDatabaseRecord(normalizedReport) {
|
||||
module.exports = {
|
||||
ValidationError,
|
||||
extractReportId,
|
||||
extractReportInfo,
|
||||
validateAndNormalizeReportInput,
|
||||
toDatabaseRecord,
|
||||
};
|
||||
|
||||
@@ -18,6 +18,31 @@ function createResponseInfo() {
|
||||
};
|
||||
}
|
||||
|
||||
function createPersistRequest() {
|
||||
return {
|
||||
reportId: 'report-001',
|
||||
sourceType: 'MANUAL_CAPTURE',
|
||||
sourceReportId: null,
|
||||
aadvid: '1648829117232140',
|
||||
payload: {
|
||||
name: '测试报告',
|
||||
startTime: '2025-03-01',
|
||||
endTime: '2026-02-28',
|
||||
categories: [],
|
||||
analysisDims: ['MARKETOVERVIEW'],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('validateAndNormalizeReportInput accepts persist request with payload', () => {
|
||||
const input = createPersistRequest();
|
||||
|
||||
const result = validateAndNormalizeReportInput(input);
|
||||
|
||||
assert.equal(result.reportId, 'report-001');
|
||||
assert.deepEqual(result.reportInfo, input.payload);
|
||||
});
|
||||
|
||||
test('validateAndNormalizeReportInput accepts response info with data.reportId', () => {
|
||||
const input = createResponseInfo();
|
||||
|
||||
@@ -74,3 +99,20 @@ test('toDatabaseRecord maps response info into segmented_market_reports columns'
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test('toDatabaseRecord maps persist request payload into segmented_market_reports report_info', () => {
|
||||
const normalized = validateAndNormalizeReportInput(createPersistRequest());
|
||||
|
||||
const record = toDatabaseRecord(normalized);
|
||||
|
||||
assert.deepEqual(record, {
|
||||
report_id: 'report-001',
|
||||
report_info: {
|
||||
name: '测试报告',
|
||||
startTime: '2025-03-01',
|
||||
endTime: '2026-02-28',
|
||||
categories: [],
|
||||
analysisDims: ['MARKETOVERVIEW'],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,6 +14,22 @@ function createResponseInfo() {
|
||||
};
|
||||
}
|
||||
|
||||
function createPersistRequest() {
|
||||
return {
|
||||
reportId: 'report-001',
|
||||
sourceType: 'MANUAL_CAPTURE',
|
||||
sourceReportId: null,
|
||||
aadvid: '1648829117232140',
|
||||
payload: {
|
||||
name: '测试报告',
|
||||
startTime: '2025-03-01',
|
||||
endTime: '2026-02-28',
|
||||
categories: [],
|
||||
analysisDims: ['MARKETOVERVIEW'],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function withServer(repository, callback) {
|
||||
const app = createApp({
|
||||
repository,
|
||||
@@ -131,3 +147,43 @@ test('POST /api/reports persists raw response info and returns created response'
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
test('POST /api/reports persists persist request payload as report_info', async () => {
|
||||
let savedRecord = null;
|
||||
|
||||
await withServer(
|
||||
{
|
||||
async save(record) {
|
||||
savedRecord = record;
|
||||
return {
|
||||
reportId: record.report_id,
|
||||
created: true,
|
||||
};
|
||||
},
|
||||
},
|
||||
async (baseUrl) => {
|
||||
const persistRequest = createPersistRequest();
|
||||
const response = await fetch(`${baseUrl}/api/reports`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(persistRequest),
|
||||
});
|
||||
const body = await response.json();
|
||||
|
||||
assert.equal(response.status, 201);
|
||||
assert.deepEqual(body, {
|
||||
success: true,
|
||||
data: {
|
||||
reportId: 'report-001',
|
||||
created: true,
|
||||
},
|
||||
});
|
||||
assert.deepEqual(savedRecord, {
|
||||
report_id: 'report-001',
|
||||
report_info: persistRequest.payload,
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user