-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL3.test.js
More file actions
69 lines (56 loc) · 1.95 KB
/
SQL3.test.js
File metadata and controls
69 lines (56 loc) · 1.95 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
const SQL3 = require('./SQL3');
async function runExample () {
// Create a new database instance
const db = new SQL3(':memory:');
try {
// Create a test table
await db.createTable('test_users', {
id: 'INTEGER PRIMARY KEY AUTOINCREMENT',
name: 'TEXT NOT NULL',
email: 'TEXT UNIQUE',
created_at: 'DATETIME DEFAULT CURRENT_TIMESTAMP'
});
console.log('✅ Table created successfully');
// Insert some test data
const insert1 = await db.insert('test_users', {
name: 'John Doe',
email: 'john@example.com'
});
const insert2 = await db.insert('test_users', {
name: 'Jane Smith',
email: 'jane@example.com'
});
console.log('✅ Test data inserted:', {insert1, insert2});
// Test WHERE clause
const users = await db
.where('name', '%John%', 'AND', 'LIKE')
.get('test_users');
console.log('✅ Query with WHERE clause:', users);
// Test transaction
await db.beginTransaction();
try {
await db.insert('test_users', {
name: 'Transaction Test',
email: 'transaction@test.com'
});
await db.commit();
console.log('✅ Transaction test passed');
} catch (error) {
await db.rollback();
console.error('❌ Transaction test failed:', error);
}
// Test count
const count = await db.count('test_users');
console.log('✅ Total users:', count);
// Test pagination
const page = await db.paginate('test_users', 1, 2);
console.log('✅ Pagination test:', page);
} catch (error) {
console.error('❌ Test failed:', error);
} finally {
await db.close();
console.log('✅ Database closed');
}
}
// Run the example
runExample().catch(console.error);