modelfactory 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +11 -0
- data/Manifest.txt +0 -2
- data/README.txt +1 -1
- data/Rakefile +3 -3
- data/lib/modelfactory.rb +17 -2
- data/lib/modelfactory/factory.rb +2 -2
- data/lib/modelfactory/version.rb +1 -1
- data/test/fixtures/schema.rb +1 -0
- data/test/modelfactory_test.rb +28 -1
- data/test/non_orm_test.rb +23 -0
- data/test/test_helper.rb +2 -0
- metadata +4 -5
- data/lib/fixture_converter.rb +0 -97
- data/script/fixtures2factories +0 -18
data/History.txt
CHANGED
@@ -1,23 +1,34 @@
|
|
1
|
+
== 0.9.2 2009-08-10
|
2
|
+
|
3
|
+
* Fall back on parent class factory definitions.
|
4
|
+
|
1
5
|
== 0.9.1 2009-06-04
|
6
|
+
|
2
7
|
* Error was being raised when nil is passed to create, fixed.
|
3
8
|
* Corrected API documentation in README.txt.
|
4
9
|
|
5
10
|
== 0.9.0 2009-06-04
|
11
|
+
|
6
12
|
* More API changes
|
7
13
|
* Built-in counters for generating values
|
8
14
|
|
9
15
|
== 0.8.9 2009-05-19
|
16
|
+
|
10
17
|
* Renamed the required lib to 'modelfactory'
|
11
18
|
* Completely redesigned API, see the documentation in README.txt
|
12
19
|
|
13
20
|
== 0.8.1 2009-05-08
|
21
|
+
|
14
22
|
* Ruby 1.9.1 compatibility
|
15
23
|
|
16
24
|
== 0.8.0 2008-12-10
|
25
|
+
|
17
26
|
* added the ability to specify multiple named types of defaults
|
18
27
|
|
19
28
|
== 0.7.0 2008-11-11
|
29
|
+
|
20
30
|
* Documentation and rubygem
|
21
31
|
|
22
32
|
== 0.0.1 2008-01-25
|
33
|
+
|
23
34
|
* Initial release
|
data/Manifest.txt
CHANGED
@@ -3,14 +3,12 @@ License.txt
|
|
3
3
|
Manifest.txt
|
4
4
|
README.txt
|
5
5
|
Rakefile
|
6
|
-
lib/fixture_converter.rb
|
7
6
|
lib/model_factory.rb
|
8
7
|
lib/modelfactory.rb
|
9
8
|
lib/modelfactory/factory.rb
|
10
9
|
lib/modelfactory/legacy.rb
|
11
10
|
lib/modelfactory/version.rb
|
12
11
|
script/destroy
|
13
|
-
script/fixtures2factories
|
14
12
|
script/generate
|
15
13
|
script/txt2html
|
16
14
|
setup.rb
|
data/README.txt
CHANGED
@@ -26,7 +26,7 @@ opaque ActiveRecord objects whose contents are unimportant.
|
|
26
26
|
|
27
27
|
ModelFactory[User].create.name # => 'Factory User'
|
28
28
|
|
29
|
-
Defaults can be
|
29
|
+
Defaults can be overridden on instance creation. By not specifying unimportant
|
30
30
|
values, the intention of your tests becomes clearer:
|
31
31
|
|
32
32
|
def test_welcome
|
data/Rakefile
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__) + '/lib')
|
1
2
|
require 'rubygems'
|
2
3
|
require 'hoe'
|
3
4
|
require 'hanna/rdoctask'
|
4
|
-
$:.unshift(File.dirname(__FILE__) + "/lib")
|
5
5
|
require 'modelfactory/version'
|
6
6
|
|
7
|
-
Hoe.
|
7
|
+
Hoe.spec('ModelFactory') do |p|
|
8
|
+
p.version = ModelFactory::VERSION::STRING
|
8
9
|
p.name = "modelfactory"
|
9
10
|
p.author = ['Justin Balthrop', 'Zack Hobson']
|
10
11
|
p.description = "A replacement for fixtures."
|
@@ -22,4 +23,3 @@ Hoe.new('ModelFactory', ModelFactory::VERSION::STRING) do |p|
|
|
22
23
|
]
|
23
24
|
end
|
24
25
|
|
25
|
-
|
data/lib/modelfactory.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'rubygems'
|
2
1
|
require 'active_record'
|
3
2
|
require File.dirname(__FILE__) + '/modelfactory/factory'
|
4
3
|
require File.dirname(__FILE__) + '/modelfactory/legacy'
|
@@ -39,7 +38,12 @@ module ModelFactory
|
|
39
38
|
|
40
39
|
# Wraps a given class in a configured Factory instance.
|
41
40
|
def wrap(klass)
|
42
|
-
|
41
|
+
# We might have a definition for a parent class. If so, use that.
|
42
|
+
target = klass
|
43
|
+
while target && !@config.class_opts.key?(target)
|
44
|
+
target = target.superclass
|
45
|
+
end
|
46
|
+
@factory[klass] ||= Factory.new(klass, @config.class_opts[target])
|
43
47
|
end
|
44
48
|
end
|
45
49
|
|
@@ -57,5 +61,16 @@ module ModelFactory
|
|
57
61
|
@class_opts[klass] ||= {}
|
58
62
|
@class_opts[klass][method] = block
|
59
63
|
end
|
64
|
+
|
65
|
+
# Route common method names to method_missing.
|
66
|
+
# XXX There must be a better way to do this, assuming we should be doing
|
67
|
+
# this at all. BasicObject doesn't work because I lose instance_eval.
|
68
|
+
[ :inspect, :send, :id, :object_id ].each do |meth|
|
69
|
+
eval %{
|
70
|
+
def #{meth}(*args, &block)
|
71
|
+
method_missing(:#{meth}, *args, &block)
|
72
|
+
end
|
73
|
+
}
|
74
|
+
end
|
60
75
|
end
|
61
76
|
end
|
data/lib/modelfactory/factory.rb
CHANGED
@@ -42,8 +42,8 @@ module ModelFactory # :nodoc:
|
|
42
42
|
|
43
43
|
def create_named(name, opt, &block)
|
44
44
|
instance = new_named(name, opt, &block)
|
45
|
-
instance.save!
|
46
|
-
instance.reload
|
45
|
+
instance.save! if instance.respond_to? :save!
|
46
|
+
instance.reload if instance.respond_to? :reload
|
47
47
|
instance
|
48
48
|
end
|
49
49
|
|
data/lib/modelfactory/version.rb
CHANGED
data/test/fixtures/schema.rb
CHANGED
data/test/modelfactory_test.rb
CHANGED
@@ -18,7 +18,34 @@ class ModelFactoryTest < Test::Unit::TestCase
|
|
18
18
|
should "not raise on nil params" do
|
19
19
|
assert_nothing_raised { Widget.factory.create(nil) }
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
|
+
should "permit use of overloaded names in configs" do
|
23
|
+
assert_nothing_raised do
|
24
|
+
ModelFactory.configure do
|
25
|
+
inspect(Widget) { name {'foobaz' } }
|
26
|
+
self.puts(Widget) { name {'fooboo' } }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
assert_equal Widget.factory.create_inspect.name, 'foobaz'
|
30
|
+
assert_equal Widget.factory.create_puts.name, 'fooboo'
|
31
|
+
end
|
32
|
+
|
33
|
+
should "permit use of overloaded names in fields" do
|
34
|
+
assert_nothing_raised do
|
35
|
+
ModelFactory.configure do
|
36
|
+
default(Widget) { self.puts {'shazbot' } }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
assert_equal Widget.factory.create.puts, 'shazbot'
|
40
|
+
end
|
41
|
+
|
42
|
+
should "use parent class options" do
|
43
|
+
ModelFactory.configure do
|
44
|
+
default(Widget) { name { 'shazbot' } }
|
45
|
+
end
|
46
|
+
assert_equal AnotherWidget.factory.create.name, 'shazbot'
|
47
|
+
end
|
48
|
+
|
22
49
|
context "with a specified default" do
|
23
50
|
setup do
|
24
51
|
ModelFactory.configure do
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/modelfactory'
|
3
|
+
|
4
|
+
class ModelFactoryTest < Test::Unit::TestCase
|
5
|
+
foo = Struct.new('Foo', :name)
|
6
|
+
|
7
|
+
context "configured for a non-relational type" do
|
8
|
+
setup do
|
9
|
+
ModelFactory.configure do
|
10
|
+
default(foo) { name { 'wubbo' } }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
should "apply config to a created instance" do
|
15
|
+
assert_equal 'wubbo', ModelFactory[foo].create.name
|
16
|
+
end
|
17
|
+
|
18
|
+
should "apply config to a new instance" do
|
19
|
+
assert_equal 'wubbo', ModelFactory[foo].new.name
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modelfactory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Balthrop
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-
|
13
|
+
date: 2009-08-10 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
requirements:
|
82
82
|
- - ">="
|
83
83
|
- !ruby/object:Gem::Version
|
84
|
-
version:
|
84
|
+
version: 2.3.1
|
85
85
|
version:
|
86
86
|
description: A replacement for fixtures.
|
87
87
|
email: justin@geni.com
|
@@ -100,14 +100,12 @@ files:
|
|
100
100
|
- Manifest.txt
|
101
101
|
- README.txt
|
102
102
|
- Rakefile
|
103
|
-
- lib/fixture_converter.rb
|
104
103
|
- lib/model_factory.rb
|
105
104
|
- lib/modelfactory.rb
|
106
105
|
- lib/modelfactory/factory.rb
|
107
106
|
- lib/modelfactory/legacy.rb
|
108
107
|
- lib/modelfactory/version.rb
|
109
108
|
- script/destroy
|
110
|
-
- script/fixtures2factories
|
111
109
|
- script/generate
|
112
110
|
- script/txt2html
|
113
111
|
- setup.rb
|
@@ -152,5 +150,6 @@ test_files:
|
|
152
150
|
- test/counter_test.rb
|
153
151
|
- test/modelfactory_test.rb
|
154
152
|
- test/relation_test.rb
|
153
|
+
- test/non_orm_test.rb
|
155
154
|
- test/legacy_test.rb
|
156
155
|
- test/named_test.rb
|
data/lib/fixture_converter.rb
DELETED
@@ -1,97 +0,0 @@
|
|
1
|
-
class FixtureConverter # :nodoc:
|
2
|
-
def initialize(opts = {})
|
3
|
-
@body = []
|
4
|
-
@header = []
|
5
|
-
@indent_depth = opts[:indent_depth] || 0
|
6
|
-
@output_style = opts[:output_style]
|
7
|
-
end
|
8
|
-
|
9
|
-
def powder?
|
10
|
-
@output_style == :clay
|
11
|
-
end
|
12
|
-
|
13
|
-
def convert_fixture(path)
|
14
|
-
fixture = YAML.load_file(path)
|
15
|
-
return if not fixture
|
16
|
-
|
17
|
-
plural_model_name = path.basename.to_s.split('.').first
|
18
|
-
model_name = plural_model_name.singularize
|
19
|
-
header do
|
20
|
-
"#{plural_model_name} = {}"
|
21
|
-
end
|
22
|
-
|
23
|
-
body 'before(:all) do' if powder?
|
24
|
-
indent(powder?) do
|
25
|
-
body "# Setup #{plural_model_name}"
|
26
|
-
body "# Generated from fixture in #{path.dirname.basename}/#{path.basename}"
|
27
|
-
body '#'
|
28
|
-
body ''
|
29
|
-
fixture.each do |name, record|
|
30
|
-
max_length = record.keys.collect {|key| key.length}.max
|
31
|
-
|
32
|
-
body "#{plural_model_name}[:#{name}] = Factory.create_#{model_name}("
|
33
|
-
indent do
|
34
|
-
body do
|
35
|
-
record.collect do |key,value|
|
36
|
-
value = "\"#{value}\"" if value.kind_of?(String)
|
37
|
-
key = key.ljust(max_length)
|
38
|
-
":#{key} => #{value}"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
body ')'
|
43
|
-
end
|
44
|
-
end
|
45
|
-
body 'end' if powder?
|
46
|
-
body ''
|
47
|
-
end
|
48
|
-
|
49
|
-
def convert_scenario(path)
|
50
|
-
path.each_entry do |file|
|
51
|
-
next if file.extname != '.yml'
|
52
|
-
next if file.basename.to_s =~ /relationships/
|
53
|
-
convert_fixture( path + file )
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def out
|
58
|
-
puts @header.join("\n")
|
59
|
-
puts "\n"
|
60
|
-
puts @body.join("\n")
|
61
|
-
end
|
62
|
-
|
63
|
-
private
|
64
|
-
INDENT = ' '
|
65
|
-
|
66
|
-
def indent(enabled = true)
|
67
|
-
@indent_depth += 1 if enabled
|
68
|
-
yield
|
69
|
-
@indent_depth -= 1 if enabled
|
70
|
-
end
|
71
|
-
|
72
|
-
def body(*lines)
|
73
|
-
lines = yield if block_given?
|
74
|
-
lines = [lines].flatten
|
75
|
-
|
76
|
-
lines.each do |line|
|
77
|
-
@body << indent_line(line, @indent_depth)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def header(*lines)
|
82
|
-
lines = yield if block_given?
|
83
|
-
lines = [lines].flatten
|
84
|
-
|
85
|
-
lines.each do |line|
|
86
|
-
@header << line
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def indent_line(line, indent_depth)
|
91
|
-
ws = ''
|
92
|
-
indent_depth.times do
|
93
|
-
ws += INDENT
|
94
|
-
end
|
95
|
-
ws + line
|
96
|
-
end
|
97
|
-
end
|
data/script/fixtures2factories
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
#!/usr/bin/ruby
|
2
|
-
require 'rubygems'
|
3
|
-
require 'active_support'
|
4
|
-
require 'pp'
|
5
|
-
require 'pathname'
|
6
|
-
require 'yaml'
|
7
|
-
require File.dirname(__FILE__) + '/../lib/fixture_converter'
|
8
|
-
|
9
|
-
path = Pathname.new(ARGV[0])
|
10
|
-
|
11
|
-
fc = FixtureConverter.new
|
12
|
-
if path.file?
|
13
|
-
fc.convert_fixture(path)
|
14
|
-
else
|
15
|
-
fc.convert_scenario(path)
|
16
|
-
end
|
17
|
-
fc.out
|
18
|
-
|