Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database Seeders vs. Factories #48

Closed
laceysanderson opened this issue Jun 2, 2018 · 6 comments
Closed

Database Seeders vs. Factories #48

laceysanderson opened this issue Jun 2, 2018 · 6 comments

Comments

@laceysanderson
Copy link
Member

laceysanderson commented Jun 2, 2018

What is the difference between Database Seeders and Factories? All I could gleam from the README is that both are re-usable ways to add data for tests and that factories can be used in database seeders.

Which raises my next question, when should I use factories vs. database seeders?

PS. For context, I'm adding some simple tests to Tripal BLAST :-D

@almasaeed2010
Copy link
Contributor

almasaeed2010 commented Jun 2, 2018

Hi!

Here are a few things factories provide:

  • simple per table syntax
  • an easy way to generate multiple records by provide a second argument
  • however, they can’t be used to define complex operations such as using user_save() or entity_save()

Seeders:

  • usually used to run complex operations
  • can be run using the command line using “tripaltest db:seed”
  • the most important feature is that seeders can be used to populate the database with persistent data. So you’d run the seeder once to add the data and be able to use and test the GUI

BTW, both the seeder class and the tripal test case class provide a “$this->publish($chado_table_name, $record_id)” method to convert chado records into entities in case you wanted to run tests on entities

@laceysanderson
Copy link
Member Author

Oh, I think I see! :-) Thanks!

Just to check my understanding:

  • I would create a database seeder for creating fake "BlastDB" nodes used for testing the blast form or job since it should be done with node_save()?
  • I could use a factory for creating fake dbxrefs when testing "BlastDB" node CRUD and for use in the database seeder above?

@almasaeed2010
Copy link
Contributor

Precisely!

@bradfordcondon
Copy link
Member

@almasaeed2010 do seeders do inserts only or updates? I'm thinking about seeders running multiple times and getting db errors because of it.

@laceysanderson i think if you are using seeders in the context of testing, then you'll want to a) run the seeders in the Travis container before running your tests and b) document that you need to run them for tests to pass for contributors. Factories on the other hand are meant to be lightweight, run once per test.

@almasaeed2010
Copy link
Contributor

Seeders don't run automatically. You have to use MySeeder::seed() in the test method (which you can run within a transaction) or by running them through the command line which is not revertible.

@bradfordcondon
Copy link
Member

oh thats neat, so running within test, with transactions, is do-able. So in that case its for "complex operations" as you say. Cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants