251 lines
7.5 KiB
JavaScript
251 lines
7.5 KiB
JavaScript
/**
|
|
* 测试经纬度更新功能
|
|
* @file test-update-location.js
|
|
* @description 专门测试更新操作中经纬度数据的正确处理
|
|
*/
|
|
|
|
const { Farm } = require('./models');
|
|
const { sequelize } = require('./config/database-simple');
|
|
|
|
async function testLocationUpdate() {
|
|
try {
|
|
console.log('开始测试经纬度更新功能...');
|
|
|
|
// 1. 创建初始记录
|
|
console.log('\n1. 创建初始记录');
|
|
const initialData = {
|
|
name: '更新测试养殖场',
|
|
type: 'farm',
|
|
location: { lng: 106.123456, lat: 38.654321 },
|
|
address: '初始地址',
|
|
contact: '初始联系人',
|
|
phone: '13800138000',
|
|
status: 'active'
|
|
};
|
|
|
|
const farm = await Farm.create(initialData);
|
|
console.log('初始记录创建成功:');
|
|
console.log('- ID:', farm.id);
|
|
console.log('- Location:', JSON.stringify(farm.location));
|
|
|
|
// 2. 测试更新经纬度
|
|
console.log('\n2. 测试更新经纬度');
|
|
const newLongitude = 107.987654;
|
|
const newLatitude = 39.123456;
|
|
|
|
console.log('新的经纬度值:', { longitude: newLongitude, latitude: newLatitude });
|
|
|
|
// 模拟控制器的更新逻辑
|
|
const location = { ...(farm.location || {}) };
|
|
if (newLongitude !== undefined && newLongitude !== null) {
|
|
location.lng = parseFloat(newLongitude);
|
|
}
|
|
if (newLatitude !== undefined && newLatitude !== null) {
|
|
location.lat = parseFloat(newLatitude);
|
|
}
|
|
|
|
console.log('处理后的location对象:', location);
|
|
|
|
// 执行更新
|
|
await farm.update({
|
|
location
|
|
});
|
|
|
|
// 重新查询验证
|
|
const updatedFarm = await Farm.findByPk(farm.id);
|
|
console.log('\n更新后的记录:');
|
|
console.log('- Location:', JSON.stringify(updatedFarm.location));
|
|
console.log('- Location.lng:', updatedFarm.location.lng);
|
|
console.log('- Location.lat:', updatedFarm.location.lat);
|
|
|
|
// 验证更新是否成功
|
|
const updateSuccess = (
|
|
updatedFarm.location.lng === newLongitude &&
|
|
updatedFarm.location.lat === newLatitude
|
|
);
|
|
|
|
console.log('\n更新验证结果:');
|
|
console.log('- 期望经度:', newLongitude);
|
|
console.log('- 实际经度:', updatedFarm.location.lng);
|
|
console.log('- 期望纬度:', newLatitude);
|
|
console.log('- 实际纬度:', updatedFarm.location.lat);
|
|
console.log('- 更新成功:', updateSuccess ? '✅' : '❌');
|
|
|
|
// 3. 测试部分更新(只更新经度)
|
|
console.log('\n3. 测试部分更新(只更新经度)');
|
|
const partialLongitude = 108.555555;
|
|
|
|
const partialLocation = { ...(updatedFarm.location || {}) };
|
|
if (partialLongitude !== undefined && partialLongitude !== null) {
|
|
partialLocation.lng = parseFloat(partialLongitude);
|
|
}
|
|
// 注意:这里不更新纬度
|
|
|
|
await farm.update({ location: partialLocation });
|
|
|
|
const partialUpdatedFarm = await Farm.findByPk(farm.id);
|
|
console.log('部分更新后的记录:');
|
|
console.log('- Location:', JSON.stringify(partialUpdatedFarm.location));
|
|
console.log('- 经度是否更新:', partialUpdatedFarm.location.lng === partialLongitude ? '✅' : '❌');
|
|
console.log('- 纬度是否保持:', partialUpdatedFarm.location.lat === newLatitude ? '✅' : '❌');
|
|
|
|
// 4. 测试清空经纬度
|
|
console.log('\n4. 测试清空经纬度');
|
|
const emptyLocation = {};
|
|
|
|
await farm.update({ location: emptyLocation });
|
|
|
|
const emptyUpdatedFarm = await Farm.findByPk(farm.id);
|
|
console.log('清空后的记录:');
|
|
console.log('- Location:', JSON.stringify(emptyUpdatedFarm.location));
|
|
console.log('- 是否为空对象:', Object.keys(emptyUpdatedFarm.location).length === 0 ? '✅' : '❌');
|
|
|
|
// 5. 清理测试数据
|
|
console.log('\n5. 清理测试数据');
|
|
await farm.destroy();
|
|
console.log('测试数据已清理');
|
|
|
|
console.log('\n✅ 经纬度更新功能测试完成');
|
|
|
|
} catch (error) {
|
|
console.error('❌ 测试过程中发生错误:', error);
|
|
console.error('错误详情:', error.message);
|
|
if (error.sql) {
|
|
console.error('SQL语句:', error.sql);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 测试通过API接口的完整流程
|
|
async function testAPIFlow() {
|
|
try {
|
|
console.log('\n=== 测试API接口流程 ===');
|
|
|
|
// 模拟前端发送的请求数据
|
|
const createRequest = {
|
|
body: {
|
|
name: 'API测试养殖场',
|
|
owner: 'API测试负责人',
|
|
phone: '13900139000',
|
|
address: 'API测试地址',
|
|
longitude: 106.789123,
|
|
latitude: 38.456789,
|
|
status: 'active'
|
|
}
|
|
};
|
|
|
|
console.log('\n1. 模拟创建请求');
|
|
console.log('请求数据:', {
|
|
longitude: createRequest.body.longitude,
|
|
latitude: createRequest.body.latitude
|
|
});
|
|
|
|
// 模拟createFarm控制器逻辑
|
|
const { name, owner, longitude, latitude, address, phone, status } = createRequest.body;
|
|
|
|
const location = {};
|
|
if (longitude !== undefined && longitude !== null) {
|
|
location.lng = parseFloat(longitude);
|
|
}
|
|
if (latitude !== undefined && latitude !== null) {
|
|
location.lat = parseFloat(latitude);
|
|
}
|
|
|
|
const farm = await Farm.create({
|
|
name,
|
|
type: 'farm',
|
|
location,
|
|
address,
|
|
contact: owner,
|
|
phone,
|
|
status: status || 'active'
|
|
});
|
|
|
|
console.log('创建结果:', {
|
|
id: farm.id,
|
|
location: farm.location
|
|
});
|
|
|
|
// 模拟更新请求
|
|
const updateRequest = {
|
|
params: { id: farm.id },
|
|
body: {
|
|
name: farm.name,
|
|
owner: farm.contact,
|
|
phone: farm.phone,
|
|
address: farm.address,
|
|
longitude: 107.111222,
|
|
latitude: 39.333444,
|
|
status: farm.status
|
|
}
|
|
};
|
|
|
|
console.log('\n2. 模拟更新请求');
|
|
console.log('更新数据:', {
|
|
longitude: updateRequest.body.longitude,
|
|
latitude: updateRequest.body.latitude
|
|
});
|
|
|
|
// 模拟updateFarm控制器逻辑
|
|
const updateData = updateRequest.body;
|
|
const updateLocation = { ...(farm.location || {}) };
|
|
if (updateData.longitude !== undefined && updateData.longitude !== null) {
|
|
updateLocation.lng = parseFloat(updateData.longitude);
|
|
}
|
|
if (updateData.latitude !== undefined && updateData.latitude !== null) {
|
|
updateLocation.lat = parseFloat(updateData.latitude);
|
|
}
|
|
|
|
await farm.update({
|
|
name: updateData.name,
|
|
location: updateLocation,
|
|
address: updateData.address,
|
|
contact: updateData.owner,
|
|
phone: updateData.phone,
|
|
status: updateData.status
|
|
});
|
|
|
|
// 验证更新结果
|
|
const finalFarm = await Farm.findByPk(farm.id);
|
|
console.log('更新结果:', {
|
|
location: finalFarm.location
|
|
});
|
|
|
|
const apiUpdateSuccess = (
|
|
finalFarm.location.lng === updateData.longitude &&
|
|
finalFarm.location.lat === updateData.latitude
|
|
);
|
|
|
|
console.log('API流程验证:', apiUpdateSuccess ? '✅ 成功' : '❌ 失败');
|
|
|
|
// 清理
|
|
await farm.destroy();
|
|
|
|
} catch (error) {
|
|
console.error('API流程测试失败:', error);
|
|
}
|
|
}
|
|
|
|
// 主函数
|
|
async function main() {
|
|
try {
|
|
await sequelize.authenticate();
|
|
console.log('数据库连接成功');
|
|
|
|
await testLocationUpdate();
|
|
await testAPIFlow();
|
|
|
|
} catch (error) {
|
|
console.error('测试失败:', error);
|
|
} finally {
|
|
await sequelize.close();
|
|
console.log('\n数据库连接已关闭');
|
|
}
|
|
}
|
|
|
|
// 运行测试
|
|
if (require.main === module) {
|
|
main();
|
|
}
|
|
|
|
module.exports = { testLocationUpdate, testAPIFlow }; |