ardm-sweatshop 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,166 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMapper::Sweatshop do
4
+
5
+ class Parent
6
+ include DataMapper::Resource
7
+ property :id, Serial
8
+ property :type, Discriminator
9
+ property :first_name, String
10
+ property :last_name, String
11
+ end
12
+
13
+ class Child < Parent
14
+ property :age, Integer
15
+ end
16
+
17
+ before(:each) do
18
+ DataMapper.auto_migrate!
19
+ DataMapper::Sweatshop.model_map.clear
20
+ DataMapper::Sweatshop.record_map.clear
21
+ end
22
+
23
+ supported_by :all do
24
+
25
+ describe ".model_map" do
26
+ it "should return a Hash if the model is not mapped" do
27
+ DataMapper::Sweatshop.model_map[Class.new].should be_is_a(Hash)
28
+ end
29
+
30
+ it "should return a map for names to an array of procs if the model is not mapped" do
31
+ DataMapper::Sweatshop.model_map[Class.new][:unnamed].should be_is_a(Array)
32
+ end
33
+ end
34
+
35
+ describe ".add" do
36
+ it "should app a generator proc to the model map" do
37
+ proc = lambda {}
38
+ lambda {
39
+ DataMapper::Sweatshop.add(Parent, :default, &proc)
40
+ }.should change {
41
+ DataMapper::Sweatshop.model_map[Parent][:default].first
42
+ }.from(nil).to(proc)
43
+ end
44
+
45
+ it "should push repeat procs onto the mapped array" do
46
+ proc1, proc2 = lambda {}, lambda {}
47
+
48
+ DataMapper::Sweatshop.add(Parent, :default, &proc1)
49
+ DataMapper::Sweatshop.add(Parent, :default, &proc2)
50
+
51
+ DataMapper::Sweatshop.model_map[Parent][:default].first.should == proc1
52
+ DataMapper::Sweatshop.model_map[Parent][:default].last.should == proc2
53
+ end
54
+ end
55
+
56
+ describe ".expand_callable_values" do
57
+ it 'evalues values that respond to call' do
58
+ DataMapper::Sweatshop.
59
+ expand_callable_values({ :value => Proc.new { "a" + "b" } }).
60
+ should == { :value => "ab" }
61
+ end
62
+ end
63
+
64
+ describe ".attributes" do
65
+ it "should return an attributes hash" do
66
+ DataMapper::Sweatshop.add(Parent, :default) {{
67
+ :first_name => /\w+/.gen.capitalize,
68
+ :last_name => /\w+/.gen.capitalize
69
+ }}
70
+
71
+ DataMapper::Sweatshop.attributes(Parent, :default).should be_is_a(Hash)
72
+ end
73
+
74
+ it "should call the attribute proc on each call to attributes" do
75
+ calls = 0
76
+ proc = lambda {{:calls => (calls += 1)}}
77
+
78
+ DataMapper::Sweatshop.add(Parent, :default, &proc)
79
+ DataMapper::Sweatshop.attributes(Parent, :default).should == {:calls => 1}
80
+ DataMapper::Sweatshop.attributes(Parent, :default).should == {:calls => 2}
81
+ end
82
+
83
+ it "expands callable values" do
84
+ DataMapper::Sweatshop.add(Parent, :default) do
85
+ { :value => Proc.new { "a" + "b" } }
86
+ end
87
+ DataMapper::Sweatshop.attributes(Parent, :default).should == {
88
+ :value => "ab"
89
+ }
90
+ end
91
+
92
+ it "should call attributes with the superclass if the class is not mapped" do
93
+ DataMapper::Sweatshop.add(Parent, :default) {{:first_name => 'Bob'}}
94
+ DataMapper::Sweatshop.attributes(Child, :default).should == {:first_name => 'Bob'}
95
+ end
96
+
97
+ it "should raise an error if neither the class or it's parent class(es) have been mapped" do
98
+ lambda { DataMapper::Sweatshop.attributes(Child, :default) }.
99
+ should raise_error(DataMapper::Sweatshop::NoFixtureExist, /default fixture was not found for class/)
100
+ end
101
+ end
102
+
103
+ describe ".create" do
104
+ it "should call create on the model class with the attributes generated from a mapped proc" do
105
+ DataMapper::Sweatshop.add(Parent, :default) {{
106
+ :first_name => 'Kenny',
107
+ :last_name => 'Rogers'
108
+ }}
109
+
110
+ Parent.should_receive(:create).with(:first_name => 'Kenny', :last_name => 'Rogers')
111
+
112
+ DataMapper::Sweatshop.create(Parent, :default)
113
+ end
114
+
115
+ it "should call create on the model with a parent class' mapped attributes proc when the original class has not been maped" do
116
+ DataMapper::Sweatshop.add(Parent, :default) {{
117
+ :first_name => 'Kenny',
118
+ :last_name => 'Rogers'
119
+ }}
120
+
121
+ Child.should_receive(:create).with(:first_name => 'Kenny', :last_name => 'Rogers')
122
+
123
+ DataMapper::Sweatshop.create(Child, :default)
124
+ end
125
+
126
+ it "should merge in any attributes as an argument" do
127
+ DataMapper::Sweatshop.add(Parent, :default) {{
128
+ :first_name => 'Kenny',
129
+ :last_name => 'Rogers'
130
+ }}
131
+
132
+ Parent.should_receive(:create).with(:first_name => 'Roddy', :last_name => 'Rogers')
133
+
134
+ DataMapper::Sweatshop.create(Parent, :default, :first_name => 'Roddy')
135
+ end
136
+ end
137
+
138
+ describe ".make" do
139
+ it "should call new on the model class with the attributes generated from a mapped proc" do
140
+ DataMapper::Sweatshop.add(Parent, :default) {{
141
+ :first_name => 'Kenny',
142
+ :last_name => 'Rogers'
143
+ }}
144
+
145
+ Parent.should_receive(:new).with(:first_name => 'Kenny', :last_name => 'Rogers')
146
+
147
+ DataMapper::Sweatshop.make(Parent, :default)
148
+ end
149
+ end
150
+
151
+ describe ".pick" do
152
+ it "should return a pre existing instance of a model from the record map" do
153
+ DataMapper::Sweatshop.add(Parent, :default) {{
154
+ :first_name => 'George',
155
+ :last_name => 'Clinton'
156
+ }}
157
+
158
+ DataMapper::Sweatshop.create(Parent, :default)
159
+
160
+ DataMapper::Sweatshop.pick(Parent, :default).should be_is_a(Parent)
161
+ end
162
+ end
163
+
164
+ end
165
+
166
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ begin
4
+ require 'parse_tree'
5
+ rescue LoadError
6
+ skip_tests = true
7
+ end
8
+
9
+ describe DataMapper::Sweatshop::Unique do
10
+ describe '#unique' do
11
+ before(:each) do
12
+ @ss = DataMapper::Sweatshop
13
+ DataMapper::Sweatshop::UniqueWorker.class_eval do
14
+ self.count_map = Hash.new() { 0 }
15
+ end
16
+ end
17
+
18
+ unless skip_tests
19
+ it 'for the same block, yields an incrementing value' do
20
+ (1..3).to_a.collect { @ss.unique {|x| "a#{x}"} }.should ==
21
+ %w(a0 a1 a2)
22
+ end
23
+
24
+ it 'for the different blocks, yields separately incrementing values' do
25
+ (1..3).to_a.collect { @ss.unique {|x| "a#{x}"} }.should ==
26
+ %w(a0 a1 a2)
27
+ (1..3).to_a.collect { @ss.unique {|x| "b#{x}"} }.should ==
28
+ %w(b0 b1 b2)
29
+ (1..3).to_a.collect { @ss.unique {|x| "a#{x}"} }.should ==
30
+ %w(a3 a4 a5)
31
+ end
32
+ else
33
+ it 'requires the ParseTree gem to test'
34
+ end
35
+
36
+ describe 'when the block has an arity less than 1' do
37
+ it 'keeps yielding until a unique value is generated' do
38
+ a = [1,1,1,2]
39
+ (1..2).collect { @ss.unique(:a) { a.shift }}.should ==
40
+ [1, 2]
41
+ end
42
+
43
+ it 'raises when a unique value cannot be generated' do
44
+ a = [1,1,1, nil]
45
+ lambda {
46
+ (1..3).collect { @ss.unique(:a) { a.shift }}
47
+ }.should raise_error(DataMapper::Sweatshop::Unique::TooManyTriesException)
48
+ end
49
+ end
50
+
51
+ it 'allows an optional key to be specified' do
52
+ (1..3).to_a.collect { @ss.unique(:a) {|x| "a#{x}"} }.should ==
53
+ %w(a0 a1 a2)
54
+ (1..3).to_a.collect { @ss.unique(:b) {|x| "a#{x}"} }.should ==
55
+ %w(a0 a1 a2)
56
+ end
57
+
58
+ describe 'when ParseTree is unavilable' do
59
+ it 'raises when no key is provided' do
60
+ Object.stub!(:const_defined?).with("ParseTree").and_return(false)
61
+ lambda {
62
+ @ss.unique {}
63
+ }.should raise_error
64
+ end
65
+
66
+ it 'does not raise when a key is provided' do
67
+ lambda {
68
+ @ss.unique(:a) { "a" }
69
+ }.should_not raise_error
70
+ end
71
+ end
72
+ end
73
+
74
+ describe 'when mixing into an object' do
75
+ it 'only the unique method is added to the public interface' do
76
+ obj = Object.new
77
+ old = obj.methods
78
+ obj.extend(DataMapper::Sweatshop::Unique)
79
+ obj.should respond_to(:unique)
80
+ new = obj.methods
81
+ (new - old).collect {|x| x.to_s }.should == ["unique"]
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,6 @@
1
+ --exclude "spec,^/"
2
+ --sort coverage
3
+ --callsites
4
+ --xrefs
5
+ --profile
6
+ --text-summary
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --loadby random
3
+ --format profile
4
+ --backtrace
@@ -0,0 +1,17 @@
1
+ require 'dm-core/spec/setup'
2
+ require 'dm-core/spec/lib/adapter_helpers'
3
+
4
+ require 'dm-sweatshop'
5
+ require 'dm-migrations'
6
+ require 'dm-validations'
7
+
8
+ Spec::Runner.configure do |config|
9
+ config.extend(DataMapper::Spec::Adapters::Helpers)
10
+ end
11
+
12
+ begin
13
+ Randexp::Dictionary.load_dictionary
14
+ rescue RuntimeError
15
+ warn '[WARNING] Neither /usr/share/dict/words or /usr/dict/words found, skipping dm-sweatshop specs'
16
+ exit
17
+ end
@@ -0,0 +1,38 @@
1
+ spec_defaults = lambda do |spec|
2
+ spec.pattern = 'spec/**/*_spec.rb'
3
+ spec.libs << 'lib' << 'spec'
4
+ spec.spec_opts << '--options' << 'spec/spec.opts'
5
+ end
6
+
7
+ begin
8
+ require 'spec/rake/spectask'
9
+
10
+ Spec::Rake::SpecTask.new(:spec, &spec_defaults)
11
+ rescue LoadError
12
+ task :spec do
13
+ abort 'rspec is not available. In order to run spec, you must: gem install rspec'
14
+ end
15
+ end
16
+
17
+ begin
18
+ require 'rcov'
19
+ require 'spec/rake/verify_rcov'
20
+
21
+ Spec::Rake::SpecTask.new(:rcov) do |rcov|
22
+ spec_defaults.call(rcov)
23
+ rcov.rcov = true
24
+ rcov.rcov_opts = File.read('spec/rcov.opts').split(/\s+/)
25
+ end
26
+
27
+ RCov::VerifyTask.new(:verify_rcov => :rcov) do |rcov|
28
+ rcov.threshold = 100
29
+ end
30
+ rescue LoadError
31
+ %w[ rcov verify_rcov ].each do |name|
32
+ task name do
33
+ abort "rcov is not available. In order to run #{name}, you must: gem install rcov"
34
+ end
35
+ end
36
+ end
37
+
38
+ task :default => :spec
@@ -0,0 +1,9 @@
1
+ begin
2
+ require 'yard'
3
+
4
+ YARD::Rake::YardocTask.new
5
+ rescue LoadError
6
+ task :yard do
7
+ abort 'YARD is not available. In order to run yard, you must: gem install yard'
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ begin
2
+ require 'pathname'
3
+ require 'yardstick/rake/measurement'
4
+ require 'yardstick/rake/verify'
5
+
6
+ # yardstick_measure task
7
+ Yardstick::Rake::Measurement.new
8
+
9
+ # verify_measurements task
10
+ Yardstick::Rake::Verify.new do |verify|
11
+ verify.threshold = 100
12
+ end
13
+ rescue LoadError
14
+ %w[ yardstick_measure verify_measurements ].each do |name|
15
+ task name.to_s do
16
+ abort "Yardstick is not available. In order to run #{name}, you must: gem install yardstick"
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ardm-sweatshop
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Emde
8
+ - Ben Burkert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-01-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ardm-core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: randexp
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '0.1'
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: 0.1.5
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - "~>"
43
+ - !ruby/object:Gem::Version
44
+ version: '0.1'
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.5
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ description: DataMapper plugin for building pseudo random models
77
+ email:
78
+ - me@martinemde.com
79
+ - ben [a] benburkert [d] com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files:
83
+ - LICENSE
84
+ - README.rdoc
85
+ files:
86
+ - ".gitignore"
87
+ - ".travis.yml"
88
+ - Gemfile
89
+ - LICENSE
90
+ - README.rdoc
91
+ - Rakefile
92
+ - ardm-sweatshop.gemspec
93
+ - lib/ardm-sweatshop.rb
94
+ - lib/dm-sweatshop.rb
95
+ - lib/dm-sweatshop/model.rb
96
+ - lib/dm-sweatshop/support/class_attributes.rb
97
+ - lib/dm-sweatshop/sweatshop.rb
98
+ - lib/dm-sweatshop/unique.rb
99
+ - lib/dm-sweatshop/version.rb
100
+ - spec/dm-sweatshop/model_spec.rb
101
+ - spec/dm-sweatshop/sweatshop_spec.rb
102
+ - spec/dm-sweatshop/unique_spec.rb
103
+ - spec/rcov.opts
104
+ - spec/spec.opts
105
+ - spec/spec_helper.rb
106
+ - tasks/spec.rake
107
+ - tasks/yard.rake
108
+ - tasks/yardstick.rake
109
+ homepage: https://github.com/ar-dm/ardm-sweatshop
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.2.2
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Ardm fork of dm-sweatshop
133
+ test_files: []
134
+ has_rdoc: