125 lines
3.3 KiB
JavaScript
125 lines
3.3 KiB
JavaScript
|
|
const axios = require('axios');
|
|||
|
|
|
|||
|
|
const BASE_URL = 'http://localhost:5352/api/epidemic-activity';
|
|||
|
|
|
|||
|
|
// 创建axios实例,添加认证头
|
|||
|
|
const api = axios.create({
|
|||
|
|
baseURL: BASE_URL,
|
|||
|
|
headers: {
|
|||
|
|
'Authorization': 'Bearer test-token', // 测试用的token
|
|||
|
|
'Content-Type': 'application/json'
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 测试获取防疫活动列表
|
|||
|
|
async function testGetActivities() {
|
|||
|
|
try {
|
|||
|
|
console.log('测试获取防疫活动列表...');
|
|||
|
|
const response = await api.get('/');
|
|||
|
|
console.log('获取成功:', response.data);
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('获取失败:', error.response?.data || error.message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 测试创建防疫活动
|
|||
|
|
async function testCreateActivity() {
|
|||
|
|
try {
|
|||
|
|
console.log('测试创建防疫活动...');
|
|||
|
|
const newActivity = {
|
|||
|
|
activityName: '测试防疫活动',
|
|||
|
|
livestockCategory: '牛',
|
|||
|
|
diseaseCategory: '口蹄疫',
|
|||
|
|
vaccineUsed: '测试疫苗',
|
|||
|
|
vaccineBatch: 'TEST001',
|
|||
|
|
preventionDate: '2023-12-01至2023-12-31',
|
|||
|
|
activityStatus: 'active'
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
const response = await api.post('/', newActivity);
|
|||
|
|
console.log('创建成功:', response.data);
|
|||
|
|
return response.data.data.id;
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('创建失败:', error.response?.data || error.message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 测试更新防疫活动
|
|||
|
|
async function testUpdateActivity(id) {
|
|||
|
|
try {
|
|||
|
|
console.log('测试更新防疫活动...');
|
|||
|
|
const updateData = {
|
|||
|
|
activityName: '更新后的防疫活动',
|
|||
|
|
livestockCategory: '羊',
|
|||
|
|
diseaseCategory: '布鲁氏菌病',
|
|||
|
|
vaccineUsed: '更新疫苗',
|
|||
|
|
vaccineBatch: 'UPDATE001',
|
|||
|
|
preventionDate: '2023-12-15至2024-01-15',
|
|||
|
|
activityStatus: 'inactive'
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
const response = await api.put(`/${id}`, updateData);
|
|||
|
|
console.log('更新成功:', response.data);
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('更新失败:', error.response?.data || error.message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 测试切换活动状态
|
|||
|
|
async function testToggleStatus(id) {
|
|||
|
|
try {
|
|||
|
|
console.log('测试切换活动状态...');
|
|||
|
|
const response = await api.patch(`/${id}/status`);
|
|||
|
|
console.log('状态切换成功:', response.data);
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('状态切换失败:', error.response?.data || error.message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 测试删除防疫活动
|
|||
|
|
async function testDeleteActivity(id) {
|
|||
|
|
try {
|
|||
|
|
console.log('测试删除防疫活动...');
|
|||
|
|
const response = await api.delete(`/${id}`);
|
|||
|
|
console.log('删除成功:', response.data);
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('删除失败:', error.response?.data || error.message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 运行所有测试
|
|||
|
|
async function runTests() {
|
|||
|
|
console.log('开始测试防疫活动管理API...\n');
|
|||
|
|
|
|||
|
|
await testGetActivities();
|
|||
|
|
console.log('\n' + '='.repeat(50) + '\n');
|
|||
|
|
|
|||
|
|
const createdId = await testCreateActivity();
|
|||
|
|
console.log('\n' + '='.repeat(50) + '\n');
|
|||
|
|
|
|||
|
|
if (createdId) {
|
|||
|
|
await testUpdateActivity(createdId);
|
|||
|
|
console.log('\n' + '='.repeat(50) + '\n');
|
|||
|
|
|
|||
|
|
await testToggleStatus(createdId);
|
|||
|
|
console.log('\n' + '='.repeat(50) + '\n');
|
|||
|
|
|
|||
|
|
await testDeleteActivity(createdId);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('\n测试完成!');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 如果直接运行此文件
|
|||
|
|
if (require.main === module) {
|
|||
|
|
runTests();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
module.exports = {
|
|||
|
|
testGetActivities,
|
|||
|
|
testCreateActivity,
|
|||
|
|
testUpdateActivity,
|
|||
|
|
testToggleStatus,
|
|||
|
|
testDeleteActivity
|
|||
|
|
};
|