Skip to content

Commit 864e810

Browse files
committed
Test against Postgres
1 parent 3145b8a commit 864e810

File tree

8 files changed

+100
-63
lines changed

8 files changed

+100
-63
lines changed

.travis.yml

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
language: ruby
22
sudo: false
3+
services:
4+
- postgresql
35
env:
4-
- "RAILS_VERSION=4.2.11"
5-
- "RAILS_VERSION=5.0.7.2"
6-
- "RAILS_VERSION=5.1.7"
7-
- "RAILS_VERSION=5.2.3"
8-
- "RAILS_VERSION=6.0.0"
9-
# - "RAILS_VERSION=master"
6+
- RAILS_VERSION=6.0.0 DATABASE_URL=postgres://postgres@localhost/jr_test
7+
- RAILS_VERSION=6.0.0
8+
- RAILS_VERSION=5.2.3 DATABASE_URL=postgres://postgres@localhost/jr_test
9+
- RAILS_VERSION=5.2.3
10+
- RAILS_VERSION=5.1.7
11+
- RAILS_VERSION=5.0.7.2
12+
- RAILS_VERSION=4.2.11
1013
rvm:
11-
- 2.3.8
12-
- 2.4.7
13-
- 2.5.6
14-
- 2.6.4
14+
- 2.4.9
15+
- 2.5.7
16+
- 2.6.5
1517
matrix:
16-
allow_failures:
17-
- env: "RAILS_VERSION=master"
1818
exclude:
19-
- rvm: 2.6.4
19+
- rvm: 2.6.5
2020
env: "RAILS_VERSION=4.2.11"
21-
- rvm: 2.3.8
22-
env: "RAILS_VERSION=6.0.0"
23-
- rvm: 2.4.7
21+
- rvm: 2.4.9
2422
env: "RAILS_VERSION=6.0.0"
23+
- rvm: 2.4.9
24+
env: "RAILS_VERSION=6.0.0 DATABASE_URL=postgres://postgres@localhost/jr_test"
25+
- rvm: 2.4.9
26+
env: "RAILS_VERSION=5.2.3 DATABASE_URL=postgres://postgres@localhost/jr_test"
2527
before_install:
26-
- gem install bundler --version 1.17.3
28+
- gem install bundler --version 1.17.3
29+
before_script:
30+
- sh -c "if [ '$DATABASE_URL' = 'postgres://postgres@localhost/jr_test' ]; then psql -c 'DROP DATABASE IF EXISTS jr_test;' -U postgres; fi"
31+
- sh -c "if [ '$DATABASE_URL' = 'postgres://postgres@localhost/jr_test' ]; then psql -c 'CREATE DATABASE jr_test;' -U postgres; fi"

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ end
99
version = ENV['RAILS_VERSION'] || 'default'
1010

1111
platforms :ruby do
12+
gem 'pg'
13+
1214
if version.start_with?('4.2', '5.0')
1315
gem 'sqlite3', '~> 1.3.13'
1416
else

test/config/database.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

test/controllers/controller_test.rb

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def set_content_type_header!
66

77
class PostsControllerTest < ActionController::TestCase
88
def setup
9+
super
910
JSONAPI.configuration.raise_if_parameters_not_allowed = true
1011
JSONAPI.configuration.always_include_to_one_linkage_data = false
1112
end
@@ -445,7 +446,7 @@ def test_sorting_asc
445446
assert_cacheable_get :index, params: {sort: 'title'}
446447

447448
assert_response :success
448-
assert_equal "A First Post", json_response['data'][0]['attributes']['title']
449+
assert_equal "A 1ST Post", json_response['data'][0]['attributes']['title']
449450
end
450451

451452
def test_sorting_desc
@@ -459,7 +460,7 @@ def test_sorting_by_multiple_fields
459460
assert_cacheable_get :index, params: {sort: 'title,body'}
460461

461462
assert_response :success
462-
assert_equal '14', json_response['data'][0]['id']
463+
assert_equal '15', json_response['data'][0]['id']
463464
end
464465

465466
def create_alphabetically_first_user_and_post
@@ -473,8 +474,15 @@ def test_sorting_by_relationship_field
473474

474475
assert_response :success
475476
assert json_response['data'].length > 10, 'there are enough records to show sort'
476-
assert_equal '17', json_response['data'][0]['id'], 'nil is at the top'
477-
assert_equal post.id.to_s, json_response['data'][1]['id'], 'alphabetically first user is second'
477+
478+
# Postgres sorts nulls last, whereas sqlite and mysql sort nulls first
479+
if ENV['DATABASE_URL'].starts_with?('postgres')
480+
assert_equal '17', json_response['data'][-1]['id'], 'nil is at the start'
481+
assert_equal post.id.to_s, json_response['data'][0]['id'], 'alphabetically first user is not first'
482+
else
483+
assert_equal '17', json_response['data'][0]['id'], 'nil is at the end'
484+
assert_equal post.id.to_s, json_response['data'][1]['id'], 'alphabetically first user is second'
485+
end
478486
end
479487

480488
def test_desc_sorting_by_relationship_field
@@ -483,8 +491,15 @@ def test_desc_sorting_by_relationship_field
483491

484492
assert_response :success
485493
assert json_response['data'].length > 10, 'there are enough records to show sort'
486-
assert_equal '17', json_response['data'][-1]['id'], 'nil is at the bottom'
487-
assert_equal post.id.to_s, json_response['data'][-2]['id'], 'alphabetically first user is second last'
494+
495+
# Postgres sorts nulls last, whereas sqlite and mysql sort nulls first
496+
if ENV['DATABASE_URL'].starts_with?('postgres')
497+
assert_equal '17', json_response['data'][0]['id'], 'nil is at the start'
498+
assert_equal post.id.to_s, json_response['data'][-1]['id']
499+
else
500+
assert_equal '17', json_response['data'][-1]['id'], 'nil is at the end'
501+
assert_equal post.id.to_s, json_response['data'][-2]['id'], 'alphabetically first user is second last'
502+
end
488503
end
489504

490505
def test_sorting_by_relationship_field_include
@@ -493,8 +508,14 @@ def test_sorting_by_relationship_field_include
493508

494509
assert_response :success
495510
assert json_response['data'].length > 10, 'there are enough records to show sort'
496-
assert_equal '17', json_response['data'][0]['id'], 'nil is at the top'
497-
assert_equal post.id.to_s, json_response['data'][1]['id'], 'alphabetically first user is second'
511+
512+
if ENV['DATABASE_URL'].starts_with?('postgres')
513+
assert_equal '17', json_response['data'][-1]['id'], 'nil is at the top'
514+
assert_equal post.id.to_s, json_response['data'][0]['id']
515+
else
516+
assert_equal '17', json_response['data'][0]['id'], 'nil is at the top'
517+
assert_equal post.id.to_s, json_response['data'][1]['id'], 'alphabetically first user is second'
518+
end
498519
end
499520

500521
def test_invalid_sort_param
@@ -3107,7 +3128,7 @@ def test_type_formatting
31073128
assert json_response['data'].is_a?(Hash)
31083129
assert_equal 'Jane Author', json_response['data']['attributes']['spouseName']
31093130
assert_equal 'First man to run across Antartica.', json_response['data']['attributes']['bio']
3110-
assert_equal 23.89/45.6, json_response['data']['attributes']['qualityRating']
3131+
assert_equal (23.89/45.6).round(5), json_response['data']['attributes']['qualityRating'].round(5)
31113132
assert_equal '47000.56', json_response['data']['attributes']['salary']
31123133
assert_equal '2013-08-07T20:25:00.000Z', json_response['data']['attributes']['dateTimeJoined']
31133134
assert_equal '1965-06-30', json_response['data']['attributes']['birthday']
@@ -4707,7 +4728,12 @@ def test_fetch_robots_with_sort_by_name
47074728
Robot.create! name: 'jane', version: 1
47084729
assert_cacheable_get :index, params: {sort: 'name'}
47094730
assert_response :success
4710-
assert_equal 'John', json_response['data'].first['attributes']['name']
4731+
4732+
if ENV['DATABASE_URL'].starts_with?('postgres')
4733+
assert_equal 'jane', json_response['data'].first['attributes']['name']
4734+
else
4735+
assert_equal 'John', json_response['data'].first['attributes']['name']
4736+
end
47114737
end
47124738

47134739
def test_fetch_robots_with_sort_by_lower_name

test/fixtures/posts.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ post_14:
8585

8686
post_15:
8787
id: 15
88-
title: AAAA First Post
88+
title: A 1ST Post
8989
body: First!!!!!!!!!
9090
author_id: 1003
9191

test/integration/requests/request_test.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,13 +1438,16 @@ def test_sort_primary_attribute
14381438
end
14391439

14401440
def test_sort_included_attribute
1441+
# Postgres sorts nulls last, whereas sqlite and mysql sort nulls first
1442+
pg = ENV['DATABASE_URL'].starts_with?('postgres')
1443+
14411444
get '/api/v6/authors?sort=author_detail.author_stuff', headers: { 'Accept' => JSONAPI::MEDIA_TYPE }
14421445
assert_jsonapi_response 200
1443-
assert_equal '1000', json_response['data'][0]['id']
1446+
assert_equal pg ? '1001' : '1000', json_response['data'][0]['id']
14441447

14451448
get '/api/v6/authors?sort=-author_detail.author_stuff', headers: { 'Accept' => JSONAPI::MEDIA_TYPE }
14461449
assert_jsonapi_response 200
1447-
assert_equal '1002', json_response['data'][0]['id']
1450+
assert_equal pg ? '1000' : '1002', json_response['data'][0]['id']
14481451
end
14491452

14501453
def test_include_parameter_quoted

test/test_helper.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
end
2222
end
2323

24+
ENV['DATABASE_URL'] ||= "sqlite3:test_db"
25+
2426
require 'active_record/railtie'
2527
require 'rails/test_help'
2628
require 'minitest/mock'
@@ -68,6 +70,9 @@ class TestApp < Rails::Application
6870
end
6971
end
7072

73+
DatabaseCleaner.allow_remote_database_url = true
74+
DatabaseCleaner.strategy = :transaction
75+
7176
module MyEngine
7277
class Engine < ::Rails::Engine
7378
isolate_namespace MyEngine
@@ -477,8 +482,6 @@ class CatResource < JSONAPI::Resource
477482
jsonapi_resources :people
478483
end
479484

480-
DatabaseCleaner.strategy = :transaction
481-
482485
# Ensure backward compatibility with Minitest 4
483486
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
484487

test/unit/active_relation_resource_finder/join_manager_test.rb

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33

44
class JoinTreeTest < ActiveSupport::TestCase
55

6+
def db_true
7+
case ActiveRecord::Base.connection.adapter_name
8+
when 'SQLite'
9+
if Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
10+
"1"
11+
else
12+
"'t'"
13+
end
14+
when 'PostgreSQL'
15+
'TRUE'
16+
end
17+
end
18+
619
def test_no_added_joins
720
join_manager = JSONAPI::ActiveRelation::JoinManager.new(resource_klass: PostResource)
821

@@ -79,11 +92,9 @@ def test_add_joins_source_relationship_with_custom_apply
7992
records = Api::V10::PostResource.records({})
8093
records = join_manager.join(records, {})
8194

82-
if Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
83-
assert_equal 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE "comments"."approved" = 1', records.to_sql
84-
else
85-
assert_equal 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE "comments"."approved" = \'t\'', records.to_sql
86-
end
95+
sql = 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" WHERE "comments"."approved" = ' + db_true
96+
97+
assert_equal sql, records.to_sql
8798

8899
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details)
89100
end
@@ -99,11 +110,9 @@ def test_add_nested_scoped_joins
99110
records = Api::V10::PostResource.records({})
100111
records = join_manager.join(records, {})
101112

102-
if Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
103-
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id" LEFT OUTER JOIN "people" "authors_comments" ON "authors_comments"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" WHERE "comments"."approved" = 1 AND "author"."special" = 1', records.to_sql
104-
else
105-
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id" LEFT OUTER JOIN "people" "authors_comments" ON "authors_comments"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" WHERE "comments"."approved" = \'t\' AND "author"."special" = \'t\'', records.to_sql
106-
end
113+
sql = 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id" LEFT OUTER JOIN "people" "authors_comments" ON "authors_comments"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" WHERE "comments"."approved" = ' + db_true + ' AND "author"."special" = ' + db_true
114+
115+
assert_equal sql, records.to_sql
107116

108117
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details)
109118
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)))
@@ -123,11 +132,10 @@ def test_add_nested_scoped_joins
123132
records = join_manager.join(records, {})
124133

125134
# Note sql is in different order, but aliases should still be right
126-
if Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
127-
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" "authors_comments" ON "authors_comments"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" WHERE "comments"."approved" = 1 AND "author"."special" = 1', records.to_sql
128-
else
129-
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" "authors_comments" ON "authors_comments"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" WHERE "comments"."approved" = \'t\' AND "author"."special" = \'t\'', records.to_sql
130-
end
135+
sql = 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" "authors_comments" ON "authors_comments"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" WHERE "comments"."approved" = ' + db_true + ' AND "author"."special" = ' + db_true
136+
137+
assert_equal sql, records.to_sql
138+
131139
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details)
132140
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)))
133141
assert_hash_equals({alias: 'authors_comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::CommentResource._relationship(:author)))
@@ -163,11 +171,9 @@ def test_add_nested_joins_with_fields
163171
records = Api::V10::PostResource.records({})
164172
records = join_manager.join(records, {})
165173

166-
if Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
167-
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id" LEFT OUTER JOIN "people" "authors_comments" ON "authors_comments"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" WHERE "comments"."approved" = 1 AND "author"."special" = 1', records.to_sql
168-
else
169-
assert_equal 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id" LEFT OUTER JOIN "people" "authors_comments" ON "authors_comments"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" WHERE "comments"."approved" = \'t\' AND "author"."special" = \'t\'', records.to_sql
170-
end
174+
sql = 'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" ON "people"."id" = "posts"."author_id" LEFT OUTER JOIN "people" "authors_comments" ON "authors_comments"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" WHERE "comments"."approved" = ' + db_true + ' AND "author"."special" = ' + db_true
175+
176+
assert_equal sql, records.to_sql
171177

172178
assert_hash_equals({alias: 'posts', join_type: :root}, join_manager.source_join_details)
173179
assert_hash_equals({alias: 'comments', join_type: :left}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)))
@@ -184,11 +190,9 @@ def test_add_joins_with_sub_relationship
184190
records = Api::V10::PostResource.records({})
185191
records = join_manager.join(records, {})
186192

187-
if Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
188-
assert_equal 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" ON "people"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" LEFT OUTER JOIN "comments" "comments_people" ON "comments_people"."author_id" = "people"."id" WHERE "comments"."approved" = 1 AND "author"."special" = 1', records.to_sql
189-
else
190-
assert_equal 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" ON "people"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" LEFT OUTER JOIN "comments" "comments_people" ON "comments_people"."author_id" = "people"."id" WHERE "comments"."approved" = \'t\' AND "author"."special" = \'t\'', records.to_sql
191-
end
193+
sql = 'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "people" ON "people"."id" = "comments"."author_id" LEFT OUTER JOIN "comments_tags" ON "comments_tags"."comment_id" = "comments"."id" LEFT OUTER JOIN "tags" ON "tags"."id" = "comments_tags"."tag_id" LEFT OUTER JOIN "comments" "comments_people" ON "comments_people"."author_id" = "people"."id" WHERE "comments"."approved" = ' + db_true + ' AND "author"."special" = ' + db_true
194+
195+
assert_equal sql, records.to_sql
192196

193197
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.source_join_details)
194198
assert_hash_equals({alias: 'comments', join_type: :inner}, join_manager.join_details_by_relationship(Api::V10::PostResource._relationship(:comments)))

0 commit comments

Comments
 (0)