216 lines
6.8 KiB
JavaScript
216 lines
6.8 KiB
JavaScript
/**
|
||
* 测试经纬度数据传递和存储
|
||
* @file test-location-data.js
|
||
* @description 验证经纬度输入值是否正确传递到数据库操作层
|
||
*/
|
||
|
||
const { Farm } = require('./models');
|
||
const { sequelize } = require('./config/database-simple');
|
||
|
||
// 测试数据
|
||
const testData = {
|
||
name: '测试养殖场_经纬度验证',
|
||
owner: '测试负责人',
|
||
phone: '13800138000',
|
||
address: '测试地址',
|
||
longitude: 106.123456,
|
||
latitude: 38.654321,
|
||
status: 'active'
|
||
};
|
||
|
||
async function testLocationDataFlow() {
|
||
try {
|
||
console.log('开始测试经纬度数据传递流程...');
|
||
|
||
// 1. 测试创建养殖场时的经纬度处理
|
||
console.log('\n1. 测试创建养殖场时的经纬度处理');
|
||
console.log('输入数据:', {
|
||
longitude: testData.longitude,
|
||
latitude: testData.latitude
|
||
});
|
||
|
||
// 模拟后端控制器的处理逻辑
|
||
const { longitude, latitude } = testData;
|
||
const location = {};
|
||
if (longitude !== undefined && longitude !== null) {
|
||
location.lng = parseFloat(longitude);
|
||
}
|
||
if (latitude !== undefined && latitude !== null) {
|
||
location.lat = parseFloat(latitude);
|
||
}
|
||
|
||
console.log('处理后的location对象:', location);
|
||
|
||
// 创建养殖场记录
|
||
const farm = await Farm.create({
|
||
name: testData.name,
|
||
type: 'farm',
|
||
location,
|
||
address: testData.address,
|
||
contact: testData.owner,
|
||
phone: testData.phone,
|
||
status: testData.status
|
||
});
|
||
|
||
console.log('创建成功,数据库中的记录:');
|
||
console.log('- ID:', farm.id);
|
||
console.log('- Name:', farm.name);
|
||
console.log('- Location:', JSON.stringify(farm.location));
|
||
console.log('- Location.lng:', farm.location.lng);
|
||
console.log('- Location.lat:', farm.location.lat);
|
||
|
||
// 2. 测试更新养殖场时的经纬度处理
|
||
console.log('\n2. 测试更新养殖场时的经纬度处理');
|
||
const newLongitude = 107.987654;
|
||
const newLatitude = 39.123456;
|
||
|
||
console.log('新的输入数据:', {
|
||
longitude: newLongitude,
|
||
latitude: newLatitude
|
||
});
|
||
|
||
// 模拟更新逻辑
|
||
const updateLocation = farm.location || {};
|
||
if (newLongitude !== undefined && newLongitude !== null) {
|
||
updateLocation.lng = parseFloat(newLongitude);
|
||
}
|
||
if (newLatitude !== undefined && newLatitude !== null) {
|
||
updateLocation.lat = parseFloat(newLatitude);
|
||
}
|
||
|
||
console.log('处理后的location对象:', updateLocation);
|
||
|
||
await farm.update({
|
||
location: updateLocation
|
||
});
|
||
|
||
// 重新查询验证
|
||
const updatedFarm = await Farm.findByPk(farm.id);
|
||
console.log('更新后数据库中的记录:');
|
||
console.log('- Location:', JSON.stringify(updatedFarm.location));
|
||
console.log('- Location.lng:', updatedFarm.location.lng);
|
||
console.log('- Location.lat:', updatedFarm.location.lat);
|
||
|
||
// 3. 测试数据类型验证
|
||
console.log('\n3. 测试数据类型验证');
|
||
console.log('原始输入类型:', typeof testData.longitude, typeof testData.latitude);
|
||
console.log('parseFloat后类型:', typeof parseFloat(testData.longitude), typeof parseFloat(testData.latitude));
|
||
console.log('数据库存储值类型:', typeof updatedFarm.location.lng, typeof updatedFarm.location.lat);
|
||
|
||
// 4. 测试边界值
|
||
console.log('\n4. 测试边界值处理');
|
||
const boundaryTests = [
|
||
{ lng: -180, lat: -90, desc: '最小边界值' },
|
||
{ lng: 180, lat: 90, desc: '最大边界值' },
|
||
{ lng: 0, lat: 0, desc: '零值' },
|
||
{ lng: 106.123456789, lat: 38.987654321, desc: '高精度值' }
|
||
];
|
||
|
||
for (const test of boundaryTests) {
|
||
const testLocation = {
|
||
lng: parseFloat(test.lng),
|
||
lat: parseFloat(test.lat)
|
||
};
|
||
|
||
await farm.update({ location: testLocation });
|
||
const verifyFarm = await Farm.findByPk(farm.id);
|
||
|
||
console.log(`${test.desc}:`);
|
||
console.log(` 输入: lng=${test.lng}, lat=${test.lat}`);
|
||
console.log(` 存储: lng=${verifyFarm.location.lng}, lat=${verifyFarm.location.lat}`);
|
||
console.log(` 精度保持: ${test.lng === verifyFarm.location.lng && test.lat === verifyFarm.location.lat}`);
|
||
}
|
||
|
||
// 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);
|
||
}
|
||
}
|
||
}
|
||
|
||
// 测试空值处理
|
||
async function testNullValues() {
|
||
try {
|
||
console.log('\n=== 测试空值处理 ===');
|
||
|
||
const testCases = [
|
||
{ longitude: undefined, latitude: undefined, desc: 'undefined值' },
|
||
{ longitude: null, latitude: null, desc: 'null值' },
|
||
{ longitude: '', latitude: '', desc: '空字符串' },
|
||
{ longitude: 106.123, latitude: undefined, desc: '仅经度有值' },
|
||
{ longitude: undefined, latitude: 38.456, desc: '仅纬度有值' }
|
||
];
|
||
|
||
for (let i = 0; i < testCases.length; i++) {
|
||
const testCase = testCases[i];
|
||
console.log(`\n测试案例 ${i + 1}: ${testCase.desc}`);
|
||
console.log('输入:', { longitude: testCase.longitude, latitude: testCase.latitude });
|
||
|
||
// 模拟控制器处理逻辑
|
||
const location = {};
|
||
if (testCase.longitude !== undefined && testCase.longitude !== null && testCase.longitude !== '') {
|
||
location.lng = parseFloat(testCase.longitude);
|
||
}
|
||
if (testCase.latitude !== undefined && testCase.latitude !== null && testCase.latitude !== '') {
|
||
location.lat = parseFloat(testCase.latitude);
|
||
}
|
||
|
||
console.log('处理后location:', location);
|
||
|
||
try {
|
||
const farm = await Farm.create({
|
||
name: `测试空值_${i + 1}`,
|
||
type: 'farm',
|
||
location,
|
||
address: '测试地址',
|
||
contact: '测试联系人',
|
||
status: 'active'
|
||
});
|
||
|
||
console.log('创建成功,存储的location:', JSON.stringify(farm.location));
|
||
|
||
// 清理
|
||
await farm.destroy();
|
||
} catch (error) {
|
||
console.log('创建失败:', error.message);
|
||
}
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('空值测试失败:', error);
|
||
}
|
||
}
|
||
|
||
// 主函数
|
||
async function main() {
|
||
try {
|
||
// 确保数据库连接
|
||
await sequelize.authenticate();
|
||
console.log('数据库连接成功');
|
||
|
||
await testLocationDataFlow();
|
||
await testNullValues();
|
||
|
||
} catch (error) {
|
||
console.error('测试失败:', error);
|
||
} finally {
|
||
await sequelize.close();
|
||
console.log('\n数据库连接已关闭');
|
||
}
|
||
}
|
||
|
||
// 运行测试
|
||
if (require.main === module) {
|
||
main();
|
||
}
|
||
|
||
module.exports = { testLocationDataFlow, testNullValues }; |