Selaa lähdekoodia

Add Instance CRUD tests for tripal_jbrowse_mgmt

Lacey Sanderson 5 vuotta sitten
vanhempi
commit
325fc5c26b

+ 2 - 0
.gitignore

@@ -5,3 +5,5 @@ vendor/
 _build/
 
 .env
+
+tests/_build

+ 10 - 10
composer.lock

@@ -1754,16 +1754,16 @@
         },
         {
             "name": "symfony/console",
-            "version": "v4.3.4",
+            "version": "v4.3.5",
             "source": {
                 "type": "git",
                 "url": "https://github.com/symfony/console.git",
-                "reference": "de63799239b3881b8a08f8481b22348f77ed7b36"
+                "reference": "929ddf360d401b958f611d44e726094ab46a7369"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/symfony/console/zipball/de63799239b3881b8a08f8481b22348f77ed7b36",
-                "reference": "de63799239b3881b8a08f8481b22348f77ed7b36",
+                "url": "https://api.github.com/repos/symfony/console/zipball/929ddf360d401b958f611d44e726094ab46a7369",
+                "reference": "929ddf360d401b958f611d44e726094ab46a7369",
                 "shasum": ""
             },
             "require": {
@@ -1825,7 +1825,7 @@
             ],
             "description": "Symfony Console Component",
             "homepage": "https://symfony.com",
-            "time": "2019-08-26T08:26:39+00:00"
+            "time": "2019-10-07T12:36:49+00:00"
         },
         {
             "name": "symfony/polyfill-ctype",
@@ -2004,16 +2004,16 @@
         },
         {
             "name": "symfony/service-contracts",
-            "version": "v1.1.6",
+            "version": "v1.1.7",
             "source": {
                 "type": "git",
                 "url": "https://github.com/symfony/service-contracts.git",
-                "reference": "ea7263d6b6d5f798b56a45a5b8d686725f2719a3"
+                "reference": "ffcde9615dc5bb4825b9f6aed07716f1f57faae0"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/symfony/service-contracts/zipball/ea7263d6b6d5f798b56a45a5b8d686725f2719a3",
-                "reference": "ea7263d6b6d5f798b56a45a5b8d686725f2719a3",
+                "url": "https://api.github.com/repos/symfony/service-contracts/zipball/ffcde9615dc5bb4825b9f6aed07716f1f57faae0",
+                "reference": "ffcde9615dc5bb4825b9f6aed07716f1f57faae0",
                 "shasum": ""
             },
             "require": {
@@ -2058,7 +2058,7 @@
                 "interoperability",
                 "standards"
             ],
-            "time": "2019-08-20T14:44:19+00:00"
+            "time": "2019-09-17T11:12:18+00:00"
         },
         {
             "name": "theseer/tokenizer",

+ 7 - 0
phpunit.xml

@@ -12,4 +12,11 @@
       <directory suffix="Test.php">tests</directory>
     </testsuite>
   </testsuites>
+  <filter>
+     <whitelist addUncoveredFilesFromWhitelist="true">
+       <file>./tripal_jbrowse_mgmt/tripal_jbrowse_mgmt.module</file>
+       <directory suffix=".inc">./tripal_jbrowse_mgmt</directory>
+       <directory suffix=".php">./tripal_jbrowse_mgmt</directory>
+     </whitelist>
+   </filter>
 </phpunit>

+ 12 - 0
tests/tripal_jbrowse/jbrowseInstanceNodeTest.php

@@ -20,6 +20,10 @@ class jbrowseInstanceNodeTest extends TripalTestCase {
    */
   public function testJbrowseInstanceNodeTypeExists() {
 
+    if (!module_exists('tripal_jbrowse')) {
+      $this->markTestSkipped('No need to test if not enabled.');
+    }
+
     // Get a list of all types available.
     $types = node_type_get_types();
 
@@ -52,6 +56,10 @@ class jbrowseInstanceNodeTest extends TripalTestCase {
   public function testJBrowseInstanceNodeCreate() {
     module_load_include('inc', 'node', 'node.pages');
 
+    if (!module_exists('tripal_jbrowse')) {
+      $this->markTestSkipped('No need to test if not enabled.');
+    }
+
     // Log in the god user.
     global $user;
     $user = user_load(1);
@@ -91,6 +99,10 @@ class jbrowseInstanceNodeTest extends TripalTestCase {
   public function testJBrowseInstanceNodeUpdate() {
     module_load_include('inc', 'node', 'node.pages');
 
+    if (!module_exists('tripal_jbrowse')) {
+      $this->markTestSkipped('No need to test if not enabled.');
+    }
+    
     // Log in the god user.
     global $user;
     $user = user_load(1);

+ 126 - 0
tests/tripal_jbrowse_mgmt/ApiTest.php

@@ -0,0 +1,126 @@
+<?php
+namespace Tests\tripal_jbrowse_mgmt;
+
+use StatonLab\TripalTestSuite\DBTransaction;
+use StatonLab\TripalTestSuite\TripalTestCase;
+use Faker\Factory;
+
+class ApiTest extends TripalTestCase {
+  // Uncomment to auto start and rollback db transactions per test method.
+  use DBTransaction;
+
+  /**
+   * Tests the Settings API.
+   *
+   * Specifically tripal_jbrowse_mgmt_save_settings()
+   * and tripal_jbrowse_mgmt_get_settings().
+   *
+   * @dataProvider settingsProvider
+   */
+  public function testSettings($test_settings) {
+
+    tripal_jbrowse_mgmt_save_settings($test_settings);
+    $retrieved = tripal_jbrowse_mgmt_get_settings();
+
+    // Check that all the expected settings are available.
+    $expected = ['bin_path', 'link', 'data_dir', 'menu_template'];
+    foreach ($expected as $expected_key) {
+      $this->assertArrayHasKey($expected_key, $retrieved,
+        "Retrieved settings do not contain $expected_key");
+
+      // Check that the settings match if provided in test.
+      if (isset($test_settings[$expected_key])) {
+        $this->assertEquals(
+          $test_settings[$expected_key],
+          $retrieved[$expected_key],
+          "The retrieved value for $expected_key does not match what we set."
+        );
+      }
+    }
+  }
+  // Associated Data Provider for testing settings.
+  public function settingsProvider() {
+    $faker = Factory::create();
+    $sets = [];
+
+    // No settings.
+    $sets[] = [[]];
+
+    // Full Fake Settings.
+    $sets[] = [[
+      'bin_path' => 'test/fake/path',
+	    'link' => 'test/fake/path',
+	    'data_dir' => 'test/fake/path',
+	    'menu_template' => [],
+    ]];
+
+    return $sets;
+  }
+
+  /**
+   * Test Instance Create-Retrieve-Update-Delete.
+   *
+   * @dataProvider instanceProvider
+   */
+  public function testInstanceCRUD($testdata) {
+    putenv("TRIPAL_SUPPRESS_ERRORS=TRUE");
+
+    // Check we cannot create a JBrowse instance without an organism.
+    $noOrganism = $testdata;
+    unset($noOrganism['organism_id']);
+    $id = tripal_jbrowse_mgmt_create_instance($noOrganism);
+    $this->assertFalse($id,
+      "Created an instance without an organism_id!?!");
+
+    // Now try to create an instance with all the data.
+    $id = tripal_jbrowse_mgmt_create_instance($testdata);
+    $this->assertNotFalse($id,
+      "Unable to create instance.");
+
+    // Try to retrieve the instance we just created.
+    $retrieved_instance = tripal_jbrowse_mgmt_get_instance($id);
+    $this->assertNotFalse($id, "We did not retrieve an instance?");
+    $this->assertEquals($id, $retrieved_instance->id,
+      "We retrieved a different instance then we asked for?");
+    $this->assertEquals(
+      $testdata['organism_id'], $retrieved_instance->organism_id,
+      "Retreived the same instance but the organism is not correct?");
+
+    // Change the title and test the instance was updated.
+    $new_title = $testdata;
+    $testdata['title'] = 'NEW FAKE TITLE ' . uniqid();
+    $success = tripal_jbrowse_mgmt_update_instance($id, $new_title);
+    $this->assertNotFalse($success, 'Unable to update instance title.');
+    $retrieved_instance = tripal_jbrowse_mgmt_get_instance($id);
+    $this->assertEquals(
+      $new_title['title'], $retrieved_instance->title,
+      "The title was not updated.");
+
+    // Finally delete him!
+    $success = tripal_jbrowse_mgmt_delete_instance($retrieved_instance);
+    $this->assertNotFalse($success, 'Unable to delete this instance.');
+    $one_more_time = tripal_jbrowse_mgmt_get_instance($id);
+    $this->assertFalse($one_more_time);
+
+    putenv("TRIPAL_SUPPRESS_ERRORS");
+  }
+  // Associated Data Provider for testing instances.
+  public function instanceProvider() {
+    $faker = Factory::create();
+    $sets = [];
+
+    $organism = factory('chado.organism')->create();
+
+    // Full Fake Instance Details.
+    $sets[] = [[
+      'organism_id' => $organism->organism_id,
+      'title' => $faker->words(3, TRUE),
+      'description' => $faker->sentence(25, TRUE),
+      'created_at' => $faker->unixTime(),
+      'file' => '/path/to/fake/file',
+    ]];
+
+    return $sets;
+  }
+
+}

+ 43 - 0
tests/tripal_jbrowse_mgmt/ModuleFileTest.php

@@ -0,0 +1,43 @@
+<?php
+namespace Tests\tripal_jbrowse_mgmt;
+
+use StatonLab\TripalTestSuite\DBTransaction;
+use StatonLab\TripalTestSuite\TripalTestCase;
+
+class ModuleFileTest extends TripalTestCase {
+  // Uncomment to auto start and rollback db transactions per test method.
+  // use DBTransaction;
+
+  /**
+   * Tests hook_menu(). Specifically, are all the required keys set.
+   */
+  public function testHookMenu() {
+
+    $menu_items = tripal_jbrowse_mgmt_menu();
+    $this->assertIsArray($menu_items);
+
+    foreach($menu_items as $path => $item) {
+      $this->assertArrayHasKey('title', $item,
+        "$path menu item is missing a title.");
+      $this->assertArrayHasKey('page callback', $item,
+        "$path menu item is missing a page callback.");
+      $this->assertArrayHasKey('access arguments', $item,
+        "$path menu item is missing access arguments.");
+      $this->assertIsArray($item['access arguments'],
+        "$path menu item access arguments must be an array.");
+    }
+  }
+
+  /**
+   * Tests hook_permission(). Specifically, checks format.
+   */
+  public function testHookPerm() {
+    $permissions = tripal_jbrowse_mgmt_permission();
+    $this->assertIsArray($permissions);
+
+    foreach ($permissions as $key => $perm) {
+      $this->assertArrayHasKey('title', $perm,
+        "$key permission is missing a title.");
+    }
+  }
+}

+ 15 - 0
tripal_jbrowse_mgmt/includes/tripal_jbrowse_mgmt.api.inc

@@ -93,6 +93,21 @@ function tripal_jbrowse_mgmt_get_instances($conditions = NULL) {
 function tripal_jbrowse_mgmt_create_instance($data) {
   global $user;
 
+  // Check that required fields are provided.
+  $required = ['organism_id', 'created_at'];
+  foreach ($required as $key) {
+    if (!isset($data[$key])) {
+      tripal_report_error(
+        'tripal_jbrowse_mgmt',
+        TRIPAL_ERROR,
+        'Unable to create instance due to missing :key',
+        [':key' => $key]
+      );
+      return FALSE;
+    }
+  }
+
+  // If they are all present, then insert into the database.
   $instance_id = db_insert('tripal_jbrowse_mgmt_instances')->fields([
     'uid' => $user->uid,
     'organism_id' => $data['organism_id'],