ChadoComplianceTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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
  8. * used by Tripal
  9. */
  10. class ChadoComplianceTest extends TripalTestCase {
  11. // Uncomment to auto start and rollback db transactions per test method.
  12. use DBTransaction;
  13. /**
  14. * DataProvider, a list of all chado tables.
  15. *
  16. * @return array
  17. */
  18. public function chadoTableProvider() {
  19. $chado_schema = new \ChadoSchema();
  20. $version = $chado_schema->getVersion();
  21. $dataset = [];
  22. foreach ($chado_schema->getTableNames() as $table_name) {
  23. $dataset[] = [$version, $table_name];
  24. }
  25. return $dataset;
  26. }
  27. /**
  28. * Tests Compliance for a given table.
  29. *
  30. * The following is tested:
  31. * 1. The table exists in the correct schema.
  32. * 2. It has all the fields we expect.
  33. * 3. Each field is the type we expect.
  34. * 4. It has all the constraints we expect.
  35. * 5. Each constraint consists of the columns we expect.
  36. *
  37. * @dataProvider chadoTableProvider
  38. *
  39. * @group api
  40. * @group chado
  41. * @group chado-compliance
  42. */
  43. public function testTableCompliance($schema_version, $table_name) {
  44. // Create the ChadoSchema class to aid in testing.
  45. $chado_schema = new \ChadoSchema();
  46. $version = $chado_schema->getVersion();
  47. $schema_name = $chado_schema->getSchemaName();
  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. [
  53. '!table_name' => $table_name,
  54. '!chado' => $schema_name,
  55. '!version' => $version,
  56. ])
  57. );
  58. // Retrieve the schema for this table.
  59. $table_schema = $chado_schema->getTableSchema($table_name);
  60. // For each column in this table...
  61. foreach ($table_schema['fields'] as $column_name => $column_details) {
  62. // Check #2: The given field exists in the table.
  63. $this->assertTrue(
  64. $chado_schema->checkColumnExists($table_name, $column_name),
  65. t('The column "!column" must exist in "!table" for chado v!version.',
  66. [
  67. '!column' => $column_name,
  68. '!table' => $table_name,
  69. '!version' => $version,
  70. ])
  71. );
  72. // Check #3: The field is the type we expect.
  73. $this->assertTrue(
  74. $chado_schema->checkColumnType($table_name, $column_name, $column_details['type']),
  75. t('The column "!table.!column" must be of type "!type" for chado v!version.',
  76. [
  77. '!column' => $column_name,
  78. '!table' => $table_name,
  79. '!version' => $version,
  80. '!type' => $column_details['type'],
  81. ])
  82. );
  83. }
  84. // There are three types of constraints:
  85. // primary key, unique keys, and foreign keys.
  86. //.......................................
  87. // For the primary key:
  88. // Check #4: The constraint exists.
  89. if (isset($table_schema['primary key'][0]) AND !empty($table_schema['primary key'][0])) {
  90. $pkey_column = $table_schema['primary key'][0];
  91. $this->assertTrue(
  92. $chado_schema->checkPrimaryKey($table_name, $pkey_column),
  93. t('The column "!table.!column" must be a primary key with an associated sequence and constraint for chado v!version.',
  94. [
  95. '!column' => $pkey_column,
  96. '!table' => $table_name,
  97. '!version' => $version,
  98. ])
  99. );
  100. }
  101. // For each unique key:
  102. foreach ($table_schema['unique keys'] as $constraint_name => $columns) {
  103. // @debug print "Check '$constraint_name' for '$table_name': ".implode(', ', $columns).".\n";
  104. // Check #4: The constraint exists.
  105. $this->assertTrue(
  106. $chado_schema->checkConstraintExists($table_name, $constraint_name, 'UNIQUE'),
  107. t('The unique constraint "!name" for "!table" must exist for chado v!version.',
  108. [
  109. '!name' => $constraint_name,
  110. '!table' => $table_name,
  111. '!version' => $version,
  112. ])
  113. );
  114. // Check #5: The constraint consists of the columns we expect.
  115. // @todo
  116. }
  117. // For each foreign key:
  118. foreach ($table_schema['foreign keys'] as $fk_table => $details) {
  119. foreach ($details['columns'] as $base_column => $fk_column) {
  120. // @debug print "Check '$table_name.$base_column => $fk_table.$fk_column ' foreign key.";
  121. // Check #4: The constraint exists.
  122. $constraint_name = $table_name . '_' . $base_column . '_fkey';
  123. $this->assertTrue(
  124. $chado_schema->checkFKConstraintExists($table_name, $base_column),
  125. t('The foreign key constraint "!name" for "!table.!column" => "!fktable.!fkcolumn" must exist for chado v!version.',
  126. [
  127. '!name' => $constraint_name,
  128. '!table' => $table_name,
  129. '!column' => $base_column,
  130. '!fktable' => $fk_table,
  131. '!fkcolumn' => $fk_column,
  132. '!version' => $version,
  133. ])
  134. );
  135. // Check #5: The constraint consists of the columns we expect.
  136. // @todo
  137. }
  138. }
  139. }
  140. }