minitest-sequel 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,276 +1,28 @@
1
- require "minitest/sequel/version"
1
+ require 'minitest'
2
+ require 'minitest/sequel/version'
2
3
  require 'sequel'
4
+ require 'sequel/extensions/inflector' unless ''.respond_to?(:classify)
3
5
 
4
- module Minitest
5
- module Sequel
6
-
7
-
8
- # Conveniently test your Model definitions as follows:
9
- #
10
- # let(:m) { Post.first }
11
- #
12
- # it { assert_have_column(m, :title, type: :string, db_type: 'varchar(250)', allow_null: :false ) }
13
- #
14
- # # assert_have_column(<instance>, <column_name>, <options>, <custom_error_message>)
15
- #
16
- #
17
- # `assert_have_column()` first tests if the column name is defined in the Model and then checks all passed options.
18
- # The following options are valid and checked:
19
- #
20
- # * :type
21
- # * :db_type
22
- # * :allow_null
23
- # * :max_length
24
- # * :default
25
- # * :primary_key
26
- # * :auto_increment
27
- #
28
- # In the event the specs differ from the actual database implementation an extensive error message
29
- # with the differing option(s) is provided to help speed up debugging the issue:
30
- #
31
- # Expected Post model to have column: :title with: \
32
- # { type: 'string', db_type: 'varchar(250)', allow_null: 'false' } \
33
- # but found: { db_type: 'varchar(255)' }
34
- #
35
- #
36
- # **Please NOTE!**
37
- #
38
- # To test options with a value that is either `nil`, `true` or `false`, please use `:nil`,
39
- # `:false` or `:true` and provide numbers as 'strings' instead.
40
- #
41
- #
42
- def assert_have_column(obj, attribute, opts={}, msg=nil)
43
- msg = msg.nil? ? '' : "#{msg}\n"
44
- msg << "Expected #{obj.class} model to have column: :#{attribute.to_s}"
45
- err_msg = []; conf_msg = []
46
- # check if column exists
47
- col = obj.db.schema(obj.class.table_name).detect{|col| col[0]==attribute}
48
- matching = !col.nil?
49
-
50
- # bail out if no matching column
51
- unless matching
52
- msg << " but no such column exists"
53
- assert matching, msg
54
- end
55
-
56
- # bail out if options provided are invalid
57
- valid_opts = [:type, :db_type, :allow_null, :max_length, :default, :primary_key, :auto_increment]
58
- invalid_opts = opts.keys.reject { |o| valid_opts.include?(o) }
59
- unless invalid_opts.empty?
60
- msg << " : ERROR: invalid option(s) provided: { "
61
- invalid_opts.each{|o| msg << "#{o.inspect}; " }
62
- msg << " } valid options are: #{valid_opts.inspect}"
63
- assert false, msg
64
- end
65
-
66
- # carry on...
67
-
68
- # check type
69
- unless opts[:type].nil?
70
- expected = (col[1][:type].to_s == opts[:type].to_s)
71
- conf_msg << "type: '#{opts[:type]}'"
72
- unless expected
73
- err_msg << "type: '#{col[1][:type]}'"
74
- end
75
- matching &&= expected
76
- end
77
-
78
- # check db_type
79
- unless opts[:db_type].nil?
80
- expected = (col[1][:db_type].to_s == opts[:db_type].to_s)
81
- conf_msg << "db_type: '#{opts[:db_type]}'"
82
- unless expected
83
- err_msg << "db_type: '#{col[1][:db_type]}'"
84
- end
85
- matching &&= expected
86
- end
87
-
88
- # check :allow_null
89
- unless opts[:allow_null].nil?
90
- _v = _convert_value(opts[:allow_null])
91
- expected = (opts[:allow_null] === _v)
92
- conf_msg << "allow_null: '#{opts[:allow_null]}'"
93
- unless expected
94
- err_msg << "allow_null: '#{col[1][:allow_null]}'"
95
- end
96
- matching &&= expected
97
- end
98
-
99
- # check :max_length
100
- unless opts[:max_length].nil?
101
- expected = (col[1][:max_length] === opts[:max_length])
102
- conf_msg << "max_length: '#{opts[:max_length]}'"
103
- unless expected
104
- err_msg << "max_length: '#{col[1][:max_length]}'"
105
- end
106
- matching &&= expected
107
- end
108
-
109
- # check :default
110
- unless opts[:default].nil?
111
- _v = _convert_value(opts[:default])
112
- expected = (col[1][:default] === _v )
113
- conf_msg << "default: '#{opts[:default]}'"
114
- unless expected
115
- err_msg << "default: '#{col[1][:default].inspect}'"
116
- end
117
- matching &&= expected
118
- end
119
-
120
- # check :primary_key
121
- unless opts[:primary_key].nil?
122
- _v = _convert_value(opts[:primary_key])
123
- expected = (col[1][:primary_key] === _v)
124
- conf_msg << "primary_key: '#{opts[:primary_key]}'"
125
- unless expected
126
- err_msg << "primary_key: '#{col[1][:primary_key]}'"
127
- end
128
- matching &&= expected
129
- end
130
-
131
- # check :auto_increment
132
- unless opts[:auto_increment].nil?
133
- _v = _convert_value(opts[:auto_increment])
134
- expected = (col[1][:auto_increment] === _v)
135
- conf_msg << "auto_increment: '#{opts[:auto_increment]}'"
136
- unless expected
137
- err_msg << "auto_increment: '#{col[1][:auto_increment]}'"
138
- end
139
- matching &&= expected
140
- end
141
-
142
- msg = msg << " with: { #{conf_msg.join(', ')} } but found: { #{err_msg.join(', ')} }"
143
- assert matching, msg
144
-
145
- end
146
-
147
- def assert_association_one_to_one(obj, attribute, opts={}, msg=nil)
148
- assert_association(obj.class, :one_to_one, attribute, opts, msg)
149
- end
150
-
151
- def assert_association_one_to_many(obj, attribute, opts={}, msg=nil)
152
- assert_association(obj.class, :one_to_many, attribute, opts, msg)
153
- end
154
-
155
- def assert_association_many_to_one(obj, attribute, opts={}, msg=nil)
156
- assert_association(obj.class, :many_to_one, attribute, opts, msg)
157
- end
6
+ # reopening to add additional functionality
7
+ module Minitest::Assertions
158
8
 
159
- def assert_association_many_to_many(obj, attribute, opts={}, msg=nil)
160
- assert_association(obj.class, :many_to_many, attribute, opts, msg)
161
- end
162
9
 
163
- def assert_association(klass, association_type, attribute, opts={}, msg=nil)
164
- msg = msg.nil? ? '' : "#{msg}\n"
165
- msg << "Expected #{klass.inspect} to have a #{association_type.inspect} association #{attribute.inspect}"
166
- assoc = klass.association_reflection(attribute) || {}
167
- if assoc.empty?
168
- msg << " but no association '#{attribute.inspect}' was found"
169
- arr = []
170
- klass.associations.each do |a|
171
- o = klass.association_reflection(a)
172
- if o[:type] == :many_to_many
173
- arr << { attribute: o[:name], type: o[:type], class: o[:class_name].to_sym, join_table: o[:join_table], left_keys: o[:left_keys], right_keys: o[:right_keys] }
174
- else
175
- arr << { attribute: o[:name], type: o[:type], class: o[:class_name].to_sym, keys: o[:keys] }
176
- end
177
- end
178
- msg << " - \navailable associations are: [ #{arr.join(', ')} ]\n"
179
- assert false, msg
180
- else
181
- matching = assoc[:type] == association_type
182
- err_msg = []; conf_msg = []
183
- opts.each { |key, value|
184
- conf_msg << { key => value }
185
- if assoc[key]!= value
186
- err_msg << { key => assoc[key] }
187
- matching = false
188
- end
189
- }
190
- msg << " with given options: #{conf_msg.join(', ')} but should be #{err_msg.join(', ')}"
191
- assert matching, msg
192
- end
193
- end
194
-
195
- ## VALIDATIONS
196
-
197
- def assert_validates_presence(obj, attribute, opts={}, msg=nil)
198
- assert_validates(obj, :presence, attribute, opts, msg)
199
- end
200
-
201
- def assert_validates_length(obj, attribute, opts={}, msg=nil)
202
- assert_validates(obj, :length, attribute, opts, msg)
203
- end
204
-
205
- def assert_validates_exact_length(obj, attribute, opts={}, msg=nil)
206
- assert_validates(obj, :exact_length, attribute, opts, msg)
207
- end
208
-
209
- def assert_validates_length_range(obj, attribute, opts={}, msg=nil)
210
- assert_validates(obj, :length_range, attribute, opts, msg)
211
- end
212
-
213
- def assert_validates_max_length(obj, attribute, opts={}, msg=nil)
214
- assert_validates(obj, :max_length, attribute, opts, msg)
215
- end
216
-
217
- def assert_validates_min_length(obj, attribute, opts={}, msg=nil)
218
- assert_validates(obj, :min_length, attribute, opts, msg)
219
- end
220
-
221
- def assert_validates_format(obj, attribute, opts={}, msg=nil)
222
- assert_validates(obj, :format, attribute, opts, msg)
223
- end
224
-
225
- def assert_validates_includes(obj, attribute, opts={}, msg=nil)
226
- assert_validates(obj, :includes, attribute, opts, msg)
227
- end
228
-
229
- def assert_validates_integer(obj, attribute, opts={}, msg=nil)
230
- assert_validates(obj, :integer, attribute, opts, msg)
231
- end
232
-
233
- def assert_validates_numeric(obj, attribute, opts={}, msg=nil)
234
- assert_validates(obj, :numeric, attribute, opts, msg)
235
- end
236
-
237
- def assert_validates_not_string(obj, attribute, opts={}, msg=nil)
238
- assert_validates(obj, :not_string, attribute, opts, msg)
239
- end
240
-
241
- def assert_validates_unique(obj, attribute, opts={}, msg=nil)
242
- assert_validates(obj, :unique, attribute, opts, msg)
243
- end
244
-
245
- def assert_validates(obj, validation_type, attribute, opts={}, msg=nil)
246
-
247
- end
248
-
249
-
250
- private
251
-
252
- def _convert_value(val)
253
- _v = case val
254
- when :nil
255
- nil
256
- when :false
257
- false
258
- when :true
259
- true
260
- else
261
- val
262
- end
263
- _v
264
- end
265
-
266
- def _valid_validation_options
267
- [ :allow_blank, :allow_missing, :allow_nil, :message ]
268
- end
269
-
270
- end
10
+ private
271
11
 
272
- module Assertions
273
- include Minitest::Sequel
12
+ # handles converting `:nil`, `:false` values
13
+ def _convert_value(val)
14
+ v = case val
15
+ when :nil then nil
16
+ when :false then false
17
+ when :true then true
18
+ else
19
+ val
20
+ end
21
+ v
274
22
  end
275
23
 
276
24
  end
25
+
26
+ require 'minitest/sequel/columns'
27
+ require 'minitest/sequel/associations'
28
+ require 'minitest/sequel/validations'
@@ -4,15 +4,15 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'minitest/sequel/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "minitest-sequel"
7
+ spec.name = 'minitest-sequel'
8
8
  spec.version = Minitest::Sequel::VERSION
9
- spec.authors = ["Kematzy"]
10
- spec.email = ["kematzy@gmail.com"]
9
+ spec.authors = ['Kematzy']
10
+ spec.email = ['kematzy@gmail.com']
11
11
 
12
12
  spec.summary = %q{Minitest assertions to speed-up development and testing of Sequel database setups.}
13
13
  spec.description = %q{A collection of convenient assertions to enable faster DRY'er testing of Sequel database apps/gems.}
14
- spec.homepage = "https://github.com/kematzy/minitest-sequel"
15
- spec.license = "MIT"
14
+ spec.homepage = 'https://github.com/kematzy/minitest-sequel'
15
+ spec.license = 'MIT'
16
16
 
17
17
  # # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
18
  # # delete this section to allow pushing this gem to any host.
@@ -23,13 +23,19 @@ Gem::Specification.new do |spec|
23
23
  # end
24
24
 
25
25
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
- spec.bindir = "exe"
26
+ spec.bindir = 'exe'
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
28
+ spec.require_paths = ['lib']
29
29
 
30
30
  spec.add_runtime_dependency 'minitest', '~> 5.7', '>= 5.7.0'
31
31
  spec.add_runtime_dependency 'sequel', '~> 4.26'
32
32
 
33
- spec.add_development_dependency "bundler", "~> 1.10"
34
- spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency 'bundler', '~> 1.10'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'sqlite3'
36
+ spec.add_development_dependency 'minitest-assert_errors'
37
+ spec.add_development_dependency 'minitest-hooks'
38
+ spec.add_development_dependency 'minitest-rg'
39
+ spec.add_development_dependency 'simplecov'
40
+ spec.add_development_dependency 'rubocop'
35
41
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kematzy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-21 00:00:00.000000000 Z
11
+ date: 2016-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -72,6 +72,90 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '10.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: sqlite3
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: minitest-assert_errors
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: minitest-hooks
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: minitest-rg
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: simplecov
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ type: :development
139
+ prerelease: false
140
+ version_requirements: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ - !ruby/object:Gem::Dependency
146
+ name: rubocop
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ type: :development
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
75
159
  description: A collection of convenient assertions to enable faster DRY'er testing
76
160
  of Sequel database apps/gems.
77
161
  email:
@@ -87,6 +171,9 @@ files:
87
171
  - README.md
88
172
  - Rakefile
89
173
  - lib/minitest/sequel.rb
174
+ - lib/minitest/sequel/associations.rb
175
+ - lib/minitest/sequel/columns.rb
176
+ - lib/minitest/sequel/validations.rb
90
177
  - lib/minitest/sequel/version.rb
91
178
  - minitest-sequel.gemspec
92
179
  homepage: https://github.com/kematzy/minitest-sequel
@@ -109,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
196
  version: '0'
110
197
  requirements: []
111
198
  rubyforge_project:
112
- rubygems_version: 2.4.5
199
+ rubygems_version: 2.5.1
113
200
  signing_key:
114
201
  specification_version: 4
115
202
  summary: Minitest assertions to speed-up development and testing of Sequel database