public function DataTestCaseAPI::testCRUD in Data 8
Same name and namespace in other branches
- 6 tests/data.test \DataTestCaseAPI::testCRUD()
- 7 tests/data.test \DataTestCaseAPI::testCRUD()
Run CRUD tests.
File
- tests/
data.test, line 35
Class
- DataTestCaseAPI
- Test basic Data API functionality.
Code
public function testCRUD() {
// Create table.
$table_name = data_name($this
->randomName(5, 'crud'));
if (!($table = data_create_table($table_name, $this
->testSchema()))) {
$this
->error('Could not create table.');
return;
}
else {
$this
->assertTrue(db_table_exists($table
->get('name')), 'Created table ' . $table
->get('name'));
$schema = drupal_get_schema($table
->get('name'));
$this
->assertTrue(!empty($schema), 'Schema information is available.');
}
// Save data.
$handler = data_get_handler($table
->get('name'));
$test_data = $this
->testData();
$handler
->save($test_data[0], array(
'id',
));
$handler
->save($test_data[1], array(
'id',
));
$handler
->save($test_data[1], array(
'id',
));
// Load second record.
$data = $handler
->load(array(
'id' => 1,
));
$this
->assertEqual($data[0], $test_data[1], 'Loaded data matches saved data.');
// Manipulate second record, save and load.
$data[0]['char0'] = 'test';
$handler
->save($data[0], array(
'id',
));
$data = $handler
->load(array(
'id' => 1,
));
$this
->assertEqual($data[0]['char0'], 'test', 'Saved data matches changed data.');
// Manipulate second record, update and load.
$data[0]['char0'] = 'test_update';
$handler
->update($data[0], array(
'id',
));
$data = $handler
->load(array(
'id' => 1,
));
$this
->assertEqual($data[0]['char0'], 'test_update', 'Updated data matches changed data.');
// Delete first record and insert it again.
$data = $handler
->load(array(
'id' => 0,
));
$handler
->delete(array(
'id' => 0,
));
$this
->assertFalse($handler
->load(array(
'id' => 0,
)), 'Data deleted.');
$handler
->insert($data[0]);
$data = $handler
->load(array(
'id' => 0,
));
$this
->assertEqual($data[0]['char0'], 'test00', 'Inserted data matches.');
// Load a record by string.
$data = $handler
->load(array(
'char0' => 'test_update',
));
$this
->assertEqual($data[0]['id'], 1, 'Loaded data by string type key.');
// Delete table.
$table
->drop($table_name);
$this
->assertFalse(db_table_exists($table_name), 'Dropped table.');
// Create table and drop it again.
$table = data_create_table($table_name, $this
->testSchema());
$this
->assertTrue(!empty($table), 'Created table with same name ' . $table_name);
// Delete table.
$table
->drop($table_name);
$this
->assertFalse(db_table_exists($table_name), 'Dropped table.');
}