active_mocker 1.7.3 → 1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a0c73656be49d155784e88ad0bdef8cf2440cc22
4
- data.tar.gz: 9a8c27c4c39255bb523bd30fe0ae377f2069143e
3
+ metadata.gz: 6d37886b706ee7f0b5c853537f5032c8c06c1020
4
+ data.tar.gz: 216a6de43827e700ed4da36cbb5a565fbf4923ee
5
5
  SHA512:
6
- metadata.gz: 6991147d32ce7ed27729922506adaa0930b320ecd2c763bedcf096363e7cace498b38a5d91c1467593b18a6a97ec31fa692a33b1f790ab28e863c5a24519674e
7
- data.tar.gz: 726e67dc6b993de80f1dd8fd34135f4c0ae61bc5933155887df8a1e2c9c758aaecb6e084ce004839cdad6480f53a82428fe2464131ae8626d1ab5323cd62332c
6
+ metadata.gz: 230736f97cc1d137031c30fe013e848e23a09f523d9c4110c54eb3448bdfa1da0cdb33468693b7a0571d0b59ea3fb0de7c405cf22e4b2e8cec10059784fde52e
7
+ data.tar.gz: 782b7f7577fdd3054c69239cce1e6e693eddd5e3f32267f934091d6a7379a1eb9a33b876b83eb8327fe79de8f15bf1c84b75616f7d3e8f7d5de8e97ad9f0e3c2
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
+
4
+ ## 1.8 - 2015-02-17
5
+
6
+ ### Notes
7
+ This release has minor speed improvements. You may find that some records where an attribute was nil it will now have an a value. Next release will most likely be 2.0 where I will focus on removing deprecated features and performance of the mocks runtime.
8
+
9
+ ### Enhancement
10
+ - Improve accuracy and speed in some cases, for finding associations by not assigning them at creation time but finding them when called.
11
+ - Hide the internals stack trace when calling method that will raising `NotImplementedError`.
12
+ - Add spec documentation for method #new_relation
13
+
14
+ ### Added
15
+ - Support for ruby 2.2.0 and rails 4.2
16
+ - new method #none does what it does for ActiveRecord
17
+
3
18
  ## 1.7.3 - 2014-12-01
4
19
 
5
20
  ### Fix
data/README.md CHANGED
@@ -312,6 +312,7 @@ See [Documentation](http://rdoc.info/github/zeisler/active_mocker/master/ActiveM
312
312
  * order(:field_name)
313
313
  * reverse_order
314
314
  * limit
315
+ * none
315
316
 
316
317
  **Relation Methods** - [docs](http://rdoc.info/github/zeisler/active_mocker/master/ActiveMocker/Mock/Collection)
317
318
  * concat
@@ -1,3 +1,4 @@
1
+ require 'active_support/deprecation'
1
2
  require 'active_support/dependencies/autoload'
2
3
  require 'active_support/hash_with_indifferent_access'
3
4
  require 'active_support/core_ext/module/delegation'
@@ -22,7 +23,6 @@ require 'active_mocker/mock/mock_abilities'
22
23
  require 'active_mocker/mock/exceptions'
23
24
  require 'active_mocker/mock/template_methods'
24
25
  require 'active_mocker/mock/do_nothing_active_record_methods'
25
- require 'active_mocker/mock/next_id'
26
26
  require 'active_mocker/mock/records'
27
27
  require 'active_mocker/mock/object_inspect'
28
28
  require 'active_mocker/mock/base'
@@ -5,11 +5,23 @@ module ActiveMocker
5
5
 
6
6
  attr_reader :item
7
7
 
8
- def initialize(item, child_self:, foreign_key:, foreign_id:)
9
- child_self.send(:write_attribute, foreign_key, foreign_id) if item.try(:persisted?)
8
+ def initialize(item, child_self:, foreign_key:)
9
+ save_item(item, child_self)
10
+ assign_foreign_key(child_self, foreign_key, item.try(:id))
10
11
  super
11
12
  end
12
13
 
14
+ private
15
+
16
+ def assign_foreign_key(child_self, foreign_key, foreign_id)
17
+ child_self.send(:write_attribute, foreign_key, foreign_id)
18
+ end
19
+
20
+ def save_item(item, child_self)
21
+ return if item.nil?
22
+ item.try(:save) if child_self.persisted?
23
+ end
24
+
13
25
  end
14
26
 
15
27
  end
@@ -6,6 +6,10 @@ module Mock
6
6
  class Collection
7
7
 
8
8
  include Enumerable
9
+ extend ::Forwardable
10
+ def_delegators :@collection, :[], :take, :push, :clear, :first, :last, :concat, :replace, :uniq, :count, :size, :length, :empty?, :any?, :many?, :include?, :delete
11
+ alias_method :distinct, :uniq
12
+
9
13
 
10
14
  def initialize(collection=[])
11
15
  @collection = [*collection]
@@ -15,23 +19,9 @@ module Mock
15
19
  collection.concat(records.flatten)
16
20
  end
17
21
 
18
- extend ::Forwardable
19
- def_delegators :@collection, :[], :take, :push, :clear, :first, :last, :concat, :replace, :uniq, :count, :size, :length, :empty?, :any?, :many?, :include?, :delete
20
- alias_method :distinct, :uniq
21
-
22
- def select(&block)
23
- collection.select(&block)
24
- end
25
-
26
22
  def each(&block)
27
23
  collection.each do |item|
28
- block.call(item)
29
- end
30
- end
31
-
32
- def map(&block)
33
- collection.map do |item|
34
- block.call(item)
24
+ yield(item)
35
25
  end
36
26
  end
37
27
 
@@ -51,7 +41,6 @@ module Mock
51
41
  @collection == val
52
42
  end
53
43
 
54
- # Returns true if relation is blank.
55
44
  def blank?
56
45
  to_a.blank?
57
46
  end
@@ -5,8 +5,8 @@ module ActiveMocker
5
5
 
6
6
  attr_reader :item
7
7
 
8
- def initialize(item, child_self:, foreign_key:, foreign_id:)
9
- item.send(:write_attribute, foreign_key, foreign_id) if item.respond_to?("#{foreign_key}=") && !foreign_id.nil?
8
+ def initialize(item, child_self:, foreign_key:)
9
+ item.send(:write_attribute, foreign_key, item.try(:id)) if !item.try(:id).nil?
10
10
  super
11
11
  end
12
12
 
@@ -26,8 +26,8 @@ module ActiveMocker
26
26
  self.class
27
27
  end
28
28
 
29
- def is_implemented(val, method, type)
30
- raise NotImplementedError, "#{type}#{method} for Class: #{class_name}. To continue stub the method." if val.nil?
29
+ def is_implemented(val, method, type, call_stack)
30
+ raise NotImplementedError, "#{type}#{method} for Class: #{class_name}. To continue stub the method.", call_stack if val.nil?
31
31
  end
32
32
 
33
33
  def execute_block(method)
@@ -55,9 +55,9 @@ module ActiveMocker
55
55
 
56
56
  alias_method :stub_class_method, :mock_class_method
57
57
 
58
- def call_mock_method(method, *arguments)
58
+ def call_mock_method(method, caller, *arguments)
59
59
  mock_method = mockable_class_methods[method.to_sym]
60
- is_implemented(mock_method, method, '::')
60
+ is_implemented(mock_method, method, '::', caller)
61
61
  mock_method.arguments = arguments
62
62
  execute_block(mock_method)
63
63
  end
@@ -68,10 +68,10 @@ module ActiveMocker
68
68
 
69
69
  include InstanceAndClassMethods
70
70
 
71
- def call_mock_method(method, *arguments)
71
+ def call_mock_method(method, caller, *arguments)
72
72
  mock_method = mockable_instance_methods[method.to_sym]
73
73
  mock_method = self.class.send(:mockable_instance_methods)[method.to_sym] if mock_method.nil?
74
- is_implemented(mock_method, method, '#')
74
+ is_implemented(mock_method, method, '#', caller)
75
75
  mock_method.arguments = arguments
76
76
  execute_block mock_method
77
77
  end
@@ -11,11 +11,24 @@ module Mock
11
11
 
12
12
  def is_of(conditions={})
13
13
  conditions.all? do |col, match|
14
- next match.any? { |m| @record.send(col) == m } if match.is_a? Enumerable
15
- @record.send(col) == match
14
+ if match.is_a? Enumerable
15
+ any_match(col, match)
16
+ else
17
+ compare(col, match)
18
+ end
16
19
  end
17
20
  end
18
21
 
22
+ private
23
+
24
+ def any_match(col, match)
25
+ match.any? { |m| compare(col, m) }
26
+ end
27
+
28
+ def compare(col, match)
29
+ @record.send(col) == match
30
+ end
31
+
19
32
  end
20
33
 
21
34
  class WhereNotChain
@@ -145,8 +158,8 @@ module Mock
145
158
  #
146
159
  # # Update all books that match conditions, but limit it to 5 ordered by date
147
160
  # BookMock.where(title: 'Rails').order(:created_at).limit(5).update_all(author: 'David')
148
- def update_all(conditions)
149
- all.each { |i| i.update(conditions) }
161
+ def update_all(attributes)
162
+ all.each { |i| i.update(attributes) }
150
163
  end
151
164
 
152
165
  # Updates an object (or multiple objects) and saves it.
@@ -182,7 +195,9 @@ module Mock
182
195
  #
183
196
  # Post.find_by name: 'Spartacus', rating: 4
184
197
  def find_by(conditions = {})
185
- send(:where, conditions).first
198
+ to_a.detect do |record|
199
+ Find.new(record).is_of(conditions)
200
+ end
186
201
  end
187
202
 
188
203
  # Like <tt>find_by</tt>, except that if no record is found, raises
@@ -306,6 +321,34 @@ module Mock
306
321
  new_relation(to_a || [])
307
322
  end
308
323
 
324
+ # Returns a chainable relation with zero records.
325
+ #
326
+ # Any subsequent condition chained to the returned relation will continue
327
+ # generating an empty relation.
328
+ #
329
+ # Used in cases where a method or scope could return zero records but the
330
+ # result needs to be chainable.
331
+ #
332
+ # For example:
333
+ #
334
+ # @posts = current_user.visible_posts.where(name: params[:name])
335
+ # # => the visible_posts method is expected to return a chainable Relation
336
+ #
337
+ # def visible_posts
338
+ # case role
339
+ # when 'Country Manager'
340
+ # Post.where(country: country)
341
+ # when 'Reviewer'
342
+ # Post.published
343
+ # when 'Bad User'
344
+ # Post.none # It can't be chained if [] is returned.
345
+ # end
346
+ # end
347
+ #
348
+ def none
349
+ new_relation([])
350
+ end
351
+
309
352
  private
310
353
 
311
354
  def values_by_key(key)
@@ -1,62 +1,62 @@
1
1
  module ActiveMocker
2
- module Mock
3
- class Records
2
+ module Mock
3
+ class Records
4
4
 
5
- extend Forwardable
6
- def_delegators :records, :<<, :count, :length, :to_a
5
+ extend Forwardable
6
+ def_delegators :records, :<<, :count, :length, :to_a
7
7
 
8
- attr_reader :records, :record_index
9
- private :records, :record_index
10
- def initialize(records = [])
11
- @records ||= records
12
- @record_index ||= {}
13
- end
8
+ attr_reader :records
9
+ private :records
14
10
 
15
- def insert(record)
16
- record.attributes[:id] ||= next_id
17
- validate_unique_id(record)
18
- add_to_record_index(record)
19
- records << record
20
- end
11
+ def initialize(records = [])
12
+ @records = records
13
+ end
21
14
 
22
- def delete(record)
23
- raise RecordNotFound, 'Record has not been created.' if new_record?(record)
24
- record_index.delete("#{record.id}")
25
- index = records.index(record)
26
- records.delete_at(index)
27
- end
15
+ def insert(record)
16
+ records << validate_unique_id((record.id ||= next_id), record)
17
+ end
28
18
 
29
- def exists?(record)
30
- records.include?(record)
31
- end
19
+ def delete(record)
20
+ raise RecordNotFound, 'Record has not been created.' unless records.delete(record)
21
+ end
32
22
 
33
- def new_record?(record)
34
- !exists?(record)
35
- end
23
+ def exists?(record)
24
+ records.include?(record)
25
+ end
36
26
 
37
- def persisted?(id)
38
- records.map(&:id).include?(id)
39
- end
27
+ def new_record?(record)
28
+ !exists?(record)
29
+ end
40
30
 
41
- def reset
42
- record_index.clear
43
- records.clear
44
- end
31
+ def persisted?(id)
32
+ ids.include?(id)
33
+ end
45
34
 
46
- private
35
+ def reset
36
+ records.clear
37
+ end
47
38
 
48
- def next_id
49
- NextId.new(records).next
50
- end
39
+ private
51
40
 
52
- def add_to_record_index(record)
53
- record_index.merge!({record.id.to_s => records.length})
54
- end
41
+ def ids
42
+ records.map(&:id)
43
+ end
55
44
 
56
- def validate_unique_id(record)
57
- raise IdError, "Duplicate ID found for record #{record.inspect}" if record_index.has_key?(record.id.to_s)
58
- end
45
+ def next_id
46
+ max_record.succ
47
+ rescue NoMethodError
48
+ 1
49
+ end
59
50
 
60
- end
61
- end
51
+ def max_record
52
+ ids.max
53
+ end
54
+
55
+ def validate_unique_id(id, record)
56
+ raise IdError, "Duplicate ID found for record #{record.inspect}" if persisted?(id)
57
+ record
58
+ end
59
+
60
+ end
61
+ end
62
62
  end
@@ -5,7 +5,7 @@ module ActiveMocker
5
5
 
6
6
  attr_reader :item
7
7
 
8
- def initialize(item, child_self:, foreign_key:, foreign_id:)
8
+ def initialize(item, child_self:, foreign_key:)
9
9
  @item = item
10
10
  assign_associations(child_self, item) if item.class <= Base
11
11
  end
@@ -63,12 +63,7 @@ class <%= class_name + @mock_append_name %> < <%= parent_class %>
63
63
  end
64
64
 
65
65
  def <%= meth.name %>=(val)
66
- <% association = belongs_to_foreign_key(meth.name) -%>
67
66
  write_attribute(:<%= meth.name %>, val)
68
- <% if association -%>
69
- association = classes('<%= association.class_name %>').try(:find_by, id: <%= meth.name %>)
70
- write_association(:<%= association.name %>,association) unless association.nil?
71
- <% end -%>
72
67
  end
73
68
  <% end %>
74
69
  ##################################
@@ -78,12 +73,13 @@ class <%= class_name + @mock_append_name %> < <%= parent_class %>
78
73
  <%= '# belongs_to' unless belongs_to.empty? -%>
79
74
  <% belongs_to.each do |meth| %>
80
75
  def <%= meth.name %>
81
- read_association(:<%= meth.name %>)
76
+ <% association = relation_find(:name, meth.name).first -%>
77
+ read_association(:<%= meth.name %>) || write_association(:<%= meth.name %>, classes('<%= association.class_name %>').try{ |k| k.find_by(id: <%= association.foreign_key %>)})
82
78
  end
83
79
 
84
80
  def <%= meth.name %>=(val)
85
81
  write_association(:<%= meth.name %>, val)
86
- ActiveMocker::Mock::BelongsTo.new(val, child_self: self, foreign_key: :<%= meth.foreign_key %>, foreign_id: val.try(:id)).item
82
+ ActiveMocker::Mock::BelongsTo.new(val, child_self: self, foreign_key: :<%= meth.foreign_key %>).item
87
83
  end
88
84
 
89
85
  def build_<%= meth.name %>(attributes={}, &block)
@@ -111,7 +107,7 @@ class <%= class_name + @mock_append_name %> < <%= parent_class %>
111
107
 
112
108
  def <%= meth.name %>=(val)
113
109
  write_association(:<%= meth.name %>, val)
114
- ActiveMocker::Mock::HasOne.new(val, child_self: self, foreign_key: '<%= meth.foreign_key %>', foreign_id: self.id).item
110
+ ActiveMocker::Mock::HasOne.new(val, child_self: self, foreign_key: '<%= meth.foreign_key %>').item
115
111
  end
116
112
 
117
113
  def build_<%= meth.name %>(attributes={}, &block)
@@ -182,12 +178,12 @@ class <%= class_name + @mock_append_name %> < <%= parent_class %>
182
178
 
183
179
  <% instance_methods.each do |method| %>
184
180
  def <%= method.name %><%= "(#{method.arguments})" unless method.arguments.empty? %>
185
- call_mock_method :<%= method.name %><%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %>
181
+ call_mock_method :<%= method.name %>, Kernel.caller<%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %>
186
182
  end
187
183
  <% end -%>
188
184
  <% class_methods.each do |method| %>
189
185
  def self.<%= method.name %><%= "(#{method.arguments})" unless method.arguments.empty? %>
190
- call_mock_method :<%= method.name %><%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %>
186
+ call_mock_method :<%= method.name %>, Kernel.caller<%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %>
191
187
  end
192
188
  <% end -%>
193
189
 
@@ -1,3 +1,3 @@
1
1
  module ActiveMocker
2
- VERSION = "1.7.3"
2
+ VERSION = "1.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_mocker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.3
4
+ version: '1.8'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Zeisler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-02 00:00:00.000000000 Z
11
+ date: 2015-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '2.1'
61
+ version: '2.2'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '2.1'
68
+ version: '2.2'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: unparser
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -95,33 +95,33 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0.0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: bundler
98
+ name: rake
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '1.5'
104
- type: :development
103
+ version: '10.0'
104
+ type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '1.5'
110
+ version: '10.0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: rake
112
+ name: bundler
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '10.4'
117
+ version: '1.5'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '10.4'
124
+ version: '1.5'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: rspec
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -178,20 +178,6 @@ dependencies:
178
178
  - - "~>"
179
179
  - !ruby/object:Gem::Version
180
180
  version: '3.5'
181
- - !ruby/object:Gem::Dependency
182
- name: fuubar
183
- requirement: !ruby/object:Gem::Requirement
184
- requirements:
185
- - - "~>"
186
- - !ruby/object:Gem::Version
187
- version: '2.0'
188
- type: :development
189
- prerelease: false
190
- version_requirements: !ruby/object:Gem::Requirement
191
- requirements:
192
- - - "~>"
193
- - !ruby/object:Gem::Version
194
- version: '2.0'
195
181
  - !ruby/object:Gem::Dependency
196
182
  name: appraisal
197
183
  requirement: !ruby/object:Gem::Requirement
@@ -251,7 +237,6 @@ files:
251
237
  - lib/active_mocker/mock/has_one.rb
252
238
  - lib/active_mocker/mock/hash_process.rb
253
239
  - lib/active_mocker/mock/mock_abilities.rb
254
- - lib/active_mocker/mock/next_id.rb
255
240
  - lib/active_mocker/mock/object_inspect.rb
256
241
  - lib/active_mocker/mock/queries.rb
257
242
  - lib/active_mocker/mock/records.rb
@@ -283,7 +268,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
283
268
  requirements:
284
269
  - - ">="
285
270
  - !ruby/object:Gem::Version
286
- version: 2.1.0
271
+ version: 2.1.5
287
272
  required_rubygems_version: !ruby/object:Gem::Requirement
288
273
  requirements:
289
274
  - - ">="
@@ -291,7 +276,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
291
276
  version: '0'
292
277
  requirements: []
293
278
  rubyforge_project:
294
- rubygems_version: 2.2.2
279
+ rubygems_version: 2.4.5
295
280
  signing_key:
296
281
  specification_version: 4
297
282
  summary: Creates mocks from Active Record models. Allows your test suite to run very
@@ -1,24 +0,0 @@
1
- module ActiveMocker
2
- module Mock
3
- # @api private
4
- class NextId
5
-
6
- def initialize(records)
7
- @records = records
8
- end
9
-
10
- def next
11
- return 1 if max_record.nil?
12
- return max_record.id.succ if max_record.id.is_a?(Numeric)
13
- raise IdNotNumber
14
- end
15
-
16
- private
17
-
18
- def max_record
19
- @max_record ||= @records.max { |a, b| a.id <=> b.id }
20
- end
21
-
22
- end
23
- end
24
- end