Skip to content

Commit

Permalink
Fix minor bug in serializer; improve tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschmit committed Mar 28, 2021
1 parent 4603f0a commit 924d981
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/rest_framework/serializers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def initialize(many: nil, model: nil, **kwargs)
# Determine model either explicitly, or by inspecting @object or @controller.
@model = model
@model ||= @object.class if @object.is_a?(ActiveRecord::Base)
@model ||= @object[0].class if @many && @object[0].is_a?(ActiveRecord::Base)
@model ||= @object[0].class if (
@many && @object.is_a?(Enumerable) && @object.is_a?(ActiveRecord::Base)
)
@model ||= @controller.send(:get_model) if @controller
end

Expand Down
5 changes: 4 additions & 1 deletion test/app/controllers/api2/things_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ class Api2::ThingsController < Api2Controller

class ThingsSerializer < RESTFramework::NativeSerializer
self.action_config = {list: {only: [:id, :name, :price]}}
self.singular_config = {only: [:id, :name, :shape]}
self.singular_config = {
only: [:id, :name, :shape],
include: {owner: Api2::UserController::UsersSerializer.new(many: false)},
}
self.plural_config = {only: [:id, :name]}
end

Expand Down
16 changes: 16 additions & 0 deletions test/app/controllers/api2/user_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
class Api2::UserController < Api2Controller
include RESTFramework::ModelControllerMixin

class UsersSerializer < RESTFramework::NativeSerializer
self.config = {only: [:id, :login, :is_admin, :age]}
self.action_config = {
with_things: {
only: [:id, :login, :is_admin],
include: {things: Api2::ThingsController::ThingsSerializer.new(many: true)},
},
}
end

self.singleton_controller = true
self.fields = %w(login is_admin balance)
self.extra_actions = {with_things: :get}
self.serializer_class = UsersSerializer

def with_things
return self.show
end

def get_record
return User.first!
Expand Down
8 changes: 8 additions & 0 deletions test/test/controllers/api1/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def test_index
end
end

def test_index_with_filtering
filter_key = self.class.create_params.keys[0]
filter_value = _get_model.first.send(filter_key)
get :index, as: :json, params: {:"#{filter_key}" => filter_value}
assert_response :success
assert _parsed_body.all? { |r| r[filter_key.to_s] == filter_value }
end

def test_create
post :create, as: :json, params: self.class.create_params
assert_response :success
Expand Down
3 changes: 3 additions & 0 deletions test/test/controllers/api2/things_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ def test_show
assert_response :success
assert _parsed_body['shape']
assert_nil _parsed_body['price']
assert _parsed_body['owner']['login']
assert_not_nil _parsed_body['owner']['is_admin']
assert_nil _parsed_body['owner']['balance']
end
end
22 changes: 22 additions & 0 deletions test/test/controllers/api2/user_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,26 @@


class Api2::UserControllerTest < ActionController::TestCase
def test_show
get :show, as: :json
assert_response :success
assert _parsed_body['login']
assert_not_nil _parsed_body['is_admin']
assert _parsed_body['age']
assert_nil _parsed_body['balance']
assert_nil _parsed_body['things']
end

def test_with_things
get :with_things, as: :json
assert_response :success
assert _parsed_body['login']
assert_nil _parsed_body['age']
assert_not_nil _parsed_body['is_admin']
assert_nil _parsed_body['balance']
assert _parsed_body['things']
assert _parsed_body['things'][0]['name']
assert_nil _parsed_body['things'][0]['shape']
assert_nil _parsed_body['things'][0]['price']
end
end
4 changes: 2 additions & 2 deletions test/test/unit/rest_framework_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def test_unserializable_serializer
end

def test_base_filter_get_filtered_data_not_implemented
exception = assert_raises NotImplementedError do
assert_raises NotImplementedError do
RESTFramework::BaseFilter.new(controller: nil).get_filtered_data([])
end
end

def test_base_serializer_serialize_not_implemented
exception = assert_raises NotImplementedError do
assert_raises NotImplementedError do
RESTFramework::BaseSerializer.new.serialize
end
end
Expand Down

0 comments on commit 924d981

Please sign in to comment.