active_mocker 1.3.2 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +136 -24
- data/Rakefile +8 -2
- data/active_mocker.gemspec +3 -3
- data/lib/active_mock/association.rb +7 -0
- data/lib/active_mock/base.rb +250 -0
- data/lib/active_mock/collection.rb +52 -0
- data/lib/active_mock/creators.rb +25 -0
- data/lib/active_mock/do_nothing_active_record_methods.rb +51 -0
- data/lib/active_mock/has_and_belongs_to_many.rb +7 -0
- data/lib/active_mock/has_many.rb +54 -0
- data/lib/active_mock/next_id.rb +16 -0
- data/lib/active_mock/object_inspect.rb +39 -0
- data/lib/{active_mocker/collection → active_mock}/queries.rb +26 -19
- data/lib/active_mock/records.rb +81 -0
- data/lib/active_mock/relation.rb +8 -0
- data/lib/active_mocker.rb +3 -1
- data/lib/active_mocker/active_mock.rb +26 -0
- data/lib/active_mocker/active_record.rb +18 -0
- data/lib/active_mocker/active_record/relationships.rb +57 -6
- data/lib/active_mocker/active_record/schema.rb +1 -1
- data/lib/active_mocker/active_record/scope.rb +1 -1
- data/lib/active_mocker/active_record/unknown_class_method.rb +1 -1
- data/lib/active_mocker/active_record/unknown_module.rb +10 -9
- data/lib/active_mocker/db_to_ruby_type.rb +26 -0
- data/lib/active_mocker/field.rb +16 -8
- data/lib/active_mocker/generate.rb +19 -174
- data/lib/active_mocker/loaded_mocks.rb +48 -8
- data/lib/active_mocker/logger.rb +2 -0
- data/lib/active_mocker/mock_template.erb +123 -53
- data/lib/active_mocker/model_reader.rb +42 -13
- data/lib/active_mocker/model_schema.rb +279 -0
- data/lib/active_mocker/model_schema/generate.rb +175 -0
- data/lib/active_mocker/reparameterize.rb +23 -3
- data/lib/active_mocker/table.rb +2 -2
- data/lib/active_mocker/version.rb +1 -1
- data/sample_app_rails_4/Gemfile +1 -1
- data/sample_app_rails_4/app/models/micropost.rb +13 -1
- data/sample_app_rails_4/db/schema.rb +1 -1
- data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +194 -7
- data/sample_app_rails_4/spec/micropost_mock_spec.rb +145 -0
- data/sample_app_rails_4/spec/mocks/micropost_mock.rb +81 -55
- data/sample_app_rails_4/spec/mocks/relationship_mock.rb +85 -54
- data/sample_app_rails_4/spec/mocks/user_mock.rb +71 -72
- data/sample_app_rails_4/spec/reload_spec.rb +1 -1
- data/sample_app_rails_4/spec/user_mock_spec.rb +25 -7
- data/spec/lib/acitve_mock/queriable_spec.rb +207 -0
- data/spec/lib/active_mocker/db_to_ruby_type_spec.rb +124 -0
- data/spec/lib/active_mocker/loaded_mocks_spec.rb +167 -0
- data/spec/lib/active_mocker/logger_spec.rb +32 -0
- data/spec/lib/active_mocker/model_reader_spec.rb +79 -28
- data/spec/lib/active_mocker/model_schema/generate_spec.rb +111 -0
- data/spec/lib/active_mocker/model_schema_spec.rb +145 -0
- data/spec/lib/model.rb +2 -1
- data/spec/lib/reparameterize_spec.rb +202 -0
- data/spec/unit_logger.rb +2 -2
- metadata +55 -35
- data/lib/active_hash/ar_api.rb +0 -77
- data/lib/active_hash/init.rb +0 -32
- data/lib/active_mocker/collection/association.rb +0 -12
- data/lib/active_mocker/collection/base.rb +0 -65
- data/lib/active_mocker/collection/relation.rb +0 -11
- data/lib/active_mocker/mock_class_methods.rb +0 -92
- data/lib/active_mocker/mock_instance_methods.rb +0 -84
- data/lib/active_mocker/mock_requires.rb +0 -10
- data/spec/lib/active_mocker/collection.rb +0 -94
@@ -0,0 +1,52 @@
|
|
1
|
+
module ActiveMock
|
2
|
+
|
3
|
+
class Collection
|
4
|
+
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
def initialize(collection=[])
|
8
|
+
@collection = [*collection]
|
9
|
+
end
|
10
|
+
|
11
|
+
def <<(*records)
|
12
|
+
collection.concat(records.flatten)
|
13
|
+
end
|
14
|
+
|
15
|
+
extend Forwardable
|
16
|
+
def_delegators :@collection, :take, :push, :clear, :first, :last, :concat, :replace, :distinct, :uniq, :count, :size, :length, :empty?, :any?, :include?, :delete
|
17
|
+
alias distinct uniq
|
18
|
+
|
19
|
+
def select(&block)
|
20
|
+
collection.select(&block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def each(&block)
|
24
|
+
collection.each do |item|
|
25
|
+
block.call(item)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def map(&block)
|
30
|
+
collection.map do |item|
|
31
|
+
block.call(item)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_a
|
36
|
+
@collection
|
37
|
+
end
|
38
|
+
|
39
|
+
def ==(val)
|
40
|
+
return false if val.nil?
|
41
|
+
collection.hash == val.hash
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def collection
|
47
|
+
@collection
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ActiveMock
|
2
|
+
module Creators
|
3
|
+
|
4
|
+
def create(attributes = {}, &block)
|
5
|
+
record = new
|
6
|
+
record.save
|
7
|
+
record.send(:set_properties, attributes) unless block_given?
|
8
|
+
record.send(:set_properties_block, attributes, &block) if block_given?
|
9
|
+
record
|
10
|
+
end
|
11
|
+
|
12
|
+
alias_method :create!, :create
|
13
|
+
|
14
|
+
def find_or_create_by(attributes)
|
15
|
+
find_by(attributes) || create(attributes)
|
16
|
+
end
|
17
|
+
|
18
|
+
def find_or_initialize_by(attributes)
|
19
|
+
find_by(attributes) || new(attributes)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module ActiveMock
|
2
|
+
module DoNothingActiveRecordMethods
|
3
|
+
|
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
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def readonly?
|
21
|
+
false
|
22
|
+
end
|
23
|
+
|
24
|
+
def errors
|
25
|
+
obj = Object.new
|
26
|
+
|
27
|
+
def obj.[](key)
|
28
|
+
[]
|
29
|
+
end
|
30
|
+
|
31
|
+
def obj.full_messages()
|
32
|
+
[]
|
33
|
+
end
|
34
|
+
|
35
|
+
obj
|
36
|
+
end
|
37
|
+
|
38
|
+
def valid?
|
39
|
+
true
|
40
|
+
end
|
41
|
+
|
42
|
+
def marked_for_destruction?
|
43
|
+
false
|
44
|
+
end
|
45
|
+
|
46
|
+
def destroyed?
|
47
|
+
false
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module ActiveMock
|
2
|
+
|
3
|
+
class HasMany < Association
|
4
|
+
|
5
|
+
include Queries
|
6
|
+
|
7
|
+
def self.new(collection, foreign_key=nil, foreign_id=nil, relation_class=nil)
|
8
|
+
return Association.new(collection) if relation_class.nil?
|
9
|
+
super(collection, foreign_key, foreign_id, relation_class)
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def initialize(collection, foreign_key=nil, foreign_id=nil, relation_class=nil)
|
14
|
+
@relation_class = relation_class
|
15
|
+
@foreign_key = foreign_key
|
16
|
+
@foreign_id = foreign_id
|
17
|
+
|
18
|
+
super(collection)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
attr_reader :relation_class, :foreign_key, :foreign_id
|
23
|
+
public
|
24
|
+
|
25
|
+
def build(options={}, &block)
|
26
|
+
new_record = relation_class.new({foreign_key => foreign_id}.merge!(options), &block)
|
27
|
+
|
28
|
+
def new_record.belongs_to(collection, foreign_key)
|
29
|
+
@belongs_to_collection ||={}
|
30
|
+
@belongs_to_collection[foreign_key] = collection
|
31
|
+
end
|
32
|
+
|
33
|
+
new_record.belongs_to(self, foreign_key)
|
34
|
+
|
35
|
+
def new_record.save
|
36
|
+
@belongs_to_collection.each {|k ,v| v << self}
|
37
|
+
super
|
38
|
+
end
|
39
|
+
|
40
|
+
new_record
|
41
|
+
end
|
42
|
+
|
43
|
+
def create(options={}, &block)
|
44
|
+
created_record = relation_class.create({foreign_key => foreign_id}.merge!(options), &block)
|
45
|
+
collection << created_record
|
46
|
+
created_record
|
47
|
+
end
|
48
|
+
|
49
|
+
alias_method :create!, :create
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class NextId
|
2
|
+
|
3
|
+
def initialize(records)
|
4
|
+
@records = records
|
5
|
+
end
|
6
|
+
|
7
|
+
def next
|
8
|
+
return 1 if max_record.nil?
|
9
|
+
max_record.id.succ if max_record.id.is_a?(Numeric)
|
10
|
+
end
|
11
|
+
|
12
|
+
def max_record
|
13
|
+
@max_record ||= @records.max { |a, b| a.id <=> b.id }
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class ObjectInspect
|
2
|
+
|
3
|
+
def initialize(class_name, attributes)
|
4
|
+
@class_name = class_name
|
5
|
+
@attributes = attributes
|
6
|
+
@string = create_inspections
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_s
|
10
|
+
@string
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_str
|
14
|
+
@string
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def create_inspections
|
20
|
+
inspection = @attributes.map do |name ,value|
|
21
|
+
"#{name}: #{object_for_inspect(value)}"
|
22
|
+
end
|
23
|
+
"#<#{@class_name} #{inspection.compact.join(", ")}>"
|
24
|
+
end
|
25
|
+
|
26
|
+
def object_for_inspect(value)
|
27
|
+
if value.is_a?(String) && value.length > 50
|
28
|
+
"#{value[0, 50]}...".inspect
|
29
|
+
elsif value.is_a?(Date) || value.is_a?(Time)
|
30
|
+
%("#{value.to_s(:db)}")
|
31
|
+
elsif value.is_a?(Array) && value.size > 10
|
32
|
+
inspected = value.first(10).inspect
|
33
|
+
%(#{inspected[0...-1]}, ...])
|
34
|
+
else
|
35
|
+
value.inspect
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -1,10 +1,15 @@
|
|
1
|
-
module
|
1
|
+
module ActiveMock
|
2
2
|
|
3
|
-
|
3
|
+
module Queries
|
4
4
|
|
5
|
-
|
5
|
+
def self.included(klass)
|
6
|
+
@included = klass
|
7
|
+
end
|
6
8
|
|
7
|
-
|
9
|
+
def self.included_klass
|
10
|
+
return Relation if @included.name == 'ActiveMocker::Base'
|
11
|
+
@included
|
12
|
+
end
|
8
13
|
|
9
14
|
class Find
|
10
15
|
|
@@ -28,9 +33,9 @@ module Collection
|
|
28
33
|
end
|
29
34
|
|
30
35
|
def not(options={})
|
31
|
-
@collection.reject do |record|
|
36
|
+
Queries.included_klass.new(@collection.reject do |record|
|
32
37
|
Find.new(record).is_of(options)
|
33
|
-
end
|
38
|
+
end)
|
34
39
|
end
|
35
40
|
end
|
36
41
|
|
@@ -42,19 +47,15 @@ module Collection
|
|
42
47
|
delete_all
|
43
48
|
end
|
44
49
|
|
45
|
-
def all
|
46
|
-
|
47
|
-
where(options[:conditions])
|
48
|
-
else
|
49
|
-
Relation.new( to_a || [] )
|
50
|
-
end
|
50
|
+
def all
|
51
|
+
Queries.included_klass.new( to_a || [] )
|
51
52
|
end
|
52
53
|
|
53
54
|
def where(options=nil)
|
54
55
|
return WhereNotChain.new(all) if options.nil?
|
55
|
-
all.select do |record|
|
56
|
+
Queries.included_klass.new(all.select do |record|
|
56
57
|
Find.new(record).is_of(options)
|
57
|
-
end
|
58
|
+
end)
|
58
59
|
end
|
59
60
|
|
60
61
|
def find(ids)
|
@@ -62,7 +63,7 @@ module Collection
|
|
62
63
|
results = ids_array.map do |id|
|
63
64
|
where(id: id).first
|
64
65
|
end
|
65
|
-
return
|
66
|
+
return Queries.included_klass.new(results) if ids.class == Array
|
66
67
|
results.first
|
67
68
|
end
|
68
69
|
|
@@ -81,7 +82,7 @@ module Collection
|
|
81
82
|
end
|
82
83
|
|
83
84
|
def limit(num)
|
84
|
-
|
85
|
+
Queries.included_klass.new(all.take(num))
|
85
86
|
end
|
86
87
|
|
87
88
|
def sum(key)
|
@@ -103,6 +104,14 @@ module Collection
|
|
103
104
|
values_by_key(key).max_by { |i| i }
|
104
105
|
end
|
105
106
|
|
107
|
+
def order(key)
|
108
|
+
Queries.included_klass.new(all.sort_by { |item| item.send(key) })
|
109
|
+
end
|
110
|
+
|
111
|
+
def reverse_order
|
112
|
+
Queries.included_klass.new(to_a.reverse)
|
113
|
+
end
|
114
|
+
|
106
115
|
private
|
107
116
|
|
108
117
|
def values_by_key(key)
|
@@ -111,6 +120,4 @@ module Collection
|
|
111
120
|
|
112
121
|
end
|
113
122
|
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
123
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
class Records
|
2
|
+
|
3
|
+
attr_reader :records
|
4
|
+
|
5
|
+
def initialize(records=[])
|
6
|
+
@records = records
|
7
|
+
end
|
8
|
+
|
9
|
+
def insert(record)
|
10
|
+
record.attributes[:id] ||= next_id
|
11
|
+
validate_unique_id(record)
|
12
|
+
add_to_record_index({record.id.to_s => records.length})
|
13
|
+
records << record
|
14
|
+
end
|
15
|
+
|
16
|
+
def delete(record)
|
17
|
+
record_index.delete("#{record.id}")
|
18
|
+
index = records.index(record)
|
19
|
+
records.delete_at(index)
|
20
|
+
end
|
21
|
+
|
22
|
+
def <<(record)
|
23
|
+
records << record
|
24
|
+
end
|
25
|
+
|
26
|
+
def length
|
27
|
+
records.length
|
28
|
+
end
|
29
|
+
|
30
|
+
alias_method :count, :length
|
31
|
+
|
32
|
+
def record_index
|
33
|
+
@record_index ||= {}
|
34
|
+
end
|
35
|
+
|
36
|
+
def add_to_record_index(entry)
|
37
|
+
record_index.merge!(entry)
|
38
|
+
end
|
39
|
+
|
40
|
+
def reset_record_index
|
41
|
+
record_index.clear
|
42
|
+
end
|
43
|
+
|
44
|
+
def clear
|
45
|
+
records.clear
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_a
|
49
|
+
records
|
50
|
+
end
|
51
|
+
|
52
|
+
def reset_all_records
|
53
|
+
reset_record_index
|
54
|
+
clear
|
55
|
+
end
|
56
|
+
|
57
|
+
def next_id
|
58
|
+
NextId.new(records).next
|
59
|
+
end
|
60
|
+
|
61
|
+
def validate_unique_id(record)
|
62
|
+
if record_index.has_key?(record.id.to_s)
|
63
|
+
raise ActiveMock::IdError.new("Duplicate ID found for record #{record.attributes.inspect}")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def exists?(record)
|
68
|
+
if record.id.present?
|
69
|
+
record_index[record.id.to_s].present?
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def new_record?(record)
|
74
|
+
!records.include?(record)
|
75
|
+
end
|
76
|
+
|
77
|
+
def persisted?(id)
|
78
|
+
records.map(&:id).include?(id)
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
data/lib/active_mocker.rb
CHANGED
@@ -19,5 +19,7 @@ require 'active_mocker/active_record/schema'
|
|
19
19
|
require 'active_mocker/active_record'
|
20
20
|
require 'active_mocker/model_reader'
|
21
21
|
require 'active_mocker/reparameterize'
|
22
|
-
require '
|
22
|
+
require 'active_mocker/db_to_ruby_type'
|
23
|
+
require 'active_mocker/model_schema'
|
24
|
+
require 'active_mocker/model_schema/generate'
|
23
25
|
require 'virtus'
|