ChadoComplianceTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. $schema_name = $chado_schema->getSchemaName();
  47. // Check #1: The table exists in the correct schema.
  48. $this->assertTrue(
  49. $chado_schema->checkTableExists($table_name),
  50. t('"!table_name" should exist in the "!chado" schema v!version.',
  51. array('!table_name' => $table_name, '!chado' => $schema_name, '!version' => $version))
  52. );
  53. // Retrieve the schema for this table.
  54. $table_schema = $chado_schema->getTableSchema($table_name);
  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. // There are three types of constraints:
  72. // primary key, unique keys, and foreign keys.
  73. //.......................................
  74. // For the primary key:
  75. // Check #4: The constraint exists.
  76. if (isset($table_schema['primary key'][0]) AND !empty($table_schema['primary key'][0])) {
  77. $pkey_column = $table_schema['primary key'][0];
  78. $this->assertTrue(
  79. $chado_schema->checkPrimaryKey($table_name, $pkey_column),
  80. t('The column "!table.!column" must be a primary key with an associated sequence and constraint for chado v!version.',
  81. array('!column' => $pkey_column, '!table' => $table_name, '!version' => $version))
  82. );
  83. }
  84. // For each unique key:
  85. foreach ($table_schema['unique keys'] as $constraint_name => $columns) {
  86. // @debug print "Check '$constraint_name' for '$table_name': ".implode(', ', $columns).".\n";
  87. // Check #4: The constraint exists.
  88. $this->assertTrue(
  89. $chado_schema->checkConstraintExists($table_name, $constraint_name, 'UNIQUE'),
  90. t('The unique constraint "!name" for "!table" must exist for chado v!version.',
  91. array('!name' => $constraint_name, '!table' => $table_name, '!version' => $version))
  92. );
  93. // Check #5: The constraint consists of the columns we expect.
  94. // @todo
  95. }
  96. // For each foreign key:
  97. foreach ($table_schema['foreign keys'] as $fk_table => $details) {
  98. foreach ($details['columns'] as $base_column => $fk_column) {
  99. // @debug print "Check '$table_name.$base_column => $fk_table.$fk_column ' foreign key.";
  100. // Check #4: The constraint exists.
  101. $constraint_name = $table_name . '_' . $base_column . '_fkey';
  102. $this->assertTrue(
  103. $chado_schema->checkFKConstraintExists($table_name, $base_column),
  104. t('The foreign key constraint "!name" for "!table.!column" => "!fktable.!fkcolumn" must exist for chado v!version.',
  105. array('!name' => $constraint_name,
  106. '!table' => $table_name, '!column' => $base_column,
  107. '!fktable' => $fk_table, '!fkcolumn' => $fk_column,
  108. '!version' => $version))
  109. );
  110. // Check #5: The constraint consists of the columns we expect.
  111. // @todo
  112. }
  113. }
  114. }
  115. }