minitest-sequel 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/Gemfile +1 -1
- data/README.md +247 -78
- data/Rakefile +9 -7
- data/lib/minitest/sequel.rb +13 -12
- data/lib/minitest/sequel/associations.rb +70 -71
- data/lib/minitest/sequel/columns.rb +44 -45
- data/lib/minitest/sequel/helpers.rb +69 -0
- data/lib/minitest/sequel/plugins.rb +166 -0
- data/lib/minitest/sequel/validations.rb +188 -188
- data/lib/minitest/sequel/version.rb +3 -3
- data/minitest-sequel.gemspec +25 -22
- metadata +36 -6
data/Rakefile
CHANGED
@@ -1,25 +1,27 @@
|
|
1
1
|
OSX = RUBY_PLATFORM.match(/darwin/)
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
5
|
|
6
6
|
Rake::TestTask.new(:spec) do |t|
|
7
|
-
|
8
|
-
t.libs <<
|
7
|
+
ENV["TESTOPTS"] = "--color"
|
8
|
+
t.libs << "spec"
|
9
|
+
t.libs << "lib"
|
9
10
|
t.test_files = FileList['spec/**/*_spec.rb']
|
10
11
|
end
|
11
12
|
|
12
13
|
task :default => :spec
|
14
|
+
task :test => :spec
|
13
15
|
|
14
16
|
desc "Run specs with coverage"
|
15
17
|
task :coverage do
|
16
|
-
ENV[
|
17
|
-
Rake::Task[
|
18
|
+
ENV["COVERAGE"] = "1"
|
19
|
+
Rake::Task["spec"].invoke
|
18
20
|
`open coverage/index.html` if OSX
|
19
21
|
end
|
20
22
|
|
21
23
|
|
22
|
-
desc
|
24
|
+
desc "Run Rubocop report"
|
23
25
|
task :rubocop do
|
24
26
|
res = `which rubocop`
|
25
27
|
if res != ""
|
data/lib/minitest/sequel.rb
CHANGED
@@ -1,15 +1,14 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
1
|
+
require "minitest"
|
2
|
+
require "minitest/sequel/version"
|
3
|
+
require "sequel"
|
4
|
+
require "sequel/extensions/inflector" unless "".respond_to?(:classify)
|
5
5
|
|
6
6
|
# reopening to add additional functionality
|
7
7
|
module Minitest::Assertions
|
8
|
-
|
9
|
-
|
8
|
+
|
10
9
|
private
|
11
|
-
|
12
|
-
# handles converting `:nil`, `:false` values
|
10
|
+
|
11
|
+
# handles converting `:nil`, `:false` values
|
13
12
|
def _convert_value(val)
|
14
13
|
v = case val
|
15
14
|
when :nil then nil
|
@@ -20,9 +19,11 @@ module Minitest::Assertions
|
|
20
19
|
end
|
21
20
|
v
|
22
21
|
end
|
23
|
-
|
22
|
+
|
24
23
|
end
|
25
24
|
|
26
|
-
require
|
27
|
-
require
|
28
|
-
require
|
25
|
+
require "minitest/sequel/columns"
|
26
|
+
require "minitest/sequel/associations"
|
27
|
+
require "minitest/sequel/validations"
|
28
|
+
require "minitest/sequel/plugins"
|
29
|
+
require "minitest/sequel/helpers"
|
@@ -1,58 +1,58 @@
|
|
1
|
-
require
|
1
|
+
require "minitest/sequel"
|
2
2
|
|
3
3
|
# reopening to add association functionality
|
4
4
|
module Minitest::Assertions
|
5
|
-
|
5
|
+
|
6
6
|
# Test for a :one_to_one association for the current model
|
7
|
-
#
|
7
|
+
#
|
8
8
|
# let(:m) { Post.first }
|
9
|
-
#
|
9
|
+
#
|
10
10
|
# it { assert_association_one_to_one(m, :first_comment, { class: :Comment, order: :id }) }
|
11
11
|
# it { m.must_have_one_to_one_association(:first_comment, { class: :Comment, order: :id }) }
|
12
|
-
#
|
12
|
+
#
|
13
13
|
def assert_association_one_to_one(obj, attribute, opts = {}, msg = nil)
|
14
14
|
assert_association(obj.class, :one_to_one, attribute, opts, msg)
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
# Test for a :one_to_many association for the current model
|
18
|
-
#
|
18
|
+
#
|
19
19
|
# let(:m) { Post.first }
|
20
|
-
#
|
20
|
+
#
|
21
21
|
# it { assert_association_one_to_many(m, :comments) }
|
22
22
|
# it { m.must_have_one_to_many_association(:comments) }
|
23
|
-
#
|
23
|
+
#
|
24
24
|
def assert_association_one_to_many(obj, attribute, opts = {}, msg = nil)
|
25
25
|
assert_association(obj.class, :one_to_many, attribute, opts, msg)
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
# Test for a :many_to_one association for the current model
|
29
|
-
#
|
29
|
+
#
|
30
30
|
# let(:m) { Post.first }
|
31
|
-
#
|
31
|
+
#
|
32
32
|
# it { assert_association_many_to_one(m, :author) }
|
33
33
|
# it { m.must_have_many_to_one_association(:author) }
|
34
|
-
#
|
34
|
+
#
|
35
35
|
def assert_association_many_to_one(obj, attribute, opts = {}, msg = nil)
|
36
36
|
assert_association(obj.class, :many_to_one, attribute, opts, msg)
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
# Test for a :many_to_many association for the current model
|
40
|
-
#
|
40
|
+
#
|
41
41
|
# let(:m) { Post.first }
|
42
|
-
#
|
42
|
+
#
|
43
43
|
# it { assert_association_many_to_many(m, :tags) }
|
44
44
|
# it { m.must_have_many_to_many_association(:tags) }
|
45
|
-
#
|
45
|
+
#
|
46
46
|
def assert_association_many_to_many(obj, attribute, opts = {}, msg = nil)
|
47
47
|
assert_association(obj.class, :many_to_many, attribute, opts, msg)
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
# Test for associations for the current model by passing the :association_type
|
51
|
-
#
|
51
|
+
#
|
52
52
|
# it { assert_association(Post, :many_to_many, :tags) }
|
53
|
-
#
|
53
|
+
#
|
54
54
|
def assert_association(klass, association_type, attribute, opts = {}, msg = nil)
|
55
|
-
msg = msg.nil? ?
|
55
|
+
msg = msg.nil? ? "" : "#{msg}\n"
|
56
56
|
msg << "Expected #{klass.inspect} to have a #{association_type.inspect}"
|
57
57
|
msg << " association #{attribute.inspect}"
|
58
58
|
assoc = klass.association_reflection(attribute) || {}
|
@@ -62,20 +62,20 @@ module Minitest::Assertions
|
|
62
62
|
klass.associations.each do |a|
|
63
63
|
o = klass.association_reflection(a)
|
64
64
|
if o[:type] == :many_to_many
|
65
|
-
arr << {
|
66
|
-
attribute: o[:name],
|
67
|
-
type: o[:type],
|
68
|
-
class: o[:class_name].
|
69
|
-
join_table: o[:join_table],
|
70
|
-
left_keys: o[:left_keys],
|
71
|
-
right_keys: o[:right_keys]
|
65
|
+
arr << {
|
66
|
+
attribute: o[:name],
|
67
|
+
type: o[:type],
|
68
|
+
class: o[:class_name].to_s,
|
69
|
+
join_table: o[:join_table],
|
70
|
+
left_keys: o[:left_keys],
|
71
|
+
right_keys: o[:right_keys]
|
72
72
|
}
|
73
73
|
else
|
74
|
-
arr << {
|
75
|
-
attribute: o[:name],
|
76
|
-
type: o[:type],
|
77
|
-
class: o[:class_name].
|
78
|
-
keys: o[:keys]
|
74
|
+
arr << {
|
75
|
+
attribute: o[:name],
|
76
|
+
type: o[:type],
|
77
|
+
class: o[:class_name].to_s,
|
78
|
+
keys: o[:keys]
|
79
79
|
}
|
80
80
|
end
|
81
81
|
end
|
@@ -86,9 +86,9 @@ module Minitest::Assertions
|
|
86
86
|
err_msg = []
|
87
87
|
conf_msg = []
|
88
88
|
opts.each do |key, value|
|
89
|
-
conf_msg << { key => value }
|
89
|
+
conf_msg << { key => value }
|
90
90
|
if assoc[key] != value
|
91
|
-
err_msg << { key => assoc[key] }
|
91
|
+
err_msg << { key => assoc[key].to_s }
|
92
92
|
matching = false
|
93
93
|
end
|
94
94
|
end
|
@@ -96,62 +96,62 @@ module Minitest::Assertions
|
|
96
96
|
assert(matching, msg)
|
97
97
|
end
|
98
98
|
end
|
99
|
-
|
100
|
-
|
99
|
+
|
100
|
+
|
101
101
|
# Test to ensure there is no :one_to_one association for the current model
|
102
|
-
#
|
102
|
+
#
|
103
103
|
# let(:m) { Post.first }
|
104
|
-
#
|
104
|
+
#
|
105
105
|
# it { refute_association_one_to_one(m, :first_comment, { class: :Comment, order: :id }) }
|
106
106
|
# it { m.must_have_one_to_one_association(:first_comment, { class: :Comment, order: :id }) }
|
107
|
-
#
|
108
|
-
def refute_association_one_to_one(obj, attribute,
|
109
|
-
refute_association(obj.class, :one_to_one, attribute,
|
107
|
+
#
|
108
|
+
def refute_association_one_to_one(obj, attribute, msg = nil)
|
109
|
+
refute_association(obj.class, :one_to_one, attribute, msg)
|
110
110
|
end
|
111
|
-
|
111
|
+
|
112
112
|
# Test to ensure there is no :one_to_many association for the current model
|
113
|
-
#
|
113
|
+
#
|
114
114
|
# let(:m) { Post.first }
|
115
|
-
#
|
115
|
+
#
|
116
116
|
# it { refute_association_one_to_many(m, :comments) }
|
117
117
|
# it { m.must_have_one_to_many_association(:comments) }
|
118
|
-
#
|
119
|
-
def refute_association_one_to_many(obj, attribute,
|
120
|
-
refute_association(obj.class, :one_to_many, attribute,
|
118
|
+
#
|
119
|
+
def refute_association_one_to_many(obj, attribute, msg = nil)
|
120
|
+
refute_association(obj.class, :one_to_many, attribute, msg)
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
# Test to ensure there is no :many_to_one association for the current model
|
124
|
-
#
|
124
|
+
#
|
125
125
|
# let(:m) { Post.first }
|
126
|
-
#
|
126
|
+
#
|
127
127
|
# it { refute_association_many_to_one(m, :author) }
|
128
128
|
# it { m.must_have_many_to_one_association(:author) }
|
129
|
-
#
|
130
|
-
def refute_association_many_to_one(obj, attribute,
|
131
|
-
refute_association(obj.class, :many_to_one, attribute,
|
129
|
+
#
|
130
|
+
def refute_association_many_to_one(obj, attribute, msg = nil)
|
131
|
+
refute_association(obj.class, :many_to_one, attribute, msg)
|
132
132
|
end
|
133
|
-
|
133
|
+
|
134
134
|
# Test to ensure there is no :many_to_many association for the current model
|
135
|
-
#
|
135
|
+
#
|
136
136
|
# let(:m) { Post.first }
|
137
|
-
#
|
137
|
+
#
|
138
138
|
# it { refute_association_many_to_many(m, :tags) }
|
139
139
|
# it { m.must_have_many_to_many_association(:tags) }
|
140
|
-
#
|
141
|
-
def refute_association_many_to_many(obj, attribute,
|
142
|
-
refute_association(obj.class, :many_to_many, attribute,
|
140
|
+
#
|
141
|
+
def refute_association_many_to_many(obj, attribute, msg = nil)
|
142
|
+
refute_association(obj.class, :many_to_many, attribute, msg)
|
143
143
|
end
|
144
|
-
|
144
|
+
|
145
145
|
# Test to ensure the current model does NOT have an association by type :association_type
|
146
|
-
#
|
146
|
+
#
|
147
147
|
# it { refute_association(Post, :many_to_many, :tags) }
|
148
|
-
#
|
149
|
-
def refute_association(klass, association_type, attribute,
|
150
|
-
msg = msg.nil? ?
|
148
|
+
#
|
149
|
+
def refute_association(klass, association_type, attribute, msg = nil)
|
150
|
+
msg = msg.nil? ? "" : "#{msg}\n"
|
151
151
|
msg << "Expected #{klass.inspect} to NOT have a #{association_type.inspect}"
|
152
152
|
msg << " association #{attribute.inspect}"
|
153
153
|
assoc = klass.association_reflection(attribute) || {}
|
154
|
-
|
154
|
+
|
155
155
|
if assoc.empty?
|
156
156
|
assert(true, msg)
|
157
157
|
else
|
@@ -159,25 +159,24 @@ module Minitest::Assertions
|
|
159
159
|
msg << ", but such an association was found"
|
160
160
|
assert(matching, msg)
|
161
161
|
end
|
162
|
-
|
163
162
|
end
|
164
|
-
|
163
|
+
|
165
164
|
end
|
166
165
|
|
167
166
|
|
168
167
|
# add support for Spec syntax
|
169
168
|
module Minitest::Expectations
|
170
|
-
|
169
|
+
|
171
170
|
infect_an_assertion :assert_association, :must_have_association, :reverse
|
172
171
|
infect_an_assertion :assert_association_one_to_one, :must_have_one_to_one_association, :reverse
|
173
172
|
infect_an_assertion :assert_association_one_to_many, :must_have_one_to_many_association, :reverse
|
174
173
|
infect_an_assertion :assert_association_many_to_one, :must_have_many_to_one_association, :reverse
|
175
174
|
infect_an_assertion :assert_association_many_to_many, :must_have_many_to_many_association, :reverse
|
176
|
-
|
175
|
+
|
177
176
|
infect_an_assertion :refute_association, :wont_have_association, :reverse
|
178
177
|
infect_an_assertion :refute_association_one_to_one, :wont_have_one_to_one_association, :reverse
|
179
178
|
infect_an_assertion :refute_association_one_to_many, :wont_have_one_to_many_association, :reverse
|
180
179
|
infect_an_assertion :refute_association_many_to_one, :wont_have_many_to_one_association, :reverse
|
181
180
|
infect_an_assertion :refute_association_many_to_many, :wont_have_many_to_many_association, :reverse
|
182
|
-
|
181
|
+
|
183
182
|
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
require
|
1
|
+
# require "minitest/sequel"
|
2
2
|
|
3
3
|
# reopening to add schema validation functionality
|
4
4
|
module Minitest::Assertions
|
5
|
-
|
5
|
+
|
6
6
|
# Conveniently test your Model definitions as follows:
|
7
7
|
#
|
8
8
|
# let(:m) { Post.first }
|
@@ -11,11 +11,11 @@ module Minitest::Assertions
|
|
11
11
|
# it { m.must_have_column(:title, type: :string, db_type: 'varchar(250)') }
|
12
12
|
#
|
13
13
|
# # assert_have_column(<instance>, <column_name>, <options>, <custom_error_message>)
|
14
|
-
#
|
14
|
+
#
|
15
15
|
# # <instance>.must_have_column(<column_name>, <options>, <custom_error_message>)
|
16
16
|
#
|
17
17
|
#
|
18
|
-
# `assert_have_column()` first tests if the column name is defined in the Model and then checks
|
18
|
+
# `assert_have_column()` first tests if the column name is defined in the Model and then checks
|
19
19
|
# all passed options. The following options are valid and checked:
|
20
20
|
#
|
21
21
|
# * :type
|
@@ -26,7 +26,7 @@ module Minitest::Assertions
|
|
26
26
|
# * :primary_key
|
27
27
|
# * :auto_increment
|
28
28
|
#
|
29
|
-
# In the event the specs differ from the actual database implementation an extensive error
|
29
|
+
# In the event the specs differ from the actual database implementation an extensive error
|
30
30
|
# message with the differing option(s) is provided to help speed up debugging the issue:
|
31
31
|
#
|
32
32
|
# Expected Post model to have column: :title with: \
|
@@ -36,123 +36,122 @@ module Minitest::Assertions
|
|
36
36
|
#
|
37
37
|
# **Please NOTE!**
|
38
38
|
#
|
39
|
-
# To test options with a value that is either `nil`, `true` or `false`, please use `:nil`,
|
39
|
+
# To test options with a value that is either `nil`, `true` or `false`, please use `:nil`,
|
40
40
|
# `:false` or `:true` and provide numbers as 'strings' instead.
|
41
41
|
#
|
42
42
|
#
|
43
43
|
def assert_have_column(obj, attribute, opts = {}, msg = nil)
|
44
|
-
msg = msg.nil? ?
|
44
|
+
msg = msg.nil? ? "" : "#{msg}\n"
|
45
45
|
msg << "Expected #{obj.class} model to have column: :#{attribute}"
|
46
46
|
err_msg = []
|
47
47
|
conf_msg = []
|
48
48
|
# check if column exists
|
49
|
-
|
50
|
-
matching = !
|
51
|
-
|
49
|
+
dcol = obj.db.schema(obj.class.table_name).detect { |c| c[0] == attribute }
|
50
|
+
matching = !dcol.nil?
|
51
|
+
|
52
52
|
# bail out if no matching column
|
53
53
|
unless matching
|
54
|
-
msg <<
|
54
|
+
msg << " but no such column exists"
|
55
55
|
assert(false, msg)
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
# bail out if options provided are invalid
|
59
59
|
val_opts = [:type, :db_type, :allow_null, :max_length, :default, :primary_key, :auto_increment]
|
60
|
-
invalid_opts
|
60
|
+
invalid_opts = opts.keys.reject { |o| val_opts.include?(o) }
|
61
61
|
unless invalid_opts.empty?
|
62
|
-
msg <<
|
62
|
+
msg << ", but the following invalid option(s) was found: { "
|
63
63
|
invalid_opts.each { |o| msg << "#{o.inspect}; " }
|
64
64
|
msg << " }. Valid options are: #{val_opts.inspect}"
|
65
65
|
assert(false, msg)
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
# TODO: simplify this mess. quick fix didn't work, so look at it again when time permits.
|
69
|
-
|
69
|
+
|
70
70
|
unless opts[:type].nil?
|
71
|
-
expected = (
|
71
|
+
expected = (dcol[1][:type].to_s == opts[:type].to_s)
|
72
72
|
conf_msg << "type: '#{opts[:type]}'"
|
73
73
|
unless expected
|
74
|
-
err_msg << "type: '#{
|
74
|
+
err_msg << "type: '#{dcol[1][:type]}'"
|
75
75
|
end
|
76
76
|
matching &&= expected
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
unless opts[:db_type].nil?
|
80
|
-
expected = (
|
80
|
+
expected = (dcol[1][:db_type].to_s == opts[:db_type].to_s)
|
81
81
|
conf_msg << "db_type: '#{opts[:db_type]}'"
|
82
82
|
unless expected
|
83
|
-
err_msg << "db_type: '#{
|
83
|
+
err_msg << "db_type: '#{dcol[1][:db_type]}'"
|
84
84
|
end
|
85
85
|
matching &&= expected
|
86
86
|
end
|
87
|
-
|
87
|
+
|
88
88
|
unless opts[:max_length].nil?
|
89
|
-
expected = (
|
89
|
+
expected = (dcol[1][:max_length] === opts[:max_length])
|
90
90
|
conf_msg << "max_length: '#{opts[:max_length]}'"
|
91
91
|
unless expected
|
92
|
-
err_msg << "max_length: '#{
|
92
|
+
err_msg << "max_length: '#{dcol[1][:max_length]}'"
|
93
93
|
end
|
94
94
|
matching &&= expected
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
unless opts[:allow_null].nil?
|
98
98
|
v = _convert_value(opts[:allow_null])
|
99
|
-
expected = (
|
99
|
+
expected = (dcol[1][:allow_null] === v)
|
100
100
|
conf_msg << "allow_null: '#{opts[:allow_null]}'"
|
101
101
|
unless expected
|
102
|
-
err_msg << "allow_null: '#{
|
102
|
+
err_msg << "allow_null: '#{dcol[1][:allow_null]}'"
|
103
103
|
end
|
104
104
|
matching &&= expected
|
105
105
|
end
|
106
|
-
|
106
|
+
|
107
107
|
unless opts[:default].nil?
|
108
108
|
v = _convert_value(opts[:default])
|
109
|
-
expected = (
|
109
|
+
expected = (dcol[1][:default] === v)
|
110
110
|
conf_msg << "default: '#{opts[:default]}'"
|
111
111
|
unless expected
|
112
|
-
err_msg << "default: '#{
|
112
|
+
err_msg << "default: '#{dcol[1][:default].inspect}'"
|
113
113
|
end
|
114
114
|
matching &&= expected
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
117
|
unless opts[:primary_key].nil?
|
118
118
|
v = _convert_value(opts[:primary_key])
|
119
|
-
expected = (
|
119
|
+
expected = (dcol[1][:primary_key] === v)
|
120
120
|
conf_msg << "primary_key: '#{opts[:primary_key]}'"
|
121
121
|
unless expected
|
122
|
-
err_msg << "primary_key: '#{
|
122
|
+
err_msg << "primary_key: '#{dcol[1][:primary_key]}'"
|
123
123
|
end
|
124
124
|
matching &&= expected
|
125
125
|
end
|
126
|
-
|
126
|
+
|
127
127
|
unless opts[:auto_increment].nil?
|
128
128
|
v = _convert_value(opts[:auto_increment])
|
129
|
-
expected = (
|
129
|
+
expected = (dcol[1][:auto_increment] === v)
|
130
130
|
conf_msg << "auto_increment: '#{opts[:auto_increment]}'"
|
131
131
|
unless expected
|
132
|
-
err_msg << "auto_increment: '#{
|
132
|
+
err_msg << "auto_increment: '#{dcol[1][:auto_increment]}'"
|
133
133
|
end
|
134
134
|
matching &&= expected
|
135
135
|
end
|
136
|
-
|
136
|
+
|
137
137
|
msg = msg << " with: { #{conf_msg.join(', ')} } but found: { #{err_msg.join(', ')} }"
|
138
138
|
assert(matching, msg)
|
139
139
|
end
|
140
|
-
|
140
|
+
|
141
141
|
#
|
142
|
-
def refute_have_column(obj, attribute,
|
143
|
-
msg = msg.nil? ?
|
142
|
+
def refute_have_column(obj, attribute, msg = nil)
|
143
|
+
msg = msg.nil? ? "" : "#{msg}\n"
|
144
144
|
msg << "Expected #{obj.class} model to NOT have column: :#{attribute}"
|
145
145
|
# check if column exists
|
146
|
-
|
147
|
-
matching =
|
146
|
+
dcol = obj.db.schema(obj.class.table_name).detect { |col| col[0] == attribute }
|
147
|
+
matching = dcol.nil?
|
148
148
|
unless matching
|
149
|
-
msg <<
|
149
|
+
msg << " but such a column was found"
|
150
150
|
assert(matching, msg)
|
151
151
|
end
|
152
152
|
end
|
153
|
-
|
154
|
-
end
|
155
153
|
|
154
|
+
end
|
156
155
|
|
157
156
|
# add support for Spec syntax
|
158
157
|
module Minitest::Expectations
|