ChadoComplianceTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Tests\tripal_chado\api;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. module_load_include('inc', 'tripal_chado', 'api/ChadoSchema');
  6. /**
  7. * Tests the current Chado Database is compliant with the schema definition used by Tripal
  8. */
  9. class ChadoComplianceTest extends TripalTestCase {
  10. // Uncomment to auto start and rollback db transactions per test method.
  11. use DBTransaction;
  12. /**
  13. * DataProvider, a list of all chado tables.
  14. *
  15. * @return array
  16. */
  17. public function chadoTableProvider() {
  18. $chado_schema = new \ChadoSchema();
  19. $version = $chado_schema->getVersion();
  20. $dataset = [];
  21. foreach ($chado_schema->getTableNames() as $table_name) {
  22. $dataset[] = [$version, $table_name];
  23. }
  24. return $dataset;
  25. }
  26. /**
  27. * Tests Compliance for a given table.
  28. *
  29. * The following is tested:
  30. * 1. The table exists in the correct schema.
  31. * 2. It has all the fields we expect.
  32. * 3. Each field is the type we expect.
  33. * 4. It has all the constraints we expect.
  34. * 5. Each constraint consists of the columns we expect.
  35. *
  36. * @dataProvider chadoTableProvider
  37. *
  38. * @group api
  39. * @group chado
  40. * @group chado-schema
  41. * @group chado-compliance
  42. * @group lacey
  43. */
  44. public function testTableCompliance($schema_version, $table_name) {
  45. // Create the ChadoSchema class to aid in testing.
  46. $chado_schema = new \ChadoSchema();
  47. $version = $chado_schema->getVersion();
  48. // Check #1: The table exists in the correct schema.
  49. $this->assertTrue(
  50. $chado_schema->checkTableExists($table_name),
  51. t('"!table_name" should exist in the "!chado" schema v!version.',
  52. array('!table_name' => $table_name, '!chado' => $schema_name, '!version' => $version))
  53. );
  54. // Retrieve the schema for this table.
  55. $table_schema = $chado_schema->getTableSchema($table_name);
  56. //print "Table ($table_name)! " . print_r($table_schema,TRUE) . "\n";
  57. // For each column in this table...
  58. foreach ($table_schema['fields'] as $column_name => $column_details) {
  59. // Check #2: The given field exists in the table.
  60. $this->assertTrue(
  61. $chado_schema->checkColumnExists($table_name, $column_name),
  62. t('The column "!column" must exist in "!table" for chado v!version.',
  63. array('!column' => $column_name, '!table' => $table_name, '!version' => $version))
  64. );
  65. // @todo Check #3: The field is the type we expect.
  66. }
  67. // For each constraint on this table...
  68. // Check #4: The constraint exists.
  69. // Check #5: The constraint consists of the columns we expect.
  70. }
  71. }