實作個人收藏、個人活動紀錄
This commit is contained in:
86
scripts/test-favorites.js
Normal file
86
scripts/test-favorites.js
Normal file
@@ -0,0 +1,86 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
// 資料庫連接配置
|
||||
const dbConfig = {
|
||||
host: 'mysql.theaken.com',
|
||||
port: 33306,
|
||||
user: 'AI_Platform',
|
||||
password: 'Aa123456',
|
||||
database: 'db_AI_Platform',
|
||||
charset: 'utf8mb4'
|
||||
};
|
||||
|
||||
async function testFavorites() {
|
||||
let connection;
|
||||
|
||||
try {
|
||||
console.log('連接到資料庫...');
|
||||
connection = await mysql.createConnection(dbConfig);
|
||||
|
||||
// 測試添加收藏
|
||||
const testUserId = 'test-user-123';
|
||||
const testAppId = 'test-app-456';
|
||||
|
||||
console.log('測試添加收藏...');
|
||||
|
||||
// 先清理可能存在的測試數據
|
||||
await connection.execute(
|
||||
'DELETE FROM user_favorites WHERE user_id = ? AND app_id = ?',
|
||||
[testUserId, testAppId]
|
||||
);
|
||||
|
||||
// 第一次添加收藏
|
||||
const favoriteId1 = require('crypto').randomUUID();
|
||||
await connection.execute(`
|
||||
INSERT INTO user_favorites (id, user_id, app_id, created_at)
|
||||
VALUES (?, ?, ?, NOW())
|
||||
`, [favoriteId1, testUserId, testAppId]);
|
||||
|
||||
console.log('✅ 第一次添加收藏成功');
|
||||
|
||||
// 檢查是否已收藏
|
||||
const [checkResult] = await connection.execute(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM user_favorites
|
||||
WHERE user_id = ? AND app_id = ?
|
||||
`, [testUserId, testAppId]);
|
||||
|
||||
console.log(`收藏記錄數量: ${checkResult[0].count}`);
|
||||
|
||||
// 嘗試重複添加收藏(應該失敗)
|
||||
try {
|
||||
const favoriteId2 = require('crypto').randomUUID();
|
||||
await connection.execute(`
|
||||
INSERT INTO user_favorites (id, user_id, app_id, created_at)
|
||||
VALUES (?, ?, ?, NOW())
|
||||
`, [favoriteId2, testUserId, testAppId]);
|
||||
|
||||
console.log('❌ 重複添加收藏應該失敗但成功了');
|
||||
} catch (error) {
|
||||
if (error.code === 'ER_DUP_ENTRY') {
|
||||
console.log('✅ 重複添加收藏正確地被阻止');
|
||||
} else {
|
||||
console.log('❌ 重複添加收藏失敗,但錯誤類型不正確:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 清理測試數據
|
||||
await connection.execute(
|
||||
'DELETE FROM user_favorites WHERE user_id = ? AND app_id = ?',
|
||||
[testUserId, testAppId]
|
||||
);
|
||||
|
||||
console.log('✅ 測試數據已清理');
|
||||
|
||||
} catch (error) {
|
||||
console.error('測試過程中發生錯誤:', error);
|
||||
} finally {
|
||||
if (connection) {
|
||||
await connection.end();
|
||||
console.log('資料庫連接已關閉');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 執行測試
|
||||
testFavorites().catch(console.error);
|
Reference in New Issue
Block a user