active_mocker 1.1.23 → 1.2.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -22
  3. data/active_mocker.gemspec +1 -0
  4. data/lib/active_hash/init.rb +1 -1
  5. data/lib/active_mocker.rb +4 -5
  6. data/lib/active_mocker/active_record/schema.rb +2 -4
  7. data/lib/active_mocker/config.rb +6 -22
  8. data/lib/active_mocker/field.rb +3 -1
  9. data/lib/active_mocker/generate.rb +161 -0
  10. data/lib/active_mocker/mock_class_methods.rb +82 -0
  11. data/lib/active_mocker/mock_instance_methods.rb +81 -0
  12. data/lib/active_mocker/mock_requires.rb +12 -0
  13. data/lib/active_mocker/mock_task.rb +12 -0
  14. data/lib/active_mocker/mock_template.erb +84 -0
  15. data/lib/active_mocker/public_methods.rb +6 -2
  16. data/lib/active_mocker/schema_reader.rb +35 -16
  17. data/lib/active_mocker/table.rb +2 -0
  18. data/lib/active_mocker/version.rb +1 -1
  19. data/mocks/micropost_mock.rb +100 -0
  20. data/mocks/relationship_mock.rb +100 -0
  21. data/mocks/user_mock.rb +196 -0
  22. data/plan_mock.rb +2323 -0
  23. data/spec/lib/active_mocker/generate_spec.rb +49 -0
  24. data/spec/lib/active_mocker/{base_spec.rb → performance/base_spec.rb} +17 -37
  25. data/spec/lib/active_mocker/performance/large_schema.rb +3576 -0
  26. data/spec/lib/active_mocker/performance/migration/20140327205359_migration.rb +0 -0
  27. data/spec/lib/active_mocker/performance/schema_reader_spec.rb +96 -0
  28. data/spec/lib/active_mocker/schema_reader_spec.rb +12 -27
  29. data/spec/lib/compare_mocker_and_record_spec.rb +3 -2
  30. data/spec/lib/readme_spec.rb +205 -0
  31. data/spec/mocks/micropost_mock.rb +100 -0
  32. data/spec/mocks/relationship_mock.rb +100 -0
  33. data/spec/mocks/user_mock.rb +196 -0
  34. data/spec/mocks/user_mock_spec.rb +173 -0
  35. metadata +48 -9
  36. data/lib/active_mocker/base.rb +0 -356
  37. data/spec/lib/active_mocker/active_record/schema_spec.rb +0 -2721
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.1.23
4
+ version: 1.2.pre
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-04-29 00:00:00.000000000 Z
11
+ date: 2014-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '1.3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: fuubar
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: Create mocks from active record models without loading rails or running
112
126
  a database. The Mock's methods have the same arguments as the AR model and if they
113
127
  change you get a error in your test.
@@ -138,12 +152,17 @@ files:
138
152
  - lib/active_mocker/active_record/scope.rb
139
153
  - lib/active_mocker/active_record/unknown_class_method.rb
140
154
  - lib/active_mocker/active_record/unknown_module.rb
141
- - lib/active_mocker/base.rb
142
155
  - lib/active_mocker/collection_association.rb
143
156
  - lib/active_mocker/config.rb
144
157
  - lib/active_mocker/const_sets.rb
145
158
  - lib/active_mocker/field.rb
159
+ - lib/active_mocker/generate.rb
146
160
  - lib/active_mocker/logger.rb
161
+ - lib/active_mocker/mock_class_methods.rb
162
+ - lib/active_mocker/mock_instance_methods.rb
163
+ - lib/active_mocker/mock_requires.rb
164
+ - lib/active_mocker/mock_task.rb
165
+ - lib/active_mocker/mock_template.erb
147
166
  - lib/active_mocker/model_reader.rb
148
167
  - lib/active_mocker/public_methods.rb
149
168
  - lib/active_mocker/reparameterize.rb
@@ -156,15 +175,27 @@ files:
156
175
  - lib/active_record/db/schema.rb
157
176
  - lib/file_reader.rb
158
177
  - lib/string_reader.rb
159
- - spec/lib/active_mocker/active_record/schema_spec.rb
160
- - spec/lib/active_mocker/base_spec.rb
178
+ - mocks/micropost_mock.rb
179
+ - mocks/relationship_mock.rb
180
+ - mocks/user_mock.rb
181
+ - plan_mock.rb
161
182
  - spec/lib/active_mocker/collection_association_spec.rb
183
+ - spec/lib/active_mocker/generate_spec.rb
162
184
  - spec/lib/active_mocker/model_reader_spec.rb
185
+ - spec/lib/active_mocker/performance/base_spec.rb
186
+ - spec/lib/active_mocker/performance/large_schema.rb
187
+ - spec/lib/active_mocker/performance/migration/20140327205359_migration.rb
188
+ - spec/lib/active_mocker/performance/schema_reader_spec.rb
163
189
  - spec/lib/active_mocker/schema_reader_spec.rb
164
190
  - spec/lib/compare_mocker_and_record_spec.rb
165
191
  - spec/lib/model.rb
166
192
  - spec/lib/person.rb
193
+ - spec/lib/readme_spec.rb
167
194
  - spec/lib/schema.rb
195
+ - spec/mocks/micropost_mock.rb
196
+ - spec/mocks/relationship_mock.rb
197
+ - spec/mocks/user_mock.rb
198
+ - spec/mocks/user_mock_spec.rb
168
199
  homepage: https://github.com/zeisler/active_mocker
169
200
  licenses:
170
201
  - MIT
@@ -180,9 +211,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
180
211
  version: 2.0.0
181
212
  required_rubygems_version: !ruby/object:Gem::Requirement
182
213
  requirements:
183
- - - ">="
214
+ - - ">"
184
215
  - !ruby/object:Gem::Version
185
- version: '0'
216
+ version: 1.3.1
186
217
  requirements: []
187
218
  rubyforge_project:
188
219
  rubygems_version: 2.2.2
@@ -191,12 +222,20 @@ specification_version: 4
191
222
  summary: Create mocks from active record models without loading rails or running a
192
223
  database
193
224
  test_files:
194
- - spec/lib/active_mocker/active_record/schema_spec.rb
195
- - spec/lib/active_mocker/base_spec.rb
196
225
  - spec/lib/active_mocker/collection_association_spec.rb
226
+ - spec/lib/active_mocker/generate_spec.rb
197
227
  - spec/lib/active_mocker/model_reader_spec.rb
228
+ - spec/lib/active_mocker/performance/base_spec.rb
229
+ - spec/lib/active_mocker/performance/large_schema.rb
230
+ - spec/lib/active_mocker/performance/migration/20140327205359_migration.rb
231
+ - spec/lib/active_mocker/performance/schema_reader_spec.rb
198
232
  - spec/lib/active_mocker/schema_reader_spec.rb
199
233
  - spec/lib/compare_mocker_and_record_spec.rb
200
234
  - spec/lib/model.rb
201
235
  - spec/lib/person.rb
236
+ - spec/lib/readme_spec.rb
202
237
  - spec/lib/schema.rb
238
+ - spec/mocks/micropost_mock.rb
239
+ - spec/mocks/relationship_mock.rb
240
+ - spec/mocks/user_mock.rb
241
+ - spec/mocks/user_mock_spec.rb
@@ -1,356 +0,0 @@
1
- module ActiveMocker
2
- class Base
3
- extend Config
4
- extend Forwardable
5
- @@_self = self
6
- def_delegators :@@_self,
7
- :mass_assignment,
8
- :schema_attributes,
9
- :model_attributes,
10
- :model_dir,
11
- :schema_file,
12
- :model_file_reader,
13
- :schema_file_reader,
14
- :clear_cache
15
-
16
- attr_reader :model_name, :klass
17
-
18
- def initialize(model_name)
19
- @model_name = model_name
20
- active_hash_mock_class
21
- end
22
-
23
- def self.configure(&block)
24
- config(&block)
25
- end
26
-
27
- def self.mock(model_name)
28
- self.send(:new, model_name).klass
29
- end
30
-
31
- def model_definition
32
- @model_definition ||= ModelReader.new({model_dir: model_dir, file_reader: model_file_reader}).parse(model_file_name)
33
- end
34
-
35
- def model_file_name
36
- model_name.tableize.singularize
37
- end
38
-
39
- def table_definition
40
- @table_definition ||= SchemaReader.new({schema_file: schema_file, file_reader: schema_file_reader, clear_cache: clear_cache}).search(model_name.tableize)
41
- end
42
-
43
- def active_hash_mock_class
44
- fill_templates
45
- add_method_mock_of
46
- if schema_attributes
47
- klass = create_klass
48
- fields = table_definition.column_names
49
- klass.class_eval do
50
- klass.fields(*fields)
51
- end
52
- add_column_names_method
53
- end
54
- if model_attributes
55
- add_class_methods
56
- add_instance_methods
57
- add_single_relationships
58
- add_collections_relationships
59
- end
60
- end
61
-
62
- def fill_templates
63
- klass = create_klass
64
- klass.send(:association_names=, model_definition.relationships)
65
- klass.send(:attribute_names=, table_definition.column_names)
66
- end
67
-
68
- def add_method_mock_of
69
- klass = create_klass
70
- m_name = model_name
71
- klass.instance_variable_set(:@model_class, model_definition.klass)
72
- klass.instance_eval do
73
- define_method(:mock_of) {m_name}
74
- end
75
- end
76
-
77
- def add_single_relationships
78
- klass = create_klass
79
- model_definition.single_relationships.each do |m|
80
- klass.send(:association_template)[m] = nil
81
- create_association(m)
82
- end
83
- end
84
-
85
- def add_collections_relationships
86
- klass = create_klass
87
- model_definition.collections.each do |m|
88
- klass.send(:association_template)[m] = nil
89
- begin
90
- klass.class_eval <<-eos, __FILE__, __LINE__+1
91
- def #{m}
92
- read_association(#{m.inspect})
93
- end
94
-
95
- def #{m}=(value)
96
- write_association(#{m.inspect}, CollectionAssociation.new(value))
97
- end
98
- eos
99
- rescue SyntaxError
100
- Logger_.warn "ActiveMocker :: Can't create accessor methods for #{m}.\n #{caller}"
101
- end
102
- end
103
- end
104
-
105
- def create_association(m)
106
- begin
107
- klass.class_eval <<-eos, __FILE__, __LINE__+1
108
- def #{m}
109
- read_association(#{m.inspect})
110
- end
111
-
112
- def #{m}=(value)
113
- write_association(#{m.inspect}, value)
114
- end
115
- eos
116
- rescue SyntaxError
117
- Logger_.warn "ActiveMocker :: Can't create accessor methods for #{m}.\n #{caller}"
118
- end
119
- end
120
-
121
-
122
- def add_instance_methods
123
- klass = create_klass
124
- model_definition.instance_methods_with_arguments.each do |method|
125
- m = method.keys.first
126
- if m == :attributes
127
- Logger_.warn "ActiveMocker Depends on the #attributes method. It will not be redefined for the mock."
128
- next
129
- end
130
- params = Reparameterize.call(method.values.first)
131
- params_pass = Reparameterize.call(method.values.first, true)
132
-
133
- klass.send(:model_methods_template)[m] = eval_lambda(params, %Q[raise "##{m} is not Implemented for Class: #{klass.name}"])
134
- klass.class_eval <<-eos, __FILE__, __LINE__+1
135
- def #{m}(#{params})
136
- block = model_instance_methods[#{m.inspect}].to_proc
137
- instance_exec(*[#{params_pass}], &block)
138
- end
139
- eos
140
- end
141
- end
142
-
143
- def add_class_methods
144
- klass = create_klass
145
- model_definition.class_methods_with_arguments.each do |method|
146
- m = method.keys.first
147
- params = Reparameterize.call(method.values.first)
148
- params_pass = Reparameterize.call(method.values.first, true)
149
- klass.send(:model_class_methods)[m] = eval_lambda(params, %Q[raise "::#{m} is not Implemented for Class: #{klass.name}"])
150
- klass.class_eval <<-eos, __FILE__, __LINE__+1
151
- def self.#{m}(#{params})
152
- block = model_class_methods[#{m.inspect}].to_proc
153
- instance_exec(*[#{params_pass}], &block)
154
- end
155
- eos
156
- end
157
- end
158
-
159
- def eval_lambda(arguments, block)
160
- eval(%Q[ ->(#{arguments}){ #{block} }],binding, __FILE__, __LINE__)
161
- end
162
-
163
- def add_column_names_method
164
- klass = create_klass
165
- table = table_definition
166
- klass.singleton_class.class_eval do
167
- define_method(:column_names) do
168
- table.column_names
169
- end
170
- end
171
- end
172
-
173
- def create_klass
174
- @klass ||= const_class
175
- end
176
-
177
- def const_class
178
- remove_const(mock_class_name) if class_exists? mock_class_name
179
- klass = Object.const_set(mock_class_name, Class.new(::ActiveHash::Base))
180
- klass.send(:include, ActiveHash::ARApi)
181
- klass.send(:prepend, ModelInstanceMethods) # is a private method for ruby 2.0.0
182
- klass.extend ModelClassMethods
183
- klass
184
- end
185
-
186
- def remove_const(class_name)
187
- Object.send(:remove_const, class_name)
188
- end
189
-
190
- def class_exists?(class_name)
191
- klass = Module.const_get(class_name)
192
- return klass.is_a?(Class)
193
- rescue NameError
194
- return false
195
- end
196
-
197
- def mock_class_name
198
- "#{model_name}Mock"
199
- end
200
-
201
- end
202
-
203
- module ModelInstanceMethods
204
-
205
- def mock_instance_method(method, &block)
206
- model_instance_methods[method] = block
207
- end
208
-
209
- def inspect
210
- inspection = self.class.column_names.map { |name|
211
- "#{name}: #{attribute_for_inspect(name)}"
212
- }.compact.join(", ")
213
-
214
- "#<#{self.class} #{inspection}>"
215
- end
216
-
217
- def attribute_for_inspect(attr_name)
218
- value = self.attributes[attr_name]
219
- if value.is_a?(String) && value.length > 50
220
- "#{value[0, 50]}...".inspect
221
- elsif value.is_a?(Date) || value.is_a?(Time)
222
- %("#{value.to_s(:db)}")
223
- elsif value.is_a?(Array) && value.size > 10
224
- inspected = value.first(10).inspect
225
- %(#{inspected[0...-1]}, ...])
226
- else
227
- value.inspect
228
- end
229
- end
230
-
231
- def hash
232
- attributes.hash
233
- end
234
-
235
- def ==(obj)
236
- hash == obj.attributes.hash
237
- end
238
-
239
- private
240
-
241
- def read_attribute(attr)
242
- attributes[attr]
243
- end
244
-
245
- def write_attribute(attr, value)
246
- attributes[attr] = value
247
- end
248
-
249
- def read_association(attr)
250
- @associations[attr]
251
- end
252
-
253
- def write_association(attr, value)
254
- @associations[attr] = value
255
- end
256
-
257
- def attribute_to_string
258
- attributes.map {|k, v| "#{k.to_s}: #{v.inspect}"}.join(', ')
259
- end
260
-
261
- def delegate_to_model_instance(method, *args)
262
- self.class.send(:delegate_to_model_instance, method, *args)
263
- end
264
-
265
- def delegate_to_model_class(method, *args)
266
- self.class.send(:delegate_to_model_class, method, *args)
267
- end
268
-
269
- def model_instance_methods
270
- @model_instance_methods ||= self.class.send(:model_methods_template).dup
271
- end
272
-
273
- def schema_attributes
274
- @schema_attributes ||= self.class.send(:attribute_template).dup
275
- end
276
-
277
- end
278
-
279
- module ModelClassMethods
280
-
281
- def mock_instance_method(method, &block)
282
- model_methods_template[method] = block
283
- end
284
-
285
- def mock_class_method(method, &block)
286
- model_class_methods[method] = block
287
- end
288
-
289
- def column_names
290
- schema_attributes_template
291
- end
292
-
293
- private
294
-
295
- def delegate_to_model_instance(method, *args)
296
- model_class_instance.send(method, *args)
297
- end
298
-
299
- def delegate_to_model_class(method, *args)
300
- model_class.send(method, *args)
301
- end
302
-
303
- def model_class_methods
304
- @model_class_methods ||= HashWithIndifferentAccess.new
305
- end
306
-
307
- def model_methods_template
308
- @model_methods_template ||= HashWithIndifferentAccess.new
309
- end
310
-
311
- def schema_attributes_template
312
- @schema_attributes_template ||= HashWithIndifferentAccess.new
313
- end
314
-
315
- def model_class
316
- @model_class
317
- end
318
-
319
- def model_class_instance
320
- @model_class_instance ||= model_class.new
321
- end
322
-
323
- def attribute_names
324
- @attribute_names
325
- end
326
-
327
- def attribute_names=(attributes)
328
- @attribute_names = attributes.map{|a| a.to_sym}
329
- end
330
-
331
- def attribute_template
332
- return @attribute_template unless @attribute_template.nil?
333
- @attribute_template = HashWithIndifferentAccess.new
334
- attribute_names.each {|a| @attribute_template[a] = nil}
335
- return @attribute_template
336
- end
337
-
338
- def association_names
339
- @association_names
340
- end
341
-
342
- def association_names=(associations)
343
- @association_names = associations.map{|a| a.to_sym}
344
- end
345
-
346
- def association_template
347
- return @association_template unless @association_template.nil?
348
- @association_template = HashWithIndifferentAccess.new
349
- association_names.each {|a| @association_template[a] = nil}
350
- return @association_template
351
- end
352
-
353
- end
354
-
355
- end
356
-