summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--includes/DataTable.inc1
-rw-r--r--tests/data.test109
2 files changed, 108 insertions, 2 deletions
diff --git a/includes/DataTable.inc b/includes/DataTable.inc
index 3292fcb..74d7171 100644
--- a/includes/DataTable.inc
+++ b/includes/DataTable.inc
@@ -196,7 +196,6 @@ class DataTable {
* Add a field.
*
* @todo: Check wether field name is available, otherwise change.
- * @todo: Return false if not successful.
*/
public function addField($field, $spec) {
diff --git a/tests/data.test b/tests/data.test
index 8428e3f..6216779 100644
--- a/tests/data.test
+++ b/tests/data.test
@@ -18,7 +18,7 @@ class DataTestCaseAPI extends DataTestCase {
public function getInfo() {
return array(
'name' => t('Data API'),
- 'description' => t('Unit tests for Data module\'s API'),
+ 'description' => t('Unit tests for Data module\'s API. Turn off views module in your local site to run these tests.'),
'group' => t('Data'),
);
}
@@ -79,4 +79,111 @@ class DataTestCaseAPI extends DataTestCase {
$table->drop($table_name);
$this->assertFalse(db_table_exists($table_name), 'Dropped table.');
}
+
+ /**
+ * Test API functions of DataTable and DataHandler.
+ */
+ public function testAPIFunctions() {
+
+ // Test data_create_table() API function.
+ $tablename = data_name($this->randomName(5, 'apifunc'));
+ $table = data_create_table($tablename, $this->testSchema());
+ $num_of_tables = db_result(db_query("SELECT COUNT(*) FROM {data_tables}"));
+ $this->assertTrue($num_of_tables == 1, "{data_create_table}: Exactly one table is created");
+ $db_tablename = db_result(db_query("SELECT name FROM {data_tables}"));
+ $result = db_query("SELECT * FROM {%s}", $db_tablename);
+ $this->assertTrue($result != FALSE, "{data_create_table}: The table exists in the database");
+
+ // Test data_get_table() API function.
+ $this->assertFalse(data_get_table(''), "{data_get_table}: Empty named table does not exist");
+ $this->assertFalse(data_get_table($tablename . $this->randomName(5, 'apifunc')), "{data_get_table}: Non-existing named table does not exist");
+ $table = data_get_table($tablename);
+ $this->assertTrue($table instanceof DataTable, "{data_get_table}: A DataTable object is returned by getting an existing table.");
+
+ // Test data_drop_table() API function
+ data_drop_table('');
+ data_drop_table('%');
+ data_drop_table('.');
+ data_drop_table('\%');
+ $num_of_tables = db_result(db_query("SELECT COUNT(*) FROM {data_tables}"));
+ $this->assertTrue($num_of_tables == 1, "{data_drop_table}: It's not possible to delete tables with special (non-existing) table names.");
+ data_drop_table($tablename);
+ $num_of_tables = db_result(db_query("SELECT COUNT(*) FROM {data_tables}"));
+ $this->assertTrue($num_of_tables == 0, "{data_drop_table}: The table is destroyed.");
+ $this->assertFalse(db_table_exists($db_tablename), "{data_drop_table}: The table does not exist in the database");
+
+ // Test data_get_all_tables() API function.
+ $start = count(data_get_all_tables(TRUE));
+ for ($i = 0; $i < 5; $i++) {
+ $name = data_name($this->randomName(20, 'apifunc'));
+ if (!data_create_table($name, $this->testSchema())) {
+ $this->fail('Could not create table.');
+ }
+ }
+ $tables = data_get_all_tables(TRUE);
+ $this->assertTrue(count($tables) == ($start + $i), "{data_get_all_tables}: Proper number of table entries are returned.");
+
+ // Test data_export() API function.
+ if (module_exists('ctools')) {
+ $table = array_pop(data_get_all_tables());
+ $exported = data_export($table->get('name'));
+ $this->assertTrue(strstr($exported, 'array'), "{data_export}: The schema has been exported");
+ }
+ else {
+ $msg = data_export('foo');
+ $this->assertEqual($msg, 'Export requires CTools http://drupal.org/project/ctools', 'Notification message is appeared');
+ }
+
+ // Test DataTable::get().
+ $table = array_pop(data_get_all_tables());
+ $result = $table->get('nonexistingproperty');
+ $this->assertTrue(empty($result), "DataTable::get(): Non-existing property does not return anything.");
+ $result = $table->get('name');
+ $this->assertTrue(!empty($result), "DataTable::get(): Existing property returns non-empty value.");
+
+ // Test DataTable::addField().
+ // Note: this test causes a notice if views is enabled on the system that
+ // runs the test.
+ $name = 'newfield'; // See data.test.inc and the testSchema(). It's safe to use hard-coded field name.
+ $spec = array(
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE,
+ );
+ $return = $table->addField($name, $spec);
+ $this->assertEqual($name, $return, "DataTable::addField(): Returned correct field name.");
+ $result = db_query("SELECT %s FROM {%s}", $name, $table->get('name'));
+ $this->assertTrue($result != FALSE, "DataTable::addField(): The new column exists in the database");
+
+ $table->dropField($name);
+ // This query will cause an error, suppress it.
+ @$result = db_query("SELECT %s FROM {%s}", $name, $table->get('name'));
+ $this->assertFalse($result, "DataTable::dropField(): The column is dropped");
+
+ // Test DataHandler::save(), DataHandler::truncate() and DataHandler::delete().
+ $test_data = $this->testData();
+ $table->handler()->save($test_data[0], array('id'));
+ $num_of_rows_before = db_result(db_query("SELECT COUNT(*) FROM {%s}", $table->get('name')));
+ $table->handler()->truncate();
+ $num_of_rows_after = db_result(db_query("SELECT COUNT(*) FROM {%s}", $table->get('name')));
+ $this->assertEqual($num_of_rows_before, 1, "DataTable::truncate(): One row is in the table before executing it");
+ $this->assertEqual($num_of_rows_after, 0, "DataTable::truncate(): The table is empty after executing it.");
+
+ $test_data = $this->testData();
+ $table->handler()->save($test_data[0], array('id'));
+ $table->handler()->save($test_data[1], array('id'));
+ $table->handler()->delete(array('id' => $test_data[0]['id']));
+ $count = db_result(db_query("SELECT COUNT(*) FROM {%s} WHERE id = '%d'", $table->get('name'), $test_data[0]['id']));
+ $this->assertEqual($count, 0, 'The given entry is deleted');
+ $count = db_result(db_query("SELECT COUNT(*) FROM {%s} WHERE id = '%d'", $table->get('name'), $test_data[1]['id']));
+ $this->assertEqual($count, 1, 'The other entry is still there.');
+
+ // Test DataTable::link().
+ // @todo: test removing/adding a link.
+ $meta_before = $table->get('meta');
+ $table->link('node', 'nid');
+ $meta_after = $table->get('meta');
+ $this->assertTrue(empty($meta_before), "DataTable::link(): The meta is empty before executing it");
+ $this->assertTrue(!empty($meta_after), "DataTable::link(): The meta is not empty after executing it");
+ }
} \ No newline at end of file