Преглед изворни кода

Merge pull request #659 from tripal/657_update_property

657 fix broken chado property API calls and add unit tests for each
Bradford Condon пре 6 година
родитељ
комит
cf3dcf1594

+ 242 - 0
tests/tripal_chado/api/TripalChadoPropertyAPITest.php

@@ -0,0 +1,242 @@
+<?php
+
+namespace Tests;
+
+use StatonLab\TripalTestSuite\DBTransaction;
+use StatonLab\TripalTestSuite\TripalTestCase;
+use StatonLab\TripalTestSuite\Database\Factory;
+
+class TripalChadoPropertyAPITest extends TripalTestCase {
+
+  use DBTransaction;
+
+  /**
+   * Tests chado_insert_property() with all prop tables.
+   *
+   * @dataProvider propTableProvider
+   *
+   * @group chado
+   * @group api
+   * @group chado-property
+   */
+  public function test_chado_insert_property($prop_table, $base_table) {
+
+    $base_record = factory('chado.'.$base_table)->create();
+    $base_pkey = $base_table.'_id';
+    $term = factory('chado.cvterm')->create();
+
+    $value = 'chado_API_test_value';
+
+    // Linker column
+    $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
+    $property = [
+      'type_id' => $term->cvterm_id,
+      'value' => $value,
+    ];
+
+    chado_insert_property($record, $property);
+
+    $result = db_select('chado.'.$prop_table, 'p')
+      ->fields('p')
+      ->condition('p.'.$base_pkey, $base_record->{$base_pkey})
+      ->execute()
+      ->fetchObject();
+
+    $this->assertNotEmpty($result);
+    $this->assertEquals($value, $result->value);
+    $this->assertEquals($term->cvterm_id, $result->type_id);
+    $this->assertEquals('0', $result->rank);
+
+  }
+
+
+  /**
+   * Tests chado_get_property() with all prop tables.
+   *
+   * @dataProvider propTableProvider
+   *
+   * @group chado
+   * @group api
+   * @group chado-property
+   */
+  public function test_chado_get_property($prop_table, $base_table) {
+
+    $base_record = factory('chado.'.$base_table)->create();
+    $base_pkey = $base_table.'_id';
+    $term = factory('chado.cvterm')->create();
+
+    $value = 'chado_API_test_value';
+
+    // Linker column
+    $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
+    $property = [
+      'type_id' => $term->cvterm_id,
+      'value' => $value,
+    ];
+
+    $prop = chado_insert_property($record, $property);
+    $retrieved = chado_get_property($record, $property);
+    $this->assertNotFalse($retrieved);
+    $this->assertEquals($value, $retrieved->value);
+
+    $record = ['prop_id' => $prop[$prop_table.'_id'], 'table' => $base_table];
+    $retrieved = chado_get_property($record, $property);
+
+    $this->assertNotNull($retrieved);
+    $this->assertEquals($value, $retrieved->value);
+  }
+
+  /**
+   * Tests chado_update_property() with all prop tables.
+   *
+   * @dataProvider propTableProvider
+   *
+   * @group chado
+   * @group api
+   * @group chado-property
+   */
+  public function test_chado_update_property($prop_table, $base_table) {
+
+    $base_record = factory('chado.'.$base_table)->create();
+    $base_pkey = $base_table.'_id';
+    $term = factory('chado.cvterm')->create();
+
+    $value = 'chado_API_test_value';
+    $new_value = 'chado_API_new';
+
+    // Linker column
+    $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
+    $property = [
+      'type_id' => $term->cvterm_id,
+      'value' => $value,
+    ];
+
+    chado_insert_property($record, $property);
+
+    $property['value'] = $new_value;
+
+    chado_update_property($record, $property);
+
+
+    $result = db_select('chado.'.$prop_table, 'p')
+      ->fields('p')
+      ->condition('p.'.$base_pkey, $base_record->{$base_pkey})
+      ->execute()
+      ->fetchObject();
+
+    $this->assertNotEmpty($result);
+    $this->assertEquals($new_value, $result->value);
+    $this->assertEquals($term->cvterm_id, $result->type_id);
+    $this->assertEquals('0', $result->rank);
+
+  }
+
+  /**
+   * Tests chado_delete_property() with all prop tables.
+   *
+   * @dataProvider propTableProvider
+   *
+   * @group chado
+   * @group api
+   * @group chado-property
+   */
+  public function test_chado_delete_property($prop_table, $base_table) {
+
+    $base_record = factory('chado.'.$base_table)->create();
+    $base_pkey = $base_table.'_id';
+    $term = factory('chado.cvterm')->create();
+
+    $value = 'chado_API_test_value';
+
+    // Linker column
+    $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
+    $property = [
+      'type_id' => $term->cvterm_id,
+      'value' => $value,
+    ];
+
+    chado_insert_property($record, $property);
+
+    chado_delete_property($record, $property);
+
+    $result = db_select('chado.'.$prop_table, 'p')
+      ->fields('p')
+      ->condition('p.'.$base_pkey, $base_record->{$base_pkey})
+      ->execute()
+      ->fetchObject();
+
+    $this->assertFalse($result);
+
+
+    $prop = chado_insert_property($record, $property);
+
+    $record = ['prop_id' => $prop[$prop_table.'_id'], 'table' => $base_table];
+    chado_delete_property($record, $property);
+  }
+
+
+  /**
+   * Tests chado_get_record_with_property() with all prop tables.
+   *
+   * Note: chado_get_record_with_property() gets all records in the base table 
+   *   assigned one or more properties.
+   *
+   * @dataProvider propTableProvider
+   *
+   * @group chado
+   * @group api
+   * @group chado-property
+   */
+  function test_chado_get_record_with_property($prop_table, $base_table) {
+
+    $base_record = factory('chado.'.$base_table)->create();
+    $base_pkey = $base_table.'_id';
+    $term = factory('chado.cvterm')->create();
+
+    $value = 'chado_API_test_value';
+
+    // Linker column
+    $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
+    $property = [
+      'type_id' => $term->cvterm_id,
+      'value' => $value,
+    ];
+
+    chado_insert_property($record, $property);
+
+    unset($record['id']);
+    $records = chado_get_record_with_property($record, $property);
+
+    $this->assertNotEmpty($records);
+    $this->assertEquals(1, count($records));
+
+    $base_record = factory('chado.'.$base_table)->create();
+    $record = ['table' => $base_table, 'id' => $base_record->{$base_pkey}];
+    chado_insert_property($record, $property);
+    $records = chado_get_record_with_property($record, $property);
+
+    $this->assertNotEmpty($records);
+    $this->assertEquals(2, count($records));
+  }
+
+  /**
+   * Data Provider: All base tables with associated property tables.
+   *
+   * @return
+   *   An array where each item specifies the property table
+   *   and it's associated base table.
+   */
+  function propTableProvider() {
+    $prop_tables = [];
+
+    $base_tables = chado_get_base_tables();
+    foreach ($base_tables as $base) {
+      $prop = $base . 'prop';
+      if (chado_table_exists($prop) AND Factory::exists('chado.'.$base)) {
+        $prop_tables[] = [$prop, $base];
+      }
+    }
+
+    return $prop_tables;
+  }
+}

+ 14 - 5
tripal_chado/api/tripal_chado.property.api.inc

@@ -24,7 +24,7 @@
  *     -id: The primary key value of the base table. The property will be
  *         associated with the record that matches this id.
  *     -prop_id: The primary key in the [table]prop table.  If this value
- *         is supplied then the 'table' and 'id' keys are not needed.
+ *         is supplied then the 'id' key is not needed.
  * @param $property
  *   An associative array used to specify the property to be selected.  It can
  *   contain the following keys. The keys must be specified to uniquely identify
@@ -98,15 +98,20 @@ function chado_get_property($record, $property) {
 
   // Construct the array of values to be selected.
   $values = array(
-    $fkcol => $base_id,
     'type_id' => $type,
   );
 
+  //Can supply either base_id or prop_id.
+  if ($base_id){
+    $values[$fkcol] = $base_id;
+  }
+
   // If we have the unique property_id make sure to add that to the values.
   if ($prop_id) {
     $property_pkey = $table_desc['primary key'][0];
     $values[$property_pkey] = $prop_id;
   }
+
   $results = chado_generate_var($base_table . 'prop', $values);
   if ($results) {
     $results = chado_expand_var($results, 'field', $base_table . 'prop.value');
@@ -333,9 +338,11 @@ function chado_update_property($record, $property, $options = array()) {
 
   $insert_if_missing = array_key_exists('insert_if_missing', $options) ? $options['insert_if_missing'] : FALSE;
 
+
   // First see if the property is missing (we can't update a missing property.
   $prop = chado_get_property($record, $property);
-  if (!is_array($prop) or count($prop) == 0) {
+
+  if (empty($prop)) {
     if ($insert_if_missing) {
       return chado_insert_property($record, $property);
     }
@@ -344,6 +351,7 @@ function chado_update_property($record, $property, $options = array()) {
     }
   }
 
+
   // Build the values array for checking if the CVterm exists.
   $type = array();
   if ($cv_id) {
@@ -361,6 +369,7 @@ function chado_update_property($record, $property, $options = array()) {
     $type['cvterm_id'] = $type_id;
   }
 
+
   // Make sure the CV term exists.
   $options = array();
   $term = chado_select_record('cvterm', array('cvterm_id'), $type, $options);
@@ -437,7 +446,7 @@ function chado_update_property($record, $property, $options = array()) {
  *     -id: The primary key value of the base table. The property will be
  *         deleted from the record that matches this id.
  *     -prop_id: The primary key in the [table]prop table to be deleted.  If
- *         this value is supplied then the 'table' and 'id' keys are not needed.
+ *         this value is supplied then the  'id' key is not needed.
  * @param $property
  *   An associative array used to specify the property to be updated.  It can
  *   contain the following keys. The keys must be specified to uniquely identify
@@ -518,7 +527,7 @@ function chado_delete_property($record, $property) {
   // Construct the array that will match the exact record to update.
   else {
     $match = array(
-      $fkcol => $record_id,
+      $fkcol => $base_id,
       'type_id' => $type,
     );
   }