fabricators 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +134 -0
  4. data/Rakefile +32 -0
  5. data/lib/fabricators.rb +41 -0
  6. data/lib/fabricators/callbacks.rb +23 -0
  7. data/lib/fabricators/definitions.rb +58 -0
  8. data/lib/fabricators/fabricator.rb +102 -0
  9. data/lib/fabricators/generator.rb +19 -0
  10. data/lib/fabricators/methods.rb +11 -0
  11. data/lib/fabricators/proxy.rb +45 -0
  12. data/lib/fabricators/railtie.rb +14 -0
  13. data/lib/fabricators/reader.rb +18 -0
  14. data/lib/fabricators/version.rb +5 -0
  15. data/lib/generators/fabricators/model/model_generator.rb +15 -0
  16. data/lib/generators/fabricators/model/templates/fabricator.rb +4 -0
  17. data/test/aliases_test.rb +25 -0
  18. data/test/associations_test.rb +50 -0
  19. data/test/callbacks_test.rb +36 -0
  20. data/test/dependent_test.rb +26 -0
  21. data/test/dummy/README.rdoc +28 -0
  22. data/test/dummy/Rakefile +6 -0
  23. data/test/dummy/app/assets/javascripts/application.js +13 -0
  24. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  25. data/test/dummy/app/controllers/application_controller.rb +5 -0
  26. data/test/dummy/app/helpers/application_helper.rb +2 -0
  27. data/test/dummy/app/models/group.rb +2 -0
  28. data/test/dummy/app/models/post.rb +3 -0
  29. data/test/dummy/app/models/user.rb +3 -0
  30. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  31. data/test/dummy/bin/bundle +3 -0
  32. data/test/dummy/bin/rails +4 -0
  33. data/test/dummy/bin/rake +4 -0
  34. data/test/dummy/config.ru +4 -0
  35. data/test/dummy/config/application.rb +22 -0
  36. data/test/dummy/config/boot.rb +5 -0
  37. data/test/dummy/config/database.yml +25 -0
  38. data/test/dummy/config/environment.rb +5 -0
  39. data/test/dummy/config/environments/development.rb +37 -0
  40. data/test/dummy/config/environments/production.rb +83 -0
  41. data/test/dummy/config/environments/test.rb +39 -0
  42. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  44. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  45. data/test/dummy/config/initializers/inflections.rb +16 -0
  46. data/test/dummy/config/initializers/mime_types.rb +4 -0
  47. data/test/dummy/config/initializers/session_store.rb +3 -0
  48. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/test/dummy/config/locales/en.yml +23 -0
  50. data/test/dummy/config/routes.rb +56 -0
  51. data/test/dummy/config/secrets.yml +22 -0
  52. data/test/dummy/db/migrate/20140613221835_create_users.rb +10 -0
  53. data/test/dummy/db/migrate/20140615152257_add_age_to_users.rb +5 -0
  54. data/test/dummy/db/migrate/20140615175509_add_phone_to_users.rb +5 -0
  55. data/test/dummy/db/migrate/20140615180938_create_groups.rb +9 -0
  56. data/test/dummy/db/migrate/20140615180954_create_posts.rb +9 -0
  57. data/test/dummy/db/migrate/20140615181030_add_group_id_to_users.rb +5 -0
  58. data/test/dummy/db/migrate/20140615181051_add_user_id_to_posts.rb +5 -0
  59. data/test/dummy/db/schema.rb +39 -0
  60. data/test/dummy/log/development.log +0 -0
  61. data/test/dummy/log/test.log +14513 -0
  62. data/test/dummy/public/404.html +67 -0
  63. data/test/dummy/public/422.html +67 -0
  64. data/test/dummy/public/500.html +66 -0
  65. data/test/dummy/public/favicon.ico +0 -0
  66. data/test/dummy/test/fabricators/people.rb +4 -0
  67. data/test/dummy/test/fabricators/users.rb +5 -0
  68. data/test/fabricators_test.rb +33 -0
  69. data/test/fixtures_test.rb +13 -0
  70. data/test/generators_test.rb +36 -0
  71. data/test/inheritance_test.rb +49 -0
  72. data/test/lists_test.rb +33 -0
  73. data/test/load_test.rb +13 -0
  74. data/test/merges_test.rb +31 -0
  75. data/test/test_helper.rb +27 -0
  76. metadata +211 -0
@@ -0,0 +1,11 @@
1
+ module Fabricators
2
+ module Methods
3
+
4
+ %w(build create attributes_for).each do |method|
5
+ define_method method do |name, *args|
6
+ Fabricators.definitions.find(name, :fabricator).send(method, *args)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,45 @@
1
+ module Fabricators
2
+ class Proxy
3
+ include Callbacks
4
+
5
+ def initialize(fabricator, &block)
6
+ @fabricator = fabricator
7
+ @attributes = []
8
+ instance_eval &block
9
+ end
10
+
11
+ def attributes
12
+ {}.tap do |hash|
13
+ if @fabricator.parent
14
+ hash.merge! @fabricator.parent.proxy.attributes
15
+ end
16
+ @attributes.each do |name|
17
+ hash[name] = send(name)
18
+ end
19
+ end
20
+ end
21
+
22
+ def method_missing(name, *args, &block)
23
+ unless name == :fabricator
24
+ options = args.extract_options!
25
+ strategy = options.delete(:strategy) || :build
26
+ if block_given?
27
+ logic = block
28
+ elsif fabricator = Fabricators.definitions.find(name, :fabricator) rescue nil
29
+ logic = -> { fabricator.send(strategy, options) }
30
+ elsif fabricator = Fabricators.definitions.find(name.to_s.singularize.to_sym, :fabricator) rescue nil
31
+ logic = -> { fabricator.send(strategy, (args.first || 1), options) }
32
+ elsif generator = Fabricators.definitions.find(name, :generator) rescue nil
33
+ logic = -> { generator.generate }
34
+ elsif args.any?
35
+ logic = -> { args.first }
36
+ end
37
+ if defined? logic
38
+ @attributes.send (block_given? ? :push : :unshift), name
39
+ class_eval { define_method(name, logic) }
40
+ end
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,14 @@
1
+ module Fabricators
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer 'fabricators' do
5
+ test_framework = config.app_generators.options[:rails][:test_framework]
6
+ config.app_generators.test_framework test_framework, fixture: false, fixture_replacement: :fabricators
7
+ end
8
+
9
+ config.after_initialize do
10
+ Fabricators.populate
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module Fabricators
2
+ class Reader
3
+
4
+ def initialize(name, options, &block)
5
+ @name = name
6
+ @options = options
7
+ instance_eval &block
8
+ end
9
+
10
+ def fabricator(name, options={}, &block)
11
+ Fabricators.definitions.fabricator name, @options.merge(options.merge(parent: @name)), &block
12
+ end
13
+
14
+ def method_missing(name, *args, &block)
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module Fabricators
2
+
3
+ VERSION = '0.0.1'
4
+
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/generators/named_base'
2
+
3
+ module Fabricators
4
+ module Generators
5
+ class ModelGenerator < Rails::Generators::NamedBase
6
+
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def create_fixture_file
10
+ template 'fabricator.rb', Fabricators.path.join("#{table_name}.rb")
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ Fabricators.define do
2
+ fabricator :<%= singular_table_name %> do
3
+ end
4
+ end
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ class AliasesTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ Fabricators.define do
7
+ generator :mobile, 0, aliases: :phone
8
+ fabricator :user, aliases: [:owner, :author] do
9
+ name 'name'
10
+ end
11
+ end
12
+ end
13
+
14
+ test "generator aliases" do
15
+ assert_kind_of Fabricators::Generator, Fabricators.definitions.find(:mobile, :generator)
16
+ assert_kind_of Fabricators::Generator, Fabricators.definitions.find(:phone, :generator)
17
+ end
18
+
19
+ test "fabricator aliases" do
20
+ assert_kind_of Fabricators::Fabricator, Fabricators.definitions.find(:user, :fabricator)
21
+ assert_kind_of Fabricators::Fabricator, Fabricators.definitions.find(:owner, :fabricator)
22
+ assert_kind_of Fabricators::Fabricator, Fabricators.definitions.find(:author, :fabricator)
23
+ end
24
+
25
+ end
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+
3
+ class AssociationsTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ Fabricators.define do
7
+ fabricator :user do
8
+ fabricator :user_with_built_posts do
9
+ posts
10
+ end
11
+ fabricator :user_with_created_posts do
12
+ posts 2, strategy: :create
13
+ end
14
+ end
15
+ fabricator :post do
16
+ fabricator :post_with_built_user do
17
+ user
18
+ end
19
+ fabricator :post_with_created_user do
20
+ user strategy: :create
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ test "belongs to association" do
27
+ user = build(:post_with_built_user).user
28
+ assert_kind_of User, user
29
+ assert user.new_record?
30
+
31
+ user = build(:post_with_created_user).user
32
+ assert_kind_of User, user
33
+ assert user.persisted?
34
+ end
35
+
36
+ test "has many association" do
37
+ posts = build(:user_with_built_posts).posts
38
+ assert_equal 1, posts.size
39
+ post = posts.first
40
+ assert_kind_of Post, post
41
+ assert post.new_record?
42
+
43
+ posts = build(:user_with_created_posts).posts
44
+ assert_equal 2, posts.size
45
+ post = posts.first
46
+ assert_kind_of Post, post
47
+ assert post.persisted?
48
+ end
49
+
50
+ end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ class CallbacksTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ Fabricators.define do
7
+ before(:build) { |u| u.email = 'build@example.com' }
8
+ after(:build) { |u| u.phone = 1 }
9
+ before(:create) { |u| u.email = 'create@example.com' }
10
+ after(:create) { |u| u.phone = 2 }
11
+ fabricator :user do
12
+ before(:build) { |u| u.name = 'build' }
13
+ after(:build) { |u| u.age = 1 }
14
+ before(:create) { |u| u.name = 'create' }
15
+ after(:create) { |u| u.age = 2 }
16
+ end
17
+ end
18
+ end
19
+
20
+ test "build callbacks" do
21
+ user = build(:user)
22
+ assert_equal 'build@example.com', user.email
23
+ assert_equal 1, user.phone
24
+ assert_equal 'build', user.name
25
+ assert_equal 1, user.age
26
+ end
27
+
28
+ test "create callbacks" do
29
+ user = create(:user)
30
+ assert_equal 'create@example.com', user.email
31
+ assert_equal 2, user.phone
32
+ assert_equal 'create', user.name
33
+ assert_equal 2, user.age
34
+ end
35
+
36
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class DependentTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ Fabricators.define do
7
+ fabricator :user do
8
+ name 'name'
9
+ email { "#{name}@example.com" }
10
+ end
11
+ end
12
+ end
13
+
14
+ test "return attributes" do
15
+ assert_equal 'name@example.com', attributes_for(:user)[:email]
16
+ end
17
+
18
+ test "build instance" do
19
+ assert_equal 'name@example.com', build(:user).email
20
+ end
21
+
22
+ test "create instance" do
23
+ assert_equal 'name@example.com', create(:user).email
24
+ end
25
+
26
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class Group < ActiveRecord::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ class Post < ActiveRecord::Base
2
+ belongs_to :user
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :posts
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require(*Rails.groups)
6
+ require 'fabricators'
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
15
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
16
+ # config.time_zone = 'Central Time (US & Canada)'
17
+
18
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20
+ # config.i18n.default_locale = :de
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ #
7
+ default: &default
8
+ adapter: sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ development:
13
+ <<: *default
14
+ database: db/development.sqlite3
15
+
16
+ # Warning: The database defined as "test" will be erased and
17
+ # re-generated from your development database when you run "rake".
18
+ # Do not set this db to the same as development or production.
19
+ test:
20
+ <<: *default
21
+ database: ":memory:"
22
+
23
+ production:
24
+ <<: *default
25
+ database: db/production.sqlite3