2024-11-20 19:41:09 +00:00
|
|
|
const XLSX = require('xlsx');
|
|
|
|
const { Client } = require('pg');
|
|
|
|
const { sheet_to_pg_table } = require('./sql-types');
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
async function readExcelAndTest(filename, tableName) {
|
|
|
|
console.log(`\nTesting ${filename}...`);
|
|
|
|
|
|
|
|
// Read Excel file
|
2024-11-20 22:45:26 +00:00
|
|
|
const workbook = XLSX.readFile(path.join('test_files', filename) );
|
2024-11-20 19:41:09 +00:00
|
|
|
const sheetName = workbook.SheetNames[0];
|
|
|
|
const worksheet = workbook.Sheets[sheetName];
|
|
|
|
|
|
|
|
// Convert to array of objects
|
|
|
|
const data = XLSX.utils.sheet_to_json(worksheet); // keep number formatting
|
|
|
|
console.log('Parsed Excel data:', data);
|
|
|
|
|
|
|
|
// Connect to PostgreSQL
|
|
|
|
const client = new Client({
|
|
|
|
host: 'localhost',
|
|
|
|
database: 'SheetJSPG',
|
|
|
|
user: 'postgres',
|
|
|
|
password: '7509'
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await client.connect();
|
|
|
|
|
|
|
|
// Import data
|
2024-11-20 22:45:26 +00:00
|
|
|
await sheet_to_pg_table(client, worksheet, tableName);
|
2024-11-20 19:41:09 +00:00
|
|
|
|
|
|
|
// Verify table structure
|
|
|
|
const structure = await client.query(`
|
|
|
|
SELECT column_name, data_type
|
|
|
|
FROM information_schema.columns
|
|
|
|
WHERE table_name = $1
|
|
|
|
ORDER BY ordinal_position;
|
|
|
|
`, [tableName]);
|
|
|
|
console.log('\nTable structure:', structure.rows);
|
|
|
|
|
|
|
|
// Verify data
|
|
|
|
const results = await client.query(`SELECT * FROM ${tableName}`);
|
|
|
|
console.log('\nImported data from DB:', results.rows);
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Error testing ${filename}:`, error);
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
await client.end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runAllTests() {
|
|
|
|
try {
|
|
|
|
// Test each Excel file
|
|
|
|
await readExcelAndTest('number_formats.xlsx', 'test_number_formats');
|
|
|
|
await readExcelAndTest('date_formats.xlsx', 'test_dates');
|
|
|
|
await readExcelAndTest('special_values.xlsx', 'test_special_values');
|
|
|
|
await readExcelAndTest('precision.xlsx', 'test_precision');
|
2024-11-20 22:45:26 +00:00
|
|
|
/* await readExcelAndTest('string_formats.xlsx', 'test_string_formats'); */
|
2024-11-20 19:41:09 +00:00
|
|
|
await readExcelAndTest('boolean_formats.xlsx', 'test_boolean_formats');
|
|
|
|
|
|
|
|
console.log('\nAll tests completed successfully');
|
|
|
|
} catch (error) {
|
|
|
|
console.error('\nTests failed:', error);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runAllTests().catch(console.error);
|