JamieFlournoy-machinist 1.0.6

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.
@@ -0,0 +1,151 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'machinist/sequel'
3
+ require 'logger'
4
+
5
+ # We have to define this here because Sequel needs a DB connection
6
+ # setup before you can define models
7
+ DB = Sequel.sqlite(:logger => Logger.new(File.dirname(__FILE__) + "/log/test.log"))
8
+
9
+ DB.create_table :people do
10
+ primary_key :id
11
+ String :name
12
+ String :type
13
+ String :password
14
+ Boolean :admin, :default => false
15
+ end
16
+
17
+ DB.create_table :posts do
18
+ primary_key :id
19
+ String :title
20
+ String :body
21
+ Boolean :published, :default => true
22
+ end
23
+
24
+ DB.create_table :comments do
25
+ primary_key :id
26
+ Integer :post_id
27
+ Integer :author_id
28
+ String :body
29
+ end
30
+
31
+ module MachinistSequelSpecs
32
+
33
+ class Person < Sequel::Model
34
+ set_restricted_columns :password
35
+ end
36
+
37
+ class Post < Sequel::Model
38
+ one_to_many :comments, :class => "MachinistSequelSpecs::Comment"
39
+ end
40
+
41
+ class Comment < Sequel::Model
42
+ many_to_one :post, :class => "MachinistSequelSpecs::Post"
43
+ many_to_one :author, :class => "MachinistSequelSpecs::Person"
44
+ end
45
+
46
+ describe Machinist, "Sequel adapter" do
47
+
48
+ before(:each) do
49
+ Person.clear_blueprints!
50
+ Post.clear_blueprints!
51
+ Comment.clear_blueprints!
52
+ end
53
+
54
+ describe "make method" do
55
+ it "should save the constructed object" do
56
+ Person.blueprint { }
57
+ person = Person.make
58
+ person.should_not be_new
59
+ end
60
+
61
+ it "should create and object through a many_to_one association" do
62
+ Post.blueprint { }
63
+ Comment.blueprint { post }
64
+ Comment.make.post.class.should == Post
65
+ end
66
+
67
+ it "should create an object through many_to_one association with a class_name attribute" do
68
+ Person.blueprint { }
69
+ Comment.blueprint { author }
70
+ Comment.make.author.class.should == Person
71
+ end
72
+
73
+ it "should allow setting a protected attribute in the blueprint" do
74
+ Person.blueprint do
75
+ password { "Test" }
76
+ end
77
+ Person.make.password.should == "Test"
78
+ end
79
+
80
+ it "should allow overriding a protected attribute" do
81
+ Person.blueprint do
82
+ password { "Test" }
83
+ end
84
+ Person.make(:password => "New").password.should == "New"
85
+ end
86
+
87
+ it "should allow setting the id attribute in a blueprint" do
88
+ Person.blueprint do
89
+ id { 12345 }
90
+ end
91
+ Person.make.id.should == 12345
92
+ end
93
+
94
+ # it "should allow setting the type attribute in a blueprint" do
95
+ # Person.blueprint { type "Person" }
96
+ # Person.make.type.should == "Person"
97
+ # end
98
+ end
99
+
100
+ describe "plan method" do
101
+ it "should not save the constructed object" do
102
+ lambda {
103
+ Person.blueprint { }
104
+ person = Person.plan
105
+ }.should_not change(Person,:count)
106
+ end
107
+
108
+ it "should return a regular attribute in the hash" do
109
+ Post.blueprint do
110
+ title { "Test" }
111
+ end
112
+ post = Post.plan
113
+ post[:title].should == "Test"
114
+ end
115
+
116
+ it "should create an object through a many_to_one association, and return its id" do
117
+ Post.blueprint { }
118
+ Comment.blueprint { post }
119
+ lambda {
120
+ comment = Comment.plan
121
+ comment[:post].should be_nil
122
+ comment[:post_id].should_not be_nil
123
+ }.should change(Post, :count).by(1)
124
+ end
125
+ end
126
+
127
+ # Note that building up an unsaved object graph using just the
128
+ # association methods is not possible in Sequel, so
129
+ # make_unsaved will break in amusing ways unless you manually
130
+ # override the setters.
131
+ #
132
+ # From sequel-talk "Sequel does not have such an API and will not be adding one"
133
+ # Feb 17
134
+ describe "make_unsaved method" do
135
+ it "should not save the constructed object" do
136
+ Person.blueprint { }
137
+ person = Person.make_unsaved
138
+ person.should be_new
139
+ end
140
+
141
+ it "should save objects made within a passed-in block" do
142
+ Post.blueprint { }
143
+ Comment.blueprint { }
144
+ comment = nil
145
+ post = Post.make_unsaved { comment = Comment.make }
146
+ post.should be_new
147
+ comment.should_not be_new
148
+ end
149
+ end
150
+ end
151
+ end
data/spec/sham_spec.rb ADDED
@@ -0,0 +1,95 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'sham'
3
+
4
+ describe Sham do
5
+ it "should ensure generated values are unique" do
6
+ Sham.clear
7
+ Sham.half_index {|index| index/2 }
8
+ values = (1..10).map { Sham.half_index }
9
+ values.should == (0..9).to_a
10
+ end
11
+
12
+ it "should generate non-unique values when asked" do
13
+ Sham.clear
14
+ Sham.coin_toss(:unique => false) {|index| index % 2 == 1 ? 'heads' : 'tails' }
15
+ values = (1..4).map { Sham.coin_toss }
16
+ values.should == ['heads', 'tails', 'heads', 'tails']
17
+ end
18
+
19
+ it "should generate more than a dozen values" do
20
+ Sham.clear
21
+ Sham.index {|index| index }
22
+ values = (1..25).map { Sham.index }
23
+ values.should == (1..25).to_a
24
+ end
25
+
26
+ it "should generate the same sequence of values after a reset" do
27
+ Sham.clear
28
+ Sham.random { rand }
29
+ values1 = (1..10).map { Sham.random }
30
+ Sham.reset
31
+ values2 = (1..10).map { Sham.random }
32
+ values2.should == values1
33
+ end
34
+
35
+ it "should alias reset with reset(:before_all)" do
36
+ Sham.clear
37
+ Sham.random { rand }
38
+ values1 = (1..10).map { Sham.random }
39
+ Sham.reset(:before_all)
40
+ values2 = (1..10).map { Sham.random }
41
+ values2.should == values1
42
+ end
43
+
44
+ it "should generate the same sequence of values after each reset(:before_each)" do
45
+ Sham.clear
46
+ Sham.random { rand }
47
+ values1 = (1..10).map { Sham.random }
48
+ Sham.reset(:before_each)
49
+ values2 = (1..10).map { Sham.random }
50
+ Sham.reset(:before_each)
51
+ values3 = (1..10).map { Sham.random }
52
+ values2.should_not == values1
53
+ values3.should == values2
54
+ end
55
+
56
+ it "should generate a different sequence of values after reset(:before_all) followed by reset(:before_each)" do
57
+ Sham.clear
58
+ Sham.random { rand }
59
+ (1..10).map { Sham.random }
60
+ Sham.reset(:before_each)
61
+ values1 = (1..10).map { Sham.random }
62
+ Sham.reset(:before_all)
63
+ (1..5).map { Sham.random }
64
+ Sham.reset(:before_each)
65
+ values2 = (1..10).map { Sham.random }
66
+ values2.should_not == values1
67
+ end
68
+
69
+ it "should die when it runs out of unique values" do
70
+ Sham.clear
71
+ Sham.limited {|index| index%10 }
72
+ lambda {
73
+ (1..100).map { Sham.limited }
74
+ }.should raise_error(RuntimeError)
75
+ end
76
+
77
+ it "should allow over-riding the name method" do
78
+ Sham.clear
79
+ Sham.name {|index| index }
80
+ Sham.name.should == 1
81
+ end
82
+
83
+ describe "define method" do
84
+ it "should repeat messages in its block to Sham" do
85
+ block = Proc.new {}
86
+ Sham.should_receive(:name).with(&block).once.ordered
87
+ Sham.should_receive(:slug).with(:arg, &block).once.ordered
88
+ Sham.define do
89
+ name &block
90
+ slug :arg, &block
91
+ end
92
+ end
93
+ end
94
+
95
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ require 'rubygems'
3
+ require 'test/unit'
4
+ require 'spec'
5
+ require 'sham'
6
+
7
+ Spec::Runner.configure do |config|
8
+ config.before(:each) { Sham.reset }
9
+ end
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: JamieFlournoy-machinist
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 6
9
+ version: 1.0.6
10
+ platform: ruby
11
+ authors:
12
+ - Pete Yandell
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-23 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 2
30
+ - 8
31
+ version: 1.2.8
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: activerecord
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: sequel
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ type: :development
57
+ version_requirements: *id003
58
+ - !ruby/object:Gem::Dependency
59
+ name: dm-core
60
+ prerelease: false
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ type: :development
69
+ version_requirements: *id004
70
+ - !ruby/object:Gem::Dependency
71
+ name: dm-validations
72
+ prerelease: false
73
+ requirement: &id005 !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ type: :development
81
+ version_requirements: *id005
82
+ - !ruby/object:Gem::Dependency
83
+ name: data_objects
84
+ prerelease: false
85
+ requirement: &id006 !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ type: :development
93
+ version_requirements: *id006
94
+ - !ruby/object:Gem::Dependency
95
+ name: do_sqlite3
96
+ prerelease: false
97
+ requirement: &id007 !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ type: :development
105
+ version_requirements: *id007
106
+ description:
107
+ email: pete@notahat.com
108
+ executables: []
109
+
110
+ extensions: []
111
+
112
+ extra_rdoc_files:
113
+ - README.markdown
114
+ files:
115
+ - .autotest
116
+ - .gitignore
117
+ - FAQ.markdown
118
+ - MIT-LICENSE
119
+ - README.markdown
120
+ - Rakefile
121
+ - VERSION
122
+ - init.rb
123
+ - lib/machinist.rb
124
+ - lib/machinist/active_record.rb
125
+ - lib/machinist/blueprints.rb
126
+ - lib/machinist/data_mapper.rb
127
+ - lib/machinist/object.rb
128
+ - lib/machinist/sequel.rb
129
+ - lib/sham.rb
130
+ - machinist.gemspec
131
+ - spec/active_record_spec.rb
132
+ - spec/data_mapper_spec.rb
133
+ - spec/db/.gitignore
134
+ - spec/db/schema.rb
135
+ - spec/log/.gitignore
136
+ - spec/machinist_spec.rb
137
+ - spec/sequel_spec.rb
138
+ - spec/sham_spec.rb
139
+ - spec/spec_helper.rb
140
+ has_rdoc: true
141
+ homepage: http://github.com/notahat/machinist
142
+ licenses: []
143
+
144
+ post_install_message:
145
+ rdoc_options:
146
+ - --charset=UTF-8
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ segments:
154
+ - 0
155
+ version: "0"
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ segments:
161
+ - 0
162
+ version: "0"
163
+ requirements: []
164
+
165
+ rubyforge_project:
166
+ rubygems_version: 1.3.6
167
+ signing_key:
168
+ specification_version: 3
169
+ summary: Fixtures aren't fun. Machinist is.
170
+ test_files:
171
+ - spec/active_record_spec.rb
172
+ - spec/data_mapper_spec.rb
173
+ - spec/db/schema.rb
174
+ - spec/machinist_spec.rb
175
+ - spec/sequel_spec.rb
176
+ - spec/sham_spec.rb
177
+ - spec/spec_helper.rb