active_sort_order 0.9.3 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -1
  3. data/lib/active_sort_order/concerns/sort_order_concern.rb +10 -8
  4. data/lib/active_sort_order/version.rb +1 -1
  5. data/lib/active_sort_order.rb +10 -6
  6. metadata +21 -81
  7. data/test/dummy_app/Rakefile +0 -7
  8. data/test/dummy_app/app/assets/config/manifest.js +0 -3
  9. data/test/dummy_app/app/assets/javascripts/application.js +0 -0
  10. data/test/dummy_app/app/assets/stylesheets/application.css +0 -3
  11. data/test/dummy_app/app/controllers/application_controller.rb +0 -3
  12. data/test/dummy_app/app/models/application_record.rb +0 -3
  13. data/test/dummy_app/app/models/post.rb +0 -3
  14. data/test/dummy_app/app/models/post_with_base_order_a.rb +0 -7
  15. data/test/dummy_app/app/models/post_with_base_order_a_and_b.rb +0 -7
  16. data/test/dummy_app/app/models/post_with_base_order_b.rb +0 -7
  17. data/test/dummy_app/app/models/post_with_base_order_b_and_a.rb +0 -7
  18. data/test/dummy_app/app/models/post_with_volatile_base_order.rb +0 -7
  19. data/test/dummy_app/app/views/layouts/application.html.erb +0 -14
  20. data/test/dummy_app/config/application.rb +0 -61
  21. data/test/dummy_app/config/boot.rb +0 -10
  22. data/test/dummy_app/config/database.yml +0 -20
  23. data/test/dummy_app/config/environment.rb +0 -5
  24. data/test/dummy_app/config/environments/development.rb +0 -30
  25. data/test/dummy_app/config/environments/production.rb +0 -60
  26. data/test/dummy_app/config/environments/test.rb +0 -41
  27. data/test/dummy_app/config/initializers/backtrace_silencers.rb +0 -7
  28. data/test/dummy_app/config/initializers/inflections.rb +0 -10
  29. data/test/dummy_app/config/initializers/mime_types.rb +0 -5
  30. data/test/dummy_app/config/initializers/secret_token.rb +0 -11
  31. data/test/dummy_app/config/initializers/session_store.rb +0 -8
  32. data/test/dummy_app/config/initializers/wrap_parameters.rb +0 -14
  33. data/test/dummy_app/config/locales/en.yml +0 -5
  34. data/test/dummy_app/config/routes.rb +0 -6
  35. data/test/dummy_app/config/secrets.yml +0 -22
  36. data/test/dummy_app/config.ru +0 -4
  37. data/test/dummy_app/db/migrate/20210128155312_set_up_test_tables.rb +0 -15
  38. data/test/dummy_app/db/schema.rb +0 -19
  39. data/test/dummy_app/db/test.sqlite3 +0 -0
  40. data/test/dummy_app/log/test.log +0 -542
  41. data/test/test_helper.rb +0 -63
  42. data/test/unit/active_sort_order_test.rb +0 -144
  43. data/test/unit/errors_test.rb +0 -204
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
@@ -1,204 +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
-
20
- valid.each do |v|
21
- Post.sort_order(v, :asc).limit(1)
22
- end
23
-
24
- ### TEST INVALID
25
- invalid = [
26
- true,
27
- false,
28
- [],
29
- Object.new,
30
- ]
31
-
32
- if RUBY_VERSION.to_f >= 3.0
33
- invalid << {}
34
- end
35
-
36
- invalid.each do |v|
37
- assert_raise ArgumentError do
38
- Post.sort_order(v, :asc).limit(1)
39
- end
40
- end
41
-
42
- assert_raise ArgumentError do
43
- Post.sort_order(Object.new, :asc).limit(1)
44
- end
45
-
46
- ### TEST UNIQUE CASES
47
-
48
- if RUBY_VERSION.to_f < 3.0
49
- ### HASH - this is allowed because its treated as keyword arguments
50
- Post.sort_order({}).limit(1)
51
-
52
- assert_raise do
53
- Post.sort_order({}, :desc).limit(1)
54
- end
55
- end
56
- end
57
-
58
- def test_sort_direction_errors
59
- valid = [
60
- "ASC",
61
- "DESC",
62
- "ASC NULLS FIRST",
63
- "ASC NULLS LAST",
64
- "DESC NULLS FIRST",
65
- "DESC NULLS LAST",
66
- nil,
67
- "",
68
-
69
- ### NASTY BUT TECHNICALLY ALLOWED BECAUSE OF SANITIZATION TECHNIQUE
70
- "ASC NULLS FIRST",
71
- " ASC ",
72
- "ASC\n",
73
- "ASC\tNULLS\tFirst",
74
- ].freeze
75
-
76
- valid.each do |direction|
77
- PostWithBaseOrderA.sort_order("x", direction).limit(1)
78
-
79
- if direction
80
- direction = direction.try!(:downcase)
81
-
82
- PostWithBaseOrderA.sort_order("x", direction).limit(1)
83
-
84
- direction = direction.try!(:to_sym)
85
-
86
- PostWithBaseOrderA.sort_order("foobar", direction).limit(1)
87
- end
88
- end
89
-
90
- invalid = [
91
- false,
92
- true,
93
- Object.new,
94
- [],
95
- 'ASCC',
96
- ]
97
-
98
- if RUBY_VERSION.to_f >= 3.0
99
- invalid << {}
100
- end
101
-
102
- invalid.each do |direction|
103
- assert_raise ArgumentError do
104
- PostWithBaseOrderA.sort_order("foobar", direction).limit(1)
105
- end
106
- end
107
-
108
- ### TEST UNIQUE CASES
109
-
110
- if RUBY_VERSION.to_f < 3.0
111
- ### HASH - this is allowed because its treated as keyword arguments
112
- Post.sort_order("foobar", {}).limit(1).to_sql.include?("foobar ASC")
113
-
114
- assert_raise do
115
- Post.sort_order("foobar", {}, {}).limit(1)
116
- end
117
- end
118
- end
119
-
120
- def test_argument_base_sort_order_errors
121
- assert_not Post.respond_to?(:base_sort_order)
122
-
123
- valid = [
124
- nil,
125
- true,
126
- false,
127
- "",
128
- "foobar",
129
- ]
130
-
131
- valid.each do |v|
132
- Post.sort_order(base_sort_order: v).limit(1)
133
- end
134
-
135
- invalid = [
136
- :foobar,
137
- [],
138
- {},
139
- Object.new,
140
- ]
141
-
142
- invalid.each do |v|
143
- assert_raise ArgumentError do
144
- Post.sort_order(base_sort_order: v).limit(1)
145
- end
146
- end
147
- end
148
-
149
- def test_class_method_base_sort_order_errors
150
- klass = PostWithVolatileBaseOrder
151
-
152
- assert klass.respond_to?(:base_sort_order)
153
-
154
- valid = [
155
- nil,
156
- false,
157
- "",
158
- "foobar",
159
- ]
160
-
161
- valid.each do |v|
162
- silence_warnings do
163
- klass.define_singleton_method :base_sort_order do
164
- v
165
- end
166
- end
167
-
168
- if v.nil?
169
- assert_nil klass.base_sort_order
170
- else
171
- assert_equal v, klass.base_sort_order
172
- end
173
-
174
- klass.sort_order.limit(1)
175
- end
176
-
177
- invalid = [
178
- true,
179
- :foobar,
180
- [],
181
- {},
182
- Object.new,
183
- ]
184
-
185
- invalid.each do |v|
186
- silence_warnings do
187
- klass.define_singleton_method :base_sort_order do
188
- v
189
- end
190
- end
191
-
192
- if v.nil?
193
- assert_nil klass.base_sort_order
194
- else
195
- assert_equal v, klass.base_sort_order
196
- end
197
-
198
- assert_raise ArgumentError do
199
- klass.sort_order.limit(1)
200
- end
201
- end
202
- end
203
-
204
- end