lazy_fixtures 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee9e13f450b9456b2a3b44be2581ef9ee8052513
4
+ data.tar.gz: 74a2bd5481e952ed8f0ccdb4cb294ccf046ab427
5
+ SHA512:
6
+ metadata.gz: 21ac1dbc12c3fa363ae4161a56d13fc98a018bfaeb85c45a47f65ea6172def1226e071dd1b177b28e3fc621f38f30fa4f545ad79aafaf65bf7d0dd96f86ba893
7
+ data.tar.gz: c3d01fabd019fdaa37d4c796992f48e6f14267896b6e9042394b8330aab61a476a41597e2415fb697d8e4920ddc2a697fc5a6b9de9aa160f6d2fbf3f622a7168
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ .idea
15
+ *.DS_Store
16
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ script: "bundle exec rspec spec"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lazy_fixtures.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 GustavoCaso
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,84 @@
1
+ # LazyFixtures
2
+ [![Build Status](https://travis-ci.org/GustavoCaso/lazy_fixtures.svg?branch=master)](https://travis-ci.org/GustavoCaso/lazy_fixtures)
3
+
4
+ Generator to create factory girl fixtures, just with a simple ActiveRecord object
5
+ from your current database.
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'lazy_fixtures'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install lazy_fixtures
21
+
22
+ ## Usage
23
+
24
+ Inside your console simple type `LazyFixtures.generate()` inside the parenthesis place an ActiveRecord object, and this command will generate the corresponding file inside your project.
25
+
26
+ ## Initialisation
27
+
28
+ If you are using this gem inside a rails project you can use the initialise folder you can add a file to configure some attributes that will help the gem work properly.
29
+
30
+ ```ruby
31
+ require 'factory_girl'
32
+
33
+ FactoryGirl.factories.clear
34
+ FactoryGirl.find_definitions
35
+
36
+ LazyFixtures.configure do |config|
37
+ config.factory_directory = "#{specify which folder are your fixtures save}"
38
+ config.factory_names = FactoryGirl.factories.map(&:name)
39
+ end
40
+ ```
41
+
42
+ This lines will tell the gem where to store the files and it will give them a list with your actual Factory Girl fixtures, with this it will avoid factory girl exceptions.
43
+
44
+ ## Options
45
+
46
+ There are several options you can pass to the generator, the method will receive these options as a hash so the order is not redundant.
47
+
48
+ 1. **nested** => by default is set to false, set this to true and will traverse all the associations from your object and will create does fixtures for you as well.
49
+ 2. **overwrite** => by default is set to false, set this to true and will overwrite does file with same name inside the fixtures folder that you specify. If for some reason it find a file with the same name and the option is set to false it will ask you if you want to overwrite. That choice is up to you :-)
50
+ 3. **create** => by default is set to true, this options tell the generator to create the file, if is set to false it will not generate any file.
51
+ 4. **skip_attr** => this accept an array of string which will remove those attributes from the object.
52
+ 5. **change_attr** => this accepts a hash with new values for the fixture you want to set.
53
+
54
+ ## Examples
55
+
56
+ ###This will create a switch fixture:
57
+
58
+ `LazyFixtures.generate(Switch.last)`
59
+
60
+ ###This will create a switch fixture and traverse all the associations:
61
+
62
+ `LazyFixtures.generate(Switch.last, nested: true)`
63
+
64
+ ###This will create a switch fixture with skipped attributes:
65
+
66
+ `LazyFixtures.generate(Switch.last, skip_attr:['age', 'name'])`
67
+
68
+ ###This will create a switch fixture with custom values:
69
+
70
+ `LazyFixtures.generate(Switch.last, change_attr:{'age' => 34, 'name' => 'John'})`
71
+
72
+ ##Notes
73
+ This version is the first version, I want to add more functionality to it, skipping and changing nested attributes from the object, and many more.
74
+ If you use the gem I see any errors or some functionality that might be useful please let me know.
75
+ Thanks.
76
+
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it ( https://github.com/GustavoCaso/lazy_fixtures/fork )
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lazy_fixtures/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lazy_fixtures"
8
+ spec.version = LazyFixtures::VERSION
9
+ spec.authors = ["GustavoCaso"]
10
+ spec.email = ["gustavocaso@gmail.com"]
11
+ spec.summary = %q{Easy generator to save time when creating them}
12
+ spec.description = %q{With this gem you will save time creating factory model,
13
+ with different options you will be able to create a full compatible
14
+ factory tree from your database
15
+ }
16
+ spec.homepage = ""
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0")
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.7"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "activerecord"
27
+ spec.add_development_dependency "sqlite3"
28
+ spec.add_development_dependency "rspec"
29
+ end
@@ -0,0 +1,45 @@
1
+ module LazyFixtures
2
+ class AssociationManager
3
+ def initialize(item)
4
+ @item = item
5
+ @klass = @item.class.name.constantize
6
+ end
7
+
8
+ def columns_info
9
+ return_hash = {}
10
+ @klass.reflections.keys.map do |x|
11
+ reflection = @klass.reflections[x]
12
+ return_hash[x.to_s]= {
13
+ method: x,
14
+ macro: reflection.macro,
15
+ klass: reflection.class_name
16
+ }
17
+ end
18
+ return_hash
19
+ end
20
+
21
+ def determine_association(association_info, class_name, method)
22
+ relation = association_info[:macro]
23
+ text = if relation == :belongs_to
24
+ create_belongs_to_association(association_info[:klass], class_name, method)
25
+ elsif relation == :has_many || relation == :has_and_belongs_to_many
26
+ create_has_many_associations(association_info[:klass])
27
+ end
28
+ <<-EOF
29
+ #{text}
30
+ EOF
31
+ end
32
+
33
+ def create_belongs_to_association(klass, class_name, method)
34
+ method = method == klass.downcase ? klass.downcase : method
35
+ class_name = klass == class_name ? klass : class_name
36
+ "association :#{method}, factory: :#{class_name.downcase}"
37
+ end
38
+
39
+ def create_has_many_associations(klass)
40
+ %Q(after(:create) do |x|
41
+ create_list(:#{klass.downcase}, 1, #{@item.class.name.downcase}: x)
42
+ end)
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,36 @@
1
+ module LazyFixtures
2
+ class AttributesManager
3
+
4
+ attr_reader :attributes
5
+
6
+ def initialize(object, options={})
7
+ @object = object
8
+ @attributes = @object.attributes
9
+ @options = options
10
+ end
11
+
12
+ def add_attributes
13
+ return_value = ''
14
+ attributes.each do |k,v|
15
+ value = ValueMapper.new(@object, k.to_s.dup, v).map_values
16
+ key = ValueMapper.remove_encrypted(k.to_s.dup)
17
+ return_value += " #{key} #{value}\n"
18
+ end
19
+ return_value
20
+ end
21
+
22
+ def manipulate_attributes
23
+ @options[:skip_attr].each { |x| attributes.delete(x)} unless @options[:skip_attr].empty?
24
+ attributes.merge!(@options[:change_attr]) unless @options[:change_attr].empty?
25
+ attributes
26
+ end
27
+
28
+ def delete_association_attributes(method)
29
+ attributes.delete_if {|k,v| k =~ Regexp.new(method) && !v.nil?}
30
+ end
31
+
32
+ def each
33
+ attributes
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,40 @@
1
+ module LazyFixtures
2
+ class FileManager
3
+ def initialize(name, options)
4
+ @name = name
5
+ @options = options
6
+ @title = "#{@name}.rb"
7
+ @file_path = File.join(LazyFixtures.configuration.factory_directory, @title)
8
+ end
9
+
10
+ def create_file
11
+ return unless @options[:create]
12
+ if check_existence? && !@options[:overwrite]
13
+ puts "There is a file already inside that folder with the same name #{@name}"
14
+ puts "Would you like to overwrite that file? (yes/no)"
15
+ answer = gets.chomp
16
+ if answer == 'yes'
17
+ puts 'The generator is overwriting your file'
18
+ File.new(@file_path, "w")
19
+ true
20
+ else
21
+ puts 'Exiting the program'
22
+ false
23
+ end
24
+ end
25
+ puts "creating new file under #{LazyFixtures.configuration.factory_directory}/#{@name}"
26
+ File.new(@file_path, "w")
27
+ true
28
+ end
29
+
30
+ def check_existence?
31
+ File.file?(@file_path)
32
+ end
33
+
34
+ def write_file(text)
35
+ File.open(@file_path, 'w') do |f|
36
+ f.write text
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,95 @@
1
+ module LazyFixtures
2
+ class Generator
3
+
4
+ attr_reader :factory_body, :attributes, :options
5
+
6
+ DEFAULT_OPTIONS = {
7
+ nested: false,
8
+ overwrite: false,
9
+ create: true,
10
+ parent: [],
11
+ skip_attr: [],
12
+ change_attr: {}
13
+ }
14
+
15
+ def initialize(object, options = {})
16
+ @options = DEFAULT_OPTIONS.merge(options)
17
+ @object = get_object(object)
18
+ @class_name = @object.class.name
19
+ @attributes = @object.attributes
20
+ @factory_body = ''
21
+ end
22
+
23
+ def generate
24
+ # In this first implementation of the gem it only allow to manipulate the
25
+ # first object
26
+ attribute_manager.manipulate_attributes
27
+ add_associations if @options[:nested]
28
+ @factory_body += attribute_manager.add_attributes
29
+ text = generate_factory
30
+ if create_file
31
+ @file.write_file(text)
32
+ end
33
+ self
34
+ end
35
+
36
+ def generate_factory
37
+ factory_name = get_factory_name
38
+ <<-EOF
39
+ FactoryGirl.define do
40
+ factory :#{factory_name}, class: #{@class_name.constantize} do
41
+ #{@factory_body}
42
+ end
43
+ end
44
+ EOF
45
+ end
46
+
47
+ def add_associations
48
+ association.columns_info.keys.each do |method|
49
+ begin
50
+ object = @object.send(method)
51
+ object = get_object(object)
52
+ object_class = object.class.name
53
+ next if invalid_object?(object)
54
+ parent_included = @options[:parent].include? object_class
55
+ (@options[:parent] << object_class).uniq!
56
+ self.class.new(object, nested: true, parent: @options[:parent]).generate unless parent_included
57
+ @factory_body += association.determine_association(association.columns_info[method], object_class, method)
58
+ attribute_manager.delete_association_attributes(method)
59
+ rescue => e
60
+ puts "There was an error creating the association #{e} => #{e.backtrace}"
61
+ next
62
+ end
63
+ end
64
+ end
65
+
66
+ def get_object(object)
67
+ object.respond_to?('first') ? object.first : object
68
+ end
69
+
70
+ def invalid_object?(object)
71
+ object.nil? || (object.respond_to?('first') && object.empty?)
72
+ end
73
+
74
+ def create_file
75
+ @file ||= FileManager.new(@object.class.name.downcase, @options)
76
+ @file.create_file
77
+ end
78
+
79
+ def association
80
+ AssociationManager.new(@object)
81
+ end
82
+
83
+ def attribute_manager
84
+ @attribute_manager ||= AttributesManager.new(@object, @options)
85
+ end
86
+
87
+ def get_factory_name
88
+ if LazyFixtures.configuration.factory_names.include?(@class_name.downcase)
89
+ "#{@class_name.downcase}_new"
90
+ else
91
+ @class_name.downcase
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,38 @@
1
+ module LazyFixtures
2
+ class ValueMapper
3
+ def initialize(object, key, value)
4
+ @object = object
5
+ @object_class = @object.class.name.constantize
6
+ @key = key
7
+ @value = value
8
+ remove_encrypted_attributes
9
+ end
10
+
11
+ def remove_encrypted_attributes
12
+ if @key =~ /(encrypted_)/
13
+ key = @key.dup
14
+ key.slice!($1)
15
+ @value = @object.send(key)
16
+ end
17
+ end
18
+
19
+ def map_values
20
+ type = @object_class.columns_hash[@key].type
21
+ value = if @value.nil?
22
+ "nil"
23
+ elsif type == :string || type == :datetime || type == :text || type == :date
24
+ "\'#{@value}\'"
25
+ else
26
+ @value
27
+ end
28
+ end
29
+
30
+ def self.remove_encrypted(key)
31
+ return_key = key
32
+ if key =~ /(encrypted_)/
33
+ return_key.slice!($1)
34
+ end
35
+ return_key
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module LazyFixtures
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ require 'lazy_fixtures/version'
2
+ require 'lazy_fixtures/generator'
3
+ require 'lazy_fixtures/association_manager'
4
+ require 'lazy_fixtures/file_manager'
5
+ require 'lazy_fixtures/value_mapper'
6
+ require 'lazy_fixtures/attributes_manager'
7
+
8
+ module LazyFixtures
9
+ class << self
10
+ attr_accessor :configuration
11
+ end
12
+
13
+ def self.configure
14
+ self.configuration ||= Configuration.new
15
+ yield(configuration)
16
+ end
17
+
18
+ class Configuration
19
+ attr_accessor :factory_directory, :factory_names
20
+
21
+ def initialize
22
+ @factory_directory = ''
23
+ @factory_names = []
24
+ end
25
+ end
26
+
27
+ def self.generate(object, options = {})
28
+ LazyFixtures::Generator.new(object, options).generate
29
+ end
30
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe LazyFixtures::AssociationManager do
4
+
5
+ before(:each) do
6
+ @user = User.create(name:'Evaristo', age: 67)
7
+ @post = Post.create(title:'Post title', post_body: 'This is not important', user_id: 3)
8
+ end
9
+
10
+ describe '#columns_info' do
11
+ it 'will return hash with reflections information' do
12
+ return_value = LazyFixtures::AssociationManager.new(@user).columns_info
13
+ expect(return_value).to eq({'posts' => {method: :posts, macro: :has_many, klass: 'Post'}})
14
+ end
15
+
16
+ it 'will return hash with reflections information' do
17
+ return_value = LazyFixtures::AssociationManager.new(@post).columns_info
18
+ expect(return_value).to eq('user' => {method: :user, macro: :belongs_to, klass: 'User'})
19
+ end
20
+ end
21
+
22
+ describe '#determine_association' do
23
+ it 'will return text for belongs to association' do
24
+ user_association_info = LazyFixtures::AssociationManager.new(@post).columns_info['user']
25
+ return_value = LazyFixtures::AssociationManager.new(@post).determine_association(user_association_info, 'User', 'user')
26
+ expect(return_value).to eq " association :user, factory: :user\n"
27
+ end
28
+
29
+ it 'will return text for belongs to association with right association' do
30
+ user_association_info = LazyFixtures::AssociationManager.new(@post).columns_info['user']
31
+ return_value = LazyFixtures::AssociationManager.new(@post).determine_association(user_association_info, 'User', 'writter')
32
+ expect(return_value).to eq " association :writter, factory: :user\n"
33
+ end
34
+
35
+ it 'will return text for belongs to association with right factory' do
36
+ user_association_info = LazyFixtures::AssociationManager.new(@post).columns_info['user']
37
+ return_value = LazyFixtures::AssociationManager.new(@post).determine_association(user_association_info, 'Writter', 'user')
38
+ expect(return_value).to eq " association :user, factory: :writter\n"
39
+ end
40
+
41
+
42
+ it 'will return text for has_many to association' do
43
+ post_association_info = LazyFixtures::AssociationManager.new(@user).columns_info['posts']
44
+ return_value = LazyFixtures::AssociationManager.new(@user).determine_association(post_association_info, 'Post', 'post')
45
+ expect(return_value).to eq " after(:create) do |x|\n create_list(:post, 1, user: x)\n end\n"
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe LazyFixtures::AttributesManager do
4
+
5
+ it 'will return has with attributes' do
6
+ user = User.create(name: 'Caita', age: 57)
7
+ attributes_manager = LazyFixtures::AttributesManager.new(user,
8
+ LazyFixtures::Generator::DEFAULT_OPTIONS)
9
+ expect_return = {'id' => user.id, 'name' => user.name,
10
+ 'age' => user.age, 'created_at' => user.created_at,
11
+ 'updated_at' => user.updated_at}
12
+ expect(attributes_manager.each).to eq expect_return
13
+ end
14
+
15
+ it 'will return string with all the value from the object' do
16
+ user = User.create(name: 'Caita', age: 57)
17
+ attributes_manager = LazyFixtures::AttributesManager.new(user,
18
+ LazyFixtures::Generator::DEFAULT_OPTIONS)
19
+ expected_attributes = " id #{user.id}\n name 'Caita'\n age 57\n created_at '1986-09-29T00:00:00+00:00'\n updated_at '1986-09-29T00:00:00+00:00'\n"
20
+ expect(attributes_manager.add_attributes).to eq expected_attributes
21
+ end
22
+
23
+ it 'will return string with all value from the object removing the encrypted part' do
24
+ user = UserWithEncrypted.create(name: 'Eduardo', age: 60,
25
+ encrypted_password: 'retvbhj')
26
+ attributes_manager = LazyFixtures::AttributesManager.new(user,
27
+ LazyFixtures::Generator::DEFAULT_OPTIONS)
28
+
29
+ expected_attributes = " id #{user.id}\n name 'Eduardo'\n age 60\n password 'retvbhj'\n"
30
+ expect(attributes_manager.add_attributes).to eq expected_attributes
31
+ end
32
+
33
+ it 'will skip attributes from the options hash' do
34
+ user = User.create(name: 'Caita', age: 57)
35
+ options = LazyFixtures::Generator::DEFAULT_OPTIONS.merge({skip_attr: ['age']})
36
+ attributes_manager = LazyFixtures::AttributesManager.new(user, options)
37
+ expected_attributes = {'id' => user.id, 'name' => user.name,
38
+ 'created_at' => user.created_at,
39
+ 'updated_at' => user.updated_at}
40
+ expect(attributes_manager.manipulate_attributes).to eq expected_attributes
41
+ end
42
+
43
+ it 'will modify attributes value from the options hash' do
44
+ user = User.create(name: 'Caita', age: 57)
45
+ options = LazyFixtures::Generator::DEFAULT_OPTIONS.merge({change_attr: {'age' => 109}})
46
+ attributes_manager = LazyFixtures::AttributesManager.new(user, options)
47
+ expected_attributes = {'id' => user.id, 'name' => user.name,
48
+ 'age' => 109, 'created_at' => user.created_at,
49
+ 'updated_at' => user.updated_at}
50
+ expect(attributes_manager.manipulate_attributes).to eq expected_attributes
51
+ end
52
+
53
+ it 'will remove attributes from association which are not nill' do
54
+ user = User.create(name: 'Caita', age: 57)
55
+ post = Post.create(title:'Post title', post_body: 'This is not important', user_id: user.id)
56
+ options = LazyFixtures::Generator::DEFAULT_OPTIONS
57
+ attributes_manager = LazyFixtures::AttributesManager.new(post, options)
58
+ expected_attributes = {'id' => post.id, 'title' => post.title,
59
+ 'post_body' => post.post_body}
60
+ expect(attributes_manager.delete_association_attributes('user')).to eq expected_attributes
61
+ end
62
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe LazyFixtures::Generator do
4
+
5
+ describe 'attr_reader' do
6
+ before(:each) do
7
+ @user = User.create(name: 'Gustavo', age: 26)
8
+ @factory = LazyFixtures::Generator.new(@user, create: false).generate
9
+ end
10
+
11
+ it '#attributes' do
12
+ attributes = {
13
+ "id" => @user.id,
14
+ "name" => 'Gustavo',
15
+ "age" => 26,
16
+ "created_at" => DateTime.new(1986,9,29),
17
+ "updated_at" => DateTime.new(1986,9,29)
18
+ }
19
+
20
+ expect(@factory.attributes).to eq attributes
21
+ end
22
+
23
+ it '#factory_body' do
24
+ text = <<-EOF
25
+ id #{@user.id}
26
+ name 'Gustavo'
27
+ age 26
28
+ created_at '1986-09-29T00:00:00+00:00'
29
+ updated_at '1986-09-29T00:00:00+00:00'
30
+ EOF
31
+
32
+ factory_body = @factory.factory_body
33
+ expect(factory_body).to eq text
34
+ end
35
+
36
+ it '#options' do
37
+ options = {
38
+ nested: false,
39
+ overwrite: false,
40
+ create: false,
41
+ parent: [],
42
+ skip_attr: [],
43
+ change_attr: {}
44
+ }
45
+ expect(@factory.options).to eq options
46
+
47
+ end
48
+ end
49
+
50
+ describe 'generator methods' do
51
+ before(:each) do
52
+ @user = User.create(name: 'Gustavo', age: 26)
53
+ end
54
+
55
+ describe 'getter object and determine validity of object methods' do
56
+
57
+ before(:each) do
58
+ @factory = LazyFixtures::Generator.new(@user, create: false)
59
+ end
60
+
61
+ it '#invalid_object? will return true with nil value' do
62
+ return_value = @factory.invalid_object?(nil)
63
+ expect(return_value).to eq true
64
+ end
65
+
66
+ it '#invalid_object? will return true with empty value' do
67
+ return_value = @factory.invalid_object?([])
68
+ expect(return_value).to eq true
69
+ end
70
+
71
+ it '#invalid_object? will return false with valid value' do
72
+ return_value = @factory.invalid_object?(@user)
73
+ expect(return_value).to eq false
74
+ end
75
+
76
+ it '#get_object will return the object' do
77
+ return_value = @factory.get_object(@user)
78
+ expect(return_value).to eq @user
79
+ end
80
+
81
+ it '#get_object will return the object' do
82
+ return_value = @factory.get_object(nil)
83
+ expect(return_value).to eq nil
84
+ end
85
+
86
+ it '#get_object will return the object' do
87
+ return_value = @factory.get_object([])
88
+ expect(return_value).to eq nil
89
+ end
90
+
91
+ it '#get_object will return the object' do
92
+ user2 = User.new(name: 'Test', age: 30)
93
+ return_value = @factory.get_object([user2, @user])
94
+ expect(return_value).to eq user2
95
+ end
96
+
97
+ it '#get_factory_name will return the name with appended string' do
98
+ LazyFixtures.configuration.factory_names = ['user']
99
+ return_value = @factory.get_factory_name
100
+ expect(return_value).to eq 'user_new'
101
+ end
102
+
103
+ it '#get_factory_name will return the name' do
104
+ LazyFixtures.configuration.factory_names = ['switch']
105
+ return_value = @factory.get_factory_name
106
+ expect(return_value).to eq 'user'
107
+ end
108
+
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,21 @@
1
+ require 'active_record'
2
+ require 'lazy_fixtures'
3
+
4
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
5
+ load File.dirname(__FILE__) + '/support/schema.rb'
6
+ require File.dirname(__FILE__) +'/support/models'
7
+
8
+ LazyFixtures.configure do |config|
9
+ config.factory_directory = '.'
10
+ config.factory_names = []
11
+ end
12
+
13
+ RSpec.configure do |config|
14
+ config.before(:each) do
15
+ User.destroy_all
16
+ end
17
+
18
+ config.after(:each) do
19
+ User.destroy_all
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :posts
3
+ before_save :create_timestamps
4
+
5
+ def create_timestamps
6
+ self.created_at = DateTime.new(1986,9,29)
7
+ self.updated_at = DateTime.new(1986,9,29)
8
+ end
9
+ end
10
+
11
+ class Post < ActiveRecord::Base
12
+ belongs_to :user
13
+ end
14
+
15
+ class UserWithEncrypted < ActiveRecord::Base
16
+
17
+ def password
18
+ encrypted_password
19
+ end
20
+
21
+ end
@@ -0,0 +1,21 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :users, force: true do |t|
5
+ t.string :name
6
+ t.integer :age
7
+ t.timestamps
8
+ end
9
+
10
+ create_table :posts, force: true do |t|
11
+ t.string :title
12
+ t.string :post_body
13
+ t.integer :user_id
14
+ end
15
+
16
+ create_table :user_with_encrypteds, force: true do |t|
17
+ t.string :name
18
+ t.integer :age
19
+ t.string :encrypted_password
20
+ end
21
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe LazyFixtures::ValueMapper do
4
+ describe '#map_values' do
5
+ before(:each) do
6
+ @user = User.create(name: 'Gustavo', age: 26)
7
+ allow(@user).to receive(:columns_hash).and_return({})
8
+ end
9
+
10
+ it 'return correct value format for integer' do
11
+ allow({}).to receive(:type).and_return(:integer)
12
+ value = LazyFixtures::ValueMapper.new(@user, 'id', 6).map_values
13
+ expect(value).to eq 6
14
+ end
15
+
16
+ it 'return correct format for datetime' do
17
+ allow({}).to receive(:type).and_return(:datetime)
18
+ value = LazyFixtures::ValueMapper.new(@user, 'created_at', '1986-09-29T00:00:00+00:00').map_values
19
+ expect(value).to eq "'1986-09-29T00:00:00+00:00'"
20
+ end
21
+
22
+ it 'return correct format for string' do
23
+ allow({}).to receive(:type).and_return(:string)
24
+ value = LazyFixtures::ValueMapper.new(@user, 'name', 'Gustavo').map_values
25
+ expect(value).to eq "'Gustavo'"
26
+ end
27
+
28
+ it 'return correct format for float' do
29
+ allow({}).to receive(:type).and_return(:float)
30
+ value = LazyFixtures::ValueMapper.new(@user, 'id', 1.5).map_values
31
+ expect(value).to eq 1.5
32
+ end
33
+
34
+ it 'return correct format for nil' do
35
+ allow({}).to receive(:type).and_return(:string)
36
+ value = LazyFixtures::ValueMapper.new(@user, 'name', nil).map_values
37
+ expect(value).to eq "nil"
38
+ end
39
+ end
40
+
41
+ describe '#remove_encrypted_attributes' do
42
+ it 'return non encrypted values' do
43
+ dummy = Struct.new('User', :encrypted_name, :age, :name)
44
+ test_user = dummy.new('dueucvwuevcbwbcw', 23, 'gustavo')
45
+ value = LazyFixtures::ValueMapper.new(test_user, 'encrypted_name', 'dueucvwuevcbwbcw').remove_encrypted_attributes
46
+ expect(value).to eq 'gustavo'
47
+ end
48
+ end
49
+
50
+ describe 'self#remove_encrypted' do
51
+ it 'return key without encrypted in it' do
52
+ key = 'encrypted_password'
53
+ return_value = LazyFixtures::ValueMapper.remove_encrypted(key)
54
+ expect(return_value).to eq 'password'
55
+ end
56
+ end
57
+
58
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_fixtures
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - GustavoCaso
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: "With this gem you will save time creating factory model,\n with
84
+ different options you will be able to create a full compatible\n factory
85
+ tree from your database\n "
86
+ email:
87
+ - gustavocaso@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - lazy_fixtures.gemspec
99
+ - lib/lazy_fixtures.rb
100
+ - lib/lazy_fixtures/association_manager.rb
101
+ - lib/lazy_fixtures/attributes_manager.rb
102
+ - lib/lazy_fixtures/file_manager.rb
103
+ - lib/lazy_fixtures/generator.rb
104
+ - lib/lazy_fixtures/value_mapper.rb
105
+ - lib/lazy_fixtures/version.rb
106
+ - spec/association_manager_spec.rb
107
+ - spec/attributes_manager_spec.rb
108
+ - spec/generator_spec.rb
109
+ - spec/spec_helper.rb
110
+ - spec/support/models.rb
111
+ - spec/support/schema.rb
112
+ - spec/value_mapper_spec.rb
113
+ homepage: ''
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.4.5
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Easy generator to save time when creating them
137
+ test_files:
138
+ - spec/association_manager_spec.rb
139
+ - spec/attributes_manager_spec.rb
140
+ - spec/generator_spec.rb
141
+ - spec/spec_helper.rb
142
+ - spec/support/models.rb
143
+ - spec/support/schema.rb
144
+ - spec/value_mapper_spec.rb