named_seeds 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ken Collins
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # NamedSeeds
2
+
3
+ **Make you tests fast by augmenting them with transactional fixtures powered by your favorite factory library!**
4
+
5
+ We all know that ActiveRecord::Fixtures suck because they are authored in YAML files. But Rails did get something right, 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!
6
+
7
+ The idea is to leverage your tests' existing factories to generate fixtures that will be populated before testing starts and to use a database transaction strategy around each test. In this way you have a populated story that can be accessed via convienient helper methods like `users(:admin)` which inturn yields much faster test runs. Database fixtures, even those seeded by factories, are not a pancea and we highly suggested that you continue to use factories in your tests when it makes sense to do so. For those that think this is mad or have FUD to share, please see my blog articles..
8
+
9
+ * Inprogress...
10
+ * Inprogress...
11
+
12
+
13
+ ## Installation
14
+
15
+ 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.
16
+
17
+ ```ruby
18
+ group :development, :test do
19
+ gem 'named_seeds'
20
+ end
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ NamedSeeds requires that you create a `db/test/seeds.rb` file. The contents of this file can be anything you want. We recommend using some factory library like FactoryGirl or Machinist.
26
+
27
+ ```ruby
28
+ require 'factory_girl'
29
+ require "#{Rails.root}/test/factories"
30
+
31
+ @bob = FactoryGirl.create :user, :id => NamedSeeds.identify(:bob), :email => 'bob@test.com'
32
+ ```
33
+
34
+ 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
+
36
+
37
+ #### Rails
38
+
39
+ 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.
40
+
41
+ ```ruby
42
+ ENV["RAILS_ENV"] = "test"
43
+ require File.expand_path('../../config/environment', __FILE__)
44
+ require 'rails/test_help'
45
+
46
+ class ActiveSupport::TestCase
47
+ named_seeds :users, :posts
48
+ end
49
+ ```
50
+
51
+ Now you can use both the `users` and `posts` helper methods to find any named identity from your seed file.
52
+
53
+ ```ruby
54
+ require 'test_helper'
55
+ class UserTest < ActiveSupport::TestCase
56
+ setup { @user = users(:bob) }
57
+ tests "should work" do
58
+ assert_equal 'bob@test.com', @user.posts
59
+ end
60
+ end
61
+ ```
62
+
63
+
64
+ #### RSpec
65
+
66
+ Coming soon...
67
+
68
+ #### Cucumber
69
+
70
+ Coming soon...
71
+
72
+
73
+ ## Advanced Usage
74
+
75
+ Review how helper methods may map to custom table names. Like Rails did with #fixture_table_names...
76
+
77
+
78
+ ## Configurations
79
+
80
+ NamedSeeds is a `Rails::Railtie` that exposes a few `config` options. So open up the `config/environments/test.rb` and use the `config.named_seeds` options below.
81
+
82
+ * *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.
83
+ * *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.
84
+
85
+ ```ruby
86
+ My::Application.configure do
87
+ config.named_seeds.app_load_seed = true
88
+ config.named_seeds.engines_with_load_seed += [GeoData::Engine, OurLookupTables]
89
+ end
90
+ ```
91
+
92
+ 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.
93
+
94
+ * *orm* - The ORM module to use. Default is `:active_record`.
95
+ * *connection* - The connection name to use. Default is `:test`.
96
+ * *strategy* - Strategy to clean the database with. Default is `:truncation`.
97
+ * *strategy_args* - Args to be passed to the strategy. Default is an empty hash.
98
+
99
+ ```ruby
100
+ My::Application.configure do
101
+ config.named_seeds.db_cleaner.orm = :active_record
102
+ config.named_seeds.db_cleaner.connection = :test
103
+ config.named_seeds.db_cleaner.strategy = :truncation
104
+ config.named_seeds.db_cleaner.strategy_args = {:except => ['geodata', 'lookuptable']}
105
+ end
106
+ ```
107
+
108
+
109
+
110
+ ## Todo
111
+
112
+ Show Rails implementation using ActiveSupport::TestCase or RSpec.
113
+
114
+ Show examples with these 3 factory libraries.
115
+
116
+ * https://github.com/thoughtbot/factory_girl
117
+ * https://github.com/paulelliott/fabrication
118
+ * https://github.com/notahat/machinist
119
+
120
+ Setup a dummy_application in test.
121
+
122
+ How are we different from:
123
+
124
+ * https://github.com/mbleigh/seed-fu
125
+ * https://github.com/sevenwire/bootstrapper/
126
+ * https://github.com/franciscotufro/environmental_seeder
127
+
128
+ Talk about http://railscasts.com/episodes/179-seed-data
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ desc 'Test the NamedSeeds gem.'
6
+ Rake::TestTask.new do |t|
7
+ t.libs = ['lib','test']
8
+ t.test_files = Dir.glob("test/**/*_test.rb").sort
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => [:test]
@@ -0,0 +1,4 @@
1
+ require 'active_support/test_case'
2
+
3
+ ActiveSupport::TestCase.send :include, NamedSeeds::Read
4
+
@@ -0,0 +1,11 @@
1
+ require 'zlib'
2
+
3
+ module NamedSeeds
4
+
5
+ # A direct copy of ActiveRecord::Fixtures.identify.
6
+ # Returns a consistent, platform-independent identifier for +label+ that are positive integers less than 2^32.
7
+ def self.identify(label)
8
+ Zlib.crc32(label.to_s) % (2 ** 30 - 1)
9
+ end
10
+
11
+ end
@@ -0,0 +1,57 @@
1
+ module NamedSeeds
2
+ class Railtie < Rails::Railtie
3
+
4
+ config.named_seeds = ActiveSupport::OrderedOptions.new
5
+ config.named_seeds.app_load_seed = false
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
+
14
+ config.before_initialize do |app|
15
+ Rails.application.paths.add 'db/test/seeds', :with => 'db/test/seeds.rb'
16
+ end
17
+
18
+ rake_tasks do
19
+ load "named_seeds/railties/databases.rake"
20
+ end
21
+
22
+ def load_seed
23
+ setup_test_environment
24
+ clean_test_database
25
+ load_all_seeds
26
+ end
27
+
28
+
29
+ protected
30
+
31
+ def setup_test_environment
32
+ unless Rails.env.test?
33
+ ActiveRecord::Base.clear_all_connections!
34
+ ActiveRecord::Base.configurations.clear
35
+ silence_warnings { Object.const_set :RAILS_ENV, 'test' ; ENV['RAILS_ENV'] = 'test' ; Rails.instance_variable_set :@_env, nil }
36
+ ActiveRecord::Base.configurations = Rails.configuration.database_configuration
37
+ ActiveRecord::Base.establish_connection
38
+ end
39
+ end
40
+
41
+ def clean_test_database
42
+ require 'database_cleaner'
43
+ DatabaseCleaner.logger = Rails.logger
44
+ cleaner_opts = config.named_seeds.db_cleaner
45
+ cleaner = DatabaseCleaner[cleaner_opts.orm, {:connection => cleaner_opts.connection}]
46
+ cleaner.clean_with cleaner_opts.strategy, cleaner_opts.strategy_args
47
+ end
48
+
49
+ def load_all_seeds
50
+ Rails.application.load_seed if config.named_seeds.app_load_seed
51
+ config.named_seeds.engines_with_load_seed.each { |engine| engine.load_seed }
52
+ seed_file = Rails.application.paths["db/test/seeds"].existent.first
53
+ load(seed_file) if seed_file
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,13 @@
1
+ namespace :db do
2
+ namespace :test do
3
+
4
+ desc "Run the seed data from db/test/seeds.rb"
5
+ task :seed => :environment do
6
+ Rake::Task["db:abort_if_pending_migrations"].invoke
7
+ NamedSeeds::Railtie.load_seed
8
+ end
9
+
10
+ end
11
+ end
12
+
13
+ task 'test:prepare' => 'db:test:seed'
@@ -0,0 +1,50 @@
1
+ require 'active_support/concern'
2
+
3
+ module NamedSeeds
4
+ module Read
5
+
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+
10
+ def named_seeds(name, options = {})
11
+ klass = options[:klass] || name.to_s.classify.constantize
12
+ identities = options[:identities]
13
+ case identities
14
+ when Hash
15
+ define_method(name) do |*fixnames|
16
+ objs = fixnames.map do |fixname|
17
+ id = identities[fixname.to_sym]
18
+ find_named_seed name, klass, id, fixname
19
+ end
20
+ fixnames.one? ? objs.first : objs
21
+ end
22
+ else
23
+ define_method(name) do |*fixnames|
24
+ objs = fixnames.map do |fixname|
25
+ id = NamedSeeds.identify(fixname)
26
+ find_named_seed name, klass, id, fixname
27
+ end
28
+ fixnames.one? ? objs.first : objs
29
+ end
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ protected
36
+
37
+ def find_named_seed(name, klass, id, fixname)
38
+ begin
39
+ klass.find(id)
40
+ rescue ActiveRecord::RecordNotFound => e
41
+ nsfinder = :"#{name}_#{fixname}"
42
+ raise e unless respond_to?(nsfinder)
43
+ send(nsfinder)
44
+ end
45
+ end
46
+
47
+
48
+ end
49
+ end
50
+
@@ -0,0 +1,3 @@
1
+ module NamedSeeds
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'rails'
2
+ require 'active_record'
3
+ require 'named_seeds/version'
4
+ require 'named_seeds/identity'
5
+ require 'named_seeds/read'
6
+ require 'named_seeds/railtie'
7
+ require 'named_seeds/active_support'
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "named_seeds/version"
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "named_seeds"
7
+ gem.version = NamedSeeds::VERSION
8
+ gem.authors = ["Ken Collins"]
9
+ gem.email = ["ken@metaskills.net"]
10
+ gem.summary = %q|Replace ActiveRecord::Fixtures With #{your_factory_lib}.|
11
+ gem.description = %q|Make you tests fast by augmenting them with transactional fixtures powered by your favorite factory library!|
12
+ gem.homepage = "http://github.com/metaskills/named_seeds"
13
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ gem.require_paths = ["lib"]
17
+ gem.add_runtime_dependency 'rails', '~> 3.1'
18
+ gem.add_runtime_dependency 'database_cleaner'
19
+ gem.add_development_dependency 'sqlite3', '~> 1.3'
20
+ gem.add_development_dependency 'rake', '~> 0.9.2'
21
+ gem.add_development_dependency 'minitest', '~> 2.8.1'
22
+ end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ class BaseTest < NamedSeeds::Spec
4
+
5
+ describe 'identifiy' do
6
+
7
+ it 'works on strings' do
8
+ NamedSeeds.identify('foo').must_equal NamedSeeds.identify('foo')
9
+ NamedSeeds.identify('foo').wont_equal NamedSeeds.identify('FOO')
10
+ end
11
+
12
+ it 'works on symbols' do
13
+ NamedSeeds.identify(:foo).must_equal NamedSeeds.identify(:foo)
14
+ end
15
+
16
+ it 'identifies consistently' do
17
+ NamedSeeds.identify(:ruby).must_equal 207281424
18
+ NamedSeeds.identify(:sapphire_2).must_equal 1066363776
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,4 @@
1
+
2
+ class User < ActiveRecord::Base
3
+
4
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Base.class_eval do
2
+ silence do
3
+
4
+ connection.create_table :users, :force => true do |t|
5
+ t.string :name, :email
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require "bundler/setup"
3
+ Bundler.require
4
+ require 'named_seeds'
5
+ require 'active_record/base'
6
+ require 'minitest/autorun'
7
+
8
+ ActiveRecord::Base.logger = nil
9
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
10
+
11
+ require 'support/schema'
12
+
13
+ module NamedSeeds
14
+ class Spec < MiniTest::Spec
15
+
16
+
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: named_seeds
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Ken Collins
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-27 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 5
29
+ segments:
30
+ - 3
31
+ - 1
32
+ version: "3.1"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: database_cleaner
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: sqlite3
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ hash: 9
58
+ segments:
59
+ - 1
60
+ - 3
61
+ version: "1.3"
62
+ type: :development
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: rake
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ hash: 63
73
+ segments:
74
+ - 0
75
+ - 9
76
+ - 2
77
+ version: 0.9.2
78
+ type: :development
79
+ version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: minitest
82
+ prerelease: false
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ hash: 45
89
+ segments:
90
+ - 2
91
+ - 8
92
+ - 1
93
+ version: 2.8.1
94
+ type: :development
95
+ version_requirements: *id005
96
+ description: Make you tests fast by augmenting them with transactional fixtures powered by your favorite factory library!
97
+ email:
98
+ - ken@metaskills.net
99
+ executables: []
100
+
101
+ extensions: []
102
+
103
+ extra_rdoc_files: []
104
+
105
+ files:
106
+ - .gitignore
107
+ - Gemfile
108
+ - LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - lib/named_seeds.rb
112
+ - lib/named_seeds/active_support.rb
113
+ - lib/named_seeds/identity.rb
114
+ - lib/named_seeds/railtie.rb
115
+ - lib/named_seeds/railties/databases.rake
116
+ - lib/named_seeds/read.rb
117
+ - lib/named_seeds/version.rb
118
+ - named_seeds.gemspec
119
+ - test/cases/base_test.rb
120
+ - test/support/models.rb
121
+ - test/support/schema.rb
122
+ - test/test_helper.rb
123
+ homepage: http://github.com/metaskills/named_seeds
124
+ licenses: []
125
+
126
+ post_install_message:
127
+ rdoc_options: []
128
+
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ hash: 3
137
+ segments:
138
+ - 0
139
+ version: "0"
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ requirements: []
150
+
151
+ rubyforge_project:
152
+ rubygems_version: 1.8.17
153
+ signing_key:
154
+ specification_version: 3
155
+ summary: "Replace ActiveRecord::Fixtures With #{your_factory_lib}."
156
+ test_files:
157
+ - test/cases/base_test.rb
158
+ - test/support/models.rb
159
+ - test/support/schema.rb
160
+ - test/test_helper.rb