浏览代码

wip start tripal_chado tests and fix some functionality

bradford.condon 6 年之前
父节点
当前提交
0508c6be3d
共有 2 个文件被更改,包括 198 次插入3 次删除
  1. 191 0
      tests/tripal_chado/api/TripalChadoPropertyAPITest.php
  2. 7 3
      tripal_chado/api/tripal_chado.property.api.inc

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

@@ -0,0 +1,191 @@
+<?php
+
+namespace Tests;
+
+use StatonLab\TripalTestSuite\DBTransaction;
+use StatonLab\TripalTestSuite\TripalTestCase;
+
+class TripalChadoPropertyAPITest extends TripalTestCase {
+
+  use DBTransaction;
+
+  /**
+   * @group chado
+   * @group api
+   *
+   */
+  public function test_chado_insert_property() {
+
+    $feature = factory('chado.feature')->create();
+    $term = factory('chado.cvterm')->create();
+
+    $value = 'chado_API_test_value';
+
+    // Linker column
+    $record = ['table' => 'feature', 'id' => $feature->feature_id];
+    $property = [
+      'type_id' => $term->cvterm_id,
+      'value' => $value,
+    ];
+
+    chado_insert_property($record, $property);
+
+    $result = db_select('chado.featureprop', 'f')
+      ->fields('f')
+      ->condition('f.feature_id', $feature->feature_id)
+      ->execute()
+      ->fetchObject();
+
+    $this->assertNotEmpty($result);
+    $this->assertEquals($value, $result->value);
+    $this->assertEquals($term->cvterm_id, $result->type_id);
+    $this->assertEquals('0', $result->rank);
+
+  }
+
+
+  /**
+   * @group chado
+   * @group api
+   * @group wip
+   *
+   */
+  public function test_chado_get_property() {
+
+    $feature = factory('chado.feature')->create();
+    $term = factory('chado.cvterm')->create();
+
+    $value = 'chado_API_test_value';
+
+    // Linker column
+    $record = ['table' => 'feature', 'id' => $feature->feature_id];
+    $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['featureprop_id'], 'table' => 'feature'];
+    $retrieved = chado_get_property($record, $property);
+    $this->assertNotFalse($retrieved);
+    $this->assertEquals($value, $retrieved->value);
+  }
+
+  /**
+   * @group chado
+   * @group api
+   */
+  public function test_chado_update_property() {
+    $feature = factory('chado.feature')->create();
+    $term = factory('chado.cvterm')->create();
+
+    $value = 'chado_API_test_value';
+    $new_value = 'chado_API_new';
+
+    // Linker column
+    $record = ['table' => 'feature', 'id' => $feature->feature_id];
+    $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.featureprop', 'f')
+      ->fields('f')
+      ->condition('f.feature_id', $feature->feature_id)
+      ->execute()
+      ->fetchObject();
+
+    $this->assertNotEmpty($result);
+    $this->assertEquals($new_value, $result->value);
+    $this->assertEquals($term->cvterm_id, $result->type_id);
+    $this->assertEquals('0', $result->rank);
+
+  }
+
+  /**
+   * @group chado
+   * @group api
+   */
+  public function test_chado_delete_property() {
+    $feature = factory('chado.feature')->create();
+    $term = factory('chado.cvterm')->create();
+
+    $value = 'chado_API_test_value';
+
+    // Linker column
+    $record = ['table' => 'feature', 'id' => $feature->feature_id];
+    $property = [
+      'type_id' => $term->cvterm_id,
+      'value' => $value,
+    ];
+
+    chado_insert_property($record, $property);
+
+    chado_delete_property($record, $property);
+
+    $result = db_select('chado.featureprop', 'f')
+      ->fields('f')
+      ->condition('f.feature_id', $feature->feature_id)
+      ->execute()
+      ->fetchObject();
+
+    $this->assertFalse($result);
+
+
+    $prop = chado_insert_property($record, $property);
+
+    $record = ['prop_id' => $prop['featureprop_id'], 'table' => 'feature'];
+    chado_delete_property($record, $property);
+  }
+
+
+  /**
+   * @group wip
+   * @group chado
+   * @group api
+   */
+  function test_chado_get_record_with_property() {
+    //  * Get all records in the base table assigned one or more properties.
+
+    $feature = factory('chado.feature')->create();
+    $term = factory('chado.cvterm')->create();
+
+    $value = 'chado_API_test_value';
+
+    // Linker column
+    $record = ['table' => 'feature', 'id' => $feature->feature_id];
+    $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));
+
+    $feature = factory('chado.feature')->create();
+    $record = ['table' => 'feature', 'id' => $feature->feature_id];
+    chado_insert_property($record, $property);
+    $records = chado_get_record_with_property($record, $property);
+
+    $this->assertNotEmpty($records);
+    $this->assertEquals(2, count($records));
+  }
+
+
+}

+ 7 - 3
tripal_chado/api/tripal_chado.property.api.inc

@@ -333,9 +333,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 +346,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 +364,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 +441,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 +522,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,
     );
   }