factory_girl_json 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.
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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in factory_girl_json.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Nikos Gereoudakis
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
+ # FactoryGirlJson
2
+
3
+ This gem facilitates the creation of JSON fixtures for javascript testing in ruby apps.
4
+ While testing composite views in front end "MVC" frameworks (Backbone, Emder, Angular etc)
5
+ you might need to create some quite complicated json fixtures to seed the models for the tests.
6
+
7
+ This gem will use your `FactoryGirl` factories to create these json fixtures for you.
8
+
9
+ Furthermore it supports custom serializers (e.g. [active_model_serializers](https://github.com/josevalim/active_model_serializers) or any class that takes a model in its constructor and responds to `to_json`).
10
+
11
+ The fixtures can be loaded in your tests using a javascript library such as [jasmine-jquery](https://github.com/velesin/jasmine-jquery#json-fixtures).
12
+
13
+ ## Installation
14
+
15
+ The gem has been used only in a Rails project in combination with `factory_girl_rails` but it should work with `factory_girl` gem as well.
16
+
17
+ Add these lines to your application's Gemfile:
18
+
19
+ group :test do
20
+ gem 'factory_girl_rails'
21
+ gem 'factory_girl_json'
22
+ end
23
+
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install factory_girl_json
32
+
33
+ ## Usage
34
+
35
+ First, define some factories with factory girl.
36
+ You can add another option `json_serializer` which indicates which class will be used for the serialization of the model.
37
+
38
+ FactoryGirl.define do
39
+ factory :user do
40
+ sequence(:name) {|n| "user name #{n}"}
41
+ sequence(:email) {|n| "useremail#{n}@email.com"}
42
+ age 12
43
+ end
44
+
45
+ factory :user_with_posts, parent: :user, json_serializer: UserSerializer do
46
+ after(:create) do |user|
47
+ create :post, user: user
48
+ end
49
+ end
50
+ end
51
+
52
+
53
+ class UserSerializer
54
+ def initialize(user)
55
+ @user = user
56
+ end
57
+
58
+ def to_json(options = {})
59
+ posts = {posts: @user.posts.as_json}
60
+ user = @user.as_json
61
+ user['user'].merge! posts
62
+ JSON.generate user
63
+ end
64
+ end
65
+
66
+
67
+ Now if you run this command
68
+
69
+ `RAILS_ENV=test bundle exec rake factory_girl_json:export['user']`
70
+
71
+ you will get a `user.json` file with data:
72
+
73
+ {
74
+ "user": {
75
+ "age": 12,
76
+ "email": "useremail1@email.com",
77
+ "id": 1,
78
+ "name": "user name 1"
79
+ }
80
+ }
81
+
82
+ `RAILS_ENV=test bundle exec rake factory_girl_json:export['user_with_posts']`
83
+
84
+ you will get a `user_with_posts.json` file with data:
85
+
86
+ {
87
+ "user": {
88
+ "age": 12,
89
+ "email": "useremail1@email.com",
90
+ "id": 1,
91
+ "name": "user name 1",
92
+ "posts": [
93
+ {
94
+ "post": {
95
+ "body": "post body",
96
+ "id": 1,
97
+ "title": "post title",
98
+ "user_id": 1
99
+ }
100
+ }
101
+ ]
102
+ }
103
+ }
104
+
105
+ ### All rake tasks
106
+ rake factory_girl_json:all # Exports json fixtures for all FactoryGirl factories
107
+ rake factory_girl_json:export[factory_name] # Exports json fixture for the selected FactoryGirl factory
108
+ rake factory_girl_json:export_serialized # Exports json fixtures for FactoryGirl factories with the json_serializer option
109
+
110
+ ### Default exporting paths
111
+ if Rails is used it will write the json files to `spec\javascripts\fixtures\json` or `test\javascripts\fixtures\json`
112
+ otherwise it uses the current path.
113
+
114
+ ## Contributing
115
+
116
+ 1. Fork it
117
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
118
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
119
+ 4. Push to the branch (`git push origin my-new-feature`)
120
+ 5. Create new Pull Request
121
+
122
+ ## Related projects
123
+
124
+ [Backbone-Factory](https://github.com/SupportBee/Backbone-Factory)
125
+
126
+ ## Author
127
+
128
+ [Nikos Gereoudakis](https://twitter.com/ni_ger)
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
8
+
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/factory_girl_json/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Nikos Gereoudakis"]
6
+ gem.email = ["gereoudakis@gmail.com"]
7
+ gem.description = %q{Create json fixtures from your FactoryGirl factories}
8
+ gem.summary = %q{Create json fixtures from your FactoryGirl factories}
9
+ gem.homepage = "https://github.com/stream7/factory_girl_json"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "factory_girl_json"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = FactoryGirlJson::VERSION
17
+
18
+ gem.add_dependency "factory_girl"
19
+ gem.add_dependency "json"
20
+ gem.add_dependency "database_cleaner"
21
+ gem.add_development_dependency "rspec"
22
+ gem.add_development_dependency "activerecord"
23
+ gem.add_development_dependency "sqlite3"
24
+ end
@@ -0,0 +1,33 @@
1
+ require 'factory_girl'
2
+ require 'fileutils'
3
+ require 'database_cleaner'
4
+ require 'factory_girl_json/factory_girl_ext/valid_options'
5
+ require 'factory_girl_json/version'
6
+ require 'factory_girl_json/exporter'
7
+ require 'factory_girl_json/railtie' if defined? Rails
8
+
9
+ module FactoryGirlJson
10
+ class << self
11
+
12
+ def export(factory_name)
13
+ clean
14
+ Exporter.new(factory_name).export
15
+ end
16
+
17
+ def export_all
18
+ clean
19
+ Exporter.new(:all).export
20
+ end
21
+
22
+ def export_serialized
23
+ clean
24
+ Exporter.new(:serialized).export
25
+ end
26
+
27
+ def clean
28
+ DatabaseCleaner.clean_with :truncation
29
+ DatabaseCleaner.clean
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,73 @@
1
+ module FactoryGirlJson
2
+ class Exporter
3
+ def initialize(factories)
4
+ @factories = find_factories(factories)
5
+ $stdout.puts "Exporting factories: #{@factories.map(&:name)}"
6
+ end
7
+
8
+ def export
9
+ @factories.each do |factory|
10
+ export_factory(factory)
11
+ end
12
+ end
13
+
14
+ def export_factory(factory)
15
+ $stdout.puts "Exporting factory #{factory.name}"
16
+ path = file_path(factory)
17
+ unless File.exists? path
18
+ model = factory.run(:create, {})
19
+ json = to_json(model, factory.json_serializer)
20
+ write_file(json, path)
21
+ else
22
+ $stderr.puts "File #{factory.name}.json exists, delete file and run again to rewrite"
23
+ end
24
+ end
25
+
26
+ def to_json(model, serializer = nil)
27
+ if serializer
28
+ serializer.new(model).to_json
29
+ else
30
+ model.to_json
31
+ end
32
+ end
33
+
34
+ def write_file(json, path)
35
+ FileUtils.mkdir_p File.dirname(path)
36
+ File.open(path, 'w') do |file|
37
+ file.puts JSON.pretty_generate(JSON.parse(json))
38
+ end
39
+ $stdout.puts "Wrote file #{path}"
40
+ end
41
+
42
+ def file_path(factory)
43
+ if defined? Rails
44
+ if Dir.exists? Rails.root.join('spec').to_s
45
+ Rails.root.join("spec/javascripts/fixtures/json/#{factory.name}.json").to_s
46
+ elsif Dir.exists? Rails.root.join('test').to_s
47
+ Rails.root.join("test/javascripts/fixtures/json/#{factory.name}.json").to_s
48
+ else
49
+ "#{Dir.pwd}/#{factory.name}.json"
50
+ end
51
+ else
52
+ "#{Dir.pwd}/#{factory.name}.json"
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def find_factories(factories)
59
+ case factories
60
+ when :all
61
+ FactoryGirl.factories.to_a
62
+ when :serialized
63
+ FactoryGirl.factories.to_a.select { |f| f.json_serializer }
64
+ else
65
+ if factories.is_a? Array
66
+ FactoryGirl.factories.to_a.select { |f| factories.include? f.name.to_s }
67
+ else
68
+ [FactoryGirl.factories.find(factories.to_sym)]
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,12 @@
1
+ module FactoryGirl
2
+ class Factory
3
+ attr_reader :json_serializer
4
+
5
+ private
6
+
7
+ def assert_valid_options(options)
8
+ options.assert_valid_keys(:class, :parent, :aliases, :traits, :json_serializer)
9
+ @json_serializer = options[:json_serializer]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module FactoryGirlJson
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load 'tasks/factory_girl_json.rake'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module FactoryGirlJson
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ namespace :factory_girl_json do
2
+
3
+ desc 'Exports json fixtures for all FactoryGirl factories'
4
+ task :all => :environment do
5
+ FactoryGirlJson.export_all
6
+ end
7
+
8
+ desc 'Exports json fixture for the selected FactoryGirl factory'
9
+ task :export, [:factory_name] => :environment do |t, args|
10
+ FactoryGirlJson.export args[:factory_name]
11
+ end
12
+
13
+ desc 'Exports json fixtures for FactoryGirl factories with the json_serializer option'
14
+ task :export_serialized => :environment do
15
+ FactoryGirlJson.export_serialized
16
+ end
17
+ end
18
+
@@ -0,0 +1,7 @@
1
+ FactoryGirl.define do
2
+ factory :post do
3
+ user
4
+ title 'post title'
5
+ body 'post body'
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ FactoryGirl.define do
2
+ factory :user do
3
+ sequence(:name) {|n| "user name #{n}"}
4
+ sequence(:email) {|n| "useremail#{n}@email.com"}
5
+ age 12
6
+ end
7
+
8
+ factory :user_with_posts, parent: :user, json_serializer: UserSerializer do
9
+ after(:create) do |user|
10
+ create :post, user: user
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe FactoryGirlJson do
4
+ it "should genarate a file" do
5
+ FactoryGirlJson.export('user')
6
+ path = "#{Dir.pwd}/user.json"
7
+ File.exists?(path).should be_true
8
+ File.delete path
9
+ end
10
+
11
+ it "should genarate a file with user json" do
12
+ FactoryGirlJson.export('user')
13
+ path = "#{Dir.pwd}/user.json"
14
+ File.exists?(path).should be_true
15
+ hash = JSON.parse(File.read(path))
16
+ user = hash['user']
17
+ user['id'].should_not be_nil
18
+ user['name'].should_not be_nil
19
+ user['email'].should_not be_nil
20
+ user['age'].should_not be_nil
21
+ File.delete path
22
+ end
23
+
24
+ it "should use json_serializer if present" do
25
+ FactoryGirlJson.export('user_with_posts')
26
+ path = "#{Dir.pwd}/user_with_posts.json"
27
+ File.exists?(path).should be_true
28
+ hash = JSON.parse(File.read(path))
29
+ user = hash['user']
30
+ user['id'].should_not be_nil
31
+ user['name'].should_not be_nil
32
+ user['email'].should_not be_nil
33
+ user['age'].should_not be_nil
34
+ user['posts'].should_not be_nil
35
+ user['posts'][0]['post']['title'].should_not be_nil
36
+ File.delete path
37
+ end
38
+
39
+
40
+ describe ".export_all" do
41
+ it "should export three files" do
42
+ FactoryGirlJson.export_all
43
+ Dir['*.json'].size.should eq(3)
44
+ Dir['*.json'].each {|f| File.delete(f) }
45
+ end
46
+ end
47
+
48
+
49
+ describe ".export_serialized" do
50
+ it "should export three files" do
51
+ FactoryGirlJson.export_serialized
52
+ Dir['*.json'].size.should eq(1)
53
+ Dir['*.json'].first.should eq('user_with_posts.json')
54
+ Dir['*.json'].each {|f| File.delete(f) }
55
+ end
56
+ end
57
+
58
+ 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,12 @@
1
+ class UserSerializer
2
+ def initialize(user)
3
+ @user = user
4
+ end
5
+
6
+ def to_json(options = {})
7
+ posts = {posts: @user.posts.as_json}
8
+ user = @user.as_json
9
+ user['user'].merge! posts
10
+ JSON.generate user
11
+ end
12
+ end
@@ -0,0 +1,29 @@
1
+ require 'factory_girl_json'
2
+ require 'active_record'
3
+ require 'models/user'
4
+ require 'models/post'
5
+ require 'serializers/user_serializer'
6
+ require 'factories/posts'
7
+ require 'factories/users'
8
+
9
+ ActiveRecord::Base.establish_connection(
10
+ :adapter => 'sqlite3',
11
+ :database => ':memory:'
12
+ )
13
+
14
+ ActiveRecord::Schema.define do
15
+ self.verbose = false
16
+
17
+ create_table :users, :force => true do |t|
18
+ t.string :name
19
+ t.string :email
20
+ t.integer :age
21
+ end
22
+
23
+ create_table :posts, :force => true do |t|
24
+ t.integer :user_id
25
+ t.string :title
26
+ t.text :body
27
+ end
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: factory_girl_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nikos Gereoudakis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: factory_girl
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: database_cleaner
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: activerecord
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Create json fixtures from your FactoryGirl factories
111
+ email:
112
+ - gereoudakis@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rspec
119
+ - Gemfile
120
+ - LICENSE
121
+ - README.md
122
+ - Rakefile
123
+ - factory_girl_json.gemspec
124
+ - lib/factory_girl_json.rb
125
+ - lib/factory_girl_json/exporter.rb
126
+ - lib/factory_girl_json/factory_girl_ext/valid_options.rb
127
+ - lib/factory_girl_json/railtie.rb
128
+ - lib/factory_girl_json/version.rb
129
+ - lib/tasks/factory_girl_json.rake
130
+ - spec/factories/posts.rb
131
+ - spec/factories/users.rb
132
+ - spec/factory_girl_json_spec.rb
133
+ - spec/models/post.rb
134
+ - spec/models/user.rb
135
+ - spec/serializers/user_serializer.rb
136
+ - spec/spec_helper.rb
137
+ homepage: https://github.com/stream7/factory_girl_json
138
+ licenses: []
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 1.8.24
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: Create json fixtures from your FactoryGirl factories
161
+ test_files:
162
+ - spec/factories/posts.rb
163
+ - spec/factories/users.rb
164
+ - spec/factory_girl_json_spec.rb
165
+ - spec/models/post.rb
166
+ - spec/models/user.rb
167
+ - spec/serializers/user_serializer.rb
168
+ - spec/spec_helper.rb