Skip to content

Commit 9759c6e

Browse files
committed
Update tests and test data
1 parent f37dd10 commit 9759c6e

File tree

16 files changed

+259
-227
lines changed

16 files changed

+259
-227
lines changed

test/controllers/controller_test.rb

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def test_index_filter_with_empty_result
245245
assert_equal 0, json_response['data'].size
246246
end
247247

248-
def test_index_filter_by_id
248+
def test_index_filter_by_single_id
249249
assert_cacheable_get :index, params: {filter: {id: '1'}}
250250
assert_response :success
251251
assert json_response['data'].is_a?(Array)
@@ -266,7 +266,7 @@ def test_index_filter_with_hash_values
266266
assert_equal 1, json_response['data'].size
267267
end
268268

269-
def test_index_filter_by_ids
269+
def test_index_filter_by_array_of_ids
270270
assert_cacheable_get :index, params: {filter: {ids: '1,2'}}
271271
assert_response :success
272272
assert json_response['data'].is_a?(Array)
@@ -2050,14 +2050,14 @@ class PicturesControllerTest < ActionController::TestCase
20502050
def test_pictures_index
20512051
assert_cacheable_get :index
20522052
assert_response :success
2053-
assert_equal 7, json_response['data'].size
2053+
assert_equal 8, json_response['data'].size
20542054
end
20552055

20562056
def test_pictures_index_with_polymorphic_include_one_level
2057-
assert_cacheable_get :index, params: {include: 'imageable'}
2057+
get :index, params: {include: 'imageable'}
20582058
assert_response :success
2059-
assert_equal 7, json_response['data'].try(:size)
2060-
assert_equal 4, json_response['included'].try(:size)
2059+
assert_equal 8, json_response['data'].try(:size)
2060+
assert_equal 5, json_response['included'].try(:size)
20612061
end
20622062

20632063
def test_update_relationship_to_one_polymorphic
@@ -2075,14 +2075,14 @@ class DocumentsControllerTest < ActionController::TestCase
20752075
def test_documents_index
20762076
assert_cacheable_get :index
20772077
assert_response :success
2078-
assert_equal 4, json_response['data'].size
2078+
assert_equal 5, json_response['data'].size
20792079
end
20802080

20812081
def test_documents_index_with_polymorphic_include_one_level
20822082
assert_cacheable_get :index, params: {include: 'pictures'}
20832083
assert_response :success
2084-
assert_equal 4, json_response['data'].size
2085-
assert_equal 5, json_response['included'].size
2084+
assert_equal 5, json_response['data'].size
2085+
assert_equal 6, json_response['included'].size
20862086
end
20872087
end
20882088

@@ -3877,9 +3877,6 @@ def test_complex_includes_two_level
38773877
assert_equal 'things', json_response['included'][1]['type']
38783878
assert_equal '10001', json_response['included'][1]['relationships']['user']['data']['id']
38793879
assert_nil json_response['included'][1]['relationships']['things']['data']
3880-
3881-
assert_equal '10001', json_response['included'][2]['id']
3882-
assert_equal 'users', json_response['included'][2]['type']
38833880
end
38843881

38853882
def test_complex_includes_things_nested_things
@@ -3916,9 +3913,6 @@ def test_complex_includes_nested_things_secondary_users
39163913
assert_equal 'things', json_response['included'][1]['type']
39173914
assert_equal '10001', json_response['included'][1]['relationships']['user']['data']['id']
39183915
assert_equal '10', json_response['included'][1]['relationships']['things']['data'][0]['id']
3919-
3920-
assert_equal '10001', json_response['included'][2]['id']
3921-
assert_equal 'users', json_response['included'][2]['type']
39223916
end
39233917
end
39243918

@@ -3945,49 +3939,6 @@ def test_fields_with_delegated_attribute
39453939
end
39463940
end
39473941

3948-
class WidgetsControllerTest < ActionController::TestCase
3949-
def teardown
3950-
Widget.delete_all
3951-
Indicator.delete_all
3952-
Agency.delete_all
3953-
end
3954-
3955-
def test_fetch_widgets_sort_by_agency_name
3956-
agency_1 = Agency.create! name: 'beta'
3957-
agency_2 = Agency.create! name: 'alpha'
3958-
indicator_1 = Indicator.create! name: 'bar', agency: agency_1
3959-
indicator_2 = Indicator.create! name: 'foo', agency: agency_2
3960-
Widget.create! name: 'bar', indicator: indicator_1
3961-
widget = Widget.create! name: 'foo', indicator: indicator_2
3962-
assert_cacheable_get :index, params: {sort: 'indicator.agency.name'}
3963-
assert_response :success
3964-
assert_equal widget.id.to_s, json_response['data'].first['id']
3965-
end
3966-
end
3967-
3968-
class IndicatorsControllerTest < ActionController::TestCase
3969-
def teardown
3970-
Widget.delete_all
3971-
Indicator.delete_all
3972-
Agency.delete_all
3973-
end
3974-
3975-
def test_fetch_indicators_sort_by_widgets_name
3976-
agency = Agency.create! name: 'test'
3977-
indicator_1 = Indicator.create! name: 'bar', agency: agency
3978-
indicator_2 = Indicator.create! name: 'foo', agency: agency
3979-
Widget.create! name: 'omega', indicator: indicator_1
3980-
Widget.create! name: 'beta', indicator: indicator_1
3981-
Widget.create! name: 'alpha', indicator: indicator_2
3982-
Widget.create! name: 'zeta', indicator: indicator_2
3983-
assert_cacheable_get :index, params: {sort: 'widgets.name'}
3984-
assert_response :success
3985-
assert_equal indicator_2.id.to_s, json_response['data'].first['id']
3986-
assert_equal 2, json_response['data'].size
3987-
end
3988-
3989-
end
3990-
39913942
class RobotsControllerTest < ActionController::TestCase
39923943

39933944
def teardown
@@ -4017,5 +3968,4 @@ def test_fetch_robots_with_sort_by_version
40173968
assert_response 400
40183969
assert_equal 'version is not a valid sort criteria for robots', json_response['errors'].first['detail']
40193970
end
4020-
40213971
end

test/fixtures/active_record.rb

Lines changed: 107 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,26 @@
334334
t.string :name
335335
end
336336

337+
create_table :painters, force: true do |t|
338+
t.string :name
339+
340+
t.timestamps null: false
341+
end
342+
343+
create_table :paintings, force: true do |t|
344+
t.string :title
345+
t.string :category
346+
t.belongs_to :painter
347+
348+
t.timestamps null: false
349+
end
350+
351+
create_table :collectors, force: true do |t|
352+
t.string :name
353+
t.belongs_to :painting
354+
end
355+
356+
# special cases
337357
create_table :storages, force: true do |t|
338358
t.string :token, null: false
339359
t.string :name
@@ -433,6 +453,11 @@ class Person < ActiveRecord::Base
433453
has_one :author_detail
434454

435455
has_and_belongs_to_many :books, join_table: :book_authors
456+
has_and_belongs_to_many :not_banned_books, -> {
457+
merge(Book.not_banned)
458+
},
459+
class_name: 'Book',
460+
join_table: :book_authors
436461

437462
has_many :even_posts, -> { where('posts.id % 2 = 0') }, class_name: 'Post', foreign_key: 'author_id'
438463
has_many :odd_posts, -> { where('posts.id % 2 = 1') }, class_name: 'Post', foreign_key: 'author_id'
@@ -619,14 +644,18 @@ class Book < ActiveRecord::Base
619644
has_many :approved_book_comments, -> { where(approved: true) }, class_name: "BookComment"
620645

621646
has_and_belongs_to_many :authors, join_table: :book_authors, class_name: "Person"
647+
648+
scope :not_banned, -> {
649+
where(banned: false)
650+
}
622651
end
623652

624653
class BookComment < ActiveRecord::Base
625654
belongs_to :author, class_name: 'Person', foreign_key: 'author_id'
626655
belongs_to :book
627656

628657
def self.for_user(current_user)
629-
records = self
658+
records = self.all
630659
# Hide the unapproved comments from people who are not book admins
631660
unless current_user && current_user.book_admin
632661
records = records.where(approved: true)
@@ -688,8 +717,8 @@ class Category < ActiveRecord::Base
688717
class Picture < ActiveRecord::Base
689718
belongs_to :imageable, polymorphic: true
690719

691-
# belongs_to :document, -> { where( pictures: { imageable_type: 'Document' } ).includes( :pictures ) }, foreign_key: 'imageable_id'
692-
# belongs_to :product, -> { where( pictures: { imageable_type: 'Product' } ).includes( :pictures ) }, foreign_key: 'imageable_id'
720+
belongs_to :document, -> { where( pictures: { imageable_type: 'Document' } ).eager_load( :pictures ) }, foreign_key: 'imageable_id'
721+
belongs_to :product, -> { where( pictures: { imageable_type: 'Product' } ).eager_load( :pictures ) }, foreign_key: 'imageable_id'
693722
end
694723

695724
class Vehicle < ActiveRecord::Base
@@ -797,6 +826,19 @@ class Widget < ActiveRecord::Base
797826
class Robot < ActiveRecord::Base
798827
end
799828

829+
class Painter < ActiveRecord::Base
830+
has_many :paintings
831+
end
832+
833+
class Painting < ActiveRecord::Base
834+
belongs_to :painter
835+
has_many :collectors
836+
end
837+
838+
class Collector < ActiveRecord::Base
839+
belongs_to :painting
840+
end
841+
800842
### CONTROLLERS
801843
class SessionsController < ActionController::Base
802844
include JSONAPI::ActsAsResourceController
@@ -1014,6 +1056,9 @@ class ExpenseEntriesController < JSONAPI::ResourceController
10141056

10151057
class IsoCurrenciesController < JSONAPI::ResourceController
10161058
end
1059+
1060+
class PaintersController < JSONAPI::ResourceController
1061+
end
10171062
end
10181063

10191064
module V6
@@ -1349,6 +1394,11 @@ def title=(title)
13491394
records.where(title: values.first['title'])
13501395
}
13511396

1397+
filter 'tags.name'
1398+
1399+
filter 'comments.author.name'
1400+
filter 'comments.tags.name'
1401+
13521402
def self.updatable_fields(context)
13531403
super(context) - [:author, :subject]
13541404
end
@@ -1508,7 +1558,7 @@ class CraterResource < JSONAPI::Resource
15081558

15091559
filter :description, apply: -> (records, value, options) {
15101560
fail "context not set" unless options[:context][:current_user] != nil && options[:context][:current_user] == $test_user
1511-
records.where(concat_table_field(options[:table_alias], :description) => value)
1561+
records.where(concat_table_field(options[:related_alias], :description) => value)
15121562
}
15131563

15141564
def self.verify_key(key, context = nil)
@@ -1545,7 +1595,16 @@ class CategoryResource < JSONAPI::Resource
15451595
class PictureResource < JSONAPI::Resource
15461596
attribute :name
15471597
has_one :imageable, polymorphic: true
1548-
# has_one :imageable, polymorphic: true, polymorphic_relations: [:document, :product]
1598+
1599+
filter 'imageable.name', perform_joins: true, apply: -> (records, value, options) {
1600+
joins = options[:joins]
1601+
relationship = _relationship(:imageable)
1602+
or_parts = relationship.polymorphic_relations.collect do |relation|
1603+
table_alias = joins["imageable[#{relation}]"][:alias]
1604+
"#{concat_table_field(table_alias, "name")} = '#{value.first}'"
1605+
end
1606+
records.where(or_parts.join(' OR '))
1607+
}
15491608
end
15501609

15511610
class DocumentResource < JSONAPI::Resource
@@ -1744,33 +1803,15 @@ class AuthorResource < JSONAPI::Resource
17441803
model_name 'Person'
17451804
attributes :name
17461805

1747-
has_many :books, inverse_relationship: :authors,
1748-
custom_methods: {
1749-
apply_join: -> (options) {
1750-
relationship = options[:relationship]
1751-
relation_name = relationship.relation_name(options[:options])
1752-
1753-
records = options[:records].joins(relation_name).references(relation_name)
1754-
1755-
unless options[:context][:current_user].try(:book_admin)
1756-
records = records.where("#{relation_name}.banned" => false)
1757-
end
1758-
records
1759-
}
1760-
}
1806+
has_many :books, inverse_relationship: :authors, relation_name: -> (options) {
1807+
if options[:context][:current_user].try(:book_admin)
1808+
:books
1809+
else
1810+
:not_banned_books
1811+
end
1812+
}
17611813

17621814
has_many :book_comments
1763-
1764-
def records_for(rel_name)
1765-
records = _model.public_send(rel_name)
1766-
if rel_name == :books
1767-
# Hide indirect access to banned books unless current user is a book admin
1768-
unless context[:current_user].try(:book_admin)
1769-
records = records.where(banned: false)
1770-
end
1771-
end
1772-
return records
1773-
end
17741815
end
17751816

17761817
class BookResource < JSONAPI::Resource
@@ -1804,6 +1845,7 @@ class BookResource < JSONAPI::Resource
18041845
:book_comments
18051846
end
18061847

1848+
# Using an inner join here, which is different than the new default left_join
18071849
return records.joins(relation).references(relation).where('book_comments.id' => value)
18081850
}
18091851

@@ -1944,6 +1986,36 @@ class AuthorDetailResource < JSONAPI::Resource
19441986
attributes :author_stuff
19451987
end
19461988

1989+
class PaintingResource < JSONAPI::Resource
1990+
model_name 'Painting'
1991+
attributes :title, :category #, :collector_roster
1992+
has_one :painter
1993+
has_many :collectors
1994+
1995+
filter :title
1996+
filter :category
1997+
filter :collectors
1998+
1999+
def collector_roster
2000+
collectors.map(&:name)
2001+
end
2002+
end
2003+
2004+
class CollectorResource < JSONAPI::Resource
2005+
attributes :name
2006+
has_one :painting
2007+
end
2008+
2009+
class PainterResource < JSONAPI::Resource
2010+
model_name 'Painter'
2011+
attributes :name
2012+
has_many :paintings
2013+
2014+
filter :name, apply: lambda { |records, value, options|
2015+
records.where('name LIKE ?', value)
2016+
}
2017+
end
2018+
19472019
class PersonResource < PersonResource; end
19482020
class PostResource < PostResource; end
19492021
class TagResource < TagResource; end
@@ -2262,23 +2334,18 @@ def show
22622334
module Api
22632335
class BoxResource < JSONAPI::Resource
22642336
has_many :things
2337+
2338+
filter 'things.things.name'
2339+
filter 'things.name'
22652340
end
22662341

22672342
class ThingResource < JSONAPI::Resource
22682343
has_one :box
22692344
has_one :user
22702345

2271-
has_many :things,
2272-
custom_methods: {
2273-
apply_join: -> (options) {
2274-
table_alias = "aliased_#{options[:table_alias]}"
2275-
options[:table_alias] = table_alias
2276-
2277-
join_stmt = "LEFT OUTER JOIN related_things related_things_#{table_alias} ON related_things_#{table_alias}.from_id = things.id LEFT OUTER JOIN things \"#{table_alias}\" ON \"#{table_alias}\".id = related_things_#{table_alias}.to_id"
2346+
has_many :things
22782347

2279-
return options[:records].joins(join_stmt)
2280-
}
2281-
}
2348+
filter 'things.things.name'
22822349
end
22832350

22842351
class UserResource < JSONAPI::Resource

test/fixtures/boxes.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
box_100:
22
id: 100
3+
4+
box_102:
5+
id: 102

0 commit comments

Comments
 (0)