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.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/README.md +136 -24
  4. data/Rakefile +8 -2
  5. data/active_mocker.gemspec +3 -3
  6. data/lib/active_mock/association.rb +7 -0
  7. data/lib/active_mock/base.rb +250 -0
  8. data/lib/active_mock/collection.rb +52 -0
  9. data/lib/active_mock/creators.rb +25 -0
  10. data/lib/active_mock/do_nothing_active_record_methods.rb +51 -0
  11. data/lib/active_mock/has_and_belongs_to_many.rb +7 -0
  12. data/lib/active_mock/has_many.rb +54 -0
  13. data/lib/active_mock/next_id.rb +16 -0
  14. data/lib/active_mock/object_inspect.rb +39 -0
  15. data/lib/{active_mocker/collection → active_mock}/queries.rb +26 -19
  16. data/lib/active_mock/records.rb +81 -0
  17. data/lib/active_mock/relation.rb +8 -0
  18. data/lib/active_mocker.rb +3 -1
  19. data/lib/active_mocker/active_mock.rb +26 -0
  20. data/lib/active_mocker/active_record.rb +18 -0
  21. data/lib/active_mocker/active_record/relationships.rb +57 -6
  22. data/lib/active_mocker/active_record/schema.rb +1 -1
  23. data/lib/active_mocker/active_record/scope.rb +1 -1
  24. data/lib/active_mocker/active_record/unknown_class_method.rb +1 -1
  25. data/lib/active_mocker/active_record/unknown_module.rb +10 -9
  26. data/lib/active_mocker/db_to_ruby_type.rb +26 -0
  27. data/lib/active_mocker/field.rb +16 -8
  28. data/lib/active_mocker/generate.rb +19 -174
  29. data/lib/active_mocker/loaded_mocks.rb +48 -8
  30. data/lib/active_mocker/logger.rb +2 -0
  31. data/lib/active_mocker/mock_template.erb +123 -53
  32. data/lib/active_mocker/model_reader.rb +42 -13
  33. data/lib/active_mocker/model_schema.rb +279 -0
  34. data/lib/active_mocker/model_schema/generate.rb +175 -0
  35. data/lib/active_mocker/reparameterize.rb +23 -3
  36. data/lib/active_mocker/table.rb +2 -2
  37. data/lib/active_mocker/version.rb +1 -1
  38. data/sample_app_rails_4/Gemfile +1 -1
  39. data/sample_app_rails_4/app/models/micropost.rb +13 -1
  40. data/sample_app_rails_4/db/schema.rb +1 -1
  41. data/sample_app_rails_4/spec/compare_mocker_and_record_spec.rb +194 -7
  42. data/sample_app_rails_4/spec/micropost_mock_spec.rb +145 -0
  43. data/sample_app_rails_4/spec/mocks/micropost_mock.rb +81 -55
  44. data/sample_app_rails_4/spec/mocks/relationship_mock.rb +85 -54
  45. data/sample_app_rails_4/spec/mocks/user_mock.rb +71 -72
  46. data/sample_app_rails_4/spec/reload_spec.rb +1 -1
  47. data/sample_app_rails_4/spec/user_mock_spec.rb +25 -7
  48. data/spec/lib/acitve_mock/queriable_spec.rb +207 -0
  49. data/spec/lib/active_mocker/db_to_ruby_type_spec.rb +124 -0
  50. data/spec/lib/active_mocker/loaded_mocks_spec.rb +167 -0
  51. data/spec/lib/active_mocker/logger_spec.rb +32 -0
  52. data/spec/lib/active_mocker/model_reader_spec.rb +79 -28
  53. data/spec/lib/active_mocker/model_schema/generate_spec.rb +111 -0
  54. data/spec/lib/active_mocker/model_schema_spec.rb +145 -0
  55. data/spec/lib/model.rb +2 -1
  56. data/spec/lib/reparameterize_spec.rb +202 -0
  57. data/spec/unit_logger.rb +2 -2
  58. metadata +55 -35
  59. data/lib/active_hash/ar_api.rb +0 -77
  60. data/lib/active_hash/init.rb +0 -32
  61. data/lib/active_mocker/collection/association.rb +0 -12
  62. data/lib/active_mocker/collection/base.rb +0 -65
  63. data/lib/active_mocker/collection/relation.rb +0 -11
  64. data/lib/active_mocker/mock_class_methods.rb +0 -92
  65. data/lib/active_mocker/mock_instance_methods.rb +0 -84
  66. data/lib/active_mocker/mock_requires.rb +0 -10
  67. data/spec/lib/active_mocker/collection.rb +0 -94
@@ -0,0 +1,111 @@
1
+ require 'rspec'
2
+ $:.unshift File.expand_path('../../../../../lib', __FILE__)
3
+ require 'active_mocker'
4
+ require 'json'
5
+ require 'active_support/core_ext/string/inflections'
6
+ require 'active_support/core_ext/object/deep_dup.rb'
7
+ require 'active_mocker/model_schema'
8
+ require 'active_mocker/model_schema'
9
+ require 'active_mocker/model_schema/generate'
10
+ require_relative '../../../unit_logger'
11
+
12
+ describe ActiveMocker::ModelSchema::Generate do
13
+
14
+ let(:app_root) { File.expand_path('../../../../../', __FILE__) }
15
+ let(:schema_file){ File.join(app_root, 'sample_app_rails_4/db/schema.rb') }
16
+ # let(:schema_file){ '/Users/zeisler/dev/fbi/db/schema.rb' }
17
+ let(:models_dir){ File.join(app_root, 'sample_app_rails_4/app/models') }
18
+ # let(:models_dir){'/Users/zeisler/dev/fbi/app/models' }
19
+ let(:run){described_class.new(schema_file: schema_file, models_dir: models_dir, logger: UnitLogger.unit).run.sort_by{|m| m.class_name}
20
+ }
21
+
22
+ it 'test' do
23
+ result = described_class.new(schema_file: schema_file, models_dir: models_dir, logger: UnitLogger.unit).run
24
+ expect(result.count).to eq 3
25
+ end
26
+
27
+ it 'relationships' do
28
+ expect(run[-1].relationships.map{ |r| JSON.parse(r.to_json)})
29
+ .to eq [{"name" => "microposts", "class_name" => "Micropost", "type" => "has_many", "foreign_key" => "user_id"},
30
+ {"name" => "relationships", "class_name" => "Relationship", "type" => "has_many", "foreign_key" => "follower_id"},
31
+ {"name" => "followed_users", "class_name" => "FollowedUser", "type" => "has_many", "foreign_key" => "user_id", "through" => "relationships"},
32
+ {"name" => "reverse_relationships", "class_name" => "Relationship", "type" => "has_many", "foreign_key" => "followed_id"},
33
+ {"name" => "followers", "class_name" => "Follower", "type" => "has_many", "foreign_key" => "user_id", "through" => "reverse_relationships"}]
34
+ end
35
+
36
+ it 'methods' do
37
+ expect(run[-1].methods.map { |r| JSON.parse(r.to_json) })
38
+ .to eq([{"name" => "feed", "arguments" => [], "type" => "instance"},
39
+ {"name" => "following?", "arguments" => [["req", "other_user"]], "type" => "instance"},
40
+ {"name" => "follow!", "arguments" => [["req", "other_user"]], "type" => "instance"},
41
+ {"name" => "unfollow!", "arguments" => [["req", "other_user"]], "type" => "instance"},
42
+ {"name" => "new_remember_token", "arguments" => [], "type" => "class"},
43
+ {"name" => "digest", "arguments" => [["req", "token"]], "type" => "class"}])
44
+ end
45
+
46
+ it 'arguments' do
47
+ expect(run[-1].methods.last.arguments.passable).to eq('token')
48
+ end
49
+
50
+ it 'attributes' do
51
+ expect(run[-1].attributes.map { |r| JSON.parse(r.to_json) })
52
+ .to eq([{"name" => "id", "type" => "integer", "primary_key" => true},
53
+ {"name" => "name", "type" => "string"},
54
+ {"name" => "email", "type" => "string", "default_value" => ""},
55
+ {"name" => "credits", "type" => "decimal", "precision" => 19, "scale" => 6},
56
+ {"name" => "created_at", "type" => "datetime"},
57
+ {"name" => "updated_at", "type" => "datetime"},
58
+ {"name" => "password_digest", "type" => "string"},
59
+ {"name" => "remember_token", "type" => "boolean", "default_value" => true},
60
+ {"name" => "admin", "type" => "boolean", "default_value" => false}])
61
+ end
62
+
63
+ describe '#primary_key' do
64
+
65
+ context 'when primary model has a primary key' do
66
+
67
+ let(:given_model){OpenStruct.new(primary_key: 'user_id')}
68
+ let(:given_attributes){ [OpenStruct.new(name: 'user_id'), OpenStruct.new(name: 'name')] }
69
+
70
+ it 'will return attributes with that name' do
71
+ expect(described_class.new.primary_key(given_attributes, given_model)).to eq(given_attributes.first)
72
+ end
73
+
74
+ end
75
+
76
+ context 'when attributes has a primary key' do
77
+
78
+ let(:given_model) { OpenStruct.new(primary_key: false) }
79
+ let(:given_attributes) { [OpenStruct.new(name: 'user_id', primary_key: true), OpenStruct.new(name: 'name')] }
80
+
81
+ it 'will return attributes with that name' do
82
+ expect(described_class.new.primary_key(given_attributes, given_model)).to eq(given_attributes.first)
83
+ end
84
+
85
+ end
86
+
87
+ context 'when attributes or model has no primary key' do
88
+
89
+ let(:given_model) { OpenStruct.new(primary_key: false) }
90
+ let(:given_attributes) { [OpenStruct.new(name: 'id'), OpenStruct.new(name: 'name')] }
91
+
92
+ it 'will default to attribute with id' do
93
+ expect(described_class.new.primary_key(given_attributes, given_model)).to eq(given_attributes.first)
94
+ end
95
+
96
+ end
97
+
98
+ context 'when attributes or model has no primary key and there is no id attribute' do
99
+
100
+ let(:given_model) { OpenStruct.new(primary_key: false) }
101
+ let(:given_attributes) { [OpenStruct.new(name: 'something'), OpenStruct.new(name: 'name')] }
102
+
103
+ it 'will return nil' do
104
+ expect(described_class.new.primary_key(given_attributes, given_model)).to eq(nil)
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+
111
+ end
@@ -0,0 +1,145 @@
1
+ require 'rspec'
2
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
3
+ require 'json'
4
+ require_relative '../../unit_logger'
5
+ require 'active_mocker/model_schema'
6
+
7
+ describe ActiveMocker::ModelSchema do
8
+
9
+ subject{described_class.new(class_name: 'Model',
10
+ table_name: 'models'
11
+ )
12
+ }
13
+
14
+ it '#class_name' do
15
+ expect(subject.class_name).to eq 'Model'
16
+ end
17
+
18
+ it '#table_name' do
19
+ expect(subject.table_name).to eq 'models'
20
+ end
21
+
22
+ describe '#primary_key' do
23
+
24
+ let(:a_primary_key_attribute){ ActiveMocker::ModelSchema::Attributes.new(name: 'key', primary_key: true, type: nil)}
25
+
26
+ it 'will return the first attribute that is a primary key' do
27
+ subject = described_class.new(attributes: [a_primary_key_attribute])
28
+ expect(subject.primary_key).to eq a_primary_key_attribute
29
+ end
30
+
31
+ let(:a_non_primary_key_attribute){ ActiveMocker::ModelSchema::Attributes.new(name: 'key', primary_key: false, type: nil)}
32
+
33
+ it 'will create a default primary_key of id if non is found' do
34
+ subject = described_class.new(attributes: [a_non_primary_key_attribute])
35
+ expect(subject.primary_key.name).to eq 'id'
36
+ expect(subject.attributes.count).to eq 2
37
+ end
38
+
39
+ end
40
+
41
+ describe '#attributes -> Array' do
42
+
43
+ subject { described_class.new(
44
+ attributes: [ActiveMocker::ModelSchema::Attributes.new(name: 'name',
45
+ type: :string,
46
+ precision: nil,
47
+ scale: nil,
48
+ default_value: 'default'
49
+ )]
50
+ ).attributes.first
51
+ }
52
+
53
+ it 'name' do
54
+ expect(subject.name).to eq 'name'
55
+ end
56
+
57
+ it 'type' do
58
+ expect(subject.type).to eq :string
59
+ end
60
+
61
+ it 'precision' do
62
+ expect(subject.precision).to eq nil
63
+ end
64
+
65
+ it 'scale' do
66
+ expect(subject.scale).to eq nil
67
+ end
68
+
69
+ it 'default_value' do
70
+ expect(subject.default_value).to eq 'default'
71
+ end
72
+
73
+ end
74
+
75
+ describe '#relationships -> Array' do
76
+
77
+ subject { described_class.new(
78
+ relationships:
79
+ [
80
+ ActiveMocker::ModelSchema::Relationships.new(name: 'name',
81
+ class_name: 'ClassName',
82
+ type: 'Relationship Type',
83
+ through: 'link',
84
+ foreign_key:'relationship_id',
85
+ join_table: 'join_table'
86
+ )
87
+ ]
88
+ ).relationships.first
89
+ }
90
+
91
+ it 'name' do
92
+ expect(subject.name).to eq 'name'
93
+ end
94
+
95
+ it 'class_name' do
96
+ expect(subject.class_name).to eq 'ClassName'
97
+ end
98
+
99
+ it 'type' do
100
+ expect(subject.type).to eq 'Relationship Type'
101
+ end
102
+
103
+ it 'through' do
104
+ expect(subject.through).to eq 'link'
105
+ end
106
+
107
+ it 'foreign_key' do
108
+ expect(subject.foreign_key).to eq 'relationship_id'
109
+ end
110
+
111
+ it 'join_table' do
112
+ expect(subject.join_table).to eq 'join_table'
113
+ end
114
+
115
+ end
116
+
117
+ describe '#methods -> Array' do
118
+
119
+ subject { described_class.new(
120
+ methods:
121
+ [
122
+ ActiveMocker::ModelSchema::Methods.new(name: 'method_name',
123
+ arguments: 'argument_array',
124
+ type: 'class_method'
125
+ )
126
+ ]
127
+ ).methods.first
128
+ }
129
+
130
+ it 'name' do
131
+ expect(subject.name).to eq 'method_name'
132
+ end
133
+
134
+ it 'arguments' do
135
+ expect(subject.arguments.class).to eq ActiveMocker::ModelSchema::Methods::Arguments
136
+ expect(subject.arguments.arguments).to eq 'argument_array'
137
+ end
138
+
139
+ it 'type' do
140
+ expect(subject.type).to eq 'class_method'
141
+ end
142
+
143
+ end
144
+
145
+ end
data/spec/lib/model.rb CHANGED
@@ -4,7 +4,8 @@ class Model < ActiveRecord::Base
4
4
 
5
5
  include ModelCore::PlanService::Dah
6
6
 
7
- belongs_to :company, class_name: 'PlanServiceCategory'
7
+ belongs_to :company, class_name: 'PlanServiceCategory', foreign_key: 'category_id'
8
+ belongs_to :person, through: 'customer'
8
9
  has_many :users
9
10
  has_one :account
10
11
  has_and_belongs_to_many :disclosures
@@ -0,0 +1,202 @@
1
+ require 'rspec'
2
+ require 'rspec/given'
3
+ $:.unshift File.expand_path('../../../lib', __FILE__)
4
+ require 'active_mocker/reparameterize'
5
+
6
+ describe ActiveMocker::Reparameterize do
7
+
8
+ describe '::call' do
9
+
10
+ describe 'create parameter arguments' do
11
+
12
+ context 'keyreq' do
13
+
14
+ Given(:parameters) { [[:keyreq, 'named_param']] }
15
+
16
+ When(:call) { described_class.call(parameters) }
17
+
18
+ Then { expect(call).to eq('named_param:') }
19
+
20
+ end
21
+
22
+ context 'key' do
23
+
24
+ Given(:parameters) { [[:key, 'named_param']] }
25
+
26
+ When (:call) { described_class.call(parameters) }
27
+
28
+ Then { expect(call).to eq('named_param: nil') }
29
+
30
+ end
31
+
32
+ context 'req' do
33
+
34
+ Given(:parameters) { [[:req, 'req_param']] }
35
+
36
+ When (:call) { described_class.call(parameters) }
37
+
38
+ Then { expect(call).to eq('req_param') }
39
+
40
+ end
41
+
42
+ context 'rest' do
43
+
44
+ Given(:parameters) { [[:rest, 'rest_param']] }
45
+
46
+ When (:call) { described_class.call(parameters) }
47
+
48
+ Then { expect(call).to eq('*rest_param') }
49
+
50
+ end
51
+
52
+
53
+ context 'opt' do
54
+
55
+ Given(:parameters) { [[:opt, 'opt_param']] }
56
+
57
+ When(:call) { described_class.call(parameters) }
58
+
59
+ Then { expect(call).to eq('opt_param=nil') }
60
+
61
+ end
62
+
63
+ context 'req, rest' do
64
+
65
+ Given(:parameters) { [[:req, 'req_param'], [:rest, 'rest_param']] }
66
+
67
+ When(:call) { described_class.call(parameters) }
68
+
69
+ Then { expect(call).to eq('req_param, *rest_param') }
70
+
71
+ end
72
+
73
+ context 'req, opt' do
74
+
75
+ Given(:parameters) { [[:req, 'req_param'], [:opt, 'opt_param']] }
76
+
77
+ When(:call) { described_class.call(parameters) }
78
+
79
+ Then { expect(call).to eq('req_param, opt_param=nil') }
80
+
81
+ end
82
+
83
+ context 'key, keyreq' do
84
+
85
+ Given(:parameters) { [[:key, 'named_param'], [:keyreq, 'named_param']] }
86
+
87
+ When(:call) { described_class.call(parameters) }
88
+
89
+ Then { expect(call).to eq('named_param: nil, named_param:') }
90
+
91
+ end
92
+
93
+ context 'rep, key' do
94
+
95
+ Given(:parameters) { [[:key, 'key_param'], [:req, 'req_param']] }
96
+
97
+ When (:call) { described_class.call(parameters) }
98
+
99
+ Then { expect(call).to eq('key_param: nil, req_param') }
100
+
101
+ end
102
+
103
+ end
104
+
105
+ context 'create a parameter passable list' do
106
+
107
+ context 'keyreq' do
108
+
109
+ Given(:parameters) { [[:keyreq, 'named_param']] }
110
+
111
+ When (:call) { described_class.call(parameters, param_list: true) }
112
+
113
+ Then { expect(call).to eq('named_param: named_param') }
114
+
115
+ end
116
+
117
+ context 'key' do
118
+
119
+ Given(:parameters) { [[:key, 'named_param']] }
120
+
121
+ When (:call) { described_class.call(parameters, param_list: true) }
122
+
123
+ Then { expect(call).to eq('named_param: named_param') }
124
+
125
+ end
126
+
127
+ context 'req' do
128
+
129
+ Given(:parameters) { [[:req, 'req_param']] }
130
+
131
+ When (:call) { described_class.call(parameters, param_list: true) }
132
+
133
+ Then { expect(call).to eq('req_param') }
134
+
135
+ end
136
+
137
+ context 'rest' do
138
+
139
+ Given(:parameters) { [[:rest, 'rest_param']] }
140
+
141
+ When (:call) { described_class.call(parameters, param_list: true) }
142
+
143
+ Then { expect(call).to eq('rest_param') }
144
+
145
+ end
146
+
147
+
148
+ context 'opt' do
149
+
150
+ Given(:parameters) { [[:opt, 'opt_param']] }
151
+
152
+ When (:call) { described_class.call(parameters, param_list: true) }
153
+
154
+ Then { expect(call).to eq('opt_param') }
155
+
156
+ end
157
+
158
+ context 'req, rest' do
159
+
160
+ Given(:parameters) { [[:req, 'req_param'], [:rest, 'rest_param']] }
161
+
162
+ When (:call) { described_class.call(parameters, param_list: true) }
163
+
164
+ Then { expect(call).to eq('req_param, rest_param') }
165
+
166
+ end
167
+
168
+ context 'req, opt' do
169
+
170
+ Given(:parameters) { [[:req, 'req_param'], [:opt, 'opt_param']] }
171
+
172
+ When (:call) { described_class.call(parameters, param_list: true) }
173
+
174
+ Then { expect(call).to eq('req_param, opt_param') }
175
+
176
+ end
177
+
178
+ context 'key, keyreq' do
179
+
180
+ Given(:parameters) { [[:key, 'key_param'], [:keyreq, 'keyreq_param']] }
181
+
182
+ When (:call) { described_class.call(parameters, param_list: true) }
183
+
184
+ Then { expect(call).to eq('key_param: key_param, keyreq_param: keyreq_param') }
185
+
186
+ end
187
+
188
+ context 'rep, key' do
189
+
190
+ Given(:parameters) { [[:key, 'key_param'], [:req, 'req_param']] }
191
+
192
+ When (:call) { described_class.call(parameters, param_list: true) }
193
+
194
+ Then { expect(call).to eq('key_param: key_param, req_param') }
195
+
196
+ end
197
+
198
+ end
199
+
200
+ end
201
+
202
+ end