ChadoSchemaTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace Tests\tripal_chado\api;
  3. use StatonLab\TripalTestSuite\DBTransaction;
  4. use StatonLab\TripalTestSuite\TripalTestCase;
  5. use Faker\Factory;
  6. module_load_include('inc', 'tripal_chado', 'api/ChadoSchema');
  7. /**
  8. * Tests the ChadoSchema class.
  9. *
  10. * @todo test "Check" functions in the ChadoSchema class.
  11. */
  12. class ChadoSchemaTest extends TripalTestCase {
  13. use DBTransaction;
  14. /**
  15. * Tests that the class can be initiated with or without a record specified
  16. *
  17. * @group api
  18. * @group chado
  19. * @group chado-schema
  20. * @group lacey
  21. */
  22. public function testInitClass() {
  23. // Test with no parameters.
  24. $chado_schema = new \ChadoSchema();
  25. $this->assertNotNull($chado_schema);
  26. // Test with version.
  27. $chado_schema = new \ChadoSchema('1.3');
  28. $this->assertNotNull($chado_schema);
  29. }
  30. /**
  31. * Tests the ChadoSchema->getVersion() method.
  32. *
  33. * @group api
  34. * @group chado
  35. * @group chado-schema
  36. * @group lacey
  37. */
  38. public function testGetVersion() {
  39. // Generate a fake version.
  40. $faker = Factory::create();
  41. $version = $faker->randomFloat(2, 1, 5);
  42. // Check version can be retrieved when we set it.
  43. $chado_schema = new \ChadoSchema($version);
  44. $retrieved_version = $chado_schema->getVersion();
  45. $this->assertEquals(
  46. $version,
  47. $retrieved_version,
  48. t('The version retrieved via ChadoSchema->getVersion, "!ret", should equal that set, "!set"',
  49. array('!ret' => $retrieved_version, '!set' => $version))
  50. );
  51. // @todo Check version can be retrieved when it's looked up?
  52. }
  53. /**
  54. * Tests the ChadoSchema->getSchemaName() method.
  55. *
  56. * @group api
  57. * @group chado
  58. * @group chado-schema
  59. * @group lacey
  60. */
  61. public function testGetSchemaName() {
  62. // Generate a fake version.
  63. $faker = Factory::create();
  64. $version = $faker->randomFloat(2, 1, 5);
  65. $schema_name = $faker->word();
  66. // Check the schema name can be retrieved when we set it.
  67. $chado_schema = new \ChadoSchema($version, $schema_name);
  68. $retrieved_schema = $chado_schema->getSchemaName();
  69. $this->assertEquals(
  70. $schema_name,
  71. $retrieved_schema,
  72. t('The schema name retrieved via ChadoSchema->getSchemaName, "!ret", should equal that set, "!set"',
  73. array('!ret' => $retrieved_schema, '!set' => $schema_name))
  74. );
  75. // @todo Check schema name can be retrieved when it's looked up?
  76. }
  77. /**
  78. * Tests ChadoSchema->getTableNames() method.
  79. *
  80. * @dataProvider knownTableProvider
  81. *
  82. * @group api
  83. * @group chado
  84. * @group chado-schema
  85. * @group lacey
  86. */
  87. public function testGetTableNames($version, $known_tables) {
  88. // Check: Known tables for a given version are returned.
  89. $chado_schema = new \ChadoSchema($version);
  90. $returned_tables = $chado_schema->getTableNames();
  91. foreach ($known_tables as $table_name) {
  92. $this->assertArrayHasKey(
  93. $table_name,
  94. $returned_tables,
  95. t('The table, "!known", should exist in the returned tables list for version !version.',
  96. array(':known' => $table_name, ':version' => $version))
  97. );
  98. }
  99. }
  100. /**
  101. * Tests ChadoSchema->getTableSchema() method.
  102. *
  103. * @dataProvider chadoTableProvider
  104. *
  105. * @group api
  106. * @group chado
  107. * @group chado-schema
  108. * @group lacey
  109. */
  110. public function testGetTableSchema($version, $table_name) {
  111. // Check: a schema is returned that matches what we expect.
  112. $chado_schema = new \ChadoSchema($version);
  113. $table_schema = $chado_schema->getTableSchema($table_name);
  114. $this->assertNotEmpty(
  115. $table_schema,
  116. t('Returned schema for "!table" in chado v!version should not be empty.',
  117. array('!table' => $table_name, '!version' => $version))
  118. );
  119. $this->assertArrayHasKey(
  120. 'fields',
  121. $table_schema,
  122. t('The schema array for "!table" should have columns listed in an "fields" array',
  123. array('!table' => $table_name))
  124. );
  125. $this->assertArrayHasKey(
  126. 'primary key',
  127. $table_schema,
  128. t('The schema array for "!table" should have the primary key listed in an "primary key" array',
  129. array('!table' => $table_name))
  130. );
  131. $this->assertArrayHasKey(
  132. 'unique keys',
  133. $table_schema,
  134. t('The schema array for "!table" should have unique keys listed in an "unique keys" array',
  135. array('!table' => $table_name))
  136. );
  137. $this->assertArrayHasKey(
  138. 'foreign keys',
  139. $table_schema,
  140. t('The schema array for "!table" should have foreign keys listed in an "foreign keys" array',
  141. array('!table' => $table_name))
  142. );
  143. }
  144. /**
  145. * Data Provider: returns known tables specific to a given chado version.
  146. *
  147. * @return array
  148. */
  149. public function knownTableProvider() {
  150. // chado version, array of 3 tables specific to version.
  151. return [
  152. ['1.2', ['cell_line_relationship', 'cvprop', 'chadoprop']],
  153. ['1.3', ['analysis_cvterm', 'dbprop', 'organism_pub']],
  154. ];
  155. }
  156. /**
  157. * DataProvider, a list of all chado tables.
  158. *
  159. * @return array
  160. */
  161. public function chadoTableProvider() {
  162. $chado_schema = new \ChadoSchema();
  163. $version = $chado_schema->getVersion();
  164. $dataset = [];
  165. foreach ($chado_schema->getTableNames() as $table_name) {
  166. $dataset[] = [$version, $table_name];
  167. }
  168. return $dataset;
  169. }
  170. }