Files
SkyArtShop/backend/test-upload-db.js

125 lines
3.6 KiB
JavaScript
Raw Normal View History

2025-12-14 01:54:40 -06:00
#!/usr/bin/env node
/**
* Test Script: Upload Database Integration
*
* This script tests that file uploads are properly recorded in PostgreSQL
*/
const { pool } = require("./config/database");
async function testUploadDatabase() {
console.log("🔍 Testing Upload Database Integration...\n");
try {
// Test 1: Check if uploads table exists
console.log("1⃣ Checking uploads table...");
const tableCheck = await pool.query(`
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_name = 'uploads'
);
`);
if (tableCheck.rows[0].exists) {
console.log(" ✅ uploads table exists\n");
} else {
console.log(" ❌ uploads table not found\n");
return;
}
// Test 2: Check table structure
console.log("2⃣ Checking table structure...");
const columns = await pool.query(`
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'uploads'
ORDER BY ordinal_position;
`);
console.log(" Columns:");
columns.rows.forEach((col) => {
console.log(
` - ${col.column_name} (${col.data_type}) ${
col.is_nullable === "YES" ? "NULL" : "NOT NULL"
}`
);
});
console.log();
// Test 3: Check indexes
console.log("3⃣ Checking indexes...");
const indexes = await pool.query(`
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename = 'uploads';
`);
console.log(` Found ${indexes.rows.length} index(es):`);
indexes.rows.forEach((idx) => {
console.log(` - ${idx.indexname}`);
});
console.log();
// Test 4: Query existing uploads
console.log("4⃣ Querying existing uploads...");
const uploads = await pool.query(`
SELECT id, filename, original_name, file_size, mime_type, created_at
FROM uploads
ORDER BY created_at DESC
LIMIT 10;
`);
console.log(` Found ${uploads.rows.length} upload(s) in database:`);
if (uploads.rows.length > 0) {
uploads.rows.forEach((upload) => {
console.log(
` - [${upload.id}] ${upload.original_name} (${upload.filename})`
);
console.log(
` Size: ${(upload.file_size / 1024).toFixed(2)}KB | Type: ${
upload.mime_type
}`
);
console.log(` Uploaded: ${upload.created_at}`);
});
} else {
console.log(" No uploads found yet. Upload a file to test!");
}
console.log();
// Test 5: Check foreign key constraint
console.log("5⃣ Checking foreign key constraints...");
const fkeys = await pool.query(`
SELECT conname, conrelid::regclass, confrelid::regclass
FROM pg_constraint
WHERE contype = 'f' AND conrelid = 'uploads'::regclass;
`);
if (fkeys.rows.length > 0) {
console.log(` Found ${fkeys.rows.length} foreign key(s):`);
fkeys.rows.forEach((fk) => {
console.log(` - ${fk.conname}: ${fk.conrelid} -> ${fk.confrelid}`);
});
} else {
console.log(" No foreign keys found");
}
console.log();
console.log("✅ Database integration test complete!\n");
console.log("📋 Summary:");
console.log(" - Database: skyartshop");
console.log(" - Table: uploads");
console.log(" - Records: " + uploads.rows.length);
console.log(" - Status: Ready for production ✨\n");
} catch (error) {
console.error("❌ Test failed:", error.message);
console.error(error);
} finally {
await pool.end();
}
}
// Run test
testUploadDatabase().catch(console.error);