瀏覽代碼

CahdoSchema class: first, basic implementation with testing; ChadoComplianceTesting: outline and first check.

Lacey Sanderson 6 年之前
父節點
當前提交
2a3266ac98

+ 76 - 0
tests/tripal_chado/api/ChadoComplianceTest.php

@@ -0,0 +1,76 @@
+<?php
+namespace Tests\tripal_chado\api;
+
+use StatonLab\TripalTestSuite\DBTransaction;
+use StatonLab\TripalTestSuite\TripalTestCase;
+
+module_load_include('inc', 'tripal_chado', 'api/ChadoSchema');
+
+/**
+ * Tests the current Chado Database is compliant with the schema definition used by Tripal
+ */
+class ChadoComplianceTest extends TripalTestCase {
+  // Uncomment to auto start and rollback db transactions per test method.
+  // use DBTransaction;
+
+  /**
+   * DataProvider, a list of all chado tables.
+   *
+   * @return array
+   */
+  public function chadoTableProvider() {
+
+    // @todo expose all tables.
+    return [
+      ['organism'],
+      ['feature'],
+      ['stock'],
+    ];
+  }
+
+  /**
+   * Tests Compliance for a given table.
+   *
+   * The following is tested:
+   *   1. The table exists in the correct schema.
+   *   2. It has all the fields we expect.
+   *   3. Each field is the type we expect.
+   *   4. It has all the constraints we expect.
+   *   5. Each constraint consists of the columns we expect.
+   *
+   * @dataProvider chadoTableProvider
+   *
+   * @group api
+   * @group chado
+   * @group chado-schema
+   * @group chado-compliance
+   * @group lacey
+   */
+  public function testTableCompliance($table_name) {
+
+    // Create the ChadoSchema class to aid in testing.
+    $chado_schema = new \ChadoSchema();
+
+    // 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))
+    );
+
+    // Retrieve the schema for this table.
+
+    // For each column in this table...
+
+      // Check #2: The given field exists in the table.
+
+      // Check #3: The field is the type we expect.
+
+
+    // For each constraint on this table...
+
+      // Check #4: The constraint exists.
+
+      // Check #5: The constraint consists of the columns we expect.
+  }
+}

+ 131 - 0
tests/tripal_chado/api/ChadoSchemaTest.php

@@ -0,0 +1,131 @@
+<?php
+namespace Tests\tripal_chado\api;
+
+use StatonLab\TripalTestSuite\DBTransaction;
+use StatonLab\TripalTestSuite\TripalTestCase;
+use Faker\Factory;
+
+module_load_include('inc', 'tripal_chado', 'api/ChadoSchema');
+
+/**
+ * Tests the ChadoSchema class.
+ *
+ * @todo test "Check" functions in the ChadoSchema class.
+ */
+class ChadoSchemaTest extends TripalTestCase {
+  use DBTransaction;
+
+  /**
+   * Tests that the class can be initiated with or without a record specified
+   *
+   * @group api
+   * @group chado
+   * @group chado-schema
+   * @group lacey
+   */
+  public function testInitClass() {
+
+    // Test with no parameters.
+    $chado_schema = new \ChadoSchema();
+    $this->assertNotNull($chado_schema);
+
+    // Test with version.
+    $chado_schema = new \ChadoSchema('1.3');
+    $this->assertNotNull($chado_schema);
+  }
+
+  /**
+   * Tests the ChadoSchema->getVersion() method.
+   *
+   * @group api
+   * @group chado
+   * @group chado-schema
+   * @group lacey
+   */
+  public function testGetVersion() {
+
+    // Generate a fake version.
+    $faker = Factory::create();
+    $version = $faker->randomFloat(2, 1, 5);
+
+    // Check version can be retrieved when we set it.
+    $chado_schema = new \ChadoSchema($version);
+    $retrieved_version = $chado_schema->getVersion();
+    $this->assertEquals(
+      $version,
+      $retrieved_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?
+  }
+
+  /**
+   * Tests the ChadoSchema->getSchemaName() method.
+   *
+   * @group api
+   * @group chado
+   * @group chado-schema
+   * @group lacey
+   */
+  public function testGetSchemaName() {
+
+    // Generate a fake version.
+    $faker = Factory::create();
+    $version = $faker->randomFloat(2, 1, 5);
+    $schema_name = $faker->word();
+
+    // Check the schema name can be retrieved when we set it.
+    $chado_schema = new \ChadoSchema($version, $schema_name);
+    $retrieved_schema = $chado_schema->getSchemaName();
+    $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))
+    );
+
+    // @todo Check schema name can be retrieved when it's looked up?
+  }
+
+  /**
+   * Tests ChadoSchema->getTableNames() method.
+   *
+   * @dataProvider knownTableProvider
+   *
+   * @group api
+   * @group chado
+   * @group chado-schema
+   * @group lacey
+   */
+  public function testGetTableNames($version, $known_tables) {
+
+    // Check: Known tables for a given version are returned.
+    $chado_schema = new \ChadoSchema($version);
+    $returned_tables = $chado_schema->getTableNames();
+
+    foreach ($known_tables as $table_name) {
+      $this->assertArrayHasKey(
+        $table_name,
+        $returned_tables,
+        t('The table, :known, should exist in the returned tables list for version :version.',
+          array(':known' => $table_name, ':version' => $version))
+      );
+    }
+  }
+
+  /**
+   * Data Provider: returns known tables specific to a given chado version.
+   *
+   * @return array
+   */
+   public function knownTableProvider() {
+    // chado version, array of 3 tables specific to version.
+
+    return [
+      ['1.2', ['cell_line_relationship', 'cvprop', 'chadoprop']],
+      ['1.3', ['analysis_cvterm', 'dbprop', 'organism_pub']],
+    ];
+   }
+}

+ 142 - 0
tripal_chado/api/ChadoSchema.inc

@@ -0,0 +1,142 @@
+<?php
+/**
+ * Provides an application programming interface (API) for describing Chado tables.
+ *
+ * The functions provided in this documentation should not be called as is, but if you need
+ * the Drupal-style array definition for any table, use the following function
+ * call:
+ *
+ *   $table_desc = chado_get_schema($table)
+ *
+ * where the variable $table contains the name of the table you want to
+ * retireve.  The chado_get_schema function determines the appropriate version of
+ * Chado and uses the Drupal hook infrastructure to call the appropriate
+ * hook function to retrieve the table schema.
+ */
+class ChadoSchema {
+
+  /**
+   * @var string
+   *   The current version for this site. E.g. "1.3".
+   */
+  protected $version = '';
+
+  /**
+   * @var string
+   *   The name of the schema chado was installed in.
+   */
+  protected $schema_name = 'chado';
+
+  /**
+   * The ChadoSchema constructor.
+   *
+   * @param string $version
+   *   The current version for this site. E.g. "1.3". If a version is not provided, the
+   *   version of the current database will be looked up.
+   */
+  public function __construct($version = NULL, $schema_name = NULL) {
+
+    // Set the version of the schema.
+    if ($version === NULL) {
+      $this->version = chado_get_version(TRUE);
+    }
+    else {
+      $this->version = $version;
+    }
+
+    // Set the name of the schema.
+    if ($schema_name === NULL) {
+      $this->schema_name = chado_get_schema_name('chado');
+    }
+    else {
+      $this->schema_name = $schema_name;
+    }
+  }
+
+  /**
+   * Returns the version number of the Chado this object references.
+   *
+   * @returns
+   *   The version of Chado
+   */
+  public function getVersion() {
+    return $this->version;
+  }
+
+  /**
+   * Retrieve the name of the PostgreSQL schema housing Chado.
+   *
+   * @return
+   *   The name of the schema.
+   */
+  public function getSchemaName() {
+    return $this->schema_name;
+  }
+
+  /**
+   * Retrieves the list of tables in the Chado schema.  By default it only returns
+   * the default Chado tables, but can return custom tables added to the
+   * Chado schema if requested.
+   *
+   * @param $include_custom
+   *   Optional.  Set as TRUE to include any custom tables created in the
+   *   Chado schema. Custom tables are added to Chado using the
+   *   tripal_chado_chado_create_table() function.
+   *
+   * @returns
+   *   An associative array where the key and value pairs are the Chado table names.
+   */
+  public function getTableNames($include_custom = FALSE) {
+
+    $tables = array();
+    if ($this->version == '1.3') {
+      $tables_v1_3 = tripal_chado_chado_get_v1_3_tables();
+      foreach ($tables_v1_3 as $table) {
+        $tables[$table] = $table;
+      }
+    }
+    if ($this->version == '1.2') {
+      $tables_v1_2 = tripal_chado_chado_get_v1_2_tables();
+      foreach ($tables_v1_2 as $table) {
+        $tables[$table] = $table;
+      }
+    }
+    if ($this->version == '1.11' or $v == '1.11 or older') {
+      $tables_v1_11 = tripal_chado_chado_get_v1_11_tables();
+      foreach ($tables_v1_11 as $table) {
+        $tables[$table] = $table;
+      }
+    }
+
+    // now add in the custom tables too if requested
+    if ($include_custom) {
+      $sql = "SELECT table_name FROM {tripal_custom_tables}";
+      $resource = db_query($sql);
+
+      foreach ($resource as $r) {
+        $tables[$r->table_name] = $r->table_name;
+      }
+    }
+
+    asort($tables);
+    return $tables;
+
+  }
+
+  /**
+   * Check that any given Chado table exists.
+   *
+   * This function is necessary because Drupal's db_table_exists() function will
+   * not look in any other schema but the one where Drupal is installed
+   *
+   * @param $table
+   *   The name of the chado table whose existence should be checked.
+   *
+   * @return
+   *   TRUE if the table exists in the chado schema and FALSE if it does not.
+   */
+  public function checkTableExists($table) {
+    return chado_table_exists($table);
+  }
+
+}