├── .gitignore ├── CHANGELOG.md ├── CODE-OF-CONDUCT.md ├── CONTRIBUTING.md ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── factory_girl-seeds.gemspec ├── lib ├── factory_girl-seeds.rb └── factory_girl-seeds │ ├── seeds.rb │ └── version.rb ├── readme_content └── seed.jpg └── spec ├── factory_girl-seeds └── seeds_spec.rb └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 6 | 7 | ## [Archived] 8 | 9 | ## 2.0.0 (2017-11-22) 10 | 11 | Change gem dependency `factory_girl` to `factory_bot` 12 | 13 | ## 1.1.0 (2014-03-13) 14 | 15 | Features: 16 | 17 | - support FG's traits and attributes for seed creation (@jewilmeer) 18 | -------------------------------------------------------------------------------- /CODE-OF-CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | 3 | ### Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ### Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ### Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ### Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ### Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at codeofconduct@evrone.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ### Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thanks for taking the time to contribute! 4 | 5 | The following is a set of guidelines for contributing to this project. These are just guidelines, not rules, so use your best judgement and feel free to propose changes to this document in a pull request. 6 | 7 | ## Reporting issues 8 | 9 | Ensure the bug was not already reported by searching on GitHub under issues. If you're unable to find an open issue addressing the bug, open a new issue. 10 | 11 | Please pay attention to the following points while opening an issue: 12 | * How to reproduce the issue, step-by-step. 13 | * The expected behavior (or what is wrong). 14 | * Screenshots for GUI issues. 15 | * The application version. 16 | * The operating system. 17 | 18 | ## Pull Requests 19 | 20 | Pull Requests are always welcome. 21 | 22 | 1. When you edit the code, please check the formatting of your code before you `git commit`. 23 | 2. Ensure the PR description clearly describes the problem and solution. It should include: 24 | * The operating system on which you tested. 25 | * The relevant issue number, if applicable. 26 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in factory_girl-seeds.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2013-2019 Alexander Balashov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PLEASE NOTE, THIS PROJECT IS NO LONGER BEING MAINTAINED 2 | # FactoryGirl Seeds 3 | 4 | [![Build Status](https://travis-ci.org/evrone/factory_girl-seeds.svg?branch=master)](https://travis-ci.org/evrone/factory_girl-seeds) 5 | [![Code Climate](https://codeclimate.com/github/evrone/factory_girl-seeds.png)](https://codeclimate.com/github/evrone/factory_girl-seeds) 6 | 7 | ![](readme_content/seed.jpg) 8 | 9 | Don't like factory_girl because it is slow? Do you know that creating records in DB through factory_girl can take up to 50% of total spec run time? And even more! 10 | 11 | This tiny gem helps fix that problem by reusing data preloaded before running test suite. 12 | 13 | 14 | Sponsored by Evrone 16 | 17 | 18 | ## Demo 19 | 20 | Without seeds: 21 | 22 | ``` 23 | Finished in 4 minutes 38.7 seconds 24 | 1402 examples, 0 failures, 0 pending 25 | ``` 26 | 27 | With seeds: 28 | 29 | ``` 30 | Finished in 2 minutes 40.6 seconds 31 | 1402 examples, 0 failures, 0 pending 32 | ``` 33 | 34 | ```ruby 35 | >> (2.minutes + 40.6.seconds) / (4.minutes + 38.7.seconds) 36 | => 0.5762468604233943 37 | ``` 38 | 39 | So it is just about 58% of time before seeds optimization :) 40 | 41 | 42 | ## Getting Started 43 | ### Installation 44 | 45 | Add this line to your application's Gemfile: 46 | 47 | ```ruby 48 | group :test do 49 | gem 'factory_girl-seeds' 50 | end 51 | ``` 52 | 53 | ### Usage 54 | 55 | ### 1. Create records before test suite 56 | 57 | ```ruby 58 | FactoryGirl::SeedGenerator.create(:user, name: "Carlos Castaneda") 59 | ``` 60 | 61 | For example if you are using rspec then add this to ```config.before(:suite)```. 62 | 63 | ### 2. Use in factory definitions 64 | 65 | This is the most important step because most of time factory_girl spends on creating associations which in turn also create associations and so on recursively. 66 | 67 | ```ruby 68 | FactoryGirl.define do 69 | factory :post do 70 | title "Demo" 71 | user { seed(:user) } 72 | end 73 | end 74 | ``` 75 | 76 | ### 3. Use in `it` blocks. 77 | 78 | Also if you need standard factory without overriding attributes then do not create records. Just use one from preloaded seeds. 79 | 80 | ```ruby 81 | it "should do something" do 82 | user = FactoryGirl.seed(:user) 83 | 84 | # your code here 85 | end 86 | ``` 87 | 88 | Short DSL also available: 89 | 90 | ```ruby 91 | user = seed(:user) 92 | ``` 93 | 94 | ### 4. Using Factory Girl traits 95 | 96 | You can create models via factories and traits (like `create(:user, admin)`), but you *can not* obtain it with the `seed(:user, :admin)`. To be able to obtain a record it is recommended to define specific factories with a set of traits and unique names just like in the example in [Getting stated](https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#traits) guide: 97 | 98 | ```ruby 99 | factory :user do 100 | name "Friendly User" 101 | login { name } 102 | 103 | trait :male do 104 | name "John Doe" 105 | gender "Male" 106 | end 107 | 108 | trait :female do 109 | name "Jane Doe" 110 | gender "Female" 111 | end 112 | 113 | trait :admin do 114 | admin true 115 | end 116 | 117 | factory :male_admin, traits: [:male, :admin] 118 | factory :female_admin, traits: [:admin, :female] 119 | end 120 | ``` 121 | 122 | When factories declared in this manner, you can obtain a record with `seed(:male_admin)` 123 | 124 | ```FactoryGirl::SeedGenerator.create``` method creates record in DB before transaction begins. 125 | Then ```it``` block starts transaction so when you update record returned by ```FactoryGirl.seed``` 126 | it is wrapped in transaction. This guarantees that every ```it``` block works with clean record. 127 | 128 | ## Contributing 129 | 130 | Please read [Code of Conduct](CODE-OF-CONDUCT.md) and [Contributing Guidelines](CONTRIBUTING.md) for submitting pull requests to us. 131 | 132 | ## Versioning 133 | 134 | We use [SemVer](http://semver.org/) for versioning. For the versions available, 135 | see the [tags on this repository](https://github.com/evrone/factory_girl-seeds/tags). 136 | 137 | ## Changelog 138 | 139 | The changelog is [here](CHANGELOG.md). 140 | 141 | ## Authors 142 | 143 | * [Alexander Balashov](https://github.com/divineforest) - *Initial work* 144 | 145 | See also the list of [contributors](https://github.com/evrone/factory_girl-seeds/contributors) who participated in this project. 146 | 147 | ## License 148 | 149 | This project is licensed under the [MIT License](LICENSE). 150 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require 'rspec/core/rake_task' 3 | 4 | RSpec::Core::RakeTask.new 5 | 6 | task :default do 7 | rspec = Rake::Task[:spec] 8 | rspec.invoke 9 | end 10 | -------------------------------------------------------------------------------- /factory_girl-seeds.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'factory_girl-seeds/version' 5 | 6 | Gem::Specification.new do |gem| 7 | gem.name = "factory_girl-seeds" 8 | gem.version = FactoryGirl::Seeds::VERSION 9 | gem.authors = ["Alexander Balashov"] 10 | gem.email = ["technoforest@gmail.com"] 11 | gem.description = %q{Preseed reusable data for factory girl} 12 | gem.summary = %q{Make your test suite run time upto 2x faster and even more!} 13 | gem.homepage = "https://github.com/evrone/factory_girl-seeds" 14 | 15 | gem.files = `git ls-files`.split($/) 16 | gem.files -= ["seed.jpg"] 17 | 18 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 19 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 20 | gem.require_paths = ["lib"] 21 | 22 | gem.add_dependency "factory_bot" 23 | 24 | gem.add_development_dependency "rake" 25 | gem.add_development_dependency "rspec" 26 | gem.add_development_dependency "activerecord" 27 | gem.add_development_dependency "sqlite3" 28 | end 29 | -------------------------------------------------------------------------------- /lib/factory_girl-seeds.rb: -------------------------------------------------------------------------------- 1 | require "factory_girl-seeds/version" 2 | require "factory_girl-seeds/seeds" 3 | 4 | module FactoryGirl 5 | module Seeds 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /lib/factory_girl-seeds/seeds.rb: -------------------------------------------------------------------------------- 1 | require 'factory_bot' 2 | 3 | module FactoryGirl 4 | class SeedGenerator 5 | @ids = {} 6 | @classes = {} 7 | 8 | def self.create(factory_name, *attributes) 9 | model = FactoryBot.create(factory_name, *attributes) 10 | @ids[factory_name] = model.id 11 | @classes[factory_name] = model.class 12 | 13 | model 14 | end 15 | 16 | def self.[](factory_name) 17 | seed_id = @ids[factory_name] 18 | 19 | if seed_id 20 | seed_class = @classes[factory_name] 21 | seed_class.where(seed_class.primary_key => seed_id).first || create(factory_name) 22 | else 23 | create(factory_name) 24 | end 25 | end 26 | end 27 | end 28 | 29 | module FactoryGirl 30 | module Syntax 31 | module SeedMethods 32 | def seed(factory_name) 33 | if defined?(Rails) && !Rails.env.test? 34 | FactoryBot.create(factory_name) 35 | else 36 | FactoryGirl::SeedGenerator[factory_name] 37 | end 38 | end 39 | end 40 | end 41 | end 42 | 43 | FactoryBot::Syntax::Methods.send(:include, FactoryGirl::Syntax::SeedMethods) 44 | FactoryBot::SyntaxRunner.send(:include, FactoryGirl::Syntax::SeedMethods) 45 | FactoryBot.send(:extend, FactoryGirl::Syntax::SeedMethods) 46 | -------------------------------------------------------------------------------- /lib/factory_girl-seeds/version.rb: -------------------------------------------------------------------------------- 1 | module FactoryGirl 2 | module Seeds 3 | VERSION = "2.0.0" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /readme_content/seed.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evrone/factory_girl-seeds/d029b631d7a078f38b1c2b0e154cabd89efc9524/readme_content/seed.jpg -------------------------------------------------------------------------------- /spec/factory_girl-seeds/seeds_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe FactoryGirl::SeedGenerator do 4 | let(:klass) { FactoryGirl::SeedGenerator } 5 | it do 6 | seeded = seed(:user) 7 | expect(seeded).to eq(seed(:user)) 8 | end 9 | 10 | describe "create" do 11 | it "seeds with custom attributes" do 12 | seed = klass.create(:user, name: 'John') 13 | expect(seed.name).to eql 'John' 14 | end 15 | 16 | it "seeds with traits" do 17 | seed = klass.create(:user, :david) 18 | expect(seed.name).to eql 'David' 19 | end 20 | 21 | it "seeds with attributes and traits" do 22 | seed = klass.create(:user, :david, nickname: 'Goliath') 23 | expect(seed.name).to eql 'David' 24 | expect(seed.nickname).to eql 'Goliath' 25 | end 26 | end 27 | end 28 | 29 | describe FactoryBot do 30 | describe ".seed" do 31 | it do 32 | expect(FactoryBot.seed(:user)).to be 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'factory_bot' 2 | require 'active_record' 3 | require 'factory_girl-seeds' 4 | 5 | ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:') 6 | 7 | ActiveRecord::Migration.verbose = false 8 | ActiveRecord::Schema.define(version: 0) do 9 | create_table "users", force: true do |t| 10 | t.string "name" 11 | t.string "nickname" 12 | end 13 | end 14 | 15 | class User < ActiveRecord::Base;end 16 | 17 | FactoryBot.define do 18 | factory :user do 19 | name "AB" 20 | 21 | trait :david do 22 | name 'David' 23 | end 24 | end 25 | end 26 | 27 | RSpec.configure do |config| 28 | config.include FactoryBot::Syntax::Methods 29 | end 30 | --------------------------------------------------------------------------------