active_mocker 2.0.0.beta1 → 2.0.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -1
- data/README.md +11 -18
- data/lib/active_mocker.rb +5 -0
- data/lib/active_mocker/config.rb +7 -8
- data/lib/active_mocker/{mock → deprecated_components}/mock_abilities.rb +19 -7
- data/lib/active_mocker/deprecated_components/rspec.rb +12 -0
- data/lib/active_mocker/display_errors.rb +64 -0
- data/lib/active_mocker/error_object.rb +46 -0
- data/lib/active_mocker/generate.rb +30 -64
- data/lib/active_mocker/mock.rb +1 -2
- data/lib/active_mocker/mock/association.rb +0 -2
- data/lib/active_mocker/mock/base.rb +270 -256
- data/lib/active_mocker/mock/belongs_to.rb +14 -20
- data/lib/active_mocker/mock/collection.rb +0 -6
- data/lib/active_mocker/mock/do_nothing_active_record_methods.rb +39 -41
- data/lib/active_mocker/mock/exceptions.rb +4 -11
- data/lib/active_mocker/mock/has_and_belongs_to_many.rb +0 -2
- data/lib/active_mocker/mock/has_many.rb +4 -5
- data/lib/active_mocker/mock/has_one.rb +5 -11
- data/lib/active_mocker/mock/hash_process.rb +14 -17
- data/lib/active_mocker/mock/mock_relation.rb +10 -0
- data/lib/active_mocker/mock/object_inspect.rb +29 -32
- data/lib/active_mocker/mock/queries.rb +14 -18
- data/lib/active_mocker/mock/records.rb +45 -43
- data/lib/active_mocker/mock/relation.rb +1 -4
- data/lib/active_mocker/mock/single_relation.rb +13 -17
- data/lib/active_mocker/mock/template_methods.rb +1 -4
- data/lib/active_mocker/mock_creator.rb +4 -5
- data/lib/active_mocker/mock_template/_associations.erb +6 -6
- data/lib/active_mocker/mock_template/_class_methods.erb +1 -1
- data/lib/active_mocker/mock_template/_scopes.erb +3 -3
- data/lib/active_mocker/parent_class.rb +1 -1
- data/lib/active_mocker/public_methods.rb +5 -2
- data/lib/active_mocker/rspec.rb +0 -8
- data/lib/active_mocker/rspec_helper.rb +0 -2
- data/lib/active_mocker/task.rake +0 -2
- data/lib/active_mocker/version.rb +1 -1
- metadata +36 -19
- data/lib/active_mocker/logger.rb +0 -15
- data/lib/active_mocker/output_capture.rb +0 -32
@@ -1,29 +1,23 @@
|
|
1
1
|
module ActiveMocker
|
2
|
-
|
2
|
+
class BelongsTo < SingleRelation
|
3
|
+
attr_reader :item
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
save_item(item, child_self)
|
10
|
-
assign_foreign_key(child_self, foreign_key, item.try(:id))
|
11
|
-
super
|
12
|
-
end
|
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
|
5
|
+
def initialize(item, child_self:, foreign_key:)
|
6
|
+
save_item(item, child_self)
|
7
|
+
assign_foreign_key(child_self, foreign_key, item.try(:id))
|
8
|
+
super
|
9
|
+
end
|
19
10
|
|
20
|
-
|
21
|
-
return if item.nil?
|
22
|
-
item.try(:save) if child_self.persisted?
|
23
|
-
end
|
11
|
+
private
|
24
12
|
|
13
|
+
def assign_foreign_key(child_self, foreign_key, foreign_id)
|
14
|
+
child_self.send(:write_attribute, foreign_key, foreign_id)
|
25
15
|
end
|
26
16
|
|
17
|
+
def save_item(item, child_self)
|
18
|
+
return if item.nil?
|
19
|
+
item.try(:save) if child_self.persisted?
|
20
|
+
end
|
27
21
|
end
|
28
22
|
end
|
29
23
|
|
@@ -1,10 +1,7 @@
|
|
1
1
|
require 'forwardable'
|
2
2
|
|
3
3
|
module ActiveMocker
|
4
|
-
module Mock
|
5
|
-
|
6
4
|
class Collection
|
7
|
-
|
8
5
|
include Enumerable
|
9
6
|
extend ::Forwardable
|
10
7
|
def_delegators :@collection, :[], :take, :push, :clear, :first, :last, :concat, :replace, :uniq, :count, :size, :length, :empty?, :any?, :many?, :include?, :delete
|
@@ -48,8 +45,5 @@ module Mock
|
|
48
45
|
protected
|
49
46
|
|
50
47
|
attr_accessor :collection
|
51
|
-
|
52
48
|
end
|
53
|
-
|
54
|
-
end
|
55
49
|
end
|
@@ -1,61 +1,59 @@
|
|
1
1
|
module ActiveMocker
|
2
|
-
module
|
3
|
-
module DoNothingActiveRecordMethods
|
2
|
+
module DoNothingActiveRecordMethods
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def transaction
|
11
|
+
yield
|
12
|
+
rescue LocalJumpError => err
|
13
|
+
raise err
|
14
|
+
rescue StandardError => e
|
15
|
+
raise e
|
16
|
+
end
|
8
17
|
|
9
|
-
|
18
|
+
def column_names
|
19
|
+
attribute_names
|
20
|
+
end
|
10
21
|
|
11
|
-
def transaction
|
12
|
-
yield
|
13
|
-
rescue LocalJumpError => err
|
14
|
-
raise err
|
15
|
-
rescue StandardError => e
|
16
|
-
raise e
|
17
22
|
end
|
18
23
|
|
19
|
-
def
|
20
|
-
|
24
|
+
def readonly?
|
25
|
+
false
|
21
26
|
end
|
22
27
|
|
23
|
-
|
28
|
+
def errors
|
29
|
+
obj = Object.new
|
24
30
|
|
25
|
-
|
26
|
-
|
27
|
-
|
31
|
+
def obj.[](key)
|
32
|
+
[]
|
33
|
+
end
|
28
34
|
|
29
|
-
|
30
|
-
|
35
|
+
def obj.full_messages()
|
36
|
+
[]
|
37
|
+
end
|
31
38
|
|
32
|
-
|
33
|
-
[]
|
39
|
+
obj
|
34
40
|
end
|
35
41
|
|
36
|
-
def
|
37
|
-
|
42
|
+
def valid?
|
43
|
+
true
|
38
44
|
end
|
39
45
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
def valid?
|
44
|
-
true
|
45
|
-
end
|
46
|
+
def marked_for_destruction?
|
47
|
+
false
|
48
|
+
end
|
46
49
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
+
def destroyed?
|
51
|
+
false
|
52
|
+
end
|
50
53
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
+
def reload
|
55
|
+
self
|
56
|
+
end
|
54
57
|
|
55
|
-
def reload
|
56
|
-
self
|
57
58
|
end
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
61
59
|
end
|
@@ -1,24 +1,22 @@
|
|
1
1
|
module ActiveMocker
|
2
|
-
module Mock
|
3
2
|
class RecordNotFound < StandardError
|
4
3
|
end
|
5
4
|
|
6
|
-
|
5
|
+
module Mock
|
6
|
+
# @deprecated
|
7
|
+
RecordNotFound = ActiveMocker::RecordNotFound
|
7
8
|
end
|
8
9
|
|
9
10
|
class IdError < StandardError
|
10
11
|
end
|
11
12
|
|
12
|
-
class FileTypeMismatchError < StandardError
|
13
|
-
end
|
14
|
-
|
15
13
|
# Raised when unknown attributes are supplied via mass assignment.
|
16
14
|
class UnknownAttributeError < NoMethodError
|
17
15
|
|
18
16
|
attr_reader :record, :attribute
|
19
17
|
|
20
18
|
def initialize(record, attribute)
|
21
|
-
@record
|
19
|
+
@record = record
|
22
20
|
@attribute = attribute.to_s
|
23
21
|
super("unknown attribute: #{attribute}")
|
24
22
|
end
|
@@ -36,11 +34,6 @@ module Mock
|
|
36
34
|
class NotImplementedError < Exception
|
37
35
|
end
|
38
36
|
|
39
|
-
class IdNotNumber < Exception
|
40
|
-
end
|
41
|
-
|
42
37
|
class Error < Exception
|
43
38
|
end
|
44
|
-
|
45
|
-
end
|
46
39
|
end
|
@@ -1,8 +1,5 @@
|
|
1
1
|
module ActiveMocker
|
2
|
-
module Mock
|
3
|
-
|
4
2
|
class HasMany < Association
|
5
|
-
|
6
3
|
include Queries
|
7
4
|
|
8
5
|
def self.new(collection, options = {})
|
@@ -62,7 +59,9 @@ module Mock
|
|
62
59
|
end
|
63
60
|
|
64
61
|
end
|
65
|
-
|
66
|
-
|
62
|
+
module Mock
|
63
|
+
# @deprecated
|
64
|
+
HasMany = ActiveMocker::HasMany
|
65
|
+
end
|
67
66
|
end
|
68
67
|
|
@@ -1,17 +1,11 @@
|
|
1
1
|
module ActiveMocker
|
2
|
-
|
3
|
-
|
4
|
-
class HasOne < SingleRelation
|
5
|
-
|
6
|
-
attr_reader :item
|
7
|
-
|
8
|
-
def initialize(item, child_self:, foreign_key:)
|
9
|
-
item.send(:write_attribute, foreign_key, item.try(:id)) if !item.try(:id).nil?
|
10
|
-
super
|
11
|
-
end
|
2
|
+
class HasOne < SingleRelation
|
3
|
+
attr_reader :item
|
12
4
|
|
5
|
+
def initialize(item, child_self:, foreign_key:)
|
6
|
+
item.send(:write_attribute, foreign_key, item.try(:id)) if !item.try(:id).nil?
|
7
|
+
super
|
13
8
|
end
|
14
9
|
|
15
10
|
end
|
16
11
|
end
|
17
|
-
|
@@ -1,25 +1,22 @@
|
|
1
1
|
module ActiveMocker
|
2
|
-
|
3
|
-
|
4
|
-
class HashProcess
|
2
|
+
# @api private
|
3
|
+
class HashProcess
|
5
4
|
|
6
|
-
|
5
|
+
attr_accessor :hash, :processor
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def [](val)
|
15
|
-
@hash_process[val] ||= processor.call(hash[val])
|
16
|
-
end
|
7
|
+
def initialize(hash, processor)
|
8
|
+
@hash = hash
|
9
|
+
@processor = processor
|
10
|
+
@hash_process = {}
|
11
|
+
end
|
17
12
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
13
|
+
def [](val)
|
14
|
+
@hash_process[val] ||= processor.call(hash[val])
|
15
|
+
end
|
22
16
|
|
17
|
+
def merge(merge_hash)
|
18
|
+
self.hash = hash.merge(merge_hash.hash)
|
19
|
+
self
|
23
20
|
end
|
24
21
|
end
|
25
22
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module ActiveMocker
|
2
|
+
class MockRelation
|
3
|
+
# @param [ActiveMocker::Base] mock, a generated mock class
|
4
|
+
# @param [Array<ActiveMocker::Base>] collection, an array of mock instances
|
5
|
+
# @return [ScopeRelation] for the given mock
|
6
|
+
def self.new(mock, collection)
|
7
|
+
mock.send(:__new_relation__, collection)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -1,43 +1,40 @@
|
|
1
1
|
module ActiveMocker
|
2
|
-
|
3
|
-
class ObjectInspect
|
2
|
+
class ObjectInspect
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
def initialize(class_name, attributes)
|
5
|
+
@class_name = class_name
|
6
|
+
@attributes = attributes
|
7
|
+
@string = create_inspections
|
8
|
+
end
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
def to_s
|
11
|
+
@string
|
12
|
+
end
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
def to_str
|
15
|
+
@string
|
16
|
+
end
|
18
17
|
|
19
|
-
|
18
|
+
private
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
def create_inspections
|
21
|
+
inspection = @attributes.map do |name, value|
|
22
|
+
"#{name}: #{object_for_inspect(value)}"
|
23
|
+
end
|
24
|
+
"#<#{@class_name} #{inspection.compact.join(", ")}>"
|
24
25
|
end
|
25
|
-
"#<#{@class_name} #{inspection.compact.join(", ")}>"
|
26
|
-
end
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
27
|
+
def object_for_inspect(value)
|
28
|
+
if value.is_a?(String) && value.length > 50
|
29
|
+
"#{value[0, 50]}...".inspect
|
30
|
+
elsif value.is_a?(Date) || value.is_a?(Time)
|
31
|
+
%("#{value.to_s(:db)}")
|
32
|
+
elsif value.is_a?(Array) && value.size > 10
|
33
|
+
inspected = value.first(10).inspect
|
34
|
+
%(#{inspected[0...-1]}, ...])
|
35
|
+
else
|
36
|
+
value.inspect
|
37
|
+
end
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|
43
40
|
end
|
@@ -1,6 +1,4 @@
|
|
1
1
|
module ActiveMocker
|
2
|
-
module Mock
|
3
|
-
|
4
2
|
module Queries
|
5
3
|
|
6
4
|
class Find
|
@@ -61,9 +59,9 @@ module Mock
|
|
61
59
|
# If a limit scope is supplied, +delete_all+ raises an ActiveMocker error:
|
62
60
|
#
|
63
61
|
# Post.limit(100).delete_all
|
64
|
-
# # => ActiveMocker::
|
62
|
+
# # => ActiveMocker::Error: delete_all doesn't support limit scope
|
65
63
|
def delete_all(conditions=nil)
|
66
|
-
raise ActiveMocker::
|
64
|
+
raise ActiveMocker::Error.new("delete_all doesn't support limit scope") if from_limit?
|
67
65
|
if conditions.nil?
|
68
66
|
to_a.map(&:delete)
|
69
67
|
return to_a.clear
|
@@ -116,8 +114,8 @@ module Mock
|
|
116
114
|
#
|
117
115
|
# See WhereChain for more details on #not.
|
118
116
|
def where(conditions=nil)
|
119
|
-
return WhereNotChain.new(all, method(:
|
120
|
-
|
117
|
+
return WhereNotChain.new(all, method(:__new_relation__)) if conditions.nil?
|
118
|
+
__new_relation__(to_a.select do |record|
|
121
119
|
Find.new(record).is_of(conditions)
|
122
120
|
end)
|
123
121
|
end
|
@@ -131,13 +129,13 @@ module Mock
|
|
131
129
|
# Person.find([7, 17]) # returns an array for objects with IDs in (7, 17)
|
132
130
|
# Person.find([1]) # returns an array for the object with ID = 1
|
133
131
|
#
|
134
|
-
# <tt>ActiveMocker::
|
132
|
+
# <tt>ActiveMocker::RecordNotFound</tt> will be raised if one or more ids are not found.
|
135
133
|
def find(ids)
|
136
134
|
raise RecordNotFound.new("Couldn't find #{self.name} without an ID") if ids.nil?
|
137
135
|
results = [*ids].map do |id|
|
138
136
|
find_by!(id: id.to_i)
|
139
137
|
end
|
140
|
-
return
|
138
|
+
return __new_relation__(results) if ids.class == Array
|
141
139
|
results.first
|
142
140
|
end
|
143
141
|
|
@@ -259,7 +257,7 @@ module Mock
|
|
259
257
|
#
|
260
258
|
# User.limit(10)
|
261
259
|
def limit(num)
|
262
|
-
relation =
|
260
|
+
relation = __new_relation__(all.take(num))
|
263
261
|
relation.send(:set_from_limit)
|
264
262
|
relation
|
265
263
|
end
|
@@ -281,7 +279,7 @@ module Mock
|
|
281
279
|
# PersonMock.average(:age) # => 35.8
|
282
280
|
def average(key)
|
283
281
|
values = values_by_key(key)
|
284
|
-
total
|
282
|
+
total = values.inject { |sum, n| sum + n }
|
285
283
|
BigDecimal.new(total) / BigDecimal.new(values.count)
|
286
284
|
end
|
287
285
|
|
@@ -307,18 +305,18 @@ module Mock
|
|
307
305
|
#
|
308
306
|
# User.order(:name)
|
309
307
|
def order(key)
|
310
|
-
|
308
|
+
__new_relation__(all.sort_by { |item| item.send(key) })
|
311
309
|
end
|
312
310
|
|
313
311
|
# Reverse the existing order clause on the relation.
|
314
312
|
#
|
315
313
|
# User.order('name').reverse_order
|
316
314
|
def reverse_order
|
317
|
-
|
315
|
+
__new_relation__(to_a.reverse)
|
318
316
|
end
|
319
317
|
|
320
318
|
def all
|
321
|
-
|
319
|
+
__new_relation__(to_a || [])
|
322
320
|
end
|
323
321
|
|
324
322
|
# Returns a chainable relation with zero records.
|
@@ -346,7 +344,7 @@ module Mock
|
|
346
344
|
# end
|
347
345
|
#
|
348
346
|
def none
|
349
|
-
|
347
|
+
__new_relation__([])
|
350
348
|
end
|
351
349
|
|
352
350
|
private
|
@@ -355,13 +353,11 @@ module Mock
|
|
355
353
|
all.map { |obj| obj.send(key) }
|
356
354
|
end
|
357
355
|
|
358
|
-
def
|
359
|
-
duped
|
356
|
+
def __new_relation__(collection)
|
357
|
+
duped = self.dup
|
360
358
|
duped.collection = collection
|
361
359
|
duped
|
362
360
|
end
|
363
361
|
|
364
362
|
end
|
365
|
-
|
366
|
-
end
|
367
363
|
end
|