minitest-sequel 0.3.1 → 0.4.0
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.
- checksums.yaml +5 -5
- data/.github/dependabot.yml +24 -0
- data/.github/workflows/ruby.yml +45 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +48 -0
- data/.rubocop_todo.yml +7 -0
- data/CODE_OF_CONDUCT.md +17 -10
- data/Gemfile +81 -2
- data/Guardfile +26 -0
- data/README.md +368 -243
- data/Rakefile +16 -24
- data/lib/minitest/sequel/associations.rb +353 -163
- data/lib/minitest/sequel/columns.rb +204 -140
- data/lib/minitest/sequel/helpers.rb +190 -21
- data/lib/minitest/sequel/plugins.rb +409 -129
- data/lib/minitest/sequel/validations.rb +1122 -504
- data/lib/minitest/sequel/version.rb +4 -3
- data/lib/minitest/sequel.rb +27 -23
- data/minitest-sequel.gemspec +28 -27
- metadata +49 -143
@@ -1,160 +1,224 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
1
|
+
# frozen_string_literal: false
|
2
|
+
|
3
|
+
require 'minitest/assertions'
|
4
|
+
require 'minitest/spec'
|
5
|
+
|
6
|
+
# reopening to add validations functionality
|
7
|
+
module Minitest
|
8
|
+
# add support for Assert syntax
|
9
|
+
module Assertions
|
10
|
+
# Asserts that a given model has a specific column with optional attribute checks
|
11
|
+
#
|
12
|
+
# @param obj [Object] The model instance to check
|
13
|
+
# @param attribute [Symbol] The name of the column to check
|
14
|
+
# @param opts [Hash] Optional attributes to verify for the column
|
15
|
+
# @param msg [String, nil] Optional custom error message
|
16
|
+
#
|
17
|
+
# @example Basic usage
|
18
|
+
# let(:m) { Post.first }
|
19
|
+
# assert_have_column(m, :title, type: :string, db_type: 'varchar(250)')
|
20
|
+
#
|
21
|
+
# @example Using with Spec syntax
|
22
|
+
# m.must_have_column(:title, type: :string, db_type: 'varchar(250)')
|
23
|
+
#
|
24
|
+
# The method checks if the column exists in the model and then verifies
|
25
|
+
# any provided options. Valid options include:
|
26
|
+
#
|
27
|
+
# * :type - The Ruby type of the column
|
28
|
+
# * :db_type - The database-specific type of the column
|
29
|
+
# * :allow_null - Whether the column allows NULL values
|
30
|
+
# * :max_length - The maximum length for string-type columns
|
31
|
+
# * :default - The default value for the column
|
32
|
+
# * :primary_key - Whether the column is a primary key
|
33
|
+
# * :auto_increment - Whether the column auto-increments
|
34
|
+
#
|
35
|
+
# If the actual column attributes differ from the specified options,
|
36
|
+
# a detailed error message is provided:
|
37
|
+
#
|
38
|
+
# Expected Post model to have column: :title with: \
|
39
|
+
# { type: 'string', db_type: 'varchar(250)', allow_null: 'false' } \
|
40
|
+
# but found: { db_type: 'varchar(255)' }
|
41
|
+
#
|
42
|
+
# @note When testing for `nil`, `true`, or `false` values, use `:nil`,
|
43
|
+
# `:true`, or `:false` symbols respectively. For numeric values,
|
44
|
+
# provide them as strings.
|
45
|
+
#
|
46
|
+
# rubocop:disable Metrics/MethodLength
|
47
|
+
def assert_have_column(obj, attribute, opts = {}, msg = nil)
|
48
|
+
# Prepare the error message
|
49
|
+
msg = build_message_column(obj, attribute, msg)
|
50
|
+
|
51
|
+
# prepare output messages
|
52
|
+
msg_conf = []
|
53
|
+
msg_err = []
|
54
|
+
|
55
|
+
# extract the db table information for the column
|
56
|
+
dbcol = extract_column_info(obj, attribute)
|
57
|
+
|
58
|
+
# set matching status
|
59
|
+
matching = !dbcol.nil?
|
60
|
+
|
61
|
+
# bail if there is no matching column
|
62
|
+
bail_unless_column_exists(dbcol, msg) unless matching
|
63
|
+
# bail if passed options are invalid
|
64
|
+
bail_unless_valid_options(opts, msg)
|
65
|
+
|
66
|
+
# loop through the options passed and check them and report the result
|
67
|
+
opts.each do |key, value|
|
68
|
+
expected = compare_column_attribute(dbcol, key, value)
|
69
|
+
update_messages(msg_conf, msg_err, key, value, dbcol, expected)
|
70
|
+
matching &&= expected
|
71
|
+
end
|
57
72
|
|
58
|
-
|
59
|
-
|
60
|
-
invalid_opts = opts.keys.reject { |o| val_opts.include?(o) }
|
61
|
-
unless invalid_opts.empty?
|
62
|
-
msg << ", but the following invalid option(s) was found: { "
|
63
|
-
invalid_opts.each { |o| msg << "#{o.inspect}; " }
|
64
|
-
msg << " }. Valid options are: #{val_opts.inspect}"
|
65
|
-
assert(false, msg)
|
73
|
+
msg << " with: { #{msg_conf.join(', ')} } but found: { #{msg_err.join(', ')} }"
|
74
|
+
assert(matching, msg)
|
66
75
|
end
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
76
|
+
# rubocop:enable Metrics/MethodLength
|
77
|
+
|
78
|
+
# Asserts that a given model does not have a specific column
|
79
|
+
#
|
80
|
+
# @param obj [Object] The model instance to check
|
81
|
+
# @param attribute [Symbol] The name of the column to check for absence
|
82
|
+
# @param msg [String, nil] Optional custom error message
|
83
|
+
#
|
84
|
+
# @example
|
85
|
+
# refute_have_column(user, :admin_flag)
|
86
|
+
#
|
87
|
+
def refute_have_column(obj, attribute, msg = nil)
|
88
|
+
# Prepare the error message
|
89
|
+
msg = build_message_column(obj, attribute, msg, refute: true)
|
90
|
+
|
91
|
+
# extract the column information from the database schema
|
92
|
+
dbcol = extract_column_info(obj, attribute)
|
93
|
+
|
94
|
+
# If the column is not found (dcol is nil), the test passes
|
95
|
+
matching = dbcol.nil?
|
96
|
+
|
97
|
+
# Return early if the test passes (column doesn't exist)
|
98
|
+
return if matching
|
99
|
+
|
100
|
+
# If we reach here, it means the column was found when it shouldn't be
|
101
|
+
msg << ' but such a column was found'
|
102
|
+
|
103
|
+
# Assert that matching is true (which it isn't at this point)
|
104
|
+
# This will raise an assertion error with the prepared message
|
105
|
+
assert(matching, msg)
|
77
106
|
end
|
78
107
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
108
|
+
private
|
109
|
+
|
110
|
+
# Builds an error message for column assertions
|
111
|
+
#
|
112
|
+
# @param obj [Object] The model instance to check
|
113
|
+
# @param attribute [Symbol] The name of the column to check for absence
|
114
|
+
# @param msg [String, nil] Custom error message
|
115
|
+
# @param refute [Boolean] Whether this is for a refutation (default: false)
|
116
|
+
#
|
117
|
+
# @return [String] The formatted error message
|
118
|
+
#
|
119
|
+
def build_message_column(obj, attribute, msg, refute: false)
|
120
|
+
msg = msg.nil? ? '' : "#{msg}\n"
|
121
|
+
msg << "Expected #{obj.class} model to #{refute ? 'NOT ' : ''}have column: :#{attribute}"
|
86
122
|
end
|
87
123
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
124
|
+
# Extracts the column information for a given attribute in the database schema
|
125
|
+
#
|
126
|
+
# @param obj [Object] The model instance to check
|
127
|
+
# @param attribute [Symbol] The name of the column to find
|
128
|
+
#
|
129
|
+
# @return [Array, nil] The column information array if found, nil otherwise
|
130
|
+
#
|
131
|
+
# @example
|
132
|
+
# extract_column_info(user, :email)
|
133
|
+
# # => [:email, {:type=>:string, :db_type=>"varchar(255)", :allow_null=>true, ...}]
|
134
|
+
#
|
135
|
+
def extract_column_info(obj, attribute)
|
136
|
+
obj.db.schema(obj.class.table_name).detect { |c| c[0] == attribute }
|
95
137
|
end
|
96
138
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
139
|
+
# Bail if the specified column does not exist in the database schema
|
140
|
+
#
|
141
|
+
# @param dcol [Array, nil] The database column information, or nil if the column doesn't exist
|
142
|
+
# @param msg [String] The error message to be displayed if the column doesn't exist
|
143
|
+
#
|
144
|
+
# @raise [Minitest::Assertion] If the column doesn't exist in the database schema
|
145
|
+
#
|
146
|
+
# @example
|
147
|
+
# bail_unless_column_exists(column_info, error_message)
|
148
|
+
#
|
149
|
+
def bail_unless_column_exists(dbcol, msg)
|
150
|
+
return if dbcol
|
151
|
+
|
152
|
+
msg << ' but no such column exists'
|
153
|
+
assert(false, msg)
|
105
154
|
end
|
106
155
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
156
|
+
# Bail if the options passed to #assert_have_column are invalid
|
157
|
+
#
|
158
|
+
# @param opts [Hash] The options to validate
|
159
|
+
#
|
160
|
+
# @raise [Minitest::Assertion] If any invalid options are found
|
161
|
+
#
|
162
|
+
# @example
|
163
|
+
# bail_unless_valid_options(type: :string, db_type: 'varchar(255)')
|
164
|
+
#
|
165
|
+
def bail_unless_valid_options(opts, msg)
|
166
|
+
valid_opts = %i[type db_type allow_null default primary_key auto_increment max_length]
|
167
|
+
invalid_opts = opts.keys.reject { |o| valid_opts.include?(o) }
|
168
|
+
|
169
|
+
return if invalid_opts.empty?
|
170
|
+
|
171
|
+
msg << ', but the following invalid option(s) was found: { '
|
172
|
+
msg << invalid_opts.map(&:inspect).join(', ')
|
173
|
+
msg << " }. Valid options are: #{valid_opts.inspect}"
|
116
174
|
|
117
|
-
|
118
|
-
v = _convert_value(opts[:primary_key])
|
119
|
-
expected = (dcol[1][:primary_key] === v)
|
120
|
-
conf_msg << "primary_key: '#{opts[:primary_key]}'"
|
121
|
-
unless expected
|
122
|
-
err_msg << "primary_key: '#{dcol[1][:primary_key]}'"
|
123
|
-
end
|
124
|
-
matching &&= expected
|
175
|
+
assert(false, msg)
|
125
176
|
end
|
126
177
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
178
|
+
# Compares the actual column attribute with the expected value
|
179
|
+
#
|
180
|
+
# @param dbcol [Array] The database column information
|
181
|
+
# @param key [Symbol] The attribute key to compare
|
182
|
+
# @param value [Object] The expected value for the attribute
|
183
|
+
#
|
184
|
+
# @return [Boolean] True if the attribute matches the expected value, false otherwise
|
185
|
+
#
|
186
|
+
def compare_column_attribute(dbcol, key, value)
|
187
|
+
case key
|
188
|
+
when :type, :db_type
|
189
|
+
dbcol[1][key].to_s == value.to_s
|
190
|
+
when :allow_null, :default, :primary_key, :auto_increment
|
191
|
+
dbcol[1][key] == _convert_value(value)
|
192
|
+
when :max_length
|
193
|
+
dbcol[1][key] == value
|
133
194
|
end
|
134
|
-
matching &&= expected
|
135
195
|
end
|
136
196
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
#
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
197
|
+
# Updates the confirmation and error messages based on column attribute comparison
|
198
|
+
#
|
199
|
+
# @param msg_conf [Array] Array to store confirmation messages
|
200
|
+
# @param msg_err [Array] Array to store error messages
|
201
|
+
# @param key [Symbol] The attribute key being compared
|
202
|
+
# @param value [Object] The expected value for the attribute
|
203
|
+
# @param dbcol [Array] The database column information
|
204
|
+
# @param expected [Boolean] Whether the attribute matches the expected value
|
205
|
+
#
|
206
|
+
# @example
|
207
|
+
# update_messages(msg_conf, msg_err, :type, 'string', column_info, true)
|
208
|
+
#
|
209
|
+
# rubocop:disable Metrics/ParameterLists
|
210
|
+
def update_messages(msg_conf, msg_err, key, value, dbcol, expected)
|
211
|
+
msg_conf << "#{key}: '#{value}'"
|
212
|
+
val = key == 'default' ? dbcol[1][key].inspect : dbcol[1][key]
|
213
|
+
msg_err << "#{key}: '#{val}'" unless expected
|
151
214
|
end
|
215
|
+
# rubocop:enable Metrics/ParameterLists
|
152
216
|
end
|
217
|
+
# /module Assertions
|
153
218
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
infect_an_assertion :refute_have_column, :wont_have_column, :reverse
|
219
|
+
# add support for Spec syntax
|
220
|
+
module Expectations
|
221
|
+
infect_an_assertion :assert_have_column, :must_have_column, :reverse
|
222
|
+
infect_an_assertion :refute_have_column, :wont_have_column, :reverse
|
223
|
+
end
|
160
224
|
end
|
@@ -1,7 +1,150 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: false
|
2
|
+
|
3
|
+
require 'minitest/assertions'
|
4
|
+
require 'minitest/spec'
|
5
|
+
|
6
|
+
# reopening to add validations functionality
|
7
|
+
module Minitest
|
8
|
+
# add support for Assert syntax
|
9
|
+
module Assertions
|
10
|
+
# Asserts that a model can be created successfully.
|
11
|
+
#
|
12
|
+
# @param model [Class] The model class to test.
|
13
|
+
#
|
14
|
+
# This method attempts to create a new instance of the given model
|
15
|
+
# using the :make method (typically provided by a factory).
|
16
|
+
# It then asserts that:
|
17
|
+
# 1. The created instance is of the correct type.
|
18
|
+
# 2. The instance has no errors after creation.
|
19
|
+
#
|
20
|
+
# @raise [Minitest::Assertion] If the assertions fail.
|
21
|
+
#
|
22
|
+
def assert_crud_can_create(model)
|
23
|
+
m = model.send(:make)
|
24
|
+
|
25
|
+
msg = "CRUD:create - expected #{m.class} to be an instance of #{model}"
|
26
|
+
assert_instance_of(model, m, msg)
|
27
|
+
|
28
|
+
msg = "CRUD:create - expected .errors to be empty, but was: [#{m.errors.inspect}]"
|
29
|
+
assert_empty(m.errors, msg)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Asserts that a model can be read successfully.
|
33
|
+
#
|
34
|
+
# @param model [Class] The model class to test.
|
35
|
+
#
|
36
|
+
# This method attempts to create a new instance of the given model
|
37
|
+
# using the :make method, then retrieves the first record from the database.
|
38
|
+
# It then asserts that:
|
39
|
+
# 1. The created instance is of the correct type.
|
40
|
+
# 2. The retrieved record is equal to the created instance.
|
41
|
+
#
|
42
|
+
# @raise [Minitest::Assertion] If the assertions fail.
|
43
|
+
#
|
44
|
+
def assert_crud_can_read(model)
|
45
|
+
m = model.send(:make)
|
46
|
+
res = model.send(:first)
|
47
|
+
|
48
|
+
msg = "CRUD:read - expected #{res.class} to be an instance of #{model}"
|
49
|
+
assert_instance_of(model, m, msg)
|
50
|
+
|
51
|
+
assert_equal(m, res, "CRUD:read - expected [#{m.inspect}] to equal [#{res.inspect}]")
|
52
|
+
end
|
53
|
+
|
54
|
+
# Asserts that a model can be updated successfully.
|
55
|
+
#
|
56
|
+
# @param model [Class] The model class to test.
|
57
|
+
# @param attr [Symbol, String] The attribute to update.
|
58
|
+
#
|
59
|
+
# This method attempts to create a new instance of the given model,
|
60
|
+
# update a specific attribute, and then verify the update.
|
61
|
+
#
|
62
|
+
# @raise [Minitest::Assertion] If the assertions fail.
|
63
|
+
#
|
64
|
+
def assert_crud_can_update(model, attr)
|
65
|
+
# Create a new instance of the model
|
66
|
+
m = model.send(:make)
|
67
|
+
|
68
|
+
# Generate a new value for the attribute by appending " Updated" to its current value
|
69
|
+
tmp_str = "#{m.send(attr.to_sym)} Updated"
|
70
|
+
|
71
|
+
# Retrieve the first record from the database
|
72
|
+
res = model.send(:first)
|
73
|
+
|
74
|
+
# Assert that the retrieved record matches the created instance
|
75
|
+
assert_equal(m, res, "CRUD:update - expected [#{m.inspect}] to equal [#{res.inspect}]")
|
76
|
+
|
77
|
+
# Update the attribute with the new value
|
78
|
+
res.send(:"#{attr}=", tmp_str)
|
79
|
+
res.save
|
80
|
+
|
81
|
+
# Retrieve the updated record from the database
|
82
|
+
res2 = model.send(:first)
|
83
|
+
|
84
|
+
# Assert that the updated attribute matches the new value
|
85
|
+
msg = "CRUD:update - expected [#{res2.send(attr.to_sym)}] to equal "
|
86
|
+
msg << "the updated string: [#{tmp_str}]"
|
87
|
+
|
88
|
+
assert_equal(tmp_str, res2.send(attr.to_sym), msg)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Asserts that a model can be destroyed successfully.
|
92
|
+
#
|
93
|
+
# @param model [Class] The model class to test.
|
94
|
+
#
|
95
|
+
# This method tests the destroy functionality of a model, considering both
|
96
|
+
# hard delete and soft delete (using :deleted_at) scenarios.
|
97
|
+
#
|
98
|
+
# @raise [Minitest::Assertion] If the assertions fail.
|
99
|
+
#
|
100
|
+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
101
|
+
def assert_crud_can_destroy(model)
|
102
|
+
# Create a new instance of the model
|
103
|
+
m = model.send(:make)
|
104
|
+
|
105
|
+
# 1. test for Paranoid plugin
|
106
|
+
plugs = model.instance_variable_get(:@plugins).map(&:to_s)
|
107
|
+
paranoid = plugs.include?('Sequel::Plugins::Paranoid')
|
108
|
+
|
109
|
+
if paranoid
|
110
|
+
# If the model uses soft delete (has :deleted_at), ensure it's initially nil
|
111
|
+
msg = "CRUD:destroy - expected :deleted_at to be nil, but was [#{m.deleted_at}]"
|
112
|
+
assert_nil(m.deleted_at, msg)
|
113
|
+
|
114
|
+
assert(m.soft_delete, 'CRUD:soft_delete - expected m.soft_delete to return true')
|
115
|
+
|
116
|
+
# For soft delete, :deleted_at should be set to a timestamp
|
117
|
+
refute_nil(m.deleted_at, 'CRUD:destroy - expected m.deleted_at to be NOT be nil')
|
118
|
+
|
119
|
+
msg = 'CRUD:destroy - expected m.deleted_at to be an instance of Time'
|
120
|
+
assert_instance_of(Time, m.deleted_at, msg)
|
121
|
+
else
|
122
|
+
# Attempt to destroy the model instance
|
123
|
+
assert(m.destroy, 'CRUD:destroy - expected m.destroy to return true')
|
124
|
+
end
|
125
|
+
|
126
|
+
# Try to retrieve the first record from the database after destruction
|
127
|
+
res = model.send(:first)
|
128
|
+
|
129
|
+
if paranoid
|
130
|
+
# By default the paranoid record should be returned from the database
|
131
|
+
msg = "CRUD:destroy - expected #{model}.first to NOT return nil, but was: [#{res.inspect}]"
|
132
|
+
refute_nil(res, msg)
|
133
|
+
else
|
134
|
+
# By default the record should not be returned from the database
|
135
|
+
msg = "CRUD:destroy - expected #{model}.first to return nil, but was: [#{res.inspect}]"
|
136
|
+
assert_nil(res, msg)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
140
|
+
end
|
141
|
+
# /module Assertions
|
142
|
+
end
|
2
143
|
|
3
144
|
# reopening to add helpers functionality
|
4
|
-
|
145
|
+
# rubocop:disable Style/ClassAndModuleChildren
|
146
|
+
class Minitest::Spec < Minitest::Test
|
147
|
+
# module MinitestSequelCRUD
|
5
148
|
|
6
149
|
# Enables quick tests to ensure that the basic CRUD functionality is working correctly for a Model
|
7
150
|
#
|
@@ -19,51 +162,77 @@ class Minitest::Spec
|
|
19
162
|
# this test depends upon being able to create a new model instance for each test via using
|
20
163
|
# Sequel Factory's `#make()` method
|
21
164
|
#
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
165
|
+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
166
|
+
def self.ensure_working_crud(model, attr)
|
167
|
+
# rubocop:disable Metrics/BlockLength
|
168
|
+
describe 'a valid CRUD model' do
|
26
169
|
it "can create a #{model}" do
|
27
170
|
m = model.send(:make)
|
28
|
-
|
29
|
-
|
171
|
+
|
172
|
+
msg = "CRUD:create - expected #{m.class} to be an instance of #{model}"
|
173
|
+
assert_instance_of(model, m, msg)
|
174
|
+
|
175
|
+
msg = "CRUD:create - expected .errors to be empty, but was: [#{m.errors.inspect}]"
|
176
|
+
assert_empty(m.errors, msg)
|
30
177
|
end
|
31
178
|
|
32
179
|
it "can read a #{model}" do
|
33
180
|
m = model.send(:make)
|
34
|
-
res = model.send :
|
35
|
-
|
36
|
-
|
181
|
+
# res = model.send(:where, id: m.id)
|
182
|
+
res = model.send(:first)
|
183
|
+
# res = model[m.id]
|
184
|
+
|
185
|
+
msg = "CRUD:read - expected #{res.class} to be an instance of #{model}"
|
186
|
+
assert_instance_of(model, m, msg)
|
187
|
+
|
188
|
+
msg = "CRUD:read - expected [#{m.inspect}] to equal [#{res.inspect}]"
|
189
|
+
assert_equal(m, res, msg)
|
37
190
|
end
|
38
191
|
|
39
192
|
it "can update a #{model}" do
|
40
193
|
m = model.send(:make)
|
41
194
|
tmp_str = "#{m.send(attr.to_sym)} Updated"
|
42
195
|
res = model.send(:first)
|
196
|
+
# res = model[m.id]
|
43
197
|
|
44
|
-
|
45
|
-
|
198
|
+
msg = "CRUD:update - expected [#{m.inspect}] to equal [#{res.inspect}]"
|
199
|
+
assert_equal(m, res, msg)
|
200
|
+
res.send(:"#{attr}=", tmp_str)
|
46
201
|
res.save
|
47
202
|
|
48
203
|
res2 = model.send(:first)
|
49
|
-
|
204
|
+
# res2 = model[m.id]
|
205
|
+
|
206
|
+
msg = "CRUD:update - expected [#{res2.send(attr.to_sym)}] to equal the "
|
207
|
+
msg << "updated string: [#{tmp_str}]"
|
208
|
+
|
209
|
+
assert_equal(tmp_str, res2.send(attr.to_sym), msg)
|
50
210
|
end
|
51
211
|
|
52
212
|
it "can destroy a #{model}" do
|
53
213
|
m = model.send(:make)
|
214
|
+
|
54
215
|
if m.respond_to?(:deleted_at)
|
55
|
-
|
216
|
+
msg = "CRUD:delete - expected m.deleted_at to be nil, but was [#{m.deleted_at}]"
|
217
|
+
assert_nil(m.deleted_at, msg)
|
56
218
|
end
|
57
|
-
|
219
|
+
|
220
|
+
assert(m.destroy, 'CRUD:delete - expected m.destroy to return true')
|
58
221
|
res = model.send(:first)
|
59
|
-
|
222
|
+
|
60
223
|
if m.respond_to?(:deleted_at)
|
61
|
-
refute_nil(m.deleted_at,
|
62
|
-
|
224
|
+
refute_nil(m.deleted_at, 'CRUD:delete - expected m.deleted_at to be NOT be nil')
|
225
|
+
|
226
|
+
msg = 'CRUD:delete - expected m.deleted_at to be an instance of Time'
|
227
|
+
assert_instance_of(Time, m.deleted_at, msg)
|
228
|
+
else
|
229
|
+
msg = "CRUD:delete - expected #{model}.first to return nil, but was: [#{res.inspect}]"
|
230
|
+
assert_nil(res, msg)
|
63
231
|
end
|
64
232
|
end
|
65
|
-
|
66
233
|
end
|
234
|
+
# rubocop:enable Metrics/BlockLength
|
67
235
|
end
|
68
|
-
|
236
|
+
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
69
237
|
end
|
238
|
+
# rubocop:enable Style/ClassAndModuleChildren
|