factory_seed 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,90 @@
1
+ = Introduction
2
+
3
+ Seed introduces an easy way to instantiate factories created with Factory Girl.
4
+
5
+ = Install
6
+
7
+ sudo gem install factory_seed
8
+
9
+ If you want the source go to http://github.com/fnando/factory_seed
10
+
11
+ = Usage
12
+
13
+ Take the following factories:
14
+
15
+ Factory.define(:user) do |f|
16
+ f.username { Factory.next(:username) }
17
+ f.password "test"
18
+ f.password_confirmation "test"
19
+ end
20
+
21
+ Factory.define(:messages) do |f|
22
+ f.association :user
23
+ f.subject "Some subject"
24
+ end
25
+
26
+ Factory.define(:comments) do |f|
27
+ f.association :user
28
+ f.comment "Some comment"
29
+ end
30
+
31
+ You need to include the +Seed+ module where you want to use it.
32
+
33
+ require "seed"
34
+ include Seed
35
+
36
+ _10_users
37
+ _1_user_with_10_comments
38
+ _1_user_with_10_comments_and_3_messages
39
+
40
+ If you think that underscores are ugly, you can simply use the +seed+ method.
41
+
42
+ seed "10 users"
43
+ seed "1 user with 10 comments"
44
+
45
+ You can set variables by using the records you just created.
46
+
47
+ @user = _1_user
48
+ @user, @comments = _1_user_with_10_comments
49
+ @user, @comments, @messages = _1_user_with_10_comments_and_10_messages
50
+
51
+ You can define the associations by providing a hash.
52
+
53
+ @user = _1_user
54
+ _10_comments(:user => @user)
55
+
56
+ = Using with Test::Unit
57
+
58
+ Just include the +Seed+ module.
59
+
60
+ class SomeTest < Test::Unit::TestCase
61
+ include Seed
62
+ end
63
+
64
+ Or if you want to make Seed available to all test cases
65
+
66
+ class Test::Unit::TestCase
67
+ include Seed
68
+ end
69
+
70
+ = Using with RSpec
71
+
72
+ Include the +Seed+ module by using the +configure+ method.
73
+
74
+ Spec::Runner.configure do |config|
75
+ config.include(Seed)
76
+ end
77
+
78
+ = License
79
+
80
+ (The MIT License)
81
+
82
+ Copyright © 2010:
83
+
84
+ * Nando Vieira (http://simplesideias.com.br)
85
+
86
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
87
+
88
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
89
+
90
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/lib/seed.rb ADDED
@@ -0,0 +1,107 @@
1
+ module Seed
2
+ #++
3
+ # TODO:
4
+ # * Support the construction 10_users_and_10_posts
5
+ #--
6
+
7
+ # The +seed+ method allows you to instantiate factories by parsing a simple text.
8
+ #
9
+ # seed "1 user"
10
+ # seed "2 users"
11
+ # seed "1 admin user"
12
+ # seed "1 user with 10 posts"
13
+ # seed "1 user with 10 posts and 10 comments"
14
+ # seed "10 comments", :user => @user
15
+ #
16
+ # You can set variables by using the returned result.
17
+ #
18
+ # @user = seed("1 user")
19
+ # @users = seed("10 users")
20
+ # @user, @comments = seed("1 user with 10 comments")
21
+ # @user, @comments, @posts = seed("1 user with 10 comments and 10 posts")
22
+ #
23
+ # There's a shortcut for the +seed+ method. All you have to do is replace the spaces by underscores
24
+ # and call it as a method by prefixing it with an underscore. Confuse? Not at all!
25
+ # This
26
+ #
27
+ # seed("1 user")
28
+ #
29
+ # becomes
30
+ #
31
+ # _1_user
32
+ #
33
+ # More samples:
34
+ #
35
+ # @user = _1_user
36
+ # @users = _10_users
37
+ # @comment = _1_comment(:user => @user)
38
+ # @comments = _10_comments
39
+ # @user, @comments, @posts = _1_user_with_10_comments_and_3_posts
40
+ #
41
+ def seed(text, options = {})
42
+ _instantiate_factories(text, options)
43
+ end
44
+
45
+ def _instantiate_factories(method, options = nil, results = [], iteration = 0) # :nodoc:
46
+ iteration += 1
47
+
48
+ # Set default options
49
+ options ||= {}
50
+
51
+ # Retrieve count and main factory name
52
+ _, size, what = *method.match(/^ ?(\d+) (.*?)$/)
53
+
54
+ size = size.to_i
55
+
56
+ associations = nil
57
+ matches = what.match(/^(.*?) with (.*?)$/)
58
+
59
+ if matches
60
+ what = $1
61
+ associations = $2.split(" and ")
62
+ end
63
+
64
+ what.gsub!(/ /, "_")
65
+ what = what.singularize if size > 1
66
+
67
+ records = Array.new(size) { Factory.create(what, options) }
68
+
69
+ if size > 1 && iteration == 1
70
+ results = records
71
+ elsif size > 1
72
+ results << records
73
+ elsif size == 1 && iteration == 1 && !associations
74
+ results = records.first
75
+ else
76
+ results << records.first
77
+ end
78
+
79
+ if associations
80
+ records.each do |record|
81
+ associations.each do |assoc|
82
+ _instantiate_factories(assoc, options.merge(what => record), results, iteration)
83
+ end
84
+ end
85
+ end
86
+
87
+ results
88
+ end
89
+
90
+ def respond_to?(method, include_private = false) # :nodoc:
91
+ if method.to_s =~ /^_\d+_[a-z_]+/
92
+ true
93
+ else
94
+ super
95
+ end
96
+ end
97
+
98
+ def method_missing(method, *args) # :nodoc:
99
+ method_name = method.to_s
100
+
101
+ if method_name =~ /^_(\d+)/
102
+ _instantiate_factories(method_name.gsub(/_/, " "), args.first)
103
+ else
104
+ super
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,8 @@
1
+ module Seed
2
+ module Version # :nodoc: all
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -0,0 +1,25 @@
1
+ Factory.sequence(:username) do |i|
2
+ "johndoe#{i}"
3
+ end
4
+
5
+ Factory.sequence(:title) do |i|
6
+ "Post ##{i}"
7
+ end
8
+
9
+ Factory.define(:user) do |f|
10
+ f.username { Factory.next(:username) }
11
+ end
12
+
13
+ Factory.define(:admin_user, :parent => :user) do |f|
14
+ f.admin true
15
+ end
16
+
17
+ Factory.define(:post) do |f|
18
+ f.title { Factory.next(:title) }
19
+ f.association :user
20
+ end
21
+
22
+ Factory.define(:comment) do |f|
23
+ f.association :user
24
+ f.association :post
25
+ end
@@ -0,0 +1,13 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :posts
3
+ has_many :comments
4
+ end
5
+
6
+ class Post < ActiveRecord::Base
7
+ belongs_to :user
8
+ end
9
+
10
+ class Comment < ActiveRecord::Base
11
+ belongs_to :user
12
+ belongs_to :post
13
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,15 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :users do |t|
3
+ t.string :username
4
+ t.boolean :admin, :default => false, :null => false
5
+ end
6
+
7
+ create_table :posts do |t|
8
+ t.references :user
9
+ t.string :title
10
+ end
11
+
12
+ create_table :comments do |t|
13
+ t.references :post, :user
14
+ end
15
+ end
data/test/seed_test.rb ADDED
@@ -0,0 +1,110 @@
1
+ require "test_helper"
2
+
3
+ class SeedTest < Test::Unit::TestCase
4
+ include Seed
5
+
6
+ def setup
7
+ User.delete_all
8
+ Post.delete_all
9
+ end
10
+
11
+ def test_1_user
12
+ _1_user
13
+
14
+ assert_equal 1, User.count
15
+ end
16
+
17
+ def test_2_users
18
+ _2_users
19
+
20
+ assert_equal 2, User.count
21
+ end
22
+
23
+ def test_1_user_with_1_post
24
+ _1_user_with_1_post
25
+
26
+ assert_equal 1, User.count
27
+ assert_equal 1, Post.count
28
+ assert_equal 1, User.first.posts.count
29
+ end
30
+
31
+ def test_1_user_with_10_posts
32
+ _1_user_with_10_posts
33
+
34
+ assert_equal 1, User.count
35
+ assert_equal 10, Post.count
36
+ assert_equal 10, User.first.posts.count
37
+ end
38
+
39
+ def test_1_user_with_10_posts_and_10_comments
40
+ _1_user_with_10_posts_and_10_comments
41
+
42
+ assert_equal 10, User.first.posts.count
43
+ assert_equal 10, User.first.comments.count
44
+ end
45
+
46
+ def test_1_post_with_options
47
+ @user = Factory(:user)
48
+ _1_post(:user => @user)
49
+
50
+ assert_equal 1, @user.posts.count
51
+ end
52
+
53
+ def test_10_posts_with_options
54
+ @user = Factory(:user)
55
+ _10_posts(:user => @user)
56
+
57
+ assert_equal 10, @user.posts.count
58
+ end
59
+
60
+ def test_10_posts_using_seed_method
61
+ seed "10 users"
62
+ assert_equal 10, User.count
63
+ end
64
+
65
+ def test_return_1_user
66
+ @user = _1_user
67
+ assert_kind_of User, @user
68
+ end
69
+
70
+ def test_return_10_users
71
+ @users = _10_users
72
+ assert_equal 10, @users.count
73
+ end
74
+
75
+ def test_return_1_user_with_10_comments
76
+ @user, @comments = _1_user_with_10_comments
77
+
78
+ assert_kind_of User, @user
79
+ assert_equal 10, @user.comments.count
80
+ end
81
+
82
+ def test_return_1_user_with_10_comments_and_3_posts
83
+ @user, @comments, @posts = _1_user_with_10_comments_and_3_posts
84
+
85
+ assert_kind_of User, @user
86
+ assert_equal 10, @comments.count
87
+ assert_equal 3, @posts.count
88
+ end
89
+
90
+ def test_return_1_user_with_1_comment_and_1_post
91
+ @user, @comment, @post = _1_user_with_1_comment_and_1_post
92
+
93
+ assert_kind_of User, @user
94
+ assert_kind_of Comment, @comment
95
+ assert_kind_of Post, @post
96
+ end
97
+
98
+ def test_respond_to_methods
99
+ assert respond_to?("_10_users")
100
+ assert respond_to?("_1_user_with_10_comments")
101
+ assert respond_to?("_1_user_with_10_comments_and_1_post")
102
+ end
103
+
104
+ def test_1_user_as_admin
105
+ @user = _1_admin_user
106
+
107
+ assert_kind_of User, @user
108
+ assert @user.admin?
109
+ end
110
+ end
@@ -0,0 +1,12 @@
1
+ require "rubygems"
2
+ require "test/unit"
3
+ require "active_record"
4
+ require "active_support"
5
+ require "factory_girl"
6
+
7
+ require "seed"
8
+ require "resources/factories"
9
+ require "resources/models"
10
+
11
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
12
+ load "schema.rb"
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_seed
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Nando Vieira
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-22 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: factory_girl
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ description: Seed introduces an easy way to instantiate factories created with Factory Girl.
33
+ email: fnando.vieira@gmail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - README.rdoc
40
+ files:
41
+ - README.rdoc
42
+ - lib/seed.rb
43
+ - lib/seed/version.rb
44
+ - test/resources/factories.rb
45
+ - test/resources/models.rb
46
+ - test/schema.rb
47
+ - test/seed_test.rb
48
+ - test/test_helper.rb
49
+ has_rdoc: true
50
+ homepage: http://github.com/fnando/factory_seed
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.6
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Seed introduces an easy way to instantiate factories created with Factory Girl.
79
+ test_files:
80
+ - test/resources/factories.rb
81
+ - test/resources/models.rb
82
+ - test/schema.rb
83
+ - test/seed_test.rb
84
+ - test/test_helper.rb