2
0

test_distinct_simple.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const http = require('http');
  2. async function query(sql) {
  3. return new Promise((resolve, reject) => {
  4. const data = JSON.stringify({ sql });
  5. const options = {
  6. hostname: 'localhost',
  7. port: 8080,
  8. path: '/query',
  9. method: 'POST',
  10. headers: {
  11. 'Content-Type': 'application/json',
  12. 'Content-Length': data.length
  13. },
  14. timeout: 10000 // 10 second timeout
  15. };
  16. const req = http.request(options, (res) => {
  17. let body = '';
  18. res.on('data', (chunk) => body += chunk);
  19. res.on('end', () => {
  20. try {
  21. const result = JSON.parse(body);
  22. if (result.error) {
  23. reject(new Error(result.error.message));
  24. } else {
  25. resolve(result);
  26. }
  27. } catch (e) {
  28. reject(e);
  29. }
  30. });
  31. });
  32. req.on('error', reject);
  33. req.on('timeout', () => {
  34. req.destroy();
  35. reject(new Error('Request timeout'));
  36. });
  37. req.write(data);
  38. req.end();
  39. });
  40. }
  41. async function testDistinct() {
  42. try {
  43. console.log('Testing DISTINCT implementation...\n');
  44. // Create table
  45. console.log('1. Creating table...');
  46. await query('CREATE TABLE test_distinct (id INTEGER, status TEXT)');
  47. console.log(' ✓ Table created\n');
  48. // Insert data
  49. console.log('2. Inserting data...');
  50. for (let i = 0; i < 10; i++) {
  51. const statuses = ['pending', 'completed', 'shipped'];
  52. const status = statuses[i % 3];
  53. await query(`INSERT INTO test_distinct VALUES (${i}, '${status}')`);
  54. }
  55. console.log(' ✓ Inserted 10 rows\n');
  56. // Query without DISTINCT
  57. console.log('3. SELECT status FROM test_distinct:');
  58. const result1 = await query('SELECT status FROM test_distinct ORDER BY status');
  59. console.log(` Rows: ${result1.rows.length}`);
  60. console.log();
  61. // Query with DISTINCT
  62. console.log('4. SELECT DISTINCT status FROM test_distinct:');
  63. const result2 = await query('SELECT DISTINCT status FROM test_distinct ORDER BY status');
  64. console.log(` Rows: ${result2.rows.length}`);
  65. console.log(' Values:', result2.rows.map(r => r[0]).join(', '));
  66. console.log();
  67. // Verify
  68. if (result2.rows.length === 3) {
  69. console.log('✅ DISTINCT is working correctly!');
  70. console.log(` Expected 3 unique values, got ${result2.rows.length}`);
  71. } else {
  72. console.log(`❌ DISTINCT failed - expected 3 unique values, got ${result2.rows.length}`);
  73. }
  74. // Clean up
  75. console.log('\n5. Cleaning up...');
  76. await query('DROP TABLE test_distinct');
  77. console.log(' ✓ Table dropped');
  78. } catch (error) {
  79. console.error('Error:', error.message);
  80. process.exit(1);
  81. }
  82. }
  83. testDistinct();