active_mocker 2.0.0.pre1 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -1
- data/README.md +1 -0
- data/lib/active_mocker.rb +10 -1
- data/lib/active_mocker/display_errors.rb +20 -15
- data/lib/active_mocker/error_object.rb +2 -2
- data/lib/active_mocker/generate.rb +12 -6
- data/lib/active_mocker/hash_new_style.rb +7 -0
- data/lib/active_mocker/mock_creator.rb +44 -43
- data/lib/active_mocker/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be84f88aee5f849ac41ade637327ab87376e48ef
|
4
|
+
data.tar.gz: 3bbf50a5b83647f3f84aea50b623d5a18e7b071d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cf704f658296af715b9ff4a7f94d76639a2cb87476a4fea8eb55f33a34380919c84bd00823b5f553ab1701d3420c4853eedfce73c755296d210bbb3e452b466
|
7
|
+
data.tar.gz: d526baf349a850d6a0914a51b88ef7026a4f6ded6c7247c4fbdf661aa6d37da1cd7532362feee937a086bfc6fe6847246b69b7edc1a3234ea723c7fc2b8bcf3f
|
data/CHANGELOG.md
CHANGED
@@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
## 2.0.0.pre1 - 2015-12-13
|
5
5
|
### Enhancement
|
6
6
|
- The mock append name is now changeable using `ActiveMocker::Config.mock_append_name=`. The default still being `Mock`.
|
7
|
-
- `ActiveMocker::
|
7
|
+
- `ActiveMocker::MockRelation(mock, collection)` to create separate independent mock collections.
|
8
8
|
- Change `ActiveMocker::Mock::Base` to `ActiveMocker::Base`
|
9
9
|
- Provide more control over error when running `rake active_mocker:build`, error_verbosity now has setting for 0 to 3.
|
10
10
|
|
data/README.md
CHANGED
@@ -357,6 +357,7 @@ See [Documentation](http://rdoc.info/github/zeisler/active_mocker/master/ActiveM
|
|
357
357
|
* Sql queries, joins, etc will never be supported.
|
358
358
|
* A record that has been created and then is modified will persist changes without calling `#save`, beware of this difference.
|
359
359
|
* This is not a full replacement for ActiveRecord. It's simplified, with only the most essential parts. There are many tests to verify that is works the same as ActiveRecord, but's yours responsibility to know how ActiveRecord works.
|
360
|
+
* Primary key will always default to `id`. If this is an causes a problem open an issue.
|
360
361
|
|
361
362
|
## Inspiration
|
362
363
|
Thanks to Jeff Olfert for being my original inspiration for this project.
|
data/lib/active_mocker.rb
CHANGED
@@ -1,5 +1,13 @@
|
|
1
|
-
|
1
|
+
# _ _ __ __ _
|
2
|
+
# /\ | | (_) | \/ | | |
|
3
|
+
# / \ ___| |_ ___ _____| \ / | ___ ___| | _____ _ __
|
4
|
+
# / /\ \ / __| __| \ \ / / _ \ |\/| |/ _ \ / __| |/ / _ \ '__|
|
5
|
+
# / ____ \ (__| |_| |\ V / __/ | | | (_) | (__| < __/ |
|
6
|
+
# /_/ \_\___|\__|_| \_/ \___|_| |_|\___/ \___|_|\_\___|_|
|
7
|
+
#
|
8
|
+
# By Dustin Zeisler
|
2
9
|
|
10
|
+
require "rubygems"
|
3
11
|
require 'active_mocker/version'
|
4
12
|
require 'active_mocker/railtie' if defined?(Rails)
|
5
13
|
require 'ruby-progressbar'
|
@@ -11,6 +19,7 @@ require "reverse_parameters"
|
|
11
19
|
require "active_record_schema_scrapper"
|
12
20
|
require "dissociated_introspection"
|
13
21
|
require "colorize"
|
22
|
+
require "active_mocker/hash_new_style"
|
14
23
|
require "active_mocker/null_progress"
|
15
24
|
require "active_mocker/progress"
|
16
25
|
require "active_mocker/parent_class"
|
@@ -1,12 +1,14 @@
|
|
1
1
|
module ActiveMocker
|
2
2
|
class DisplayErrors
|
3
|
-
attr_reader :errors, :model_count
|
4
|
-
attr_accessor :success_count
|
3
|
+
attr_reader :errors, :model_count, :out
|
4
|
+
attr_accessor :success_count, :failed_models
|
5
5
|
|
6
|
-
def initialize(model_count)
|
6
|
+
def initialize(model_count, out: STDERR)
|
7
7
|
@errors = []
|
8
8
|
@success_count = 0
|
9
9
|
@model_count = model_count
|
10
|
+
@failed_models = []
|
11
|
+
@out = out
|
10
12
|
end
|
11
13
|
|
12
14
|
def add(errors)
|
@@ -27,24 +29,26 @@ module ActiveMocker
|
|
27
29
|
|
28
30
|
def display_errors
|
29
31
|
uniq_errors.each do |e|
|
32
|
+
next if e.level == :debug unless ENV["DEBUG"]
|
30
33
|
if ActiveMocker::Config.error_verbosity == 3
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
out.puts "#{e.class_name} has the following errors:"
|
35
|
+
out.puts e.message.colorize(e.level_color)
|
36
|
+
out.puts e.level
|
37
|
+
out.puts e.original_error.message.colorize(e.level_color) if e.original_error?
|
38
|
+
out.puts e.original_error.backtrace if e.original_error?
|
39
|
+
out.puts e.original_error.class.name.colorize(e.level_color) if e.original_error?
|
37
40
|
elsif ActiveMocker::Config.error_verbosity == 2
|
38
|
-
|
41
|
+
out.puts "#{e.class_name} has the following errors:"
|
42
|
+
out.puts e.message.colorize(e.level_color)
|
39
43
|
end
|
40
44
|
end
|
41
45
|
if ActiveMocker::Config.error_verbosity > 0 && uniq_errors.count > 0
|
42
|
-
|
46
|
+
out.puts "Error Summary"
|
43
47
|
error_summary
|
44
48
|
end
|
45
49
|
failure_count_message
|
46
50
|
if ActiveMocker::Config.error_verbosity > 0 && uniq_errors.count > 0
|
47
|
-
|
51
|
+
out.puts "To see more/less detail set error_verbosity = 0, 1, 2, 3"
|
48
52
|
end
|
49
53
|
end
|
50
54
|
|
@@ -52,13 +56,14 @@ module ActiveMocker
|
|
52
56
|
error_count = uniq_errors.count { |e| [:red].include?(e.level_color) }
|
53
57
|
warn = uniq_errors.count { |e| [:yellow].include?(e.level_color) }
|
54
58
|
info = uniq_errors.count { |e| [:default].include?(e.level_color) }
|
55
|
-
|
59
|
+
out.puts "errors: #{error_count}, warn: #{warn}, info: #{info}"
|
60
|
+
out.puts "Failed models: #{failed_models.join(", ")}" if failed_models.count > 0
|
56
61
|
end
|
57
62
|
|
58
63
|
def failure_count_message
|
59
64
|
if ActiveMocker::Config.error_verbosity > 0 && (success_count < model_count || uniq_errors.count > 0)
|
60
|
-
|
65
|
+
out.puts "#{ model_count - success_count } mock(s) out of #{model_count} failed."
|
61
66
|
end
|
62
67
|
end
|
63
68
|
end
|
64
|
-
end
|
69
|
+
end
|
@@ -19,9 +19,9 @@ module ActiveMocker
|
|
19
19
|
schema_scrapper = ActiveRecordSchemaScrapper.new(model: model)
|
20
20
|
File.open(mock_file_path, 'w') do |file_out|
|
21
21
|
begin
|
22
|
-
result
|
23
|
-
collect_errors(mock_file_path, result.errors, schema_scrapper, model_name)
|
24
|
-
display_errors.success_count += 1 if result.completed?
|
22
|
+
result = create_mock(file, file_out, schema_scrapper)
|
23
|
+
status = collect_errors(mock_file_path, result.errors, schema_scrapper, model_name)
|
24
|
+
display_errors.success_count += 1 if result.completed? && status.successful?
|
25
25
|
rescue => e
|
26
26
|
rescue_clean_up(e, file_out, model_name)
|
27
27
|
end
|
@@ -51,16 +51,22 @@ module ActiveMocker
|
|
51
51
|
mock_append_name: config.mock_append_name).create
|
52
52
|
end
|
53
53
|
|
54
|
+
OtherErrors = Struct.new(:successful?)
|
54
55
|
def collect_errors(mock_file_path, create_mock_errors, schema_scrapper, model_name)
|
55
|
-
|
56
|
+
display_errors.wrap_errors(schema_scrapper.associations.errors, model_name, type: :associations)
|
57
|
+
display_errors.wrap_errors(schema_scrapper.attributes.errors, model_name, type: :attributes)
|
58
|
+
if create_mock_errors.present? || schema_scrapper.attributes.errors.any? { |e| e.level == :error }
|
59
|
+
display_errors.failed_models << model_name
|
56
60
|
File.delete(mock_file_path) if File.exists?(mock_file_path)
|
57
61
|
display_errors.add(create_mock_errors)
|
62
|
+
OtherErrors.new(false)
|
63
|
+
else
|
64
|
+
OtherErrors.new(true)
|
58
65
|
end
|
59
|
-
display_errors.wrap_errors(schema_scrapper.associations.errors, model_name, type: :associations)
|
60
|
-
display_errors.wrap_errors(schema_scrapper.attributes.errors, model_name, type: :attributes)
|
61
66
|
end
|
62
67
|
|
63
68
|
def rescue_clean_up(e, file_out, model_name)
|
69
|
+
display_errors.failed_models << model_name
|
64
70
|
file_out.close unless file_out.closed?
|
65
71
|
File.delete(file_out.path) if File.exists?(file_out.path)
|
66
72
|
display_errors.wrap_an_exception(e, model_name)
|
@@ -25,7 +25,11 @@ module ActiveMocker
|
|
25
25
|
def create
|
26
26
|
verify_class
|
27
27
|
if errors.empty?
|
28
|
-
|
28
|
+
begin
|
29
|
+
template_creator.render
|
30
|
+
rescue => e
|
31
|
+
raise e unless error_already_collected?(e)
|
32
|
+
end
|
29
33
|
file_out.close
|
30
34
|
@completed = true
|
31
35
|
end
|
@@ -50,8 +54,11 @@ module ActiveMocker
|
|
50
54
|
:active_record_base_klass,
|
51
55
|
:mock_append_name
|
52
56
|
|
57
|
+
def error_already_collected?(e)
|
58
|
+
errors.any? { |eo| eo.original_error == e }
|
59
|
+
end
|
60
|
+
|
53
61
|
# -- Defaults -- #
|
54
|
-
private
|
55
62
|
def template_creator_default(file_out)
|
56
63
|
TemplateCreator.new(file_out: file_out,
|
57
64
|
erb_template: File.new(File.join(File.dirname(__FILE__), "mock_template.erb"), 'r'),
|
@@ -89,7 +96,8 @@ module ActiveMocker
|
|
89
96
|
self.extend("ActiveMocker::MockCreator::#{p.to_s.camelize}".constantize)
|
90
97
|
hash[p] = ERB.new(file.read, nil, '-', "_sub#{p}").result(binding)
|
91
98
|
rescue => e
|
92
|
-
|
99
|
+
errors << ErrorObject.new(class_name: class_name, original_error: e, type: :generation, level: :error, message: e.message)
|
100
|
+
errors << ErrorObject.new(class_name: class_name, original_error: e, type: :erb, level: :debug, message: "Erb template: #{p} failed.\n#{file.path}")
|
93
101
|
raise e
|
94
102
|
end
|
95
103
|
end)
|
@@ -109,27 +117,27 @@ module ActiveMocker
|
|
109
117
|
|
110
118
|
module ModulesConstants
|
111
119
|
def constants
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
v.class == Module || v.class == Class
|
120
|
+
class_introspector.get_class.constants.each_with_object({}) do |v, const|
|
121
|
+
c = class_introspector.get_class.const_get(v)
|
122
|
+
const[v] = c unless c.class == Module || c.class == Class
|
116
123
|
end
|
117
|
-
const
|
118
124
|
end
|
119
125
|
|
120
126
|
def modules
|
121
127
|
@modules ||= begin
|
122
128
|
{
|
123
|
-
included:
|
124
|
-
|
125
|
-
extended: reject_local_const(class_introspector.extended_modules)
|
126
|
-
.map(&:referenced_name)
|
129
|
+
included: get_module_by_reference(:included_modules),
|
130
|
+
extended: get_module_by_reference(:extended_modules)
|
127
131
|
}
|
128
132
|
end
|
129
133
|
end
|
130
134
|
|
131
135
|
private
|
132
136
|
|
137
|
+
def get_module_by_reference(type)
|
138
|
+
reject_local_const(class_introspector.public_send(type)).map(&:referenced_name)
|
139
|
+
end
|
140
|
+
|
133
141
|
def reject_local_const(source)
|
134
142
|
source.reject do |n|
|
135
143
|
class_introspector.locally_defined_constants.values.include?(n)
|
@@ -153,23 +161,15 @@ module ActiveMocker
|
|
153
161
|
include Attributes
|
154
162
|
|
155
163
|
def attributes_with_defaults
|
156
|
-
|
157
|
-
attributes.each do |attr|
|
164
|
+
attributes.each_with_object({}) do |attr, hash|
|
158
165
|
hash[attr.name] = Virtus::Attribute.build(attr.type).coerce(attr.default)
|
159
166
|
end
|
160
|
-
hash
|
161
167
|
end
|
162
168
|
|
163
169
|
def types_hash
|
164
|
-
|
165
|
-
attributes.each do |attr|
|
170
|
+
attributes.each_with_object(HashNewStyle.new) do |attr, types|
|
166
171
|
types[attr.name] = "#{attr.type}"
|
167
|
-
end
|
168
|
-
|
169
|
-
type_array = types.map do |name, type|
|
170
|
-
"#{name}: #{type}"
|
171
|
-
end
|
172
|
-
'{ ' + type_array.join(', ') + ' }'
|
172
|
+
end.inspect
|
173
173
|
end
|
174
174
|
|
175
175
|
def associations
|
@@ -199,54 +199,55 @@ module ActiveMocker
|
|
199
199
|
end
|
200
200
|
end
|
201
201
|
|
202
|
+
Method = Struct.new(:name, :arguments)
|
203
|
+
|
202
204
|
module Scopes
|
203
205
|
def scope_methods
|
204
206
|
class_introspector.class_macros.select { |h| h.keys.first == :scope }.map do |h|
|
205
207
|
a = h.values.first.first
|
206
|
-
|
208
|
+
Method.new(a[0], ReverseParameters.new(a[1]))
|
207
209
|
end
|
208
210
|
end
|
209
211
|
end
|
210
212
|
|
211
213
|
module DefinedMethods
|
212
|
-
def get_instance_methods
|
213
|
-
methods = class_introspector.get_class.public_instance_methods(false)
|
214
|
-
methods << class_introspector.get_class.superclass.public_instance_methods(false) if class_introspector.get_class.superclass != ActiveRecord::Base
|
215
|
-
methods.flatten
|
216
|
-
end
|
217
214
|
|
218
215
|
def instance_methods
|
219
|
-
|
220
|
-
|
221
|
-
|
216
|
+
class_introspector
|
217
|
+
.get_class
|
218
|
+
.public_instance_methods(false)
|
219
|
+
.map { |m| create_method(m, :instance_method) }
|
222
220
|
end
|
223
221
|
|
224
|
-
def
|
225
|
-
class_introspector
|
222
|
+
def class_methods
|
223
|
+
class_introspector
|
224
|
+
.get_class
|
225
|
+
.methods(false)
|
226
|
+
.map { |m| create_method(m, :method) }
|
226
227
|
end
|
227
228
|
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
229
|
+
private
|
230
|
+
|
231
|
+
def create_method(m, type)
|
232
|
+
Method.new(m, ReverseParameters.new(class_introspector.get_class.send(type, m).parameters))
|
232
233
|
end
|
233
234
|
end
|
234
235
|
|
235
236
|
module Associations
|
236
237
|
def has_many
|
237
|
-
|
238
|
+
relation_find(:type, __method__)
|
238
239
|
end
|
239
240
|
|
240
241
|
def has_one
|
241
|
-
|
242
|
+
relation_find(:type, __method__)
|
242
243
|
end
|
243
244
|
|
244
245
|
def belongs_to
|
245
|
-
|
246
|
+
relation_find(:type, __method__)
|
246
247
|
end
|
247
248
|
|
248
249
|
def has_and_belongs_to_many
|
249
|
-
|
250
|
+
relation_find(:type, __method__)
|
250
251
|
end
|
251
252
|
|
252
253
|
def relation_find(key, value)
|
@@ -258,4 +259,4 @@ module ActiveMocker
|
|
258
259
|
end
|
259
260
|
end
|
260
261
|
end
|
261
|
-
end
|
262
|
+
end
|
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: 2.0.0.
|
4
|
+
version: 2.0.0.rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dustin Zeisler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -176,6 +176,7 @@ files:
|
|
176
176
|
- lib/active_mocker/display_errors.rb
|
177
177
|
- lib/active_mocker/error_object.rb
|
178
178
|
- lib/active_mocker/generate.rb
|
179
|
+
- lib/active_mocker/hash_new_style.rb
|
179
180
|
- lib/active_mocker/loaded_mocks.rb
|
180
181
|
- lib/active_mocker/mock.rb
|
181
182
|
- lib/active_mocker/mock/association.rb
|