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 +4 -4
- data/CHANGELOG.md +8 -1
- data/README.md +33 -42
- data/lib/named_seeds/railtie.rb +24 -42
- data/lib/named_seeds/railties/databases.rake +7 -20
- data/lib/named_seeds/version.rb +1 -1
- data/named_seeds.gemspec +1 -2
- data/test/support/active_record.rb +14 -0
- data/test/test_helper.rb +4 -7
- metadata +9 -29
- data/test/support/schema.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2dd15c25329b3392795fb29d32abaac3a1066b4c
|
4
|
+
data.tar.gz: f8ccffe2fe567906116a45ec4f7c1cf1235b7f62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35a1cb0ee44477f9c052fc64c7808a08a1c2f2c349ec561e41a4c616b6c6175cb52015288d297daecb6b8538c83d937400059fcb84d4fc6587b1ce81f0020768
|
7
|
+
data.tar.gz: 8c8e10690ce1ff0f79dec55754a19b6e1e3212de56854b207dac944a84ac786d0180cbcd155c7ef7cf2284dff90d19d8506028fe388df5094e22108f36a8e756
|
data/CHANGELOG.md
CHANGED
@@ -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.
|
41
|
+
FactoryGirl.find_definitions rescue false
|
30
42
|
|
31
|
-
@bob = FactoryGirl.create :user, :
|
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
|
-
|
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
|
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
|
-
|
53
|
+
```
|
54
|
+
$ bundle
|
55
|
+
$ rake db:create:all
|
56
|
+
$ rake db:setup
|
57
|
+
```
|
44
58
|
|
45
|
-
|
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
|
-
|
51
|
-
|
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.
|
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
|
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 =
|
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
|
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
|
data/lib/named_seeds/railtie.rb
CHANGED
@@ -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 =
|
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', :
|
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
|
23
|
-
if
|
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
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
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
|
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 :
|
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
|
-
|
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
|
-
|
27
|
-
|
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 }
|
data/lib/named_seeds/version.rb
CHANGED
data/named_seeds.gemspec
CHANGED
@@ -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', '
|
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
|
data/test/test_helper.rb
CHANGED
@@ -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
|
-
|
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
|
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-
|
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:
|
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:
|
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.
|
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
|