Bläddra i källkod

Added getTableSchema and associated tests. Expanded some tests to all chado tables.

Lacey Sanderson 6 år sedan
förälder
incheckning
900fdddddd

+ 25 - 13
tests/tripal_chado/api/ChadoComplianceTest.php

@@ -11,7 +11,7 @@ module_load_include('inc', 'tripal_chado', 'api/ChadoSchema');
  */
 class ChadoComplianceTest extends TripalTestCase {
   // Uncomment to auto start and rollback db transactions per test method.
-  // use DBTransaction;
+  use DBTransaction;
 
   /**
    * DataProvider, a list of all chado tables.
@@ -20,12 +20,15 @@ class ChadoComplianceTest extends TripalTestCase {
    */
   public function chadoTableProvider() {
 
-    // @todo expose all tables.
-    return [
-      ['organism'],
-      ['feature'],
-      ['stock'],
-    ];
+    $chado_schema = new \ChadoSchema();
+    $version = $chado_schema->getVersion();
+
+    $dataset = [];
+    foreach ($chado_schema->getTableNames() as $table_name) {
+      $dataset[] = [$version, $table_name];
+    }
+
+    return $dataset;
   }
 
   /**
@@ -46,26 +49,35 @@ class ChadoComplianceTest extends TripalTestCase {
    * @group chado-compliance
    * @group lacey
    */
-  public function testTableCompliance($table_name) {
+  public function testTableCompliance($schema_version, $table_name) {
 
     // Create the ChadoSchema class to aid in testing.
     $chado_schema = new \ChadoSchema();
+    $version = $chado_schema->getVersion();
 
     // Check #1: The table exists in the correct schema.
     $this->assertTrue(
       $chado_schema->checkTableExists($table_name),
-      t(':table_name should exist in the :chado schema.',
-        array(':table_name' => $table_name, ':chado' => $schema_name))
+      t('"!table_name" should exist in the "!chado" schema v!version.',
+        array('!table_name' => $table_name, '!chado' => $schema_name, '!version' => $version))
     );
 
     // Retrieve the schema for this table.
+    $table_schema = $chado_schema->getTableSchema($table_name);
+    //print "Table ($table_name)! " . print_r($table_schema,TRUE) . "\n";
 
     // For each column in this table...
+    foreach ($table_schema['fields'] as $column_name => $column_details) {
 
       // Check #2: The given field exists in the table.
-
-      // Check #3: The field is the type we expect.
-
+      $this->assertTrue(
+        $chado_schema->checkColumnExists($table_name, $column_name),
+        t('The column "!column" must exist in "!table" for chado v!version.',
+          array('!column' => $column_name, '!table' => $table_name, '!version' => $version))
+      );
+
+      // @todo Check #3: The field is the type we expect.
+    }
 
     // For each constraint on this table...
 

+ 76 - 5
tests/tripal_chado/api/ChadoSchemaTest.php

@@ -54,8 +54,8 @@ class ChadoSchemaTest extends TripalTestCase {
     $this->assertEquals(
       $version,
       $retrieved_version,
-      t('The version retrieved via ChadoSchema->getVersion, :ret, should equal that set, :set',
-        array(':ret' => $retrieved_version, ':set' => $version))
+      t('The version retrieved via ChadoSchema->getVersion, "!ret", should equal that set, "!set"',
+        array('!ret' => $retrieved_version, '!set' => $version))
     );
 
     // @todo Check version can be retrieved when it's looked up?
@@ -82,8 +82,8 @@ class ChadoSchemaTest extends TripalTestCase {
     $this->assertEquals(
       $schema_name,
       $retrieved_schema,
-      t('The schema name retrieved via ChadoSchema->getSchemaName, :ret, should equal that set, :set',
-        array(':ret' => $retrieved_schema, ':set' => $schema_name))
+      t('The schema name retrieved via ChadoSchema->getSchemaName, "!ret", should equal that set, "!set"',
+        array('!ret' => $retrieved_schema, '!set' => $schema_name))
     );
 
     // @todo Check schema name can be retrieved when it's looked up?
@@ -109,12 +109,65 @@ class ChadoSchemaTest extends TripalTestCase {
       $this->assertArrayHasKey(
         $table_name,
         $returned_tables,
-        t('The table, :known, should exist in the returned tables list for version :version.',
+        t('The table, "!known", should exist in the returned tables list for version !version.',
           array(':known' => $table_name, ':version' => $version))
       );
     }
   }
 
+  /**
+   * Tests ChadoSchema->getTableSchema() method.
+   *
+   * @dataProvider chadoTableProvider
+   *
+   * @group api
+   * @group chado
+   * @group chado-schema
+   * @group lacey
+   */
+  public function testGetTableSchema($version, $table_name) {
+
+    // Check: a schema is returned that matches what we expect.
+    $chado_schema = new \ChadoSchema($version);
+
+    $table_schema = $chado_schema->getTableSchema($table_name);
+
+    $this->assertNotEmpty(
+      $table_schema,
+      t('Returned schema for "!table" in chado v!version should not be empty.',
+        array('!table' => $table_name, '!version' => $version))
+    );
+
+    $this->assertArrayHasKey(
+      'fields',
+      $table_schema,
+      t('The schema array for "!table" should have columns listed in an "fields" array',
+        array('!table' => $table_name))
+    );
+
+    $this->assertArrayHasKey(
+      'primary key',
+      $table_schema,
+      t('The schema array for "!table" should have the primary key listed in an "primary key" array',
+        array('!table' => $table_name))
+    );
+
+    $this->assertArrayHasKey(
+      'unique keys',
+      $table_schema,
+      t('The schema array for "!table" should have unique keys listed in an "unique keys" array',
+        array('!table' => $table_name))
+    );
+
+    $this->assertArrayHasKey(
+      'foreign keys',
+      $table_schema,
+      t('The schema array for "!table" should have foreign keys listed in an "foreign keys" array',
+        array('!table' => $table_name))
+    );
+
+  }
+
   /**
    * Data Provider: returns known tables specific to a given chado version.
    *
@@ -128,4 +181,22 @@ class ChadoSchemaTest extends TripalTestCase {
       ['1.3', ['analysis_cvterm', 'dbprop', 'organism_pub']],
     ];
    }
+
+  /**
+   * DataProvider, a list of all chado tables.
+   *
+   * @return array
+   */
+  public function chadoTableProvider() {
+
+    $chado_schema = new \ChadoSchema();
+    $version = $chado_schema->getVersion();
+
+    $dataset = [];
+    foreach ($chado_schema->getTableNames() as $table_name) {
+      $dataset[] = [$version, $table_name];
+    }
+
+    return $dataset;
+  }
 }

+ 91 - 2
tripal_chado/api/ChadoSchema.inc

@@ -110,11 +110,11 @@ class ChadoSchema {
 
     // now add in the custom tables too if requested
     if ($include_custom) {
-      $sql = "SELECT table_name FROM {tripal_custom_tables}";
+      $sql = "SELECT table FROM {tripal_custom_tables}";
       $resource = db_query($sql);
 
       foreach ($resource as $r) {
-        $tables[$r->table_name] = $r->table_name;
+        $tables[$r->table] = $r->table;
       }
     }
 
@@ -123,6 +123,46 @@ class ChadoSchema {
 
   }
 
+  /**
+   * Retrieves the chado tables Schema API array.
+   *
+   * @param $table
+   *   The name of the table to retrieve.  The function will use the appopriate
+   *   Tripal chado schema API hooks (e.g. v1.11 or v1.2).
+   *
+   * @returns
+   *   A Drupal Schema API array defining the table.
+   */
+  public function getTableSchema($table) {
+
+    // first get the chado version.
+    $v = $this->version;
+
+    // get the table array from the proper chado schema
+    $v = preg_replace("/\./", "_", $v); // reformat version for hook name
+
+    // Call the module_invoke_all.
+    $hook_name = "chado_schema_v" . $v . "_" . $table;
+    $table_arr = module_invoke_all($hook_name);
+
+    // If the module_invoke_all returned nothing then let's make sure there isn't
+    // An API call we can call directly.  The only time this occurs is
+    // during an upgrade of a major Drupal version and tripal_core is disabled.
+    if ((!$table_arr or !is_array($table_arr)) and
+          function_exists('tripal_chado_' . $hook_name)) {
+      $api_hook = "tripal_chado_" . $hook_name;
+      $table_arr = $api_hook();
+    }
+
+    // if the table_arr is empty then maybe this is a custom table
+    if (!is_array($table_arr) or count($table_arr) == 0) {
+      $table_arr = chado_get_custom_table_schema($table);
+    }
+
+    return $table_arr;
+
+  }
+
   /**
    * Check that any given Chado table exists.
    *
@@ -139,4 +179,53 @@ class ChadoSchema {
     return chado_table_exists($table);
   }
 
+  /**
+   * Check that any given column in a Chado table exists.
+   *
+   * This function is necessary because Drupal's db_field_exists() will not
+   * look in any other schema but the one were Drupal is installed
+   *
+   * @param $table
+   *   The name of the chado table.
+   * @param $column
+   *   The name of the column in the chado table.
+   *
+   * @return
+   *   TRUE if the column exists for the table in the chado schema and
+   *   FALSE if it does not.
+   *
+   * @ingroup tripal_chado_schema_api
+   */
+  public function checkColumnExists($table, $column) {
+    return chado_column_exists($table, $column);
+  }
+
+  /**
+   * Check that any given column in a Chado table exists.
+   *
+   * This function is necessary because Drupal's db_field_exists() will not
+   * look in any other schema but the one were Drupal is installed
+   *
+   * @param sequence
+   *   The name of the sequence
+   * @return
+   *   TRUE if the seqeuence exists in the chado schema and FALSE if it does not.
+   *
+   * @ingroup tripal_chado_schema_api
+   */
+  public function checkSequenceExists($sequence) {
+    return chado_sequence_exists($sequence);
+  }
+
+  /**
+   * A Chado-aware replacement for the db_index_exists() function.
+   *
+   * @param $table
+   *   The table to be altered.
+   * @param $name
+   *   The name of the index.
+   */
+  function checkIndexExists($table, $name) {
+    return chado_index_exists($table, $name);
+  }
 }