ChadoComplianceTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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-compliance
  41. */
  42. public function testTableCompliance($schema_version, $table_name) {
  43. // Create the ChadoSchema class to aid in testing.
  44. $chado_schema = new \ChadoSchema();
  45. $version = $chado_schema->getVersion();
  46. // Check #1: The table exists in the correct schema.
  47. $this->assertTrue(
  48. $chado_schema->checkTableExists($table_name),
  49. t('"!table_name" should exist in the "!chado" schema v!version.',
  50. array('!table_name' => $table_name, '!chado' => $schema_name, '!version' => $version))
  51. );
  52. // Retrieve the schema for this table.
  53. $table_schema = $chado_schema->getTableSchema($table_name);
  54. //print "Table ($table_name)! " . print_r($table_schema,TRUE) . "\n";
  55. // For each column in this table...
  56. foreach ($table_schema['fields'] as $column_name => $column_details) {
  57. // Check #2: The given field exists in the table.
  58. $this->assertTrue(
  59. $chado_schema->checkColumnExists($table_name, $column_name),
  60. t('The column "!column" must exist in "!table" for chado v!version.',
  61. array('!column' => $column_name, '!table' => $table_name, '!version' => $version))
  62. );
  63. // Check #3: The field is the type we expect.
  64. $this->assertTrue(
  65. $chado_schema->checkColumnType($table_name, $column_name, $column_details['type']),
  66. t('The column "!table.!column" must be of type "!type" for chado v!version.',
  67. array('!column' => $column_name, '!table' => $table_name,
  68. '!version' => $version, '!type' => $column_details['type']))
  69. );
  70. }
  71. // For each constraint on this table...
  72. // Check #4: The constraint exists.
  73. // Check #5: The constraint consists of the columns we expect.
  74. }
  75. }