active_sort_order 0.9.4 → 0.9.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -3
- data/lib/active_sort_order/version.rb +1 -1
- data/lib/active_sort_order.rb +10 -6
- metadata +21 -81
- data/test/dummy_app/Rakefile +0 -7
- data/test/dummy_app/app/assets/config/manifest.js +0 -3
- data/test/dummy_app/app/assets/javascripts/application.js +0 -0
- data/test/dummy_app/app/assets/stylesheets/application.css +0 -3
- data/test/dummy_app/app/controllers/application_controller.rb +0 -3
- data/test/dummy_app/app/models/application_record.rb +0 -3
- data/test/dummy_app/app/models/post.rb +0 -3
- data/test/dummy_app/app/models/post_with_base_order_a.rb +0 -7
- data/test/dummy_app/app/models/post_with_base_order_a_and_b.rb +0 -7
- data/test/dummy_app/app/models/post_with_base_order_b.rb +0 -7
- data/test/dummy_app/app/models/post_with_base_order_b_and_a.rb +0 -7
- data/test/dummy_app/app/models/post_with_volatile_base_order.rb +0 -7
- data/test/dummy_app/app/views/layouts/application.html.erb +0 -14
- data/test/dummy_app/config/application.rb +0 -61
- data/test/dummy_app/config/boot.rb +0 -10
- data/test/dummy_app/config/database.yml +0 -20
- data/test/dummy_app/config/environment.rb +0 -5
- data/test/dummy_app/config/environments/development.rb +0 -30
- data/test/dummy_app/config/environments/production.rb +0 -60
- data/test/dummy_app/config/environments/test.rb +0 -41
- data/test/dummy_app/config/initializers/backtrace_silencers.rb +0 -7
- data/test/dummy_app/config/initializers/inflections.rb +0 -10
- data/test/dummy_app/config/initializers/mime_types.rb +0 -5
- data/test/dummy_app/config/initializers/secret_token.rb +0 -11
- data/test/dummy_app/config/initializers/session_store.rb +0 -8
- data/test/dummy_app/config/initializers/wrap_parameters.rb +0 -14
- data/test/dummy_app/config/locales/en.yml +0 -5
- data/test/dummy_app/config/routes.rb +0 -6
- data/test/dummy_app/config/secrets.yml +0 -22
- data/test/dummy_app/config.ru +0 -4
- data/test/dummy_app/db/migrate/20210128155312_set_up_test_tables.rb +0 -15
- data/test/dummy_app/db/schema.rb +0 -19
- data/test/dummy_app/db/test.sqlite3 +0 -0
- data/test/dummy_app/log/test.log +0 -754
- data/test/test_helper.rb +0 -63
- data/test/unit/active_sort_order_test.rb +0 -144
- data/test/unit/errors_test.rb +0 -205
data/test/test_helper.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
#$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
2
|
-
ENV["RAILS_ENV"] = "test"
|
3
|
-
|
4
|
-
require "active_sort_order"
|
5
|
-
|
6
|
-
begin
|
7
|
-
require 'warning'
|
8
|
-
|
9
|
-
Warning.ignore(
|
10
|
-
%r{mail/parsers/address_lists_parser}, ### Hide mail gem warnings
|
11
|
-
)
|
12
|
-
rescue LoadError
|
13
|
-
# Do nothing
|
14
|
-
end
|
15
|
-
|
16
|
-
### Instantiates Rails
|
17
|
-
require File.expand_path("../dummy_app/config/environment.rb", __FILE__)
|
18
|
-
|
19
|
-
require "rails/test_help"
|
20
|
-
|
21
|
-
class ActiveSupport::TestCase
|
22
|
-
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
|
23
|
-
fixtures :all
|
24
|
-
end
|
25
|
-
|
26
|
-
Rails.backtrace_cleaner.remove_silencers!
|
27
|
-
|
28
|
-
require 'minitest/reporters'
|
29
|
-
Minitest::Reporters.use!(
|
30
|
-
Minitest::Reporters::DefaultReporter.new,
|
31
|
-
ENV,
|
32
|
-
Minitest.backtrace_filter
|
33
|
-
)
|
34
|
-
|
35
|
-
require "minitest/autorun"
|
36
|
-
|
37
|
-
# Run any available migration
|
38
|
-
if ActiveRecord.gem_version >= Gem::Version.new("6.0")
|
39
|
-
ActiveRecord::MigrationContext.new(File.expand_path("dummy_app/db/migrate/", __dir__), ActiveRecord::SchemaMigration).migrate
|
40
|
-
elsif ActiveRecord.gem_version >= Gem::Version.new("5.2")
|
41
|
-
ActiveRecord::MigrationContext.new(File.expand_path("dummy_app/db/migrate/", __dir__)).migrate
|
42
|
-
else
|
43
|
-
ActiveRecord::Migrator.migrate File.expand_path("dummy_app/db/migrate/", __dir__)
|
44
|
-
end
|
45
|
-
|
46
|
-
[Post].each do |klass|
|
47
|
-
if defined?(SQLite3)
|
48
|
-
ActiveRecord::Base.connection.execute("DELETE FROM #{klass.table_name};")
|
49
|
-
ActiveRecord::Base.connection.execute("UPDATE `sqlite_sequence` SET `seq` = 0 WHERE `name` = '#{klass.table_name}';")
|
50
|
-
else
|
51
|
-
ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{klass.table_name}")
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
DATA = {}.with_indifferent_access
|
56
|
-
|
57
|
-
DATA[:posts] = [
|
58
|
-
Post.find_or_create_by!(a: 1, b: 3),
|
59
|
-
Post.find_or_create_by!(a: 2, b: 2),
|
60
|
-
Post.find_or_create_by!(a: 3, b: 2),
|
61
|
-
Post.find_or_create_by!(a: 4, b: 1),
|
62
|
-
Post.find_or_create_by!(a: 5, b: 1),
|
63
|
-
].shuffle
|
@@ -1,144 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
class ActiveSortOrderTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
setup do
|
6
|
-
end
|
7
|
-
|
8
|
-
teardown do
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_exposes_main_module
|
12
|
-
assert ActiveSortOrder.is_a?(Module)
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_exposes_version
|
16
|
-
assert ActiveSortOrder::VERSION
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_base_sort_order_default_value
|
20
|
-
klass = PostWithBaseOrderA
|
21
|
-
|
22
|
-
assert PostWithBaseOrderA.unscoped.sort_order.to_sql.include?("ORDER BY #{klass.base_sort_order}")
|
23
|
-
|
24
|
-
assert PostWithBaseOrderA.unscoped.sort_order(base_sort_order: true).to_sql.include?("ORDER BY #{klass.base_sort_order}")
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_class_base_sort_order_only
|
28
|
-
assert_equal Post.all.count, DATA[:posts].count
|
29
|
-
|
30
|
-
sorted = PostWithBaseOrderA.all.sort_order
|
31
|
-
|
32
|
-
expected = DATA[:posts].sort_by{|item| item.a }
|
33
|
-
|
34
|
-
sorted.each_with_index do |item, i|
|
35
|
-
assert_equal expected[i].id, item.id
|
36
|
-
end
|
37
|
-
|
38
|
-
sorted = PostWithBaseOrderB.all.sort_order
|
39
|
-
|
40
|
-
expected = DATA[:posts].sort_by{|item| item.b }
|
41
|
-
|
42
|
-
sorted.each_with_index do |item, i|
|
43
|
-
assert_equal expected[i].b, item.b ### use b instead of id as its not unique
|
44
|
-
end
|
45
|
-
|
46
|
-
sorted = PostWithBaseOrderAAndB.all.sort_order
|
47
|
-
|
48
|
-
expected = DATA[:posts].sort_by{|item| [item.a, item.b] }
|
49
|
-
|
50
|
-
sorted.each_with_index do |item, i|
|
51
|
-
assert_equal expected[i].id, item.id
|
52
|
-
end
|
53
|
-
|
54
|
-
sorted = PostWithBaseOrderBAndA.all.sort_order
|
55
|
-
|
56
|
-
expected = DATA[:posts].sort_by{|item| [item.b, item.a] }
|
57
|
-
|
58
|
-
sorted.each_with_index do |item, i|
|
59
|
-
assert_equal expected[i].id, item.id
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_override_base_sort_order_only
|
64
|
-
assert_equal Post.all.count, DATA[:posts].count
|
65
|
-
|
66
|
-
sorted = PostWithBaseOrderA.order(b: :desc).sort_order(base_sort_order: "posts.b ASC")
|
67
|
-
|
68
|
-
expected = DATA[:posts].sort_by{|item| item.b }
|
69
|
-
|
70
|
-
sorted.each_with_index do |item, i|
|
71
|
-
assert_equal expected[i].b, item.b ### use b instead of id as its not unique
|
72
|
-
end
|
73
|
-
|
74
|
-
expected = DATA[:posts].sort_by{|item| item.id }
|
75
|
-
|
76
|
-
### NIL & FALSE
|
77
|
-
[nil, false].each do |v|
|
78
|
-
sorted = PostWithBaseOrderA.order(id: :asc).sort_order(base_sort_order: v)
|
79
|
-
|
80
|
-
sorted.each_with_index do |item, i|
|
81
|
-
assert_equal expected[i].id, item.id
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_sort_only
|
87
|
-
assert_equal Post.all.count, DATA[:posts].count
|
88
|
-
|
89
|
-
expected = DATA[:posts].sort_by{|item| item.a }.reverse
|
90
|
-
|
91
|
-
sorted = PostWithBaseOrderA.all.sort_order(:a, :desc)
|
92
|
-
|
93
|
-
sorted.each_with_index do |item, i|
|
94
|
-
assert_equal expected[i].id, item.id
|
95
|
-
end
|
96
|
-
|
97
|
-
sorted = PostWithBaseOrderA.all.sort_order("posts.a", "DESC")
|
98
|
-
|
99
|
-
sorted.each_with_index do |item, i|
|
100
|
-
assert_equal expected[i].id, item.id
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
def test_base_sort_order_and_sort
|
105
|
-
assert_equal Post.all.count, DATA[:posts].count
|
106
|
-
|
107
|
-
sorted = PostWithBaseOrderA.all.sort_order("posts.a", "DESC")
|
108
|
-
|
109
|
-
expected = DATA[:posts].sort_by{|item| item.a }.reverse
|
110
|
-
|
111
|
-
sorted.each_with_index do |item, i|
|
112
|
-
assert_equal expected[i].id, item.id
|
113
|
-
end
|
114
|
-
|
115
|
-
sorted = PostWithBaseOrderB.all.sort_order("posts.b", "DESC")
|
116
|
-
|
117
|
-
expected = DATA[:posts].sort_by{|item| item.b }.reverse
|
118
|
-
|
119
|
-
sorted.each_with_index do |item, i|
|
120
|
-
assert_equal expected[i].b, item.b ### use b instead of id as its not unique
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
def test_sort_on_multiple_fields
|
125
|
-
assert_equal Post.all.count, DATA[:posts].count
|
126
|
-
|
127
|
-
expected = DATA[:posts].sort_by{|item| [item.b, item.a] }
|
128
|
-
|
129
|
-
sorted = PostWithBaseOrderA.all.sort_order([:b, :a], :asc)
|
130
|
-
|
131
|
-
sorted.each_with_index do |item, i|
|
132
|
-
assert_equal expected[i].id, item.id
|
133
|
-
end
|
134
|
-
|
135
|
-
expected = DATA[:posts].sort_by{|item| [item.b, item.a] }.reverse
|
136
|
-
|
137
|
-
sorted = PostWithBaseOrderA.all.sort_order([:b, :a], :desc)
|
138
|
-
|
139
|
-
sorted.each_with_index do |item, i|
|
140
|
-
assert_equal expected[i].id, item.id
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
end
|
data/test/unit/errors_test.rb
DELETED
@@ -1,205 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
class ActiveSortOrderTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
setup do
|
6
|
-
end
|
7
|
-
|
8
|
-
teardown do
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_sort_str_errors
|
12
|
-
### TEST VALID
|
13
|
-
valid = [
|
14
|
-
"foo",
|
15
|
-
:foo,
|
16
|
-
nil,
|
17
|
-
"",
|
18
|
-
[],
|
19
|
-
[:foo],
|
20
|
-
]
|
21
|
-
|
22
|
-
valid.each do |v|
|
23
|
-
Post.sort_order(v, :asc).limit(1)
|
24
|
-
end
|
25
|
-
|
26
|
-
### TEST INVALID
|
27
|
-
invalid = [
|
28
|
-
true,
|
29
|
-
false,
|
30
|
-
Object.new,
|
31
|
-
]
|
32
|
-
|
33
|
-
if RUBY_VERSION.to_f >= 3.0
|
34
|
-
invalid << {}
|
35
|
-
end
|
36
|
-
|
37
|
-
invalid.each do |v|
|
38
|
-
assert_raise ArgumentError do
|
39
|
-
Post.sort_order(v, :asc).limit(1)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
assert_raise ArgumentError do
|
44
|
-
Post.sort_order(Object.new, :asc).limit(1)
|
45
|
-
end
|
46
|
-
|
47
|
-
### TEST UNIQUE CASES
|
48
|
-
|
49
|
-
if RUBY_VERSION.to_f < 3.0
|
50
|
-
### HASH - this is allowed because its treated as keyword arguments
|
51
|
-
Post.sort_order({}).limit(1)
|
52
|
-
|
53
|
-
assert_raise do
|
54
|
-
Post.sort_order({}, :desc).limit(1)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_sort_direction_errors
|
60
|
-
valid = [
|
61
|
-
"ASC",
|
62
|
-
"DESC",
|
63
|
-
"ASC NULLS FIRST",
|
64
|
-
"ASC NULLS LAST",
|
65
|
-
"DESC NULLS FIRST",
|
66
|
-
"DESC NULLS LAST",
|
67
|
-
nil,
|
68
|
-
"",
|
69
|
-
|
70
|
-
### NASTY BUT TECHNICALLY ALLOWED BECAUSE OF SANITIZATION TECHNIQUE
|
71
|
-
"ASC NULLS FIRST",
|
72
|
-
" ASC ",
|
73
|
-
"ASC\n",
|
74
|
-
"ASC\tNULLS\tFirst",
|
75
|
-
].freeze
|
76
|
-
|
77
|
-
valid.each do |direction|
|
78
|
-
PostWithBaseOrderA.sort_order("x", direction).limit(1)
|
79
|
-
|
80
|
-
if direction
|
81
|
-
direction = direction.try!(:downcase)
|
82
|
-
|
83
|
-
PostWithBaseOrderA.sort_order("x", direction).limit(1)
|
84
|
-
|
85
|
-
direction = direction.try!(:to_sym)
|
86
|
-
|
87
|
-
PostWithBaseOrderA.sort_order("foobar", direction).limit(1)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
invalid = [
|
92
|
-
false,
|
93
|
-
true,
|
94
|
-
Object.new,
|
95
|
-
[],
|
96
|
-
'ASCC',
|
97
|
-
]
|
98
|
-
|
99
|
-
if RUBY_VERSION.to_f >= 3.0
|
100
|
-
invalid << {}
|
101
|
-
end
|
102
|
-
|
103
|
-
invalid.each do |direction|
|
104
|
-
assert_raise ArgumentError do
|
105
|
-
PostWithBaseOrderA.sort_order("foobar", direction).limit(1)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
### TEST UNIQUE CASES
|
110
|
-
|
111
|
-
if RUBY_VERSION.to_f < 3.0
|
112
|
-
### HASH - this is allowed because its treated as keyword arguments
|
113
|
-
Post.sort_order("foobar", {}).limit(1).to_sql.include?("foobar ASC")
|
114
|
-
|
115
|
-
assert_raise do
|
116
|
-
Post.sort_order("foobar", {}, {}).limit(1)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
def test_argument_base_sort_order_errors
|
122
|
-
assert_not Post.respond_to?(:base_sort_order)
|
123
|
-
|
124
|
-
valid = [
|
125
|
-
nil,
|
126
|
-
true,
|
127
|
-
false,
|
128
|
-
"",
|
129
|
-
"foobar",
|
130
|
-
]
|
131
|
-
|
132
|
-
valid.each do |v|
|
133
|
-
Post.sort_order(base_sort_order: v).limit(1)
|
134
|
-
end
|
135
|
-
|
136
|
-
invalid = [
|
137
|
-
:foobar,
|
138
|
-
[],
|
139
|
-
{},
|
140
|
-
Object.new,
|
141
|
-
]
|
142
|
-
|
143
|
-
invalid.each do |v|
|
144
|
-
assert_raise ArgumentError do
|
145
|
-
Post.sort_order(base_sort_order: v).limit(1)
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
def test_class_method_base_sort_order_errors
|
151
|
-
klass = PostWithVolatileBaseOrder
|
152
|
-
|
153
|
-
assert klass.respond_to?(:base_sort_order)
|
154
|
-
|
155
|
-
valid = [
|
156
|
-
nil,
|
157
|
-
false,
|
158
|
-
"",
|
159
|
-
"foobar",
|
160
|
-
]
|
161
|
-
|
162
|
-
valid.each do |v|
|
163
|
-
silence_warnings do
|
164
|
-
klass.define_singleton_method :base_sort_order do
|
165
|
-
v
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
if v.nil?
|
170
|
-
assert_nil klass.base_sort_order
|
171
|
-
else
|
172
|
-
assert_equal v, klass.base_sort_order
|
173
|
-
end
|
174
|
-
|
175
|
-
klass.sort_order.limit(1)
|
176
|
-
end
|
177
|
-
|
178
|
-
invalid = [
|
179
|
-
true,
|
180
|
-
:foobar,
|
181
|
-
[],
|
182
|
-
{},
|
183
|
-
Object.new,
|
184
|
-
]
|
185
|
-
|
186
|
-
invalid.each do |v|
|
187
|
-
silence_warnings do
|
188
|
-
klass.define_singleton_method :base_sort_order do
|
189
|
-
v
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
if v.nil?
|
194
|
-
assert_nil klass.base_sort_order
|
195
|
-
else
|
196
|
-
assert_equal v, klass.base_sort_order
|
197
|
-
end
|
198
|
-
|
199
|
-
assert_raise ArgumentError do
|
200
|
-
klass.sort_order.limit(1)
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
end
|