active_mocker 1.3.2 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
data/spec/unit_logger.rb CHANGED
@@ -10,11 +10,11 @@ class UnitLogger
10
10
  return @logger unless @logger.nil?
11
11
  FileUtils::mkdir_p 'log' unless File.directory? 'log'
12
12
  File.open('log/test.log', 'w').close unless File.exist? 'log/test.log'
13
- @logger = Logger.new('log/test.log')
13
+ @logger = ::Logger.new('log/test.log')
14
14
  @logger.formatter = proc do |severity, datetime, progname, msg|
15
15
  msg
16
16
  end
17
- @logger.level = Logger::DEBUG
17
+ @logger.level = ::Logger::DEBUG
18
18
  @logger
19
19
  end
20
20
 
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.3.2
4
+ version: 1.4.1
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-06-04 00:00:00.000000000 Z
11
+ date: 2014-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.1'
27
- - !ruby/object:Gem::Dependency
28
- name: active_hash
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.3'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.3'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: virtus
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -84,16 +70,16 @@ dependencies:
84
70
  name: rspec
85
71
  requirement: !ruby/object:Gem::Requirement
86
72
  requirements:
87
- - - "~>"
73
+ - - '='
88
74
  - !ruby/object:Gem::Version
89
- version: '2.14'
75
+ version: 2.14.1
90
76
  type: :development
91
77
  prerelease: false
92
78
  version_requirements: !ruby/object:Gem::Requirement
93
79
  requirements:
94
- - - "~>"
80
+ - - '='
95
81
  - !ruby/object:Gem::Version
96
- version: '2.14'
82
+ version: 2.14.1
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: sqlite3
99
85
  requirement: !ruby/object:Gem::Requirement
@@ -126,16 +112,16 @@ dependencies:
126
112
  name: rspec-rails
127
113
  requirement: !ruby/object:Gem::Requirement
128
114
  requirements:
129
- - - "~>"
115
+ - - '='
130
116
  - !ruby/object:Gem::Version
131
- version: '2'
117
+ version: 2.14.1
132
118
  type: :development
133
119
  prerelease: false
134
120
  version_requirements: !ruby/object:Gem::Requirement
135
121
  requirements:
136
- - - "~>"
122
+ - - '='
137
123
  - !ruby/object:Gem::Version
138
- version: '2'
124
+ version: 2.14.1
139
125
  - !ruby/object:Gem::Dependency
140
126
  name: fuubar
141
127
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +136,20 @@ dependencies:
150
136
  - - "~>"
151
137
  - !ruby/object:Gem::Version
152
138
  version: '1.3'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec-given
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.5'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.5'
153
153
  description: Creates mocks from Active Record models. Allows your test suite to run
154
154
  very fast by not loading Rails or hooking to a database. It parse the schema definition
155
155
  and the defined methods on a model then saves a ruby file that can be included with
@@ -170,9 +170,20 @@ files:
170
170
  - README.md
171
171
  - Rakefile
172
172
  - active_mocker.gemspec
173
- - lib/active_hash/ar_api.rb
174
- - lib/active_hash/init.rb
173
+ - lib/active_mock/association.rb
174
+ - lib/active_mock/base.rb
175
+ - lib/active_mock/collection.rb
176
+ - lib/active_mock/creators.rb
177
+ - lib/active_mock/do_nothing_active_record_methods.rb
178
+ - lib/active_mock/has_and_belongs_to_many.rb
179
+ - lib/active_mock/has_many.rb
180
+ - lib/active_mock/next_id.rb
181
+ - lib/active_mock/object_inspect.rb
182
+ - lib/active_mock/queries.rb
183
+ - lib/active_mock/records.rb
184
+ - lib/active_mock/relation.rb
175
185
  - lib/active_mocker.rb
186
+ - lib/active_mocker/active_mock.rb
176
187
  - lib/active_mocker/active_record.rb
177
188
  - lib/active_mocker/active_record/const_missing.rb
178
189
  - lib/active_mocker/active_record/relationships.rb
@@ -181,22 +192,18 @@ files:
181
192
  - lib/active_mocker/active_record/unknown_class_method.rb
182
193
  - lib/active_mocker/active_record/unknown_module.rb
183
194
  - lib/active_mocker/class_exists.rb
184
- - lib/active_mocker/collection/association.rb
185
- - lib/active_mocker/collection/base.rb
186
- - lib/active_mocker/collection/queries.rb
187
- - lib/active_mocker/collection/relation.rb
188
195
  - lib/active_mocker/config.rb
189
196
  - lib/active_mocker/const_sets.rb
197
+ - lib/active_mocker/db_to_ruby_type.rb
190
198
  - lib/active_mocker/field.rb
191
199
  - lib/active_mocker/generate.rb
192
200
  - lib/active_mocker/loaded_mocks.rb
193
201
  - lib/active_mocker/logger.rb
194
- - lib/active_mocker/mock_class_methods.rb
195
- - lib/active_mocker/mock_instance_methods.rb
196
- - lib/active_mocker/mock_requires.rb
197
202
  - lib/active_mocker/mock_task.rb
198
203
  - lib/active_mocker/mock_template.erb
199
204
  - lib/active_mocker/model_reader.rb
205
+ - lib/active_mocker/model_schema.rb
206
+ - lib/active_mocker/model_schema/generate.rb
200
207
  - lib/active_mocker/public_methods.rb
201
208
  - lib/active_mocker/reparameterize.rb
202
209
  - lib/active_mocker/schema_reader.rb
@@ -348,6 +355,7 @@ files:
348
355
  - sample_app_rails_4/script/cucumber
349
356
  - sample_app_rails_4/spec/compare_mocker_and_record_spec.rb
350
357
  - sample_app_rails_4/spec/factories.rb
358
+ - sample_app_rails_4/spec/micropost_mock_spec.rb
351
359
  - sample_app_rails_4/spec/mocks/micropost_mock.rb
352
360
  - sample_app_rails_4/spec/mocks/relationship_mock.rb
353
361
  - sample_app_rails_4/spec/mocks/user_mock.rb
@@ -356,13 +364,19 @@ files:
356
364
  - sample_app_rails_4/spec/user_mock_spec.rb
357
365
  - sample_app_rails_4/vendor/assets/javascripts/.keep
358
366
  - sample_app_rails_4/vendor/assets/stylesheets/.keep
359
- - spec/lib/active_mocker/collection.rb
367
+ - spec/lib/acitve_mock/queriable_spec.rb
368
+ - spec/lib/active_mocker/db_to_ruby_type_spec.rb
360
369
  - spec/lib/active_mocker/generate_spec.rb
370
+ - spec/lib/active_mocker/loaded_mocks_spec.rb
371
+ - spec/lib/active_mocker/logger_spec.rb
361
372
  - spec/lib/active_mocker/model_reader_spec.rb
373
+ - spec/lib/active_mocker/model_schema/generate_spec.rb
374
+ - spec/lib/active_mocker/model_schema_spec.rb
362
375
  - spec/lib/active_mocker/schema_reader_spec.rb
363
376
  - spec/lib/model.rb
364
377
  - spec/lib/person.rb
365
378
  - spec/lib/readme_spec.rb
379
+ - spec/lib/reparameterize_spec.rb
366
380
  - spec/lib/schema.rb
367
381
  - spec/unit_logger.rb
368
382
  homepage: https://github.com/zeisler/active_mocker
@@ -391,12 +405,18 @@ specification_version: 4
391
405
  summary: Creates mocks from Active Record models. Allows your test suite to run very
392
406
  fast by not loading Rails or hooking to a database.
393
407
  test_files:
394
- - spec/lib/active_mocker/collection.rb
408
+ - spec/lib/acitve_mock/queriable_spec.rb
409
+ - spec/lib/active_mocker/db_to_ruby_type_spec.rb
395
410
  - spec/lib/active_mocker/generate_spec.rb
411
+ - spec/lib/active_mocker/loaded_mocks_spec.rb
412
+ - spec/lib/active_mocker/logger_spec.rb
396
413
  - spec/lib/active_mocker/model_reader_spec.rb
414
+ - spec/lib/active_mocker/model_schema/generate_spec.rb
415
+ - spec/lib/active_mocker/model_schema_spec.rb
397
416
  - spec/lib/active_mocker/schema_reader_spec.rb
398
417
  - spec/lib/model.rb
399
418
  - spec/lib/person.rb
400
419
  - spec/lib/readme_spec.rb
420
+ - spec/lib/reparameterize_spec.rb
401
421
  - spec/lib/schema.rb
402
422
  - spec/unit_logger.rb
@@ -1,77 +0,0 @@
1
- require_relative 'init'
2
- require_relative '../active_mocker/collection/queries'
3
- require_relative '../active_mocker/collection/base'
4
- require_relative '../active_mocker/collection/relation'
5
- module ActiveMocker
6
- module ActiveHash
7
-
8
- module ARApi
9
-
10
- include ::ActiveHash::ARApi::Init
11
-
12
- def delete
13
- self.class.send(:record_index).delete("#{self.id}")
14
- records = self.class.instance_variable_get(:@records)
15
- index = records.index(self)
16
- records.delete_at(index)
17
- end
18
-
19
- def update(options={})
20
- options.each do |method, value|
21
- send("#{method}=", value)
22
- end
23
-
24
- end
25
-
26
- def self.included(base)
27
- base.extend(ClassMethods)
28
- end
29
-
30
- module ClassMethods
31
- include ActiveMocker::Collection::Queries
32
-
33
- def create(attributes = {}, &block)
34
- record = new(attributes) unless block_given?
35
- record = new(attributes, &block) if block_given?
36
- record.save
37
- mark_dirty
38
- record
39
- end
40
-
41
- def find_or_create_by(attributes)
42
- find_by(attributes) || create(attributes)
43
- end
44
-
45
- def find_or_initialize_by(attributes)
46
- find_by(attributes) || new(attributes)
47
- end
48
-
49
- def delete(id)
50
- find(id).delete
51
- end
52
-
53
- def to_a
54
- @records
55
- end
56
-
57
- alias_method :destroy, :delete
58
-
59
- def delete_all(options=nil)
60
- return super() if options.nil?
61
- where(options).map{|r| r.delete}.count
62
- end
63
-
64
- alias_method :destroy_all, :delete_all
65
-
66
- end
67
-
68
- end
69
-
70
-
71
-
72
- end
73
-
74
- end
75
-
76
-
77
-
@@ -1,32 +0,0 @@
1
- require 'active_support/core_ext/hash/indifferent_access'
2
- module ActiveHash
3
- module ARApi
4
-
5
- module Init
6
-
7
- attr_reader :associations, :types
8
-
9
- def initialize(attributes = {}, &block)
10
- @types = {}
11
- yield self if block_given?
12
- attributes.each do |key, value|
13
- begin
14
- send "#{key}=", value
15
- rescue NoMethodError
16
- raise ActiveMocker::RejectedParams, "{:#{key}=>#{value.inspect}} for #{self.class.name}"
17
- end
18
- end
19
-
20
- end
21
-
22
-
23
-
24
- end
25
- end
26
-
27
- end
28
-
29
- module ActiveMocker
30
- class RejectedParams < Exception
31
- end
32
- end
@@ -1,12 +0,0 @@
1
- module ActiveMocker
2
-
3
- module Collection
4
-
5
- class Association < Base
6
-
7
- include Queries
8
-
9
- end
10
-
11
- end
12
- end
@@ -1,65 +0,0 @@
1
- module ActiveMocker
2
-
3
- module Collection
4
-
5
-
6
- class Base
7
-
8
- include Enumerable
9
-
10
- def initialize(collection=[])
11
- @collection = [*collection]
12
- end
13
-
14
- def <<(*records)
15
- collection.concat(records.flatten)
16
- end
17
-
18
- delegate :take, :push, :clear, :first, :last, :concat, :replace, :distinct, :uniq, :count, :size, :length, :empty?, :any?, :include?, :delete, to: :collection
19
- alias distinct uniq
20
-
21
- # def delete(obj)
22
- # collection.delete(obj)
23
- # end
24
-
25
- def order(key)
26
- self.class.new(collection.sort_by{ |item| item.send(key) })
27
- end
28
-
29
- def reverse_order
30
- self.class.new(collection.reverse)
31
- end
32
-
33
- def select(&block)
34
- self.class.new(collection.select(&block))
35
- end
36
-
37
- def each(&block)
38
- self.class.new(collection.each do |item|
39
- block.call(item)
40
- end)
41
- end
42
-
43
- def to_a
44
- @collection
45
- end
46
-
47
- def ==(val)
48
- return false if val.nil?
49
- collection.hash == val.hash
50
- end
51
-
52
- private
53
-
54
- def collection
55
- @collection
56
- end
57
-
58
- end
59
-
60
- end
61
-
62
-
63
-
64
-
65
- end
@@ -1,11 +0,0 @@
1
- module ActiveMocker
2
-
3
- module Collection
4
-
5
- class Relation < Base
6
- include Queries
7
-
8
- end
9
-
10
- end
11
- end
@@ -1,92 +0,0 @@
1
- module ActiveMocker
2
- module MockClassMethods
3
-
4
- def mock_instance_method(method, &block)
5
- model_instance_methods[method.to_s] = block
6
- end
7
-
8
- def mock_class_method(method, &block)
9
- model_class_methods[method.to_s] = block
10
- end
11
-
12
- def column_names
13
- schema_attributes_template
14
- end
15
-
16
- private
17
-
18
- def delegate_to_model_instance(method, *args)
19
- model_class_instance.send(method, *args)
20
- end
21
-
22
- def delegate_to_model_class(method, *args)
23
- model_class.send(method, *args)
24
- end
25
-
26
- def model_class_methods
27
- @model_class_methods ||= HashWithIndifferentAccess.new
28
- end
29
-
30
- def model_methods_template
31
- @model_methods_template ||= HashWithIndifferentAccess.new
32
- end
33
-
34
- def schema_attributes_template
35
- @schema_attributes_template ||= HashWithIndifferentAccess.new
36
- end
37
-
38
- def model_class
39
- @model_class
40
- end
41
-
42
- def model_class_instance
43
- @model_class_instance ||= model_class.new
44
- end
45
-
46
- def attribute_names
47
- @attribute_names
48
- end
49
-
50
- def attribute_names=(attributes)
51
- @attribute_names = attributes.map{|a| a.to_sym}
52
- end
53
-
54
- def attribute_template
55
- return @attribute_template unless @attribute_template.nil?
56
- @attribute_template = HashWithIndifferentAccess.new
57
- attribute_names.each {|a| @attribute_template[a] = nil}
58
- return @attribute_template
59
- end
60
-
61
- def association_names
62
- @association_names
63
- end
64
-
65
- def association_names=(associations)
66
- @association_names = associations.map{|a| a.to_sym}
67
- end
68
-
69
- def association_template
70
- return @association_template unless @association_template.nil?
71
- @association_template = HashWithIndifferentAccess.new
72
- association_names.each {|a| @association_template[a] = nil}
73
- return @association_template
74
- end
75
-
76
- def set_type(field_name, type)
77
- types[field_name] = Virtus::Attribute.build(type)
78
- end
79
-
80
- def coerce(field, new_val)
81
- type = self.class.types[field]
82
- return attributes[field] = type.coerce(new_val) unless type.nil?
83
- return new_value
84
- end
85
-
86
- public
87
- def is_implemented(val, method)
88
- raise "#{method} is not Implemented for Class: #{name}" if val == :not_implemented
89
- end
90
-
91
- end
92
- end