fixjour 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +236 -0
- data/Rakefile +14 -0
- data/fixjour.gemspec +35 -0
- data/lib/fixjour.rb +1 -1
- data/lib/fixjour/merging_proxy.rb +1 -1
- data/spec/builder_spec.rb +24 -0
- data/spec/define_spec.rb +55 -0
- data/spec/dev.rip +4 -0
- data/spec/edge_cases_spec.rb +33 -0
- data/spec/fixjour_spec.rb +555 -0
- data/spec/merging_proxy_spec.rb +48 -0
- data/spec/overrides_hash_spec.rb +61 -0
- data/spec/spec_helper.rb +90 -0
- data/spec/verify_spec.rb +136 -0
- metadata +28 -5
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe Fixjour::MergingProxy do
|
4
|
+
attr_reader :merger
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@merger = Fixjour::MergingProxy.new(Foo, :name => "MERGED!")
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#new" do
|
11
|
+
it "merges options with overrides" do
|
12
|
+
mock.proxy(defaults = { :name => "Pat" }).merge(:name => "MERGED!")
|
13
|
+
merger.new(defaults)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns an instance of the klass with the merged options" do
|
17
|
+
merger.new(:name => "Pat").should be_kind_of(Foo)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "protected" do
|
22
|
+
it "stores the protected attributes" do
|
23
|
+
@merger.protected :name
|
24
|
+
@merger.protected.should == Set.new([:name])
|
25
|
+
end
|
26
|
+
|
27
|
+
it "sends values to instance" do
|
28
|
+
mock.proxy(Foo).new(:name => "MERGED!").never
|
29
|
+
mock.proxy(Foo).new({ }).once
|
30
|
+
@merger.protected :name
|
31
|
+
@merger.new(:name => "MERGED!").name.should == "MERGED!"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "proxying to klass" do
|
36
|
+
it "proxies other methods to klass" do
|
37
|
+
merger.superclass.should == ActsAsFu::Connection
|
38
|
+
end
|
39
|
+
|
40
|
+
it "proxies respond_to? to klass" do
|
41
|
+
merger.should respond_to(:superclass)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should keep its own inspect method" do
|
45
|
+
merger.inspect.should_not == Foo.inspect
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe Fixjour::OverridesHash do
|
4
|
+
attr_reader :overrides
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@overrides = Fixjour::OverridesHash.new(:name => "Pat")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "inherits from Hash" do
|
11
|
+
Fixjour::OverridesHash.superclass.should == Hash
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#process" do
|
15
|
+
context "when the option isn't present" do
|
16
|
+
it "doesn't do anything" do
|
17
|
+
called = false
|
18
|
+
overrides.process(:not_there) do
|
19
|
+
called = true
|
20
|
+
end
|
21
|
+
called.should_not be
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns nil" do
|
25
|
+
overrides.process(:not_there).should be_nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "when the option is present" do
|
30
|
+
it "deletes the option from the hash" do
|
31
|
+
overrides.process(:name)
|
32
|
+
overrides.should_not have_key(:name)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "passes the value to the block" do
|
36
|
+
overrides.process(:name) do |value|
|
37
|
+
value.should == "Pat"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it "allows values to be re-assigned in hash" do
|
42
|
+
overrides.process(:name) do |value|
|
43
|
+
overrides[:result] = value
|
44
|
+
end
|
45
|
+
overrides[:result].should == "Pat"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns the value" do
|
49
|
+
overrides.process(:name).should == "Pat"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#delete" do
|
55
|
+
it "is private" do
|
56
|
+
proc {
|
57
|
+
overrides.delete(:name)
|
58
|
+
}.should raise_error(NoMethodError, /private/)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'cgi'
|
3
|
+
require 'spec'
|
4
|
+
require 'faker'
|
5
|
+
require 'rr'
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'acts_as_fu'
|
9
|
+
rescue LoadError
|
10
|
+
puts "You need the acts_as_fu gem to run the specs."
|
11
|
+
exit!
|
12
|
+
end
|
13
|
+
|
14
|
+
require File.dirname(__FILE__) + '/../lib/fixjour'
|
15
|
+
|
16
|
+
def pre(*args)
|
17
|
+
args.each { |arg|
|
18
|
+
puts '<pre>' + CGI.escapeHTML(arg.is_a?(String) ? arg : arg.inspect) + '</pre>'
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
Spec::Runner.configure do |c|
|
23
|
+
c.mock_with(:rr)
|
24
|
+
end
|
25
|
+
|
26
|
+
include ActsAsFu
|
27
|
+
|
28
|
+
build_model(:foos) do
|
29
|
+
attr_accessor :bizzle
|
30
|
+
|
31
|
+
string :name
|
32
|
+
integer :age
|
33
|
+
integer :bar_id
|
34
|
+
integer :person_id
|
35
|
+
|
36
|
+
belongs_to :bar
|
37
|
+
belongs_to :owner, :foreign_key => :person_id, :class_name => 'Person'
|
38
|
+
|
39
|
+
validates_presence_of :bar, :message => "BAR AIN'T THURR"
|
40
|
+
validates_presence_of :name
|
41
|
+
end
|
42
|
+
|
43
|
+
build_model(:bars) do
|
44
|
+
string :name
|
45
|
+
|
46
|
+
has_many :people
|
47
|
+
has_many :foos
|
48
|
+
end
|
49
|
+
|
50
|
+
build_model(:bazzs) do
|
51
|
+
string :name
|
52
|
+
integer :bar_id
|
53
|
+
|
54
|
+
belongs_to :bar
|
55
|
+
end
|
56
|
+
|
57
|
+
build_model(:foo_bars) do
|
58
|
+
string :name
|
59
|
+
end
|
60
|
+
|
61
|
+
build_model(:people) do
|
62
|
+
string :first_name
|
63
|
+
string :last_name
|
64
|
+
integer :bar_id
|
65
|
+
end
|
66
|
+
|
67
|
+
def define_all_builders
|
68
|
+
Fixjour.builders.clear
|
69
|
+
Fixjour do
|
70
|
+
define_builder(Foo) do |klass, overrides|
|
71
|
+
klass.new({ :name => 'Foo Namery', :bar => new_bar, :owner => new_person }.merge(overrides))
|
72
|
+
end
|
73
|
+
|
74
|
+
define_builder(Bar) do |klass, overrides|
|
75
|
+
klass.new({ :name => "Bar Namery", :people => [new_person] }.merge(overrides))
|
76
|
+
end
|
77
|
+
|
78
|
+
define_builder(FooBar) do |klass, overrides|
|
79
|
+
klass.new(:name => "foobar-#{counter(:foobar)}")
|
80
|
+
end
|
81
|
+
|
82
|
+
define_builder(Bazz) do |klass|
|
83
|
+
klass.new(:name => "Bazz Namery")
|
84
|
+
end
|
85
|
+
|
86
|
+
define_builder Person do |klass|
|
87
|
+
klass.new
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/spec/verify_spec.rb
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'spec/spec_helper'
|
2
|
+
|
3
|
+
describe Fixjour, ".verify!" do
|
4
|
+
before(:each) do
|
5
|
+
define_all_builders
|
6
|
+
Fixjour.builders.clear
|
7
|
+
end
|
8
|
+
|
9
|
+
it "can take verify as an option" do
|
10
|
+
mock(Fixjour).verify!.once
|
11
|
+
Fixjour(:verify => true) { }
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "clearing the transaction" do
|
15
|
+
before(:each) do
|
16
|
+
Fixjour do
|
17
|
+
define_builder(Foo) { |overrides| Foo.new(:name => 'ok', :bar => new_bar) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "rolls back the transaction" do
|
22
|
+
proc {
|
23
|
+
Fixjour.verify!
|
24
|
+
}.should_not change(Foo, :count)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when the builder returns an invalid object" do
|
29
|
+
context "when the validations are not related to uniqueness" do
|
30
|
+
before(:each) do
|
31
|
+
Fixjour do
|
32
|
+
define_builder(Foo) { |overrides| Foo.new(:bar => nil) }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it "raises InvalidBuilder" do
|
37
|
+
proc {
|
38
|
+
Fixjour.verify!
|
39
|
+
}.should raise_error(Fixjour::InvalidBuilder)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "includes information about the failure" do
|
43
|
+
proc {
|
44
|
+
Fixjour.verify!
|
45
|
+
}.should raise_error(/BAR AIN'T THURR/)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "when the validations are related to uniqueness" do
|
50
|
+
before(:each) do
|
51
|
+
Bar.delete_all
|
52
|
+
Bar.validates_uniqueness_of :name
|
53
|
+
Fixjour do
|
54
|
+
define_builder(Bar) { |overrides| Bar.new(:name => "percy") }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "raises DangerousBuilder" do
|
59
|
+
proc {
|
60
|
+
Fixjour.verify!
|
61
|
+
}.should raise_error(Fixjour::DangerousBuilder)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "includes information about the failure" do
|
65
|
+
proc {
|
66
|
+
Fixjour.verify!
|
67
|
+
}.should raise_error(/uniqueness/)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "when the builder returns an object that cannot be saved" do
|
73
|
+
before(:each) do
|
74
|
+
Fixjour.builders.clear
|
75
|
+
|
76
|
+
klass = build_model(:bars) {
|
77
|
+
string :name
|
78
|
+
has_many :people
|
79
|
+
}
|
80
|
+
|
81
|
+
bar_bomb = Object.new
|
82
|
+
stub(bar_bomb).save! { raise ActiveRecord::StatementInvalid.new("oops!") }
|
83
|
+
stub(bar_bomb).valid? { true }
|
84
|
+
stub(bar_bomb).new_record? { true }
|
85
|
+
stub(bar_bomb).is_a?(Bar) { true }
|
86
|
+
|
87
|
+
stub(Fixjour).new_record(anything) { bar_bomb }
|
88
|
+
|
89
|
+
Fixjour do
|
90
|
+
define_builder(klass) { |overrides| klass.new }
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
it "raises UnsavableBuilder" do
|
95
|
+
proc {
|
96
|
+
Fixjour.verify!
|
97
|
+
}.should raise_error(Fixjour::UnsavableBuilder)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "includes information about the failure" do
|
101
|
+
proc {
|
102
|
+
Fixjour.verify!
|
103
|
+
}.should raise_error(/ActiveRecord::StatementInvalid/)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context "when the builder saves the object" do
|
108
|
+
before(:each) do
|
109
|
+
Fixjour do
|
110
|
+
define_builder(Foo) do |overrides|
|
111
|
+
Foo.create(:name => 'saved!', :bar => new_bar)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
it "raises BuilderSavedRecord" do
|
117
|
+
proc {
|
118
|
+
Fixjour.verify!
|
119
|
+
}.should raise_error(Fixjour::BuilderSavedRecord)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context "when the object is not the correct type" do
|
124
|
+
before(:each) do
|
125
|
+
Fixjour do
|
126
|
+
define_builder(Foo) { |overrides| Bar.new(:name => 'saved!') }
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
it "raises WrongBuilderType" do
|
131
|
+
proc {
|
132
|
+
Fixjour.verify!
|
133
|
+
}.should raise_error(Fixjour::WrongBuilderType)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixjour
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 5
|
8
|
-
-
|
9
|
-
version: 0.5.
|
9
|
+
- 2
|
10
|
+
version: 0.5.2
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Pat Nakajima
|
@@ -21,9 +22,11 @@ dependencies:
|
|
21
22
|
name: activerecord
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
27
30
|
segments:
|
28
31
|
- 0
|
29
32
|
version: "0"
|
@@ -33,9 +36,11 @@ dependencies:
|
|
33
36
|
name: faker
|
34
37
|
prerelease: false
|
35
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
36
40
|
requirements:
|
37
41
|
- - ">="
|
38
42
|
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
39
44
|
segments:
|
40
45
|
- 0
|
41
46
|
version: "0"
|
@@ -45,9 +50,11 @@ dependencies:
|
|
45
50
|
name: acts_as_fu
|
46
51
|
prerelease: false
|
47
52
|
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
48
54
|
requirements:
|
49
55
|
- - ">="
|
50
56
|
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
51
58
|
segments:
|
52
59
|
- 0
|
53
60
|
version: "0"
|
@@ -62,21 +69,33 @@ extensions: []
|
|
62
69
|
extra_rdoc_files: []
|
63
70
|
|
64
71
|
files:
|
72
|
+
- README.textile
|
73
|
+
- Rakefile
|
74
|
+
- fixjour.gemspec
|
65
75
|
- lib/core_ext/hash.rb
|
66
76
|
- lib/core_ext/object.rb
|
77
|
+
- lib/fixjour.rb
|
67
78
|
- lib/fixjour/builder.rb
|
68
79
|
- lib/fixjour/builders.rb
|
69
80
|
- lib/fixjour/counter.rb
|
81
|
+
- lib/fixjour/define.rb
|
70
82
|
- lib/fixjour/definitions.rb
|
71
83
|
- lib/fixjour/deprecation.rb
|
72
|
-
- lib/fixjour/define.rb
|
73
84
|
- lib/fixjour/errors.rb
|
74
85
|
- lib/fixjour/generator.rb
|
75
86
|
- lib/fixjour/merging_proxy.rb
|
76
87
|
- lib/fixjour/overrides_hash.rb
|
77
88
|
- lib/fixjour/redundant_check.rb
|
78
89
|
- lib/fixjour/verify.rb
|
79
|
-
-
|
90
|
+
- spec/builder_spec.rb
|
91
|
+
- spec/define_spec.rb
|
92
|
+
- spec/dev.rip
|
93
|
+
- spec/edge_cases_spec.rb
|
94
|
+
- spec/fixjour_spec.rb
|
95
|
+
- spec/merging_proxy_spec.rb
|
96
|
+
- spec/overrides_hash_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- spec/verify_spec.rb
|
80
99
|
has_rdoc: true
|
81
100
|
homepage: http://github.com/nakajima/fixjour
|
82
101
|
licenses: []
|
@@ -87,23 +106,27 @@ rdoc_options: []
|
|
87
106
|
require_paths:
|
88
107
|
- lib
|
89
108
|
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
90
110
|
requirements:
|
91
111
|
- - ">="
|
92
112
|
- !ruby/object:Gem::Version
|
113
|
+
hash: 3
|
93
114
|
segments:
|
94
115
|
- 0
|
95
116
|
version: "0"
|
96
117
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
97
119
|
requirements:
|
98
120
|
- - ">="
|
99
121
|
- !ruby/object:Gem::Version
|
122
|
+
hash: 3
|
100
123
|
segments:
|
101
124
|
- 0
|
102
125
|
version: "0"
|
103
126
|
requirements: []
|
104
127
|
|
105
128
|
rubyforge_project:
|
106
|
-
rubygems_version: 1.3.
|
129
|
+
rubygems_version: 1.3.7
|
107
130
|
signing_key:
|
108
131
|
specification_version: 2
|
109
132
|
summary: Object creation methods everyone already has
|