machinist 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,146 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'machinist/sequel'
3
+
4
+ # We have to define this here because Sequel needs a DB connection
5
+ # setup before you can define models
6
+ DB = Sequel.sqlite(:logger => Logger.new(File.dirname(__FILE__) + "/log/test.log"))
7
+
8
+ DB.create_table :people do
9
+ primary_key :id
10
+ String :name
11
+ String :type
12
+ String :password
13
+ Boolean :admin, :default => false
14
+ end
15
+
16
+ DB.create_table :posts do
17
+ primary_key :id
18
+ String :title
19
+ String :body
20
+ Boolean :published, :default => true
21
+ end
22
+
23
+ DB.create_table :comments do
24
+ primary_key :id
25
+ Integer :post_id
26
+ Integer :author_id
27
+ String :body
28
+ end
29
+
30
+ module MachinistSequelSpecs
31
+
32
+ class Person < Sequel::Model
33
+ set_restricted_columns :password
34
+ end
35
+
36
+ class Post < Sequel::Model
37
+ one_to_many :comments, :class => "MachinistSequelSpecs::Comment"
38
+ end
39
+
40
+ class Comment < Sequel::Model
41
+ many_to_one :post, :class => "MachinistSequelSpecs::Post"
42
+ many_to_one :author, :class => "MachinistSequelSpecs::Person"
43
+ end
44
+
45
+ describe Machinist, "Sequel adapter" do
46
+
47
+ before(:each) do
48
+ Person.clear_blueprints!
49
+ Post.clear_blueprints!
50
+ Comment.clear_blueprints!
51
+ end
52
+
53
+ describe "make method" do
54
+ it "should save the constructed object" do
55
+ Person.blueprint { }
56
+ person = Person.make
57
+ person.should_not be_new
58
+ end
59
+
60
+ it "should create and object through a many_to_one association" do
61
+ Post.blueprint { }
62
+ Comment.blueprint { post }
63
+ Comment.make.post.class.should == Post
64
+ end
65
+
66
+ it "should create an object through many_to_one association with a class_name attribute" do
67
+ Person.blueprint { }
68
+ Comment.blueprint { author }
69
+ Comment.make.author.class.should == Person
70
+ end
71
+
72
+ it "should allow setting a protected attribute in the blueprint" do
73
+ Person.blueprint do
74
+ password "Test"
75
+ end
76
+ Person.make.password.should == "Test"
77
+ end
78
+
79
+ it "should allow overriding a protected attribute" do
80
+ Person.blueprint do
81
+ password "Test"
82
+ end
83
+ Person.make(:password => "New").password.should == "New"
84
+ end
85
+
86
+ it "should allow setting the id attribute in a blueprint" do
87
+ Person.blueprint { id 12345 }
88
+ Person.make.id.should == 12345
89
+ end
90
+
91
+ # it "should allow setting the type attribute in a blueprint" do
92
+ # Person.blueprint { type "Person" }
93
+ # Person.make.type.should == "Person"
94
+ # end
95
+ end
96
+
97
+ describe "plan method" do
98
+ it "should not save the constructed object" do
99
+ lambda {
100
+ Person.blueprint { }
101
+ person = Person.plan
102
+ }.should_not change(Person,:count)
103
+ end
104
+
105
+ it "should return a regular attribute in the hash" do
106
+ Post.blueprint { title "Test" }
107
+ post = Post.plan
108
+ post[:title].should == "Test"
109
+ end
110
+
111
+ it "should create an object through a many_to_one association, and return its id" do
112
+ Post.blueprint { }
113
+ Comment.blueprint { post }
114
+ lambda {
115
+ comment = Comment.plan
116
+ comment[:post].should be_nil
117
+ comment[:post_id].should_not be_nil
118
+ }.should change(Post, :count).by(1)
119
+ end
120
+ end
121
+
122
+ # Note that building up an unsaved object graph using just the
123
+ # association methods is not possible in Sequel, so
124
+ # make_unsaved will break in amusing ways unless you manually
125
+ # override the setters.
126
+ #
127
+ # From sequel-talk "Sequel does not have such an API and will not be adding one"
128
+ # Feb 17
129
+ describe "make_unsaved method" do
130
+ it "should not save the constructed object" do
131
+ Person.blueprint { }
132
+ person = Person.make_unsaved
133
+ person.should be_new
134
+ end
135
+
136
+ it "should save objects made within a passed-in block" do
137
+ Post.blueprint { }
138
+ Comment.blueprint { }
139
+ comment = nil
140
+ post = Post.make_unsaved { comment = Comment.make }
141
+ post.should be_new
142
+ comment.should_not be_new
143
+ end
144
+ end
145
+ end
146
+ end
@@ -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 CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: machinist
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Yandell
@@ -9,33 +9,60 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-20 00:00:00 +11:00
12
+ date: 2009-11-29 00:00:00 +11:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.8
24
+ version:
16
25
  description:
17
- email: pete@nothat.com
26
+ email: pete@notahat.com
18
27
  executables: []
19
28
 
20
29
  extensions: []
21
30
 
22
- extra_rdoc_files: []
23
-
31
+ extra_rdoc_files:
32
+ - README.markdown
24
33
  files:
34
+ - .autotest
35
+ - .gitignore
36
+ - FAQ.markdown
37
+ - MIT-LICENSE
38
+ - README.markdown
39
+ - Rakefile
40
+ - VERSION
41
+ - init.rb
25
42
  - lib/machinist.rb
26
- - lib/sham.rb
27
- - lib/machinist/blueprints.rb
28
- - lib/machinist/object.rb
29
43
  - lib/machinist/active_record.rb
44
+ - lib/machinist/blueprints.rb
30
45
  - lib/machinist/data_mapper.rb
46
+ - lib/machinist/object.rb
31
47
  - lib/machinist/sequel.rb
48
+ - lib/sham.rb
49
+ - machinist.gemspec
50
+ - spec/active_record_spec.rb
51
+ - spec/data_mapper_spec.rb
52
+ - spec/db/.gitignore
53
+ - spec/db/schema.rb
54
+ - spec/log/.gitignore
55
+ - spec/machinist_spec.rb
56
+ - spec/sequel_spec.rb
57
+ - spec/sham_spec.rb
58
+ - spec/spec_helper.rb
32
59
  has_rdoc: true
33
60
  homepage: http://github.com/notahat/machinist
34
61
  licenses: []
35
62
 
36
63
  post_install_message:
37
- rdoc_options: []
38
-
64
+ rdoc_options:
65
+ - --charset=UTF-8
39
66
  require_paths:
40
67
  - lib
41
68
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -57,5 +84,11 @@ rubygems_version: 1.3.5
57
84
  signing_key:
58
85
  specification_version: 3
59
86
  summary: Fixtures aren't fun. Machinist is.
60
- test_files: []
61
-
87
+ test_files:
88
+ - spec/active_record_spec.rb
89
+ - spec/data_mapper_spec.rb
90
+ - spec/db/schema.rb
91
+ - spec/machinist_spec.rb
92
+ - spec/sequel_spec.rb
93
+ - spec/sham_spec.rb
94
+ - spec/spec_helper.rb