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
@@ -1,84 +0,0 @@
1
- module ActiveMocker
2
- module MockInstanceMethods
3
-
4
-
5
- def mock_instance_method(method, &block)
6
- model_instance_methods[method] = block
7
- end
8
-
9
- def inspect
10
- inspection = self.class.column_names.map { |name|
11
- "#{name}: #{attribute_for_inspect(name)}"
12
- }.compact.join(", ")
13
-
14
- "#<#{self.class} #{inspection}>"
15
- end
16
-
17
- def attribute_for_inspect(attr_name)
18
- value = self.attributes[attr_name]
19
- if value.is_a?(String) && value.length > 50
20
- "#{value[0, 50]}...".inspect
21
- elsif value.is_a?(Date) || value.is_a?(Time)
22
- %("#{value.to_s(:db)}")
23
- elsif value.is_a?(Array) && value.size > 10
24
- inspected = value.first(10).inspect
25
- %(#{inspected[0...-1]}, ...])
26
- else
27
- value.inspect
28
- end
29
- end
30
-
31
- def hash
32
- attributes.hash
33
- end
34
-
35
- def ==(obj)
36
- return false if obj.nil?
37
- return hash == obj.attributes.hash if obj.respond_to?(:attributes)
38
- hash == obj.hash if obj.respond_to?(:hash)
39
- end
40
-
41
- private
42
-
43
- def read_attribute(attr)
44
- @attributes[attr]
45
- end
46
-
47
- def write_attribute(attr, value)
48
- @attributes[attr] = value
49
- end
50
-
51
- def read_association(attr)
52
- @associations[attr]
53
- end
54
-
55
- def write_association(attr, value)
56
- @associations[attr] = value
57
- end
58
-
59
- def attribute_to_string
60
- attributes.map {|k, v| "#{k.to_s}: #{v.inspect}"}.join(', ')
61
- end
62
-
63
- def delegate_to_model_instance(method, *args)
64
- self.class.send(:delegate_to_model_instance, method, *args)
65
- end
66
-
67
- def delegate_to_model_class(method, *args)
68
- self.class.send(:delegate_to_model_class, method, *args)
69
- end
70
-
71
- def model_instance_methods
72
- @model_instance_methods ||= self.class.send(:model_instance_methods)
73
- end
74
-
75
- def model_class_methods
76
- @model_class_methods ||= self.class.send(:model_class_methods)
77
- end
78
-
79
- def schema_attributes
80
- @schema_attributes ||= self.class.send(:attribute_template).dup
81
- end
82
-
83
- end
84
- end
@@ -1,10 +0,0 @@
1
- require 'active_mocker/collection/queries'
2
- require 'active_mocker/collection/base'
3
- require 'active_mocker/collection/association'
4
- require 'active_mocker/mock_class_methods'
5
- require 'active_mocker/mock_instance_methods'
6
- require 'active_mocker/loaded_mocks'
7
- require 'active_hash'
8
- require 'active_hash/ar_api'
9
- require 'active_mocker/class_exists'
10
- require 'virtus'
@@ -1,94 +0,0 @@
1
- require 'rspec'
2
- $:.unshift File.expand_path('../../', __FILE__)
3
- require 'active_support/all'
4
- require 'active_mocker/collection/base'
5
- require 'active_mocker/collection/queries'
6
- require 'active_mocker/collection/relation'
7
- require 'ostruct'
8
-
9
- describe ActiveMocker::Collection::Relation do
10
-
11
- subject{described_class.new}
12
-
13
- describe '#sum' do
14
-
15
- it 'sum values by attribute name' do
16
- subject << [OpenStruct.new(value: 1), OpenStruct.new(value: 1)]
17
- expect(subject.sum(:value)).to eq 2
18
- end
19
-
20
- end
21
-
22
- describe '#<<' do
23
-
24
- it 'will add a single item to the array' do
25
-
26
- subject << "item"
27
- expect(subject.count).to eq 1
28
- expect(subject.first).to eq 'item'
29
-
30
- end
31
-
32
- it 'will add a many item to the array' do
33
-
34
- subject << ['item1', 'item2', 'item3']
35
- expect(subject.count).to eq 3
36
- expect(subject).to eq ['item1', 'item2', 'item3']
37
-
38
- end
39
-
40
- end
41
-
42
- describe 'new' do
43
-
44
- it 'take optional item and adds to collection' do
45
-
46
- subject = described_class.new(1)
47
-
48
- expect(subject.first).to eq 1
49
-
50
- end
51
-
52
- it 'can take an array' do
53
-
54
- subject = described_class.new([1])
55
-
56
- expect(subject.first).to eq 1
57
-
58
- subject = described_class.new([1,2])
59
-
60
- expect(subject.last).to eq 2
61
-
62
- end
63
-
64
- end
65
-
66
- describe 'empty?' do
67
-
68
- it 'works' do
69
-
70
- expect(subject.empty?).to eq true
71
-
72
- end
73
-
74
- end
75
-
76
- describe 'each' do
77
-
78
- it 'works' do
79
- expect(described_class.new([1,2]).each{|a| a + a}).to eq [1, 2]
80
-
81
- end
82
-
83
- end
84
-
85
- describe 'map' do
86
-
87
- it 'works' do
88
- expect(described_class.new([1, 2]).map { |a| a + a }).to eq [2, 4]
89
- end
90
-
91
- end
92
-
93
-
94
- end