{"action":"opened","issue":{"url":"https://api.github.com/repos/tripal/tripal/issues/609","repository_url":"https://api.github.com/repos/tripal/tripal","labels_url":"https://api.github.com/repos/tripal/tripal/issues/609/labels{/name}","comments_url":"https://api.github.com/repos/tripal/tripal/issues/609/comments","events_url":"https://api.github.com/repos/tripal/tripal/issues/609/events","html_url":"https://github.com/tripal/tripal/issues/609","id":354984423,"node_id":"MDU6SXNzdWUzNTQ5ODQ0MjM=","number":609,"title":"TripalBundle Database Seeder","user":{"login":"laceysanderson","id":1566301,"node_id":"MDQ6VXNlcjE1NjYzMDE=","avatar_url":"https://avatars3.githubusercontent.com/u/1566301?v=4","gravatar_id":"","url":"https://api.github.com/users/laceysanderson","html_url":"https://github.com/laceysanderson","followers_url":"https://api.github.com/users/laceysanderson/followers","following_url":"https://api.github.com/users/laceysanderson/following{/other_user}","gists_url":"https://api.github.com/users/laceysanderson/gists{/gist_id}","starred_url":"https://api.github.com/users/laceysanderson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/laceysanderson/subscriptions","organizations_url":"https://api.github.com/users/laceysanderson/orgs","repos_url":"https://api.github.com/users/laceysanderson/repos","events_url":"https://api.github.com/users/laceysanderson/events{/privacy}","received_events_url":"https://api.github.com/users/laceysanderson/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-08-29T03:44:28Z","updated_at":"2018-08-29T03:44:28Z","closed_at":null,"author_association":"MEMBER","body":"We really need to develop a TripalBundle DatabaseSeeder for testing of entity-based functionality. I began this when working on #550 as it would have been ideal to use Fake bundles when testing all field combinations.\r\n\r\nI actually had a working DatabaseSeeder class written, however, I ran into issues when calling it within a transactional test. My bundle should have been rolled back when the test was complete... However, it was not... but the chado data was which caused my site to become unusable... As I didn't have time to fully explore where things went wrong and needed the select lists immediately, I put my ideal testing environment on hold.\r\n\r\nThis issue is meant as a place to store the code I did write for the day when I can fully test it ;-p\r\n\r\n## ChadoBundleSeeder.php\r\n```php\r\nnamespace Tests\\DatabaseSeeders;\r\n\r\nuse StatonLab\\TripalTestSuite\\Database\\Seeder;\r\nuse Faker\\Factory;\r\n\r\nclass ChadoBundleSeeder extends Seeder\r\n{\r\n\r\n    public $chado_table;\r\n    public $bundle;\r\n    public $term;\r\n    private $field_instances;\r\n    private $chado_records;\r\n\r\n    /**\r\n     * Seeds the database with Tripal Bundles using Chado as the storage backend.\r\n     *\r\n     * @return void\r\n     */\r\n    public function up()\r\n    {\r\n      $faker = \\Faker\\Factory::create();\r\n\r\n      $cvterm = factory('chado.cvterm')->create();\r\n      $cvterm = chado_generate_var('cvterm', array('cvterm_id' => $cvterm->cvterm_id));\r\n\r\n      // @todo make this dynamic.\r\n      $this->chado_table = 'analysis';\r\n\r\n      $bundle_spec = array(\r\n        'label' => $faker->words(2, TRUE),\r\n        'vocabulary' => $cvterm->dbxref_id->db_id->name,\r\n        'accession' => $cvterm->dbxref_id->accession,\r\n        'term_name' => $cvterm->name,\r\n        'storage_args' => array(\r\n          'data_table' => $this->chado_table,\r\n          'type_id' => $cvterm->cvterm_id,\r\n        ),\r\n      );\r\n\r\n      // First create the Tripal Term (Example: Genetic Marker).\r\n      $term = tripal_load_term_entity(array(\r\n        'vocabulary' => $bundle_spec['vocabulary'],\r\n        'accession' => $bundle_spec['accession']\r\n      ));\r\n\r\n      // If we have the term already then we might also have the bundle...\r\n      if ($term) {\r\n        $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));\r\n      }\r\n\r\n      // If not, then create it.\r\n      if (!$bundle) {\r\n        $error = '';\r\n        $bundle = tripal_create_bundle($bundle_spec, $error);\r\n      }\r\n\r\n      $this->term = $term;\r\n      $this->bundle = $bundle;\r\n\r\n    }\r\n\r\n    /**\r\n     * Populates the chado table this bundle is based on with fake data.\r\n     */\r\n    public function populateChadoTable($num_records = 5) {\r\n\r\n      // @todo check if there are already records...\r\n\r\n      // Use a factory to create fake chado records for the base table of this bundle.\r\n      // @todo override the type.\r\n      $this->chado_records = factory('chado.' . $this->chado_table, $num_records)->create();\r\n      return $this->chado_records;\r\n    }\r\n\r\n    /**\r\n     * Returns all field instances for this bundle.\r\n     */\r\n    public function getFieldInstances() {\r\n\r\n      $this->field_instances = field_info_instances('TripalEntity', $this->bundle->name);\r\n      return $this->field_instances;\r\n    }\r\n}\r\n```\r\n\r\n## How to use it\r\n```php\r\n// Creates the fake bundle with it'w own unique fake term.\r\n$bundle_seeder = \\Tests\\DatabaseSeeders\\ChadoBundleSeeder::seed();\r\n\r\n// Retrieves the bundle that was created.\r\n$bundle = $bundle_seeder->bundle;\r\n\r\n// Retrieve all the field instances automagically created for the new bundle.\r\n// This is great if you want to run tests on all fields for a given bundle type\r\n// or if you want to check if your field was added automagically.\r\n$field_instances = $bundle_seeder->getFieldInstances();\r\n\r\n// Add fake records to the chado table the fake bundle stores it's data in.\r\n// For example, if your fake bundle maps to the analysis table then\r\n// this method will create 100 fake entries in the analysis table using a\r\n// TripalTest Factory including following any foreign key relationships.\r\n$records = $bundle_seeder->populateChadoTable(100);\r\n```\r\n\r\n## What is Missing\r\n\r\n- a method to publish entities and return them for testing purposes\r\n- a way to set which chado table you want your bundle based on\r\n- an intuitive way to specify if it should use the type_id to indicate which records belong to the bundle or the prop table (supporting all options in the bundle creation form)\r\n- fine-tune the populateChadoTable method as indicated by @todo comments\r\n- add a method to get the field and instance info for a specific field"}}
{"actor":{"display_login":"laceysanderson"}}