named_seeds 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: faba2937d0e01b1c4fe753d384443646f6a646fa
4
- data.tar.gz: 834e8f40a54ddfd993cd06fa185e19bc0aad1dd4
3
+ metadata.gz: 2dd15c25329b3392795fb29d32abaac3a1066b4c
4
+ data.tar.gz: f8ccffe2fe567906116a45ec4f7c1cf1235b7f62
5
5
  SHA512:
6
- metadata.gz: 05516f577aff9af4ea8fa809410bdd0274096632b9e7a4270acffec2083163fb6789b5abf897b39b3f3968102d96d460a139344c4d4b4331801e547f5cd8b4e8
7
- data.tar.gz: 4053bcc81a480683425c944c71c391a7a005e59f62b717949e28b631df468e84333435fcc748bdccf7ad22a6cfbe58c5cd5862303ebf81e9ec9096f00975dff2
6
+ metadata.gz: 35a1cb0ee44477f9c052fc64c7808a08a1c2f2c349ec561e41a4c616b6c6175cb52015288d297daecb6b8538c83d937400059fcb84d4fc6587b1ce81f0020768
7
+ data.tar.gz: 8c8e10690ce1ff0f79dec55754a19b6e1e3212de56854b207dac944a84ac786d0180cbcd155c7ef7cf2284dff90d19d8506028fe388df5094e22108f36a8e756
@@ -1,4 +1,11 @@
1
1
 
2
+ ### v1.1.0
3
+
4
+ * Many changes to support new Rails 4.2 sync test schema strategy.
5
+ - The `test:prepare` Task Might Be Useless Now? - http://git.io/mu2F2Q
6
+ - Bring back `db:test:prepare` - http://git.io/VKEwhg
7
+
8
+
2
9
  ### v1.0.4
3
10
 
4
11
  * Make `db:development:seed` automatic based on Rails `db:setup` convention.
@@ -22,4 +29,4 @@
22
29
 
23
30
  ### v1.0.0
24
31
 
25
- * Initial release
32
+ * Initial release
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # NamedSeeds
2
2
 
3
+ **Almost Ready for Rails 4.2.0.beta2**
4
+
5
+ * [Various Issues With 4.2.0.beta2 DB Setup/Testing](https://github.com/rails/rails/issues/17170)
6
+ * [The `test:prepare` Task Might Be Useless Now?](https://github.com/rails/rails/issues/17171)
7
+
3
8
  **Make your tests fast by augmenting them with transactional fixtures powered by your favorite factory library!**
4
9
 
5
10
  We all know that ActiveRecord::Fixtures are hard to maintain and are disconnected from the models that save your data. But Rails did get something right with transactional tests and easy helper methods to access fixtures by name. NamedSeeds aims to be a drop-in replacement for Rails fixtures or an enhancement to RSpec and Cucumber while using any object generator of your choice!
@@ -10,6 +15,11 @@ The idea is to leverage your tests' existing factories to generate fixtures that
10
15
  * Inprogress...
11
16
 
12
17
 
18
+ ## Versions
19
+
20
+ The current master branch is for Rails v4.2.0 and up and follows v1.1 major version. Please use our `1-0-stable` branch for Rails v3.1 to v4.1.
21
+
22
+
13
23
  ## Installation
14
24
 
15
25
  Add the named_seeds gem to your Rails' Gemfile in both the development and test group as shown below. This is needed as the NamedSeeds gem exposes rake tasks needed in both environments.
@@ -20,47 +30,49 @@ group :development, :test do
20
30
  end
21
31
  ```
22
32
 
33
+ That's it! The NamedSeeds gem will hook into the Rails test cycle to make sure your factoried fixtures (and any engine seeds) are populated after your test schema is created and before your test suite runs.
34
+
23
35
  ## Usage
24
36
 
25
37
  NamedSeeds requires that you create a `db/test/seeds.rb` file. The contents of this file can be anything you want. We recommend using a factory library like [FactoryGirl](https://github.com/thoughtbot/factory_girl) or [Machinist](https://github.com/notahat/machinist).
26
38
 
27
39
  ```ruby
28
40
  require 'factory_girl'
29
- FactoryGirl.reload
41
+ FactoryGirl.find_definitions rescue false
30
42
 
31
- @bob = FactoryGirl.create :user, :id => NamedSeeds.identify(:bob), :email => 'bob@test.com'
43
+ @bob = FactoryGirl.create :user, id: NamedSeeds.identify(:bob), email: 'bob@test.com'
32
44
  ```
33
45
 
34
46
  Use the `NamedSeeds.identify` method to give a name to the identity used for this record. You will be able to find this record using that name later on.
35
47
 
36
48
 
37
- ### Rake Files
38
-
39
- **Using these tasks manually should not be needed as both are hooked into the proper `test:prepare` and `db:setup` process for you.**
49
+ #### Integration Notes
40
50
 
41
- NamedSeeds includes two rake tasks. The `db:test:seeds` is the one that does all the work and is automatically called after Rails' `test:prepare` for you. So running your rake test tasks will create your test database and seed it for you automatically before your tests run. Remember, ActiveRecords `db:test:prepare` is not a proper hook, read [my comment](https://github.com/rspec/rspec-rails/issues/663#issuecomment-11831559) on this rspec issue for more details.
51
+ The NamedSeeds gem will hook into ActiveRecord's `db:setup` task. This means that new developers can checkout your code and run the normal Rails setup process and see a fully populated development database that has the same seed/fixtures story used by your tests. For example:
42
52
 
43
- The other task is `db:development:seed`. This task invokes the normal Rails `db:seed` task, then loads the db/test/seeds.rb file while still in development mode. We automatically call this task after `db:setup` for you. This task provides a way for a developer to populate their development database with the same fixture story used for testing. This makes it easy for developers to learn your application as the test story is a 1 to 1 mapping of data in local development.
53
+ ```
54
+ $ bundle
55
+ $ rake db:create:all
56
+ $ rake db:setup
57
+ ```
44
58
 
45
- ```shell
46
- $ rake db:test:seed # Run the seed data from db/test/seeds.rb and
47
- # optionally your Rails db/seeds.rb if you have
48
- # configured `app_load_seed` below.
59
+ If you need to force a reload of your development environment's data, just use Rails conventions. For example, the `db:reset` task calls setup, so NamedSeeds will hook in at the right place.
49
60
 
50
- $ rake db:development:seed # Runs the normal Rails `db:seed` task then
51
- # loads the db/test/seeds.rb file.
61
+ ```
62
+ $ rake db:reset
52
63
  ```
53
64
 
65
+ Likewise, if you wanted to reset your test database and pre-populate it with named seeds/fixtures you can run the following:
66
+
67
+ ```
68
+ $ spring stop && rake db:test:prepare
69
+ ```
54
70
 
55
71
  #### Rails
56
72
 
57
73
  By default, Rails' ActiveSupport::TestCase has set the `use_transactional_fixtures` to true. So all you need to do is declare which tables have NamedSeeds keys.
58
74
 
59
75
  ```ruby
60
- ENV["RAILS_ENV"] = "test"
61
- require File.expand_path('../../config/environment', __FILE__)
62
- require 'rails/test_help'
63
-
64
76
  class ActiveSupport::TestCase
65
77
  named_seeds :users, :posts
66
78
  end
@@ -78,7 +90,6 @@ class UserTest < ActiveSupport::TestCase
78
90
  end
79
91
  ```
80
92
 
81
-
82
93
  #### RSpec
83
94
 
84
95
  Coming soon...
@@ -88,42 +99,22 @@ Coming soon...
88
99
  Coming soon...
89
100
 
90
101
 
91
- ## Advanced Usage
92
-
93
- Review how helper methods may map to custom table names. Like Rails did with #fixture_table_names...
94
-
95
102
 
96
103
  ## Configurations
97
104
 
98
- NamedSeeds is a `Rails::Railtie` that exposes a few `config` options. So open up the `config/environments/development.rb` **(yes in development.rb)** and use the `config.named_seeds` options below. NOTE: I have found that sometimes I need to add some configurations to `config/environments/test.rb` too. Mainly when using [spring](https://github.com/jonleighton/spring). So adding `config.named_seeds.app_load_seed = true` is what I needed.
99
-
105
+ NamedSeeds is a `Rails::Railtie` that exposes a few `config` options. So open up the `config/environments/development.rb` **(yes in development.rb)** and use the `config.named_seeds` options below. NOTE: I have found that sometimes I need to add some configurations to `config/environments/test.rb` too.
100
106
 
101
- * *app_load_seed* - Load your Rails application's db/seeds.rb file into the test database. This is done before db/test/seeds.rb is loaded. Default is false.
107
+ * *app_load_seed* - Load your Rails application's db/seeds.rb file into the test database. This is done before db/test/seeds.rb is loaded. Default is `true` but may people use this file incorrectly vs standard lookup tables.
102
108
  * *engines_with_load_seed* - Some Rails engines provide a load seed hook. If you want NamedSeed to call the engine's seed method into your tests database, push the engine constant to this array. Any object responding to `load_seed` sould work here too. Default is an empty array.
103
109
 
104
110
  ```ruby
105
111
  My::Application.configure do
106
- config.named_seeds.app_load_seed = true
112
+ config.named_seeds.app_load_seed = false
107
113
  config.named_seeds.engines_with_load_seed += [GeoData::Engine, OurLookupTables]
108
114
  end
109
115
  ```
110
116
 
111
- NamedSeeds uses DatabaseCleaner to clean the database before seeding it. Use the `config.named_seeds.db_cleaner` options below to configure its behavior. Please see the DatabaseCleaner documentation for full details.
112
-
113
- * *orm* - The ORM module to use. Default is `:active_record`.
114
- * *connection* - The connection name to use. Default is `:test`.
115
- * *strategy* - Strategy to clean the database with. Default is `:truncation`.
116
- * *strategy_args* - Args to be passed to the strategy. Default is an empty hash.
117
-
118
- ```ruby
119
- My::Application.configure do
120
- config.named_seeds.db_cleaner.orm = :active_record
121
- config.named_seeds.db_cleaner.connection = :test
122
- config.named_seeds.db_cleaner.strategy = :truncation
123
- config.named_seeds.db_cleaner.strategy_args = {:except => ['geodata', 'lookuptable']}
124
- end
125
- ```
126
-
117
+ NamedSeeds relies on ActiveRecord's `ActiveRecord::Migration.maintain_test_schema!` setting. We do not want to get into the business of coupling too much with the internals of Rails and hance assume this is true.
127
118
 
128
119
 
129
120
  ## Todo
@@ -1,62 +1,44 @@
1
1
  module NamedSeeds
2
2
  class Railtie < Rails::Railtie
3
-
3
+
4
4
  config.named_seeds = ActiveSupport::OrderedOptions.new
5
- config.named_seeds.app_load_seed = false
5
+ config.named_seeds.app_load_seed = true
6
6
  config.named_seeds.engines_with_load_seed = []
7
-
8
- config.named_seeds.db_cleaner = ActiveSupport::OrderedOptions.new
9
- config.named_seeds.db_cleaner.orm = :active_record
10
- config.named_seeds.db_cleaner.connection = :test
11
- config.named_seeds.db_cleaner.strategy = :truncation
12
- config.named_seeds.db_cleaner.strategy_args = {}
13
7
 
14
8
  config.before_initialize do |app|
15
- Rails.application.paths.add 'db/test/seeds', :with => 'db/test/seeds.rb'
9
+ Rails.application.paths.add 'db/test/seeds', with: 'db/test/seeds.rb'
16
10
  end
17
11
 
18
12
  rake_tasks do
19
13
  load "named_seeds/railties/databases.rake"
20
14
  end
21
-
22
- def load_seed
23
- if seed_file
24
- setup_test_environment
25
- clean_test_database
26
- load_all_seeds
27
- end
28
- end
29
-
30
-
31
- protected
32
-
33
- def setup_test_environment
34
- unless Rails.env.test?
35
- ActiveRecord::Base.clear_all_connections!
36
- ActiveRecord::Base.configurations.clear
37
- silence_warnings { Object.const_set :RAILS_ENV, 'test' ; ENV['RAILS_ENV'] = 'test' ; Rails.instance_variable_set :@_env, nil }
38
- ActiveRecord::Base.configurations = Rails.configuration.database_configuration
39
- ActiveRecord::Base.establish_connection
40
- end
15
+
16
+ def setup
17
+ load test_seed_file if test_seed_file
41
18
  end
42
-
43
- def clean_test_database
44
- require 'database_cleaner'
45
- DatabaseCleaner.logger = Rails.logger
46
- cleaner_opts = config.named_seeds.db_cleaner
47
- cleaner = DatabaseCleaner[cleaner_opts.orm, {:connection => cleaner_opts.connection}]
48
- cleaner.clean_with cleaner_opts.strategy, cleaner_opts.strategy_args
19
+
20
+ def prepare
21
+ return unless test_seed_file
22
+ load_all_seeds
23
+ setup
49
24
  end
50
-
25
+
26
+
27
+ protected
28
+
51
29
  def load_all_seeds
52
- Rails.application.load_seed if config.named_seeds.app_load_seed
30
+ ActiveRecord::Tasks::DatabaseTasks.load_seed if config.named_seeds.app_load_seed
53
31
  config.named_seeds.engines_with_load_seed.each { |engine| engine.load_seed }
54
- load seed_file
55
32
  end
56
-
57
- def seed_file
33
+
34
+ def test_seed_file
58
35
  Rails.application.paths["db/test/seeds"].existent.first
59
36
  end
60
-
37
+
61
38
  end
39
+
40
+ def self.prepare
41
+ NamedSeeds::Railtie.prepare
42
+ end
43
+
62
44
  end
@@ -1,27 +1,14 @@
1
- namespace :db do
2
-
3
- namespace :development do
4
-
5
- desc "Run all seeds for development"
6
- task :seed => :environment do
7
- Rake::Task["db:abort_if_pending_migrations"].invoke
8
- Rake::Task["db:seed"].invoke
9
- load NamedSeeds::Railtie.send(:seed_file)
10
- end
1
+ namespace :named_seeds do
11
2
 
3
+ task :setup => :environment do
4
+ NamedSeeds::Railtie.setup
12
5
  end
13
6
 
14
- namespace :test do
15
-
16
- desc "Run the seed data from db/test/seeds.rb"
17
- task :seed => :environment do
18
- Rake::Task["db:abort_if_pending_migrations"].invoke
19
- NamedSeeds::Railtie.load_seed
20
- end
21
-
7
+ task :prepare do
8
+ NamedSeeds::Railtie.prepare
22
9
  end
23
10
 
24
11
  end
25
12
 
26
- task 'db:setup' => 'db:development:seed'
27
- task 'test:prepare' => 'db:test:seed'
13
+ Rake::Task['db:setup'].enhance { Rake::Task['named_seeds:setup'].invoke }
14
+ Rake::Task['db:test:prepare'].enhance { Rake::Task['named_seeds:prepare'].invoke }
@@ -1,3 +1,3 @@
1
1
  module NamedSeeds
2
- VERSION = "1.0.5"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -14,8 +14,7 @@ Gem::Specification.new do |gem|
14
14
  gem.files = `git ls-files`.split("\n")
15
15
  gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
16
  gem.require_paths = ["lib"]
17
- gem.add_runtime_dependency 'rails', '>= 3.1', '< 4.2'
18
- gem.add_runtime_dependency 'database_cleaner'
17
+ gem.add_runtime_dependency 'rails', '~> 4.2.0.beta2'
19
18
  gem.add_development_dependency 'sqlite3'
20
19
  gem.add_development_dependency 'rake'
21
20
  gem.add_development_dependency 'minitest'
@@ -0,0 +1,14 @@
1
+ require 'active_record/base'
2
+
3
+ ActiveRecord::Base.logger = nil
4
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
5
+
6
+ ActiveRecord::Base.class_eval do
7
+ silence(:stdout) do
8
+
9
+ connection.create_table :users, force: true do |t|
10
+ t.string :name, :email
11
+ end
12
+
13
+ end
14
+ end
@@ -2,17 +2,14 @@ require 'rubygems'
2
2
  require "bundler/setup"
3
3
  Bundler.require
4
4
  require 'named_seeds'
5
- require 'active_record/base'
6
5
  require 'minitest/autorun'
6
+ require 'support/active_record'
7
7
 
8
- ActiveRecord::Base.logger = nil
9
- ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
10
-
11
- require 'support/schema'
8
+ ActiveSupport.test_order = :random
12
9
 
13
10
  module NamedSeeds
14
11
  class Spec < MiniTest::Spec
15
-
16
-
12
+
13
+
17
14
  end
18
15
  end
metadata CHANGED
@@ -1,49 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: named_seeds
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Collins
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-03 00:00:00.000000000 Z
11
+ date: 2014-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '3.1'
20
- - - "<"
17
+ - - "~>"
21
18
  - !ruby/object:Gem::Version
22
- version: '4.2'
19
+ version: 4.2.0.beta2
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- version: '3.1'
30
- - - "<"
31
- - !ruby/object:Gem::Version
32
- version: '4.2'
33
- - !ruby/object:Gem::Dependency
34
- name: database_cleaner
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
24
+ - - "~>"
38
25
  - !ruby/object:Gem::Version
39
- version: '0'
40
- type: :runtime
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: '0'
26
+ version: 4.2.0.beta2
47
27
  - !ruby/object:Gem::Dependency
48
28
  name: sqlite3
49
29
  requirement: !ruby/object:Gem::Requirement
@@ -109,8 +89,8 @@ files:
109
89
  - lib/named_seeds/version.rb
110
90
  - named_seeds.gemspec
111
91
  - test/cases/base_test.rb
92
+ - test/support/active_record.rb
112
93
  - test/support/models.rb
113
- - test/support/schema.rb
114
94
  - test/test_helper.rb
115
95
  homepage: http://github.com/metaskills/named_seeds
116
96
  licenses: []
@@ -131,12 +111,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
111
  version: '0'
132
112
  requirements: []
133
113
  rubyforge_project:
134
- rubygems_version: 2.3.0
114
+ rubygems_version: 2.4.2
135
115
  signing_key:
136
116
  specification_version: 4
137
117
  summary: 'Replace ActiveRecord::Fixtures With #{your_factory_lib}.'
138
118
  test_files:
139
119
  - test/cases/base_test.rb
120
+ - test/support/active_record.rb
140
121
  - test/support/models.rb
141
- - test/support/schema.rb
142
122
  - test/test_helper.rb
@@ -1,9 +0,0 @@
1
- ActiveRecord::Base.class_eval do
2
- silence(:stdout) do
3
-
4
- connection.create_table :users, :force => true do |t|
5
- t.string :name, :email
6
- end
7
-
8
- end
9
- end