2
0

test_distinct.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. };
  15. const req = http.request(options, (res) => {
  16. let body = '';
  17. res.on('data', (chunk) => body += chunk);
  18. res.on('end', () => {
  19. try {
  20. resolve(JSON.parse(body));
  21. } catch (e) {
  22. reject(e);
  23. }
  24. });
  25. });
  26. req.on('error', reject);
  27. req.write(data);
  28. req.end();
  29. });
  30. }
  31. async function test() {
  32. console.log('Testing DISTINCT implementation...\n');
  33. // Create table
  34. console.log('1. Creating table...');
  35. try {
  36. await query('DROP TABLE test_distinct');
  37. } catch (e) {
  38. // Ignore error if table doesn't exist
  39. }
  40. await query('CREATE TABLE test_distinct (id INTEGER, status TEXT)');
  41. console.log(' ✓ Table created\n');
  42. // Insert data
  43. console.log('2. Inserting data...');
  44. await query("INSERT INTO test_distinct VALUES (1, 'pending')");
  45. await query("INSERT INTO test_distinct VALUES (2, 'completed')");
  46. await query("INSERT INTO test_distinct VALUES (3, 'pending')");
  47. await query("INSERT INTO test_distinct VALUES (4, 'shipped')");
  48. await query("INSERT INTO test_distinct VALUES (5, 'pending')");
  49. await query("INSERT INTO test_distinct VALUES (6, 'completed')");
  50. console.log(' ✓ Inserted 6 rows\n');
  51. // Query without DISTINCT
  52. console.log('3. SELECT status FROM test_distinct:');
  53. const result1 = await query('SELECT status FROM test_distinct ORDER BY status');
  54. console.log(` Rows: ${result1.rows.length}`);
  55. console.log(' Values:', result1.rows.map(r => r[0]).join(', '));
  56. console.log();
  57. // Query with DISTINCT
  58. console.log('4. SELECT DISTINCT status FROM test_distinct:');
  59. const result2 = await query('SELECT DISTINCT status FROM test_distinct ORDER BY status');
  60. console.log(` Rows: ${result2.rows.length}`);
  61. console.log(' Values:', result2.rows.map(r => r[0]).join(', '));
  62. console.log();
  63. // Verify
  64. if (result2.rows.length === 3) {
  65. console.log('✅ DISTINCT is working correctly!');
  66. } else {
  67. console.log(`❌ DISTINCT failed - expected 3 unique values, got ${result2.rows.length}`);
  68. }
  69. // Clean up
  70. await query('DROP TABLE test_distinct');
  71. }
  72. test().catch(console.error);