CyTube/tests/database.js

59 lines
2 KiB
JavaScript
Raw Normal View History

2013-05-27 19:43:37 +00:00
var Database = require("../database");
2013-05-25 21:12:08 +00:00
Database.setup(require("../config-testing"));
var assert = require("assert");
var db = Database.getConnection();
// Empty database
db.realQuerySync("SHOW TABLES;");
result = db.storeResultSync();
var tables = [];
var tmp = result.fetchAllSync({"asArray": true});
tmp.forEach(function(t) { tables.push(t[0]); });
db.querySync("DROP TABLE " + tables.join(", "));
Database.init();
// Test global bans
assert(!Database.checkGlobalBan("127.0.0.1"));
Database.globalBanIP("192.168.1.12", "test");
Database.globalBanIP("192.168.2", "test");
Database.globalBanIP("192.167", "test");
Database.refreshGlobalBans();
assert(Database.checkGlobalBan("192.168.1.12"));
assert(Database.checkGlobalBan("192.168.2.24"));
assert(Database.checkGlobalBan("192.167.1.15"));
Database.globalUnbanIP("192.168.1.12");
Database.globalUnbanIP("192.167");
Database.refreshGlobalBans();
assert(!Database.checkGlobalBan("192.168.1.12"));
assert(!Database.checkGlobalBan("192.167.5.54"));
console.log("[PASS] Global Bans");
2013-05-25 22:16:44 +00:00
// Test channel registration
assert(Database.registerChannel("test"));
assert(Database.deleteChannel("test"));
console.log("[PASS] Channel registration");
// Test channel ranks
Database.registerChannel("test");
assert(Database.setChannelRank("test", "a_user", 10));
assert(Database.getChannelRank("test", "a_user") == 10);
2013-05-26 01:01:32 +00:00
assert(Database.setChannelRank("test", "user_2", 4));
assert(Database.listChannelRanks("test").length == 2);
assert(Database.getChannelRank("test", ["a_user", "user_2"])+"" == [10, 4]+"");
2013-05-25 22:16:44 +00:00
console.log("[PASS] Channel ranks");
// Test library caching
assert(Database.addToLibrary("test", {
id: "abc",
seconds: 123,
title: "Testing",
type: "yt"
}));
2013-05-26 01:01:32 +00:00
assert(db.querySync("SELECT * FROM `chan_test_library` WHERE id='abc'").fetchAllSync().length > 0);
2013-05-25 22:16:44 +00:00
assert(Database.removeFromLibrary("test", "abc"));
assert(db.querySync("SELECT * FROM `chan_test_library` WHERE id='abc'").fetchAllSync().length == 0);
console.log("[PASS] Channel library");
db.closeSync();
2013-05-25 21:12:08 +00:00
process.exit(0);