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
@@ -0,0 +1,96 @@
1
+ require 'rspec'
2
+ $:.unshift File.expand_path('../../', __FILE__)
3
+ require 'string_reader'
4
+ require 'file_reader'
5
+ require 'active_mocker/table'
6
+ require 'active_mocker/field'
7
+ require 'active_mocker/active_record/schema'
8
+ require 'active_mocker/schema_reader'
9
+ require 'active_support/all'
10
+
11
+ describe ActiveMocker::SchemaReader, pending: true do
12
+
13
+ let(:schema_file){ File.join(File.expand_path('../../', __FILE__), 'performance/large_schema.rb') }
14
+
15
+ context 'reads from file' do
16
+
17
+ let(:subject){described_class.new({schema_file: schema_file, clear_cache: true})}
18
+
19
+ describe '#search' do
20
+
21
+ it 'takes a table name and will return its attributes' do
22
+ described_class.new({schema_file: schema_file}).search("people")
23
+ end
24
+
25
+ end
26
+
27
+ let(:people_search){subject.search("people")}
28
+
29
+ describe '#column_names' do
30
+
31
+ it 'returns an array of columns from the schema.rb' do
32
+ expect(people_search.name).to eq 'people'
33
+ expect(people_search.column_names).to eq ["id", "company_id", "first_name", "middle_name", "last_name", "address_1", "address_2", "city", "state_id", "zip_code_id", "title", "department", "person_email", "work_phone", "cell_phone", "home_phone", "fax", "user_id_assistant", "birth_date", "needs_review", "created_at", "updated_at"]
34
+ end
35
+
36
+ end
37
+
38
+ describe '#fields' do
39
+
40
+ it 'returns all fields from schema' do
41
+ expect(people_search.fields[1].to_h).to eq({:name=>"company_id", :type=>:integer, :options=>[]})
42
+ end
43
+
44
+ end
45
+
46
+ describe '#name' do
47
+
48
+ it 'returns the name of the table' do
49
+ expect(people_search.name).to eq("people")
50
+ end
51
+
52
+
53
+ end
54
+
55
+ describe 'plans table' do
56
+
57
+ before(:all) do
58
+ # described_class.new({clear_cache: true})
59
+ end
60
+
61
+ after(:all) do
62
+ # described_class.new({clear_cache: true})
63
+ end
64
+
65
+ let(:subject){described_class.new({schema_file: schema_file, clear_cache: false})}
66
+
67
+ it 'find table' do
68
+
69
+ 10.times do
70
+ # 0.019135
71
+ # 8.0e-06
72
+ # 3.0e-06
73
+ # 6.3e-05
74
+ # 3.0e-06
75
+ # 2.0e-06
76
+ # 3.0e-06
77
+ # 3.0e-06
78
+ # 2.0e-06
79
+ # 2.0e-06
80
+ start = Time.now
81
+ subject.search("plans").name
82
+ # subject.search("disclosures").name
83
+ # subject.search("bmg_tpa_masters").name
84
+ # subject.search("bmg_rk_masters").name
85
+ puts Time.now - start
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+
95
+
96
+ end
@@ -59,13 +59,13 @@ describe ActiveMocker::SchemaReader do
59
59
 
60
60
  let(:subject){described_class.new({schema_file:nil, file_reader: example_schema, clear_cache: true})}
61
61
 
62
- let(:search){subject.search('people')}
62
+ let!(:search){subject.search(nil)}
63
63
 
64
64
  it 'let not read a file but return a string instead to be evaluated' do
65
- people = subject.search('people')
66
- expect(people.name).to eq 'people'
67
- expect(people.fields[2].to_h).to eq({:name=>"first_name", :type=>:string, :options=>[{:limit=>128}]})
68
- expect(subject.search('zip_codes').name).to eq 'zip_codes'
65
+ tables = subject.tables
66
+ expect(tables.first.name).to eq 'people'
67
+ expect(tables.last.name).to eq 'zip_codes'
68
+ expect(tables.last.fields.count).to eq 8
69
69
  end
70
70
 
71
71
  end
@@ -78,18 +78,19 @@ describe ActiveMocker::SchemaReader do
78
78
  describe '#search' do
79
79
 
80
80
  it 'takes a table name and will return its attributes' do
81
- described_class.new({schema_file: schema_file}).search("people")
81
+ described_class.new({schema_file: schema_file}).search(nil)
82
82
  end
83
83
 
84
84
  end
85
85
 
86
- let(:people_search){subject.search("people")}
86
+ let(:tables){subject.search("people")}
87
87
 
88
88
  describe '#column_names' do
89
89
 
90
90
  it 'returns an array of columns from the schema.rb' do
91
- expect(people_search.name).to eq 'people'
92
- expect(people_search.column_names).to eq ["id", "company_id", "first_name", "middle_name", "last_name", "address_1", "address_2", "city", "state_id", "zip_code_id", "title", "department", "person_email", "work_phone", "cell_phone", "home_phone", "fax", "user_id_assistant", "birth_date", "needs_review", "created_at", "updated_at"]
91
+ tables
92
+ expect(subject.tables.first.name).to eq 'people'
93
+ expect(subject.tables.first.column_names).to eq ["id", "company_id", "first_name", "middle_name", "last_name", "address_1", "address_2", "city", "state_id", "zip_code_id", "title", "department", "person_email", "work_phone", "cell_phone", "home_phone", "fax", "user_id_assistant", "birth_date", "needs_review", "created_at", "updated_at"]
93
94
  end
94
95
 
95
96
  end
@@ -97,28 +98,12 @@ describe ActiveMocker::SchemaReader do
97
98
  describe '#fields' do
98
99
 
99
100
  it 'returns all fields from schema' do
100
- expect(people_search.fields[1].to_h).to eq({:name=>"company_id", :type=>:integer, :options=>[]})
101
+ tables
102
+ expect(subject.tables.first.fields[1].to_h).to eq({:name=>"company_id", :type=>:integer, :options=>[]})
101
103
  end
102
104
 
103
105
  end
104
106
 
105
- describe '#name' do
106
-
107
- it 'returns the name of the table' do
108
- expect(people_search.name).to eq("people")
109
- end
110
-
111
-
112
- end
113
-
114
- it 'returns an exception if table not found in schema.rb' do
115
- expect{
116
- described_class.new(
117
- {schema_file: schema_file}
118
- ).search("disclosures")
119
- }.to raise_error 'disclosures table not found.'
120
- end
121
-
122
107
  end
123
108
 
124
109
  end
@@ -3,7 +3,7 @@ project_root = File.expand_path('../../../', __FILE__)
3
3
  require "#{project_root}/lib/active_record/ar"
4
4
  require "#{project_root}/lib/active_mocker"
5
5
 
6
- describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
6
+ describe 'Comparing ActiveMocker Api to ActiveRecord Api', pending: true do
7
7
 
8
8
  before(:each) do
9
9
  ActiveMocker::Base.configure do |config|
@@ -11,8 +11,9 @@ describe 'Comparing ActiveMocker Api to ActiveRecord Api' do
11
11
  config.model_dir = project_root + '/lib/active_record/app/models'
12
12
  config.schema_file_reader = nil
13
13
  config.model_file_reader = nil
14
- config.active_hash_as_base = true
15
14
  config.log_level = Logger::WARN
15
+ config.clear_cache = true
16
+ config.migration_dir = '/Users/zeisler/dev/active_mocker/spec/lib/active_mocker/performance/migration'
16
17
  end
17
18
  ActiveMocker::Base.mock('Person')
18
19
  end
@@ -0,0 +1,205 @@
1
+ require 'rspec'
2
+ $:.unshift File.expand_path('../../../active_mocker', __FILE__)
3
+ require 'active_mocker'
4
+ require 'string_reader'
5
+
6
+ describe 'ReadMe', pending: true do
7
+
8
+ before(:each) do
9
+ ActiveMocker.configure do |config|
10
+ # Required Options
11
+ config.schema_file = ""
12
+ config.model_dir = ""
13
+ config.schema_file_reader = schema_file
14
+ config.model_file_reader = model_file
15
+ # Additional Options
16
+ config.schema_attributes = true #default
17
+ config.model_attributes = true #default
18
+ config.clear_cache = true
19
+ # Logging
20
+ config.log_level = Logger::WARN #default
21
+ config.migration_dir = '/Users/zeisler/dev/active_mocker/spec/lib/active_mocker/performance/migration'
22
+
23
+ end
24
+ end
25
+
26
+ let(:schema_file){
27
+ StringReader.new <<-eos
28
+
29
+ ActiveRecord::Schema.define(version: 20140327205359) do
30
+
31
+ create_table "people", force: true do |t|
32
+ t.integer "account_id"
33
+ t.string "first_name", limit: 128
34
+ t.string "last_name", limit: 128
35
+ t.string "address", limit: 200
36
+ t.string "city", limit: 100
37
+ end
38
+
39
+ end
40
+
41
+ eos
42
+ }
43
+
44
+ let(:model_file){
45
+ StringReader.new <<-eos
46
+ class Person < ActiveRecord::Base
47
+ belongs_to :account
48
+
49
+ def bar(name, type=nil)
50
+ puts name
51
+ end
52
+
53
+ def self.bar
54
+ end
55
+
56
+ end
57
+ eos
58
+ }
59
+
60
+ before do
61
+ ActiveMocker.mock('Person')
62
+ end
63
+
64
+ let(:person_mock){PersonMock}
65
+
66
+ describe 'Usage' do
67
+
68
+ it 'Mock a Person' do
69
+ expect(ActiveMocker.mock('Person')).to eq PersonMock
70
+ end
71
+
72
+ it '::column_names' do
73
+ expect(PersonMock.column_names).to eq ["id", "account_id", "first_name", "last_name", "address", "city"]
74
+ end
75
+
76
+ it '::new' do
77
+ expect(PersonMock.new(first_name: "Dustin", last_name: "Zeisler").inspect).to eq("#<PersonMock id: nil, account_id: nil, first_name: \"Dustin\", last_name: \"Zeisler\", address: nil, city: nil>")
78
+ end
79
+
80
+ it '#first_name' do
81
+ person_mock = PersonMock.new(first_name: "Dustin", last_name: "Zeisler")
82
+ expect( person_mock.first_name).to eq 'Dustin'
83
+ end
84
+
85
+ end
86
+
87
+ describe 'When schema.rb changes, the mock fails' do
88
+
89
+ let(:schema_file){
90
+ StringReader.new <<-eos
91
+
92
+ ActiveRecord::Schema.define(version: 20140327205359) do
93
+
94
+ create_table "people", force: true do |t|
95
+ t.integer "account_id"
96
+ t.string "f_name", limit: 128
97
+ t.string "l_name", limit: 128
98
+ t.string "address", limit: 200
99
+ t.string "city", limit: 100
100
+ end
101
+
102
+ end
103
+
104
+ eos
105
+ }
106
+
107
+ it 'fails' do
108
+ expect{ActiveMocker.mock('Person').new(first_name: "Dustin", last_name: "Zeisler")}.to raise_error(RuntimeError)
109
+ end
110
+
111
+ end
112
+
113
+ describe 'Mocking instance and class methods' do
114
+
115
+ it 'bar is not Implemented' do
116
+ expect{person_mock.bar}.to raise_error( RuntimeError, '::bar is not Implemented for Class: PersonMock' )
117
+ end
118
+
119
+ it 'is implemented' do
120
+ person_mock.mock_instance_method(:bar) do |name, type=nil|
121
+ "Now implemented with #{name} and #{type}"
122
+ end
123
+
124
+ expect(person_mock.new.bar('foo', 'type')).to eq "Now implemented with foo and type"
125
+
126
+ person_mock.mock_class_method(:bar) do
127
+ "Now implemented"
128
+ end
129
+
130
+ expect(person_mock.bar).to eq "Now implemented"
131
+
132
+ end
133
+
134
+ it 'has argument error' do
135
+
136
+ person_mock.mock_instance_method(:bar) do |name, type=nil|
137
+ "Now implemented with #{name} and #{type}"
138
+ end
139
+
140
+ expect{person_mock.new.bar}.to raise_error(ArgumentError, 'wrong number of arguments (0 for 1..2)')
141
+
142
+ end
143
+
144
+ end
145
+
146
+ describe 'When the model changes, the mock fails' do
147
+
148
+ context 'different arguments' do
149
+
150
+ let(:model_file){
151
+ StringReader.new <<-eos
152
+ class Person < ActiveRecord::Base
153
+ belongs_to :account
154
+
155
+ def bar(name)
156
+ puts name
157
+ end
158
+
159
+ end
160
+ eos
161
+ }
162
+
163
+ it 'has argument error' do
164
+
165
+ person_mock.mock_instance_method(:bar) do |name, type=nil|
166
+ "Now implemented with #{name} and #{type}"
167
+ end
168
+
169
+ expect{person_mock.new.bar('foo', 'type')}.to raise_error(ArgumentError, 'wrong number of arguments (2 for 1)')
170
+
171
+ end
172
+
173
+ end
174
+
175
+ context 'different method name' do
176
+
177
+ let(:model_file){
178
+ StringReader.new <<-eos
179
+ class Person < ActiveRecord::Base
180
+ belongs_to :account
181
+
182
+ def foo(name)
183
+ puts name
184
+ end
185
+
186
+ end
187
+
188
+ eos
189
+ }
190
+
191
+ it 'when method name changes' do
192
+
193
+ person_mock.mock_instance_method(:bar) do |name, type=nil|
194
+ "Now implemented with #{name} and #{type}"
195
+ end
196
+
197
+ expect{person_mock.new.bar}.to raise_error(NoMethodError)
198
+
199
+ end
200
+
201
+ end
202
+
203
+ end
204
+
205
+ end
@@ -0,0 +1,100 @@
1
+ require 'active_mocker/mock_requires'
2
+
3
+ Object.send(:remove_const, 'MicropostMock') if class_exists? 'MicropostMock'
4
+
5
+ class MicropostMock < ::ActiveHash::Base
6
+ include ActiveMocker::ActiveHash::ARApi
7
+ include ActiveMocker::MockInstanceMethods
8
+ extend ActiveMocker::MockClassMethods
9
+
10
+ def self.column_names
11
+ ["id", "content", "user_id", "created_at", "updated_at"]
12
+ end
13
+
14
+ def self.association_names
15
+ @association_names = [:user]
16
+ end
17
+
18
+ def self.attribute_names
19
+ @attribute_names = [:id, :content, :user_id, :created_at, :updated_at]
20
+ end
21
+
22
+
23
+ def id
24
+ attributes['id']
25
+ end
26
+
27
+ def id=(val)
28
+ attributes['id'] = val
29
+ end
30
+
31
+ def content
32
+ attributes['content']
33
+ end
34
+
35
+ def content=(val)
36
+ attributes['content'] = val
37
+ end
38
+
39
+ def user_id
40
+ attributes['user_id']
41
+ end
42
+
43
+ def user_id=(val)
44
+ attributes['user_id'] = val
45
+ end
46
+
47
+ def created_at
48
+ attributes['created_at']
49
+ end
50
+
51
+ def created_at=(val)
52
+ attributes['created_at'] = val
53
+ end
54
+
55
+ def updated_at
56
+ attributes['updated_at']
57
+ end
58
+
59
+ def updated_at=(val)
60
+ attributes['updated_at'] = val
61
+ end
62
+
63
+
64
+
65
+ def user
66
+ associations['user']
67
+ end
68
+
69
+ def user=(val)
70
+ associations['user'] = val
71
+ end
72
+
73
+
74
+
75
+
76
+ def self.model_instance_methods
77
+ return @model_instance_methods if @model_instance_methods
78
+ @model_instance_methods = {}
79
+ @model_instance_methods
80
+ end
81
+
82
+ def self.model_class_methods
83
+ return @model_class_methods if @model_class_methods
84
+ @model_class_methods = {}
85
+ @model_class_methods[:from_users_followed_by] = :not_implemented
86
+
87
+ @model_class_methods
88
+ end
89
+
90
+
91
+
92
+
93
+ def self.from_users_followed_by(user)
94
+ block = model_class_methods[:from_users_followed_by]
95
+ is_implemented(block, "::from_users_followed_by")
96
+ instance_exec(*[user], &block)
97
+ end
98
+
99
+
100
+ end