Skip to content

Commit f70a8c6

Browse files
committed
Add and improve tests.
1 parent 64fec82 commit f70a8c6

File tree

6 files changed

+59
-0
lines changed

6 files changed

+59
-0
lines changed

lib/rest_framework/serializers/active_model_serializer_adapter_factory.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# This is a helper factory to wrap an ActiveModelSerializer to provide a `serialize` method which
22
# accepts both collections and individual records. Use `.for` to build adapters.
3+
# :nocov:
34
class RESTFramework::Serializers::ActiveModelSerializerAdapterFactory
45
def self.for(active_model_serializer)
56
return Class.new(active_model_serializer) do
@@ -13,6 +14,7 @@ def serialize
1314
end
1415
end
1516
end
17+
# :nocov:
1618

1719
# Alias for convenience.
1820
# rubocop:disable Layout/LineLength

lib/rest_framework/serializers/base_serializer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def serialize(*args)
1616
end
1717

1818
# Synonym for `serialize` for compatibility with `active_model_serializers`.
19+
# :nocov:
1920
def serializable_hash(*args)
2021
return self.serialize(*args)
2122
end
@@ -34,6 +35,7 @@ def self.fragment_cache_enabled?
3435
def associations(*args, **kwargs)
3536
return []
3637
end
38+
# :nocov:
3739
end
3840

3941
# Alias for convenience.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Api::Test::UnknownModelController < Api::TestController
2+
include RESTFramework::ModelControllerMixin
3+
end

test/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
rest_resources :fields_hash_only_except
5151
rest_resources :no_rescue_unknown_format
5252
rest_resources :read_only
53+
rest_resources :unknown_model
5354
rest_resources :users_with_sub_fields
5455

5556
rest_route :network

test/test/controllers/api/demo/movies_controller_test.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ def test_ransack_simple
1212
assert_response(:success)
1313
assert_equal(Movie.where("price > 9").count, @response.parsed_body.length)
1414
end
15+
16+
def test_ransack_distinct
17+
get(:index, as: :json, params: {q: {price_gt: 9}, distinct: true, page_size: 0})
18+
assert_response(:success)
19+
assert_equal(Movie.where("price > 9").count, @response.parsed_body.length)
20+
end
1521
end
1622

1723
def test_bulk_create
@@ -139,4 +145,40 @@ def test_filtering_predicates
139145
assert_response(:success)
140146
assert_equal(5, @response.parsed_body.length)
141147
end
148+
149+
def test_subfield_predicate
150+
# This feature is only available in Rails 7 and above.
151+
return if Rails::VERSION::MAJOR < 7
152+
153+
get(:index, as: :json, params: {"main_genre.name_in" => "History,Fantasy", page_size: 0})
154+
assert_response(:success)
155+
assert_equal(
156+
Genre.where(name: ["History", "Fantasy"]).collect { |g| g.main_movies }.flatten.count,
157+
@response.parsed_body.length,
158+
)
159+
end
160+
161+
def test_search
162+
get(:index, as: :json, params: {search: "for", page_size: 0})
163+
assert_response(:success)
164+
assert_operator(@response.parsed_body.length, :<, Movie.count)
165+
end
166+
167+
def test_ordering
168+
get(:index, as: :json, params: {ordering: "name", page_size: 0})
169+
assert_response(:success)
170+
assert_equal(Movie.order("name").pluck(:id), @response.parsed_body.map { |r| r["id"] })
171+
end
172+
173+
def test_page_2
174+
get(:index, as: :json, params: {page: 2, page_size: 2})
175+
assert_response(:success)
176+
assert_equal(2, @response.parsed_body["results"].length)
177+
end
178+
179+
def test_page_0
180+
get(:index, as: :json, params: {page: 0, page_size: 2})
181+
assert_response(:success)
182+
assert_equal(2, @response.parsed_body["results"].length)
183+
end
142184
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "test_helper"
2+
3+
class Api::Test::UnknownModelControllerTest < ActionController::TestCase
4+
def test_unknown_model_raised
5+
assert_raises(RESTFramework::UnknownModelError) do
6+
get(:index)
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)