Remove auto-creation of Chado in Tests
laceysanderson opened this issue · 0 comments
Currently when you extend the ChadoTestBrowserBase, you also inherit a setUp that creates a new chado instance specifically for testing similar to Drupal.
/**
* {@inheritdoc}
*/
protected function setUp() :void {
parent::setUp();
// Only initialize the connection to Chado once.
if (!$this->tripal_dbx) {
$this->createChadoInstallationsTable();
$this->getRealConfig();
$this->initTripalDbx();
$this->allowTestSchemas();
$this->chado = $this->getTestSchema(ChadoTestBrowserBase::INIT_CHADO_EMPTY);
}
}
Then in your tests you can access $this->chado. This is very convenient but we've since learned that storing database connections as object properties can sometimes cause "LogicException: The database connection is not serializable." exceptions.
Also, this creates a completely empty chado schema whereas many tests will need a chado instance prepared for use with Drupal/Tripal and then have to create a second instance for their tests.
The proposed solution is to move chado creation into each test class setUp or into the test method directly depending on what makes the most sense for that test. If doing this in the setUp then Best Practice is to save the newly created chado instance SCHEMA NAME and NOT THE CONNECTION as a property for future use.
Best Practice:
class MyTest extends ChadoTestBrowserBase {
/**
* The name of the chado schema to use for testing.
*/
protected $chado_schema_name;
/**
* {@inheritdoc}
*/
protected function setUp() :void {
parent::setUp();
$chado = $this->getTestSchema(ChadoTestBrowserBase::INIT_CHADO_EMPTY);
$this->chado_schema_name = $chado->getSchemaName();
}
public function testSomethingImportant() {
$chado = \Drupal::service('tripal_chado.database');
$chado->setSchemaName($this->chado_schema_name);
// Do any querying, etc. that you need to for testing.
}
}