Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix db state leaking across tests #1384

Merged
merged 2 commits into from
Dec 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 29 additions & 36 deletions test/grape_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,42 @@

class ActiveModelSerializers::GrapeTest < Minitest::Test
include Rack::Test::Methods
module Models
def self.model1
ARModels::Post.new(id: 1, title: 'Dummy Title', body: 'Lorem Ipsum')
end

def self.model2
ARModels::Post.new(id: 2, title: 'Second Dummy Title', body: 'Second Lorem Ipsum')
end

def self.all
@all ||=
begin
model1.save!
model2.save!
ARModels::Post.all
end
end
end

class GrapeTest < Grape::API
format :json
include Grape::ActiveModelSerializers

resources :grape do
get '/render' do
render ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
render Models.model1
end

get '/render_with_json_api' do
post = ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
post = Models.model1
render post, meta: { page: 1, total_pages: 2 }, adapter: :json_api
end

get '/render_array_with_json_api' do
post = ARModels::Post.create(title: 'Dummy Title', body: 'Lorem Ipsum')
post.dup.save
render ARModels::Post.all, adapter: :json_api
posts = Models.all
render posts, adapter: :json_api
end
end
end
Expand All @@ -34,7 +51,7 @@ def app
def test_formatter_returns_json
get '/grape/render'

post = ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
post = Models.model1
serializable_resource = serializable(post)

assert last_response.ok?
Expand All @@ -44,7 +61,7 @@ def test_formatter_returns_json
def test_render_helper_passes_through_options_correctly
get '/grape/render_with_json_api'

post = ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
post = Models.model1
serializable_resource = serializable(post, serializer: ARModels::PostSerializer, adapter: :json_api, meta: { page: 1, total_pages: 2 })

assert last_response.ok?
Expand All @@ -54,36 +71,12 @@ def test_render_helper_passes_through_options_correctly
def test_formatter_handles_arrays
get '/grape/render_array_with_json_api'

expected = {
'data' => [
{
id: '1',
type: 'ar_models_posts',
attributes: {
title: 'Dummy Title',
body: 'Lorem Ipsum'
},
relationships: {
comments: { data: [] },
author: { data: nil }
}
},
{
id: '2',
type: 'ar_models_posts',
attributes: {
title: 'Dummy Title',
body: 'Lorem Ipsum'
},
relationships: {
comments: { data: [] },
author: { data: nil }
}
}
]
}
posts = Models.all
serializable_resource = serializable(posts, adapter: :json_api)

assert last_response.ok?
assert_equal expected.to_json, last_response.body
assert_equal serializable_resource.to_json, last_response.body
ensure
ARModels::Post.delete_all
end
end
3 changes: 3 additions & 0 deletions test/serializers/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ def test_virtual_attribute_block
}

assert_equal expected, actual
ensure
::ARModels::Post.delete_all
::ARModels::Comment.delete_all
end

class NamespacedResourcesTest < Minitest::Test
Expand Down