Browse Source

redesign dataprovider

bradford.condon 6 years ago
parent
commit
159329e30b
1 changed files with 83 additions and 57 deletions
  1. 83 57
      tests/tripal_chado/api/ChadoRecordTest.php

+ 83 - 57
tests/tripal_chado/api/ChadoRecordTest.php

@@ -3,6 +3,7 @@
 namespace Tests;
 
 use PHPUnit\Exception;
+use Faker\Factory;
 use StatonLab\TripalTestSuite\DBTransaction;
 use StatonLab\TripalTestSuite\TripalTestCase;
 
@@ -21,13 +22,23 @@ class ChadoRecordTest extends TripalTestCase {
    */
   public function recordProvider() {
     //table, factory or NULL, record_id or NULL
-    $a = factory('chado.feature')->create();
-    $b = factory('chado.organism')->create();
+
+    $faker = \Faker\Factory::create();
+    $analysis = [
+      'name' => $faker->word,
+      'description' => $faker->text,
+      'program' => $faker->word,
+      'programversion' => $faker->word,
+    ];
+    $organism = [
+      'genus' => $faker->word,
+      'species' => $faker->word,
+      'common_name' => $faker->word,
+    ];
 
     return [
-      ['feature', NULL, NULL],
-      ['feature', $a->feature_id, $a],
-      ['organism', $b->organism_id, $b],
+      ['analysis', $analysis],
+      ['organism', $organism],
     ];
   }
 
@@ -39,11 +50,16 @@ class ChadoRecordTest extends TripalTestCase {
    * @group wip
    * @dataProvider recordProvider
    */
-  public function testInitClass($table, $id, $factory) {
-    $record = new \ChadoRecord($table, $id);
+  public function testInitClass($table, $values) {
+    $record = new \ChadoRecord($table);
+    $this->assertNotNull($record);
+    $chado_record = factory('chado.' . $table)->create($values);
+    $record_column = $table.'_id';
+    $record = new \ChadoRecord($table, $chado_record->$record_column);
     $this->assertNotNull($record);
   }
 
+
   /**
    * @group api
    * @group chado
@@ -52,8 +68,8 @@ class ChadoRecordTest extends TripalTestCase {
    * @dataProvider recordProvider
    */
 
-  public function testGetTable($table, $id, $factory) {
-    $record = new \ChadoRecord($table, $id);
+  public function testGetTable($table, $values) {
+    $record = new \ChadoRecord($table);
     $this->assertEquals($table, $record->getTable());
   }
 
@@ -65,15 +81,14 @@ class ChadoRecordTest extends TripalTestCase {
    *
    * @throws \Exception
    */
-  public function testGetID($table, $id, $factory) {
+  public function testGetID($table, $values) {
+    $chado_record = factory('chado.' . $table)->create();
+    $record_column = $table.'_id';
+    $id = $chado_record->$record_column;
+
     $record = new \ChadoRecord($table, $id);
     $returned_id = $record->getID();
-    if ($id) {
-      $this->assertEquals($id, $returned_id);
-    }
-    else {
-      $this->assertNull($returned_id);
-    }
+    $this->assertEquals($id, $returned_id);
   }
 
   /**
@@ -84,21 +99,17 @@ class ChadoRecordTest extends TripalTestCase {
    *
    *
    */
-  public function testGetValues($table, $id, $factory) {
+  public function testGetValues($table, $values) {
+    $chado_record = factory('chado.' . $table)->create($values);
+    $record_column = $table.'_id';
+    $id = $chado_record->$record_column;
     $record = new \ChadoRecord($table, $id);
 
-    if (!$id) {
-      $returned_vals = $record->getValues();
-      $this->assertEmpty($returned_vals);
-    }
-    else {
-      $values = $record->getValues();
-      $this->assertNotEmpty($values);
-      foreach ($factory as $key => $value) {
-        $this->assertArrayHasKey($key, $values);
-        $this->assertEquals($value, $values[$key]);
-      }
-
+    $values = $record->getValues();
+    $this->assertNotEmpty($values);
+    foreach ($values as $key => $value) {
+      $this->assertArrayHasKey($key, $values);
+      $this->assertEquals($value, $values[$key]);
     }
   }
 
@@ -109,18 +120,16 @@ class ChadoRecordTest extends TripalTestCase {
    * @dataProvider recordProvider
    *
    */
-  public function testGetValue($table, $id, $factory) {
-    $record = new \ChadoRecord($table, $id);
+  public function testGetValue($table, $values) {
 
-    if (!$id) {
-      $returned_id = $record->getValue($table . '_id');
-      $this->assertNull($returned_id);
-    }
-    else {
-      foreach ($factory as $key => $value) {
-        $returned_value = $record->getValue($key);
-        $this->assertEquals($value, $returned_value);
-      }
+    $chado_record = factory('chado.' . $table)->create($values);
+    $record_column = $table.'_id';
+    $id = $chado_record->$record_column;
+
+    $record = new \ChadoRecord($table, $id);
+    foreach ($values as $key => $value) {
+      $returned_value = $record->getValue($key);
+      $this->assertEquals($value, $returned_value);
     }
   }
 
@@ -131,35 +140,52 @@ class ChadoRecordTest extends TripalTestCase {
    * @dataProvider recordProvider
    */
 
-  public function testFind($table, $id, $factory) {
+  public function testFind($table, $values) {
 
+    $chado_record = factory('chado.' . $table)->create($values);
+    $record_column = $table.'_id';
+    $id = $chado_record->$record_column;
+    
     $record = new \ChadoRecord($table);
 
-    $values = (array) $factory;
     $record->setValues($values);
-    if ($id) {
-      $found = $record->find();
+    $found = $record->find();
+
+    $this->assertNotNull($found);
+    $this->assertEquals(1, $found);
 
-      $this->assertNotNull($found);
-      $this->assertEquals(1, $found);
-    }
-    else {
-      //There isnt a record in the DB, so find should throw an exception
-      $record->setValue($table . '_id', 'unfindable');
-      $this->expectException(Exception);
-      $found = $record->find();
-    }
   }
 
   /**
-   * This test will not use providers.
+   * Check that the find method throws an exception when it cant find anything.
+   *
+   * @throws \Exception
    */
-  public function testSetValue() {
 
-    $record = new \ChadoRecord('feature');
-    $values = [];
+  public function testFindFail() {
+    $table = 'organism';
+    $record = new \ChadoRecord($table);
+
+    $record->setValue($table . '_id', 'unfindable');
+    $this->expectException(Exception);
+    $found = $record->find();
+  }
+
+  /**
+   * @param $table
+   * @param $values
+   *
+   * @throws \Exception
+   */
+  public function testSetandGetValue($table, $values) {
+
+    $record = new \ChadoRecord($table);
     $record->setValues($values);
+    $vals = $record->getValues();
 
+    foreach ($vals as $val_key => $val) {
+      $this->assertEquals($values[$val_key], $val, "The getValues did not match what was provided for setValues");
+    }
   }
 
 }