142 lines
4.6 KiB
JavaScript
142 lines
4.6 KiB
JavaScript
/**
|
|
* 测试前端和后端数据同步
|
|
* 验证经纬度数据的完整流程
|
|
* @file test-data-sync.js
|
|
*/
|
|
|
|
const { Farm } = require('./models');
|
|
const { sequelize } = require('./config/database-simple');
|
|
|
|
async function testDataSync() {
|
|
try {
|
|
console.log('连接数据库...');
|
|
await sequelize.authenticate();
|
|
|
|
console.log('\n=== 测试数据同步 ===');
|
|
|
|
// 1. 创建测试记录
|
|
console.log('\n1. 创建测试记录...');
|
|
const testFarm = await Farm.create({
|
|
name: '数据同步测试农场',
|
|
type: 'farm',
|
|
location: {
|
|
lng: 106.28,
|
|
lat: 38.47
|
|
},
|
|
address: '宁夏回族自治区银川市测试区',
|
|
contact: '测试管理员',
|
|
phone: '13800138000',
|
|
status: 'active'
|
|
});
|
|
|
|
console.log(`✅ 创建成功 - ID: ${testFarm.id}`);
|
|
console.log(` 经度: ${testFarm.location.lng}`);
|
|
console.log(` 纬度: ${testFarm.location.lat}`);
|
|
|
|
// 2. 查询记录验证
|
|
console.log('\n2. 查询记录验证...');
|
|
const retrievedFarm = await Farm.findByPk(testFarm.id);
|
|
console.log(`查询结果:`);
|
|
console.log(` ID: ${retrievedFarm.id}`);
|
|
console.log(` 名称: ${retrievedFarm.name}`);
|
|
console.log(` Location类型: ${typeof retrievedFarm.location}`);
|
|
console.log(` Location值: ${JSON.stringify(retrievedFarm.location)}`);
|
|
console.log(` 经度: ${retrievedFarm.location.lng} (类型: ${typeof retrievedFarm.location.lng})`);
|
|
console.log(` 纬度: ${retrievedFarm.location.lat} (类型: ${typeof retrievedFarm.location.lat})`);
|
|
|
|
// 3. 模拟API响应格式
|
|
console.log('\n3. 模拟API响应格式...');
|
|
const apiResponse = {
|
|
success: true,
|
|
data: {
|
|
id: retrievedFarm.id,
|
|
name: retrievedFarm.name,
|
|
location: retrievedFarm.location,
|
|
address: retrievedFarm.address,
|
|
contact: retrievedFarm.contact,
|
|
phone: retrievedFarm.phone,
|
|
status: retrievedFarm.status
|
|
}
|
|
};
|
|
|
|
console.log('API响应格式:');
|
|
console.log(JSON.stringify(apiResponse, null, 2));
|
|
|
|
// 4. 模拟前端数据解析
|
|
console.log('\n4. 模拟前端数据解析...');
|
|
const frontendData = apiResponse.data;
|
|
const formData = {
|
|
id: frontendData.id,
|
|
name: frontendData.name,
|
|
address: frontendData.address,
|
|
contact: frontendData.contact,
|
|
phone: frontendData.phone,
|
|
status: frontendData.status,
|
|
longitude: frontendData.location?.lng,
|
|
latitude: frontendData.location?.lat
|
|
};
|
|
|
|
console.log('前端表单数据:');
|
|
console.log(` 经度: ${formData.longitude} (类型: ${typeof formData.longitude})`);
|
|
console.log(` 纬度: ${formData.latitude} (类型: ${typeof formData.latitude})`);
|
|
|
|
// 5. 模拟更新操作
|
|
console.log('\n5. 模拟更新操作...');
|
|
const updateData = {
|
|
longitude: 106.30,
|
|
latitude: 38.50
|
|
};
|
|
|
|
// 构建新的location对象
|
|
const newLocation = {
|
|
lng: parseFloat(updateData.longitude),
|
|
lat: parseFloat(updateData.latitude)
|
|
};
|
|
|
|
await retrievedFarm.update({ location: newLocation });
|
|
|
|
// 验证更新结果
|
|
const updatedFarm = await Farm.findByPk(testFarm.id);
|
|
console.log('更新后的数据:');
|
|
console.log(` 经度: ${updatedFarm.location.lng}`);
|
|
console.log(` 纬度: ${updatedFarm.location.lat}`);
|
|
|
|
// 6. 验证数据一致性
|
|
console.log('\n6. 验证数据一致性...');
|
|
const isConsistent = (
|
|
updatedFarm.location.lng === updateData.longitude &&
|
|
updatedFarm.location.lat === updateData.latitude
|
|
);
|
|
|
|
console.log(`数据一致性检查: ${isConsistent ? '✅ 通过' : '❌ 失败'}`);
|
|
console.log(` 期望经度: ${updateData.longitude}`);
|
|
console.log(` 实际经度: ${updatedFarm.location.lng}`);
|
|
console.log(` 期望纬度: ${updateData.latitude}`);
|
|
console.log(` 实际纬度: ${updatedFarm.location.lat}`);
|
|
|
|
// 7. 清理测试数据
|
|
console.log('\n7. 清理测试数据...');
|
|
await testFarm.destroy();
|
|
console.log('✅ 测试数据已清理');
|
|
|
|
console.log('\n=== 测试完成 ===');
|
|
console.log(`结果: ${isConsistent ? '数据同步正常' : '数据同步异常'}`);
|
|
|
|
} catch (error) {
|
|
console.error('测试失败:', error.message);
|
|
if (error.sql) {
|
|
console.error('SQL:', error.sql);
|
|
}
|
|
} finally {
|
|
await sequelize.close();
|
|
console.log('\n数据库连接已关闭');
|
|
}
|
|
}
|
|
|
|
// 运行测试
|
|
if (require.main === module) {
|
|
console.log('开始测试数据同步...');
|
|
testDataSync();
|
|
}
|
|
|
|
module.exports = { testDataSync }; |