effective_test_bot 0.4.1 → 0.4.2

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.
@@ -1,145 +0,0 @@
1
- module TestBotable
2
- module CrudTest
3
- extend ActiveSupport::Concern
4
-
5
- CRUD_TESTS = [:new, :create_valid, :create_invalid, :edit, :update_valid, :update_invalid, :index, :show, :destroy]
6
-
7
- module ClassMethods
8
-
9
- # All this does is define a 'test_bot' method for each required action on this class
10
- # So that MiniTest will see the test functions and run them
11
- def crud_test(obj, user, options = {})
12
- # Check for expected usage
13
- unless (obj.kind_of?(Class) || obj.kind_of?(ActiveRecord::Base)) && user.kind_of?(User) && options.kind_of?(Hash)
14
- raise 'invalid parameters passed to crud_test(), expecting crud_test(Post || Post.new(), User.first, options_hash)'
15
- end
16
-
17
- if (options[:skip] || options[:skips]).present? && (options[:skip] || options[:skips]).kind_of?(Hash) == false
18
- raise 'invalid skip syntax, expecting skip: {create_invalid: [:path]}'
19
- end
20
-
21
- skips = options.delete(:skip) || options.delete(:skips) || {} # So you can skip sub tests
22
- test_options = crud_test_options(obj, user, options) # returns a Hash of let! options
23
- tests_prefix = crud_tests_prefix(options) # returns a string something like "test_bot (3)"
24
-
25
- crud_tests_to_define(options).each do |test|
26
- test_name = case test
27
- when :new ; "#{tests_prefix} #new"
28
- when :create_valid ; "#{tests_prefix} #create valid"
29
- when :create_invalid ; "#{tests_prefix} #create invalid"
30
- when :edit ; "#{tests_prefix} #edit"
31
- when :update_valid ; "#{tests_prefix} #update valid"
32
- when :update_invalid ; "#{tests_prefix} #update invalid"
33
- when :index ; "#{tests_prefix} #index"
34
- when :show ; "#{tests_prefix} #show"
35
- when :destroy ; "#{tests_prefix} #destroy"
36
- end
37
-
38
- if skips[test].present?
39
- define_method(test_name) { crud_action_test(test, test_options.merge(skips: Array(skips[test]))) }
40
- else
41
- define_method(test_name) { crud_action_test(test, test_options) }
42
- end
43
- end
44
- end
45
-
46
- # Parses and validates lots of options
47
- # The output is what gets sent to each test and defined as lets
48
- def crud_test_options(obj, user, options = {})
49
- # Make sure Obj.new() works
50
- if obj.kind_of?(Class) && (obj.new() rescue false) == false
51
- raise "effective_test_bot: failed to initialize object with #{obj}.new(), unable to proceed"
52
- end
53
-
54
- # Parse the resource and resource class
55
- resource = obj.kind_of?(Class) ? obj.new() : obj
56
- resource_class = obj.kind_of?(Class) ? obj : obj.class
57
-
58
- # If obj is an ActiveRecord object with attributes, Post.new(:title => 'My Title')
59
- # then compute any explicit attributes, so forms will be filled with those values
60
- resource_attributes = if obj.kind_of?(ActiveRecord::Base)
61
- empty = resource_class.new()
62
- {}.tap { |atts| resource.attributes.each { |k, v| atts[k] = v if empty.attributes[k] != v } }
63
- end || {}
64
-
65
- # Final options to call each test with
66
- {
67
- resource: resource,
68
- resource_class: resource_class,
69
- resource_name: resource_class.name.underscore,
70
- resource_attributes: resource_attributes,
71
- controller_namespace: options[:namespace],
72
- user: user,
73
- skips: Array(options[:skip] || options[:skips])
74
- }
75
- end
76
-
77
- # Run any test_bot tests first, in the order they're defined
78
- # then the rest of the tests with whatever order they come in
79
- def runnable_methods
80
- self.public_instance_methods.select { |name| name.to_s.starts_with?('crud_test') }.map(&:to_s) +
81
- super.reject { |name| name.starts_with?('test_bot') }
82
- end
83
-
84
- private
85
-
86
- # Parses the incoming options[:only] and [:except]
87
- # To only define the appropriate methods
88
- # This guarantees the functions will be defined in the same order as CRUD_TESTS
89
- def crud_tests_to_define(options)
90
- to_run = if options[:only]
91
- options[:only] = Array(options[:only]).flatten.compact.map(&:to_sym)
92
- options[:only] = options[:only] + [:create_valid, :create_invalid] if options[:only].delete(:create)
93
- options[:only] = options[:only] + [:update_valid, :update_invalid] if options[:only].delete(:update)
94
-
95
- CRUD_TESTS & options[:only]
96
- elsif options[:except]
97
- options[:except] = Array(options[:except]).flatten.compact.map(&:to_sym)
98
- options[:except] = options[:except] + [:create_valid, :create_invalid] if options[:except].delete(:create)
99
- options[:except] = options[:except] + [:update_valid, :update_invalid] if options[:except].delete(:update)
100
-
101
- CRUD_TESTS - options[:except]
102
- else
103
- CRUD_TESTS
104
- end
105
- end
106
-
107
- # You can't define multiple methods with the same name
108
- # So we need to create a unique name, where appropriate, that still looks good in MiniTest output
109
- def crud_tests_prefix(options)
110
- @num_defined_crud_tests = (@num_defined_crud_tests || 0) + 1
111
-
112
- if options[:label].present?
113
- "crud_test: (#{options[:label]})"
114
- elsif @num_defined_crud_tests > 1
115
- "crud_test: (#{@num_defined_crud_tests})"
116
- else
117
- 'crud_test:'
118
- end
119
- end
120
-
121
- end
122
-
123
- # Instance Methods
124
-
125
- # This should allow you to run a crud_test method in a test
126
- # crud_action_test(:create_valid, Clinic, User.first)
127
- #
128
- # If obj is a Hash {:resource => ...} just skip over parsing options
129
- # And assume it's already been done (by the ClassMethod crud_test)
130
- def crud_action_test(test, obj, user = nil, options = {})
131
- if obj.kind_of?(Hash) && obj.key?(:resource)
132
- obj
133
- else
134
- # Check for expected usage
135
- unless (obj.kind_of?(Class) || obj.kind_of?(ActiveRecord::Base)) && user.kind_of?(User) && options.kind_of?(Hash)
136
- raise 'invalid parameters passed to crud_action_test(), expecting crud_action_test(:new, Post || Post.new(), User.first, options_hash)'
137
- end
138
-
139
- self.class.crud_test_options(obj, user, options)
140
- end.each { |k, v| self.class.let(k) { v } } # Using the regular let(:foo) { 'bar'} syntax
141
-
142
- self.public_send(test)
143
- end
144
- end
145
- end
@@ -1,10 +0,0 @@
1
- require 'test_helper'
2
-
3
- module TestBot
4
- class HomePageTest < ActionDispatch::IntegrationTest
5
- test 'home page loads successfully' do
6
- visit root_path
7
- assert_equal page.status_code, 200
8
- end
9
- end
10
- end