diff options
Diffstat (limited to 'trunk/etherpad/src/etherpad/testing')
7 files changed, 460 insertions, 0 deletions
diff --git a/trunk/etherpad/src/etherpad/testing/testutils.js b/trunk/etherpad/src/etherpad/testing/testutils.js new file mode 100644 index 0000000..eac7840 --- /dev/null +++ b/trunk/etherpad/src/etherpad/testing/testutils.js @@ -0,0 +1,23 @@ +/** + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +function assertTruthy(x) { + if (!x) { + throw new Error("assertTruthy failure: "+x); + } +} + + diff --git a/trunk/etherpad/src/etherpad/testing/unit_tests/t0000_test.js b/trunk/etherpad/src/etherpad/testing/unit_tests/t0000_test.js new file mode 100644 index 0000000..9e0e78b --- /dev/null +++ b/trunk/etherpad/src/etherpad/testing/unit_tests/t0000_test.js @@ -0,0 +1,22 @@ +/** + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +function run() { + return "This is a test test."; +} + + diff --git a/trunk/etherpad/src/etherpad/testing/unit_tests/t0001_sqlbase_transaction_rollback.js b/trunk/etherpad/src/etherpad/testing/unit_tests/t0001_sqlbase_transaction_rollback.js new file mode 100644 index 0000000..96a74e4 --- /dev/null +++ b/trunk/etherpad/src/etherpad/testing/unit_tests/t0001_sqlbase_transaction_rollback.js @@ -0,0 +1,48 @@ +/** + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import("sqlbase.sqlcommon.{withConnection,inTransaction,closing}"); +import("sqlbase.sqlobj"); + +import("etherpad.testing.testutils.*"); + +function run() { + + withConnection(function(conn) { + var s = conn.createStatement(); + closing(s, function() { + s.execute("delete from just_a_test"); + }); + }); + + sqlobj.insert("just_a_test", {id: 1, x: "a"}); + + try { // this should fail + inTransaction(function(conn) { + sqlobj.updateSingle("just_a_test", {id: 1}, {id: 1, x: "b"}); + // note: this will be pritned to the console, but that's OK + throw Error(); + }); + } catch (e) {} + + var testRecord = sqlobj.selectSingle("just_a_test", {id: 1}); + + assertTruthy(testRecord.x == "a"); +} + + + + diff --git a/trunk/etherpad/src/etherpad/testing/unit_tests/t0002_license_generation.js b/trunk/etherpad/src/etherpad/testing/unit_tests/t0002_license_generation.js new file mode 100644 index 0000000..67c79d8 --- /dev/null +++ b/trunk/etherpad/src/etherpad/testing/unit_tests/t0002_license_generation.js @@ -0,0 +1,89 @@ +/** + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import("stringutils"); +import("sqlbase.sqlobj"); + +import("etherpad.licensing"); + +jimport("java.util.Random"); + +function run() { + var r = new Random(0); + + function testLicense(name, org, expires, editionId, userQuota) { + function keydataString() { + return "{name: "+name+", org: "+org+", expires: "+expires+", editionId: "+editionId+", userQuota: "+userQuota+"}"; + } + var key = licensing.generateNewKey(name, org, expires, editionId, userQuota); + var info = licensing.decodeLicenseInfoFromKey(key); + if (!info) { + println("Generated key does not decode at all: "+keydataString()); + println(" generated key: "+key); + throw new Error("Generated key does not decode at all. See stdout."); + } + function testMatch(name, x, y) { + if (x != y) { + println("key match error ("+name+"): ["+x+"] != ["+y+"]"); + println(" key data: "+keydataString()); + println(" generated key: "+key); + println(" decoded key: "+info.toSource()); + throw new Error(name+" mismatch. see stdout."); + } + } + testMatch("personName", info.personName, name); + testMatch("orgName", info.organizationName, org); + testMatch("expires", +info.expiresDate, +expires); + testMatch("editionName", info.editionName, licensing.getEditionName(editionId)); + testMatch("userQuota", +info.userQuota, +userQuota); + } + + testLicense("aaron", "test", +(new Date)+1000*60*60*24*30, licensing.getEditionId('PRIVATE_NETWORK_EVALUATION'), 1001); + + for (var editionId = 0; editionId < 3; editionId++) { + for (var unlimitedUsers = 0; unlimitedUsers <= 1; unlimitedUsers++) { + for (var noExpiry = 0; noExpiry <= 1; noExpiry++) { + for (var j = 0; j < 100; j++) { + var name = stringutils.randomString(1+r.nextInt(39)); + var org = stringutils.randomString(1+r.nextInt(39)); + var expires = null; + if (noExpiry == 0) { + expires = +(new Date)+(1000*60*60*24*r.nextInt(100)); + } + var userQuota = -1; + if (unlimitedUsers == 1) { + userQuota = r.nextInt(1e6); + } + + testLicense(name, org, expires, editionId, userQuota); + } + } + } + } + + // test that all previously generated keys continue to decode. + var historicalKeys = sqlobj.selectMulti('eepnet_signups', {}, {}); + historicalKeys.forEach(function(d) { + var key = d.licenseKey; + if (key && !licensing.isValidKey(key)) { + throw new Error("Historical license key no longer validates: "+key); + } + }); + +} + + + diff --git a/trunk/etherpad/src/etherpad/testing/unit_tests/t0003_persistent_vars.js b/trunk/etherpad/src/etherpad/testing/unit_tests/t0003_persistent_vars.js new file mode 100644 index 0000000..0898fbe --- /dev/null +++ b/trunk/etherpad/src/etherpad/testing/unit_tests/t0003_persistent_vars.js @@ -0,0 +1,42 @@ +/** + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import("sqlbase.persistent_vars"); + +import("stringutils"); + +import("etherpad.testing.testutils.*"); + +function run() { + var varname = stringutils.randomString(50); + var varval = stringutils.randomString(50); + + var x = persistent_vars.get(varname); + assertTruthy(!x); + + persistent_vars.put(varname, varval); + + for (var i = 0; i < 3; i++) { + x = persistent_vars.get(varname); + assertTruthy(x == varval); + } + + persistent_vars.remove(varname); + + var x = persistent_vars.get(varname); + assertTruthy(!x); +} + diff --git a/trunk/etherpad/src/etherpad/testing/unit_tests/t0004_sqlobj.js b/trunk/etherpad/src/etherpad/testing/unit_tests/t0004_sqlobj.js new file mode 100644 index 0000000..7f8c996 --- /dev/null +++ b/trunk/etherpad/src/etherpad/testing/unit_tests/t0004_sqlobj.js @@ -0,0 +1,214 @@ +/** + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import("jsutils.*"); +import("stringutils"); + +import("sqlbase.sqlcommon"); +import("sqlbase.sqlobj"); + +import("etherpad.globals.*"); +import("etherpad.testing.testutils.*"); + +function run() { + cleanUpTables(); + testGeneral(); + testAlterColumn(); + cleanUpTables(); +} + +function _getTestTableName() { + return 'sqlobj_unit_test_'+stringutils.randomString(10); +} + +function testGeneral() { + + if (isProduction()) { + return; // we dont run this in productin! + } + + // create a test table + var tableName = _getTestTableName(); + + sqlobj.createTable(tableName, { + id: sqlobj.getIdColspec(), + varChar: 'VARCHAR(128)', + dateTime: sqlobj.getDateColspec("NOT NULL"), + int11: 'INT', + tinyInt: sqlobj.getBoolColspec("DEFAULT 0") + }); + + // add some columns + sqlobj.addColumns(tableName, { + a: 'VARCHAR(256)', + b: 'VARCHAR(256)', + c: 'VARCHAR(256)', + d: 'VARCHAR(256)' + }); + + // drop columns + sqlobj.dropColumn(tableName, 'c'); + sqlobj.dropColumn(tableName, 'd'); + + // list tables and make sure it contains tableName + var l = sqlobj.listTables(); + var found = false; + l.forEach(function(x) { + if (x == tableName) { found = true; } + }); + assertTruthy(found); + + if (sqlcommon.isMysql()) { + for (var i = 0; i < 3; i++) { + ['MyISAM', 'InnoDB'].forEach(function(e) { + sqlobj.setTableEngine(tableName, e); + assertTruthy(e == sqlobj.getTableEngine(tableName)); + }); + } + } + + sqlobj.createIndex(tableName, ['a', 'b']); + sqlobj.createIndex(tableName, ['int11', 'a', 'b']); + + // test null columns + for (var i = 0; i < 10; i++) { + var id = sqlobj.insert(tableName, {dateTime: new Date(), a: null, b: null}); + sqlobj.deleteRows(tableName, {id: id}); + } + + //---------------------------------------------------------------- + // data management + //---------------------------------------------------------------- + + // insert + selectSingle + function _randomDate() { + // millisecond accuracy is lost in DB. + var d = +(new Date); + d = Math.round(d / 1000) * 1000; + return new Date(d); + } + var obj_data_list = []; + for (var i = 0; i < 40; i++) { + var obj_data = { + varChar: stringutils.randomString(20), + dateTime: _randomDate(), + int11: +(new Date) % 10000, + tinyInt: !!(+(new Date) % 2), + a: "foo", + b: "bar" + }; + obj_data_list.push(obj_data); + + var obj_id = sqlobj.insert(tableName, obj_data); + var obj_result = sqlobj.selectSingle(tableName, {id: obj_id}); + + assertTruthy(obj_result.id == obj_id); + keys(obj_data).forEach(function(k) { + var d1 = obj_data[k]; + var d2 = obj_result[k]; + if (k == "dateTime") { + d1 = +d1; + d2 = +d2; + } + if (d1 != d2) { + throw Error("result mismatch ["+k+"]: "+d1+" != "+d2); + } + }); + } + + // selectMulti: no constraints, no options + var obj_result_list = sqlobj.selectMulti(tableName, {}, {}); + assertTruthy(obj_result_list.length == obj_data_list.length); + // orderBy + ['int11', 'a', 'b'].forEach(function(colName) { + obj_result_list = sqlobj.selectMulti(tableName, {}, {orderBy: colName}); + assertTruthy(obj_result_list.length == obj_data_list.length); + for (var i = 1; i < obj_result_list.length; i++) { + assertTruthy(obj_result_list[i-1][colName] <= obj_result_list[i][colName]); + } + + obj_result_list = sqlobj.selectMulti(tableName, {}, {orderBy: "-"+colName}); + assertTruthy(obj_result_list.length == obj_data_list.length); + for (var i = 1; i < obj_result_list.length; i++) { + assertTruthy(obj_result_list[i-1][colName] >= obj_result_list[i][colName]); + } + }); + + // selectMulti: with constraints + var obj_result_list1 = sqlobj.selectMulti(tableName, {tinyInt: true}, {}); + var obj_result_list2 = sqlobj.selectMulti(tableName, {tinyInt: false}, {}); + assertTruthy((obj_result_list1.length + obj_result_list2.length) == obj_data_list.length); + obj_result_list1.forEach(function(o) { + assertTruthy(o.tinyInt == true); + }); + obj_result_list2.forEach(function(o) { + assertTruthy(o.tinyInt == false); + }); + + // updateSingle + obj_result_list1.forEach(function(o) { + o.a = "ttt"; + sqlobj.updateSingle(tableName, {id: o.id}, o); + }); + // update + sqlobj.update(tableName, {tinyInt: false}, {a: "fff"}); + // verify + obj_result_list = sqlobj.selectMulti(tableName, {}, {}); + obj_result_list.forEach(function(o) { + if (o.tinyInt) { + assertTruthy(o.a == "ttt"); + } else { + assertTruthy(o.a == "fff"); + } + }); + + // deleteRows + sqlobj.deleteRows(tableName, {a: "ttt"}); + sqlobj.deleteRows(tableName, {a: "fff"}); + // verify + obj_result_list = sqlobj.selectMulti(tableName, {}, {}); + assertTruthy(obj_result_list.length == 0); +} + +function cleanUpTables() { + // delete testing table (and any other old testing tables) + sqlobj.listTables().forEach(function(t) { + if (t.indexOf("sqlobj_unit_test") == 0) { + sqlobj.dropTable(t); + } + }); +} + +function testAlterColumn() { + var tableName = _getTestTableName(); + + sqlobj.createTable(tableName, { + x: 'INT', + a: 'INT NOT NULL', + b: 'INT NOT NULL' + }); + + if (sqlcommon.isMysql()) { + sqlobj.modifyColumn(tableName, 'a', 'INT'); + sqlobj.modifyColumn(tableName, 'b', 'INT'); + } else { + sqlobj.alterColumn(tableName, 'a', 'NULL'); + sqlobj.alterColumn(tableName, 'b', 'NULL'); + } + + sqlobj.insert(tableName, {a: 5}); +} + diff --git a/trunk/etherpad/src/etherpad/testing/unit_tests/t0005_easysync.js b/trunk/etherpad/src/etherpad/testing/unit_tests/t0005_easysync.js new file mode 100644 index 0000000..9cd3f21 --- /dev/null +++ b/trunk/etherpad/src/etherpad/testing/unit_tests/t0005_easysync.js @@ -0,0 +1,22 @@ +/** + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import("etherpad.collab.ace.easysync2_tests"); + +function run() { + easysync2_tests.runTests(); +}
\ No newline at end of file |