-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_test_pg.js
More file actions
executable file
·133 lines (104 loc) · 4.67 KB
/
Copy pathsimple_test_pg.js
File metadata and controls
executable file
·133 lines (104 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
/**
* Simple Database Connection Test using pg client
*/
import { Client } from 'pg';
console.log('🧪 Starting Simple Database Test with pg client...\n');
// Test database connection directly
console.log('1️⃣ Testing Direct Database Connection...');
const connectionString = 'postgresql://vercel_user:vercel_pass@localhost:5433/vercel_ai';
async function testDirectConnection() {
const client = new Client({
connectionString: connectionString,
});
try {
await client.connect();
console.log('✅ Database connection established');
// Test query
const { rows } = await client.query('SELECT * FROM "Customer" LIMIT 3');
console.log('✅ Query executed successfully');
console.log('📊 Found customers:', rows.length);
rows.forEach((customer, index) => {
console.log(` ${index + 1}. ${customer.name} (${customer.email})`);
});
// Test data isolation by checking different customers
console.log('\n2️⃣ Testing Data Isolation...');
// Check Alice's orders
const aliceOrders = await client.query(
'SELECT o.id, o.status, p.name as product_name FROM "Order" o JOIN "Product" p ON o."productId" = p.id WHERE o."customerId" = (SELECT id FROM "Customer" WHERE email = $1)',
['alice@example.com']
);
console.log('✅ Alice\'s orders:', aliceOrders.rows.length);
aliceOrders.rows.forEach(order => {
console.log(` - Order #${order.id}: ${order.product_name} (${order.status})`);
});
// Check Bob's orders
const bobOrders = await client.query(
'SELECT o.id, o.status, p.name as product_name FROM "Order" o JOIN "Product" p ON o."productId" = p.id WHERE o."customerId" = (SELECT id FROM "Customer" WHERE email = $1)',
['bob@example.com']
);
console.log('✅ Bob\'s orders:', bobOrders.rows.length);
bobOrders.rows.forEach(order => {
console.log(` - Order #${order.id}: ${order.product_name} (${order.status})`);
});
// Test data formats
console.log('\n3️⃣ Testing Data Formats...');
const products = await client.query('SELECT id, name, price, stock FROM "Product" WHERE id = $1', [101]);
const product = products.rows[0];
console.log('✅ Product data types:');
console.log(' - ID:', typeof product.id, '=', product.id);
console.log(' - Name:', typeof product.name, '=', product.name);
console.log(' - Price:', typeof product.price, '=', product.price);
console.log(' - Stock:', typeof product.stock, '=', product.stock);
// Test error handling
console.log('\n4️⃣ Testing Error Handling...');
try {
await client.query('SELECT * FROM "NonExistentTable"');
console.error('❌ Error handling failed - non-existent table query succeeded');
} catch (error) {
console.log('✅ Error handling working - caught error for non-existent table');
}
// Test data validation
console.log('\n5️⃣ Testing Data Validation...');
// Test that we can't access data that doesn't belong to a user
const charlieData = await client.query(
'SELECT * FROM "Customer" WHERE email = $1',
['charlie@sample.net']
);
if (charlieData.rows.length > 0) {
console.log('✅ Data validation working - can retrieve specific customer data');
console.log(' - Customer:', charlieData.rows[0].name);
}
// Test support tickets
console.log('\n6️⃣ Testing Support Tickets...');
const tickets = await client.query(
'SELECT st.id, st.issue, st.status, c.name as customer_name FROM "SupportTicket" st JOIN "Customer" c ON st."customerId" = c.id WHERE c.email = $1',
['bob@example.com']
);
console.log('✅ Bob\'s support tickets:', tickets.rows.length);
tickets.rows.forEach(ticket => {
console.log(` - Ticket #${ticket.id}: ${ticket.issue} (${ticket.status})`);
});
console.log('\n🎉 All simple tests passed!');
console.log('\n📊 Summary:');
console.log(' ✅ Database connection works');
console.log(' ✅ Data isolation is maintained');
console.log(' ✅ Data formats are correct');
console.log(' ✅ Error handling is working');
console.log(' ✅ Data validation is working');
console.log(' ✅ Support tickets are accessible');
return true;
} catch (error) {
console.error('❌ Database test failed:', error.message);
console.error('Stack:', error.stack);
return false;
} finally {
await client.end();
console.log('\n🔌 Database connection closed');
}
}
// Run test
testDirectConnection().catch(error => {
console.error('💥 Test crashed:', error.message);
process.exit(1);
});