praxis 2.0.pre.1 → 2.0.pre.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +0 -1
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +5 -20
  5. data/CHANGELOG.md +32 -0
  6. data/Gemfile +1 -1
  7. data/Guardfile +2 -1
  8. data/Rakefile +1 -7
  9. data/TODO.md +28 -0
  10. data/lib/api_browser/package-lock.json +7110 -0
  11. data/lib/praxis.rb +7 -4
  12. data/lib/praxis/action_definition.rb +10 -17
  13. data/lib/praxis/action_definition/headers_dsl_compiler.rb +1 -1
  14. data/lib/praxis/api_general_info.rb +21 -0
  15. data/lib/praxis/application.rb +2 -3
  16. data/lib/praxis/bootloader_stages/routing.rb +2 -4
  17. data/lib/praxis/config.rb +1 -1
  18. data/lib/praxis/docs/generator.rb +11 -6
  19. data/lib/praxis/docs/open_api_generator.rb +255 -0
  20. data/lib/praxis/docs/openapi/info_object.rb +31 -0
  21. data/lib/praxis/docs/openapi/media_type_object.rb +59 -0
  22. data/lib/praxis/docs/openapi/operation_object.rb +40 -0
  23. data/lib/praxis/docs/openapi/parameter_object.rb +69 -0
  24. data/lib/praxis/docs/openapi/paths_object.rb +58 -0
  25. data/lib/praxis/docs/openapi/request_body_object.rb +51 -0
  26. data/lib/praxis/docs/openapi/response_object.rb +63 -0
  27. data/lib/praxis/docs/openapi/responses_object.rb +44 -0
  28. data/lib/praxis/docs/openapi/schema_object.rb +87 -0
  29. data/lib/praxis/docs/openapi/server_object.rb +24 -0
  30. data/lib/praxis/docs/openapi/tag_object.rb +21 -0
  31. data/lib/praxis/extensions/attribute_filtering.rb +2 -0
  32. data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +148 -157
  33. data/lib/praxis/extensions/attribute_filtering/active_record_patches.rb +15 -0
  34. data/lib/praxis/extensions/attribute_filtering/active_record_patches/5x.rb +90 -0
  35. data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_0.rb +68 -0
  36. data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_1_plus.rb +58 -0
  37. data/lib/praxis/extensions/attribute_filtering/filter_tree_node.rb +35 -0
  38. data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +13 -12
  39. data/lib/praxis/extensions/attribute_filtering/sequel_filter_query_builder.rb +3 -2
  40. data/lib/praxis/extensions/field_selection/active_record_query_selector.rb +24 -30
  41. data/lib/praxis/extensions/field_selection/field_selector.rb +4 -0
  42. data/lib/praxis/extensions/field_selection/sequel_query_selector.rb +32 -39
  43. data/lib/praxis/extensions/pagination.rb +130 -0
  44. data/lib/praxis/extensions/pagination/active_record_pagination_handler.rb +42 -0
  45. data/lib/praxis/extensions/pagination/header_generator.rb +70 -0
  46. data/lib/praxis/extensions/pagination/ordering_params.rb +234 -0
  47. data/lib/praxis/extensions/pagination/pagination_handler.rb +68 -0
  48. data/lib/praxis/extensions/pagination/pagination_params.rb +374 -0
  49. data/lib/praxis/extensions/pagination/sequel_pagination_handler.rb +45 -0
  50. data/lib/praxis/handlers/json.rb +2 -0
  51. data/lib/praxis/handlers/www_form.rb +5 -0
  52. data/lib/praxis/handlers/{xml.rb → xml-sample.rb} +6 -0
  53. data/lib/praxis/links.rb +4 -0
  54. data/lib/praxis/mapper/active_model_compat.rb +57 -4
  55. data/lib/praxis/mapper/resource.rb +18 -11
  56. data/lib/praxis/mapper/selector_generator.rb +99 -75
  57. data/lib/praxis/mapper/sequel_compat.rb +43 -3
  58. data/lib/praxis/media_type.rb +1 -56
  59. data/lib/praxis/media_type_identifier.rb +2 -1
  60. data/lib/praxis/multipart/part.rb +5 -2
  61. data/lib/praxis/notifications.rb +1 -1
  62. data/lib/praxis/plugins/mapper_plugin.rb +17 -3
  63. data/lib/praxis/plugins/pagination_plugin.rb +71 -0
  64. data/lib/praxis/resource_definition.rb +8 -16
  65. data/lib/praxis/response_definition.rb +1 -1
  66. data/lib/praxis/route.rb +3 -5
  67. data/lib/praxis/routing_config.rb +3 -7
  68. data/lib/praxis/tasks/api_docs.rb +23 -0
  69. data/lib/praxis/tasks/routes.rb +9 -14
  70. data/lib/praxis/trait.rb +1 -1
  71. data/lib/praxis/types/media_type_common.rb +12 -2
  72. data/lib/praxis/types/multipart.rb +1 -1
  73. data/lib/praxis/types/multipart_array.rb +64 -2
  74. data/lib/praxis/types/multipart_array/part_definition.rb +1 -1
  75. data/lib/praxis/validation_handler.rb +1 -2
  76. data/lib/praxis/version.rb +1 -1
  77. data/praxis.gemspec +11 -9
  78. data/spec/functional_spec.rb +9 -6
  79. data/spec/praxis/action_definition_spec.rb +4 -16
  80. data/spec/praxis/api_general_info_spec.rb +6 -6
  81. data/spec/praxis/application_spec.rb +1 -1
  82. data/spec/praxis/collection_spec.rb +3 -2
  83. data/spec/praxis/config_spec.rb +2 -2
  84. data/spec/praxis/extensions/attribute_filtering/active_record_filter_query_builder_spec.rb +304 -0
  85. data/spec/praxis/extensions/attribute_filtering/filter_tree_node_spec.rb +39 -0
  86. data/spec/praxis/extensions/attribute_filtering/filtering_params_spec.rb +34 -0
  87. data/spec/praxis/extensions/field_expansion_spec.rb +6 -24
  88. data/spec/praxis/extensions/field_selection/active_record_query_selector_spec.rb +110 -0
  89. data/spec/praxis/extensions/field_selection/sequel_query_selector_spec.rb +148 -0
  90. data/spec/praxis/extensions/pagination/active_record_pagination_handler_spec.rb +130 -0
  91. data/spec/praxis/extensions/support/spec_resources_active_model.rb +173 -0
  92. data/spec/praxis/extensions/support/spec_resources_sequel.rb +106 -0
  93. data/spec/praxis/mapper/selector_generator_spec.rb +306 -282
  94. data/spec/praxis/media_type_spec.rb +5 -139
  95. data/spec/praxis/middleware_app_spec.rb +1 -1
  96. data/spec/praxis/request_spec.rb +3 -22
  97. data/spec/praxis/request_stages/action_spec.rb +8 -1
  98. data/spec/praxis/resource_definition_spec.rb +1 -1
  99. data/spec/praxis/response_definition_spec.rb +15 -13
  100. data/spec/praxis/response_spec.rb +1 -1
  101. data/spec/praxis/route_spec.rb +2 -9
  102. data/spec/praxis/router_spec.rb +1 -1
  103. data/spec/praxis/routing_config_spec.rb +4 -13
  104. data/spec/praxis/types/multipart_array_spec.rb +4 -21
  105. data/spec/spec_app/app/controllers/instances.rb +1 -1
  106. data/spec/spec_app/config/environment.rb +0 -2
  107. data/spec/spec_app/design/api.rb +7 -1
  108. data/spec/spec_app/design/media_types/instance.rb +0 -8
  109. data/spec/spec_app/design/media_types/volume.rb +0 -12
  110. data/spec/spec_app/design/resources/instances.rb +1 -2
  111. data/spec/spec_helper.rb +17 -0
  112. data/spec/support/be_deep_equal_matcher.rb +39 -0
  113. data/spec/support/spec_media_types.rb +0 -73
  114. data/spec/support/spec_resources.rb +42 -49
  115. metadata +91 -56
  116. data/spec/praxis/handlers/xml_spec.rb +0 -177
  117. data/spec/praxis/links_spec.rb +0 -68
  118. data/spec/spec_app/app/models/person.rb +0 -3
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ require 'praxis/extensions/attribute_filtering'
4
+
5
+ describe Praxis::Extensions::AttributeFiltering::FilterTreeNode do
6
+
7
+ let(:filters) do
8
+ [
9
+ {name: 'one', specs: { op: '>', value: 1}},
10
+ {name: 'one', specs: { op: '<', value: 10}},
11
+ {name: 'rel1.a1', specs: { op: '=', value: 1}},
12
+ {name: 'rel1.a2', specs: { op: '=', value: 2}},
13
+ {name: 'rel1.rel2.b1', specs: { op: '=', value: 11}},
14
+ {name: 'rel1.rel2.b2', specs: { op: '=', value: 12}}
15
+ ]
16
+ end
17
+ context 'initialization' do
18
+ subject { described_class.new(filters) }
19
+ it 'holds the top conditions and the child in a TreeNode' do
20
+ expect(subject.path).to eq([])
21
+ expect(subject.conditions.size).to eq(2)
22
+ expect(subject.children.keys).to eq(['rel1'])
23
+ expect(subject.children['rel1']).to be_kind_of(described_class)
24
+ end
25
+
26
+ it 'recursively holds the conditions and the children of their children in a TreeNode' do
27
+ rel1 = subject.children['rel1']
28
+ expect(rel1.path).to eq(['rel1'])
29
+ expect(rel1.conditions.size).to eq(2)
30
+ expect(rel1.children.keys).to eq(['rel2'])
31
+ expect(rel1.children['rel2']).to be_kind_of(described_class)
32
+
33
+ rel1rel2 = rel1.children['rel2']
34
+ expect(rel1rel2.path).to eq(['rel1','rel2'])
35
+ expect(rel1rel2.conditions.size).to eq(2)
36
+ expect(rel1rel2.children.keys).to be_empty
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ require 'praxis/extensions/attribute_filtering'
4
+
5
+ describe Praxis::Extensions::AttributeFiltering::FilteringParams do
6
+
7
+ context '.load' do
8
+ subject { described_class.load(filters_string) }
9
+ context 'parses for operator' do
10
+ described_class::AVAILABLE_OPERATORS.each do |op|
11
+ it "#{op}" do
12
+ str = "thename#{op}thevalue"
13
+ parsed = [{ name: :thename, op: op, value: 'thevalue'}]
14
+ expect(described_class.load(str).parsed_array).to eq(parsed)
15
+ end
16
+ end
17
+ end
18
+ context 'with all operators at once' do
19
+ let(:filters_string) { 'one=1&two!=2&three>=3&four<=4&five<5&six>6&seven!&eight!!'}
20
+ it do
21
+ expect(subject.parsed_array).to eq([
22
+ { name: :one, op: '=', value: '1'},
23
+ { name: :two, op: '!=', value: '2'},
24
+ { name: :three, op: '>=', value: '3'},
25
+ { name: :four, op: '<=', value: '4'},
26
+ { name: :five, op: '<', value: '5'},
27
+ { name: :six, op: '>', value: '6'},
28
+ { name: :seven, op: '!', value: nil},
29
+ { name: :eight, op: '!!', value: nil},
30
+ ])
31
+ end
32
+ end
33
+ end
34
+ end
@@ -47,15 +47,10 @@ describe Praxis::Extensions::FieldExpansion do
47
47
 
48
48
  context 'and no fields provided' do
49
49
  it 'returns the fields for the default view' do
50
- expect(expansion).to eq({id: true, name: true, links: [true]})
50
+ expect(expansion).to eq({id: true, name: true})
51
51
  end
52
52
 
53
- context 'and a view' do
54
- let(:view) { :link }
55
- it 'expands the fields on the view' do
56
- expect(expansion).to eq({id: true, name: true, href: true})
57
- end
58
- end
53
+ pending 'and a view'
59
54
  end
60
55
 
61
56
  context 'with a set of fields provided' do
@@ -65,15 +60,7 @@ describe Praxis::Extensions::FieldExpansion do
65
60
  expect(expansion).to eq expected
66
61
  end
67
62
 
68
- context 'and a view' do
69
- let(:view) { :link }
70
- let(:fields) { 'id,href' }
71
-
72
- it 'returns the subset of fields that exist for the view' do
73
- expected = {id: true, href: true }
74
- expect(expansion).to eq expected
75
- end
76
- end
63
+ pending 'and a view'
77
64
  end
78
65
  end
79
66
 
@@ -81,15 +68,10 @@ describe Praxis::Extensions::FieldExpansion do
81
68
  let(:test_attributes) { {view: true} }
82
69
 
83
70
  it 'returns the fields for the default view' do
84
- expect(expansion).to eq({id: true, name: true, links: [true]})
71
+ expect(expansion).to eq({id: true, name: true})
85
72
  end
86
73
 
87
- context 'and a view' do
88
- let(:view) { :link }
89
- it 'expands the fields on the view' do
90
- expect(expansion).to eq({id: true, name: true, href: true})
91
- end
92
- end
74
+ pending 'and a view'
93
75
  end
94
76
 
95
77
 
@@ -98,7 +80,7 @@ describe Praxis::Extensions::FieldExpansion do
98
80
  let(:fields){ nil }
99
81
  let(:view){ nil }
100
82
  it 'ignores incoming parameters and expands for the default view' do
101
- expect(expansion).to eq({id: true, name: true, links: [true]})
83
+ expect(expansion).to eq({id: true, name: true})
102
84
  end
103
85
  end
104
86
  end
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+
3
+ require_relative '../support/spec_resources_active_model.rb'
4
+
5
+ describe Praxis::Extensions::FieldSelection::ActiveRecordQuerySelector do
6
+ let(:selector_fields) do
7
+ {
8
+ name: true,
9
+ author: {
10
+ id: true,
11
+ books: true
12
+ },
13
+ category: {
14
+ name: true,
15
+ books: true
16
+ },
17
+ tags: {
18
+ name: true
19
+ }
20
+ }
21
+ end
22
+ let(:expected_select_from_to_query) do
23
+ # The columns to select from the top Simple model
24
+ [
25
+ :simple_name, # from the :name alias
26
+ :author_id, # the FK needed for the author association
27
+ :added_column, # from the extra column defined in the parent property
28
+ :category_uuid, # the FK needed for the cateory association
29
+ :id # We always load the primary keys
30
+ ]
31
+ end
32
+ let(:selector_node) { Praxis::Mapper::SelectorGenerator.new.add(ActiveBookResource,selector_fields) }
33
+ let(:debug){ false }
34
+
35
+ subject(:selector) {described_class.new(query: query, selectors: selector_node, debug: debug) }
36
+ context '#generate with a mocked' do
37
+ let(:query) { double("Query") }
38
+ it 'calls the select columns for the top level, and includes the right association hashes' do
39
+ expect(query).to receive(:select).with(*expected_select_from_to_query).and_return(query)
40
+ expected_includes = {
41
+ author: {
42
+ books: {}
43
+ },
44
+ category: {
45
+ books: {}
46
+ },
47
+ tags: {}
48
+ }
49
+ expect(query).to receive(:includes).with(expected_includes).and_return(query)
50
+ expect(subject).to_not receive(:explain_query)
51
+ subject.generate
52
+ end
53
+ context 'when debug is enabled' do
54
+ let(:debug){ true }
55
+ it 'calls the explain method' do
56
+ expect(query).to receive(:select).and_return(query)
57
+ expect(query).to receive(:includes).and_return(query)
58
+ expect(subject).to receive(:explain_query)
59
+ subject.generate
60
+ end
61
+ end
62
+ end
63
+
64
+ context '#generate with a real AR model' do
65
+ let(:query) { ActiveBook }
66
+
67
+ it 'calls the select columns for the top level, and includes the right association hashes' do
68
+ expected_includes = {
69
+ author: {
70
+ books: {}
71
+ },
72
+ category: {
73
+ books: {}
74
+ },
75
+ tags: {}
76
+ }
77
+ #expect(query).to receive(:includes).with(expected_includes).and_return(query)
78
+ expect(subject).to_not receive(:explain_query)
79
+ final_query = subject.generate
80
+ expect(final_query.select_values).to match_array(expected_select_from_to_query)
81
+ # Our query selector always uses a single hash tree from the top, not an array of things
82
+ includes_hash = final_query.includes_values.first
83
+ expect(includes_hash).to match(expected_includes)
84
+ # Also, make AR do the actual query to make sure everything is wired up correctly
85
+ result = final_query.to_a
86
+ expect(result.size).to be > 2 # We are using 2 but we've seeded more
87
+ book1 = result[0]
88
+ book2 = result[1]
89
+ expect(book1.author.id).to eq 11
90
+ expect(book1.author.books.size).to eq 1
91
+ expect(book1.author.books.map(&:simple_name)).to eq(['Book1'])
92
+ expect(book1.category.name).to eq 'cat1'
93
+ expect(book1.tags.map(&:name)).to match_array(['blue','red','green'])
94
+
95
+ expect(book2.author.id).to eq 22
96
+ expect(book2.author.books.size).to eq 1
97
+ expect(book2.author.books.map(&:simple_name)).to eq(['Book2'])
98
+ expect(book2.category.name).to eq 'cat2'
99
+ expect(book2.tags.map(&:name)).to match_array(['red'])
100
+ end
101
+
102
+ it 'calls the explain debug method if enabled' do
103
+ suppress_output do
104
+ # Actually make it run all the way...but suppressing the output
105
+ subject.generate
106
+ end
107
+ end
108
+ end
109
+
110
+ end
@@ -0,0 +1,148 @@
1
+ require 'spec_helper'
2
+ require 'sequel'
3
+
4
+ require 'praxis/extensions/field_selection/sequel_query_selector'
5
+
6
+
7
+ describe Praxis::Extensions::FieldSelection::SequelQuerySelector do
8
+ class Q
9
+ attr_reader :object, :cols
10
+ def initialize
11
+ @object = {}
12
+ @cols = []
13
+ end
14
+ def eager(hash)
15
+ raise "we are only calling eager one at a time!" if hash.keys.size > 1
16
+ name = hash.keys.first
17
+ # Actually call the incoming proc with an instance of Q, to collect the further select/eager calls
18
+ @object[name] = hash[name].call(Q.new)
19
+ self
20
+ end
21
+ def select(*names)
22
+ @cols += names.map(&:column)
23
+ self
24
+ end
25
+ def dump
26
+ eagers = @object.each_with_object({}) do |(name, val), hash|
27
+ hash[name] = val.dump
28
+ end
29
+ {
30
+ columns: @cols,
31
+ eagers: eagers
32
+ }
33
+ end
34
+ end
35
+
36
+
37
+ # Pay the price for creating and connecting only in this spec instead in spec helper
38
+ # this way all other specs do not need to be slower and it's a better TDD experience
39
+
40
+ require_relative '../support/spec_resources_sequel.rb'
41
+
42
+ let(:selector_fields) do
43
+ {
44
+ name: true,
45
+ other_model: {
46
+ id: true
47
+ },
48
+ parent: {
49
+ children: true
50
+ },
51
+ tags: {
52
+ tag_name: true
53
+ }
54
+ }
55
+ end
56
+ let(:expected_select_from_to_query) do
57
+ # The columns to select from the top Simple model
58
+ [
59
+ :simple_name, # from the :name alias
60
+ :added_column, # from the extra column defined in the parent property
61
+ :id, # We always load the primary keys
62
+ :other_model_id, # the FK needed for the other_model association
63
+ :parent_id # the FK needed for the parent association
64
+ ]
65
+ end
66
+
67
+ let(:selector_node) { Praxis::Mapper::SelectorGenerator.new.add(SequelSimpleResource,selector_fields) }
68
+ subject {described_class.new(query: query, selectors: selector_node, debug: debug) }
69
+
70
+ context 'generate' do
71
+ let(:debug) { false }
72
+ context 'using the real models and DB' do
73
+ let(:query) { SequelSimpleModel }
74
+
75
+ it 'calls the select columns for the top level, and includes the right association hashes' do
76
+ ds = subject.generate
77
+ opts = ds.opts
78
+ # Top model is our simplemodel
79
+ expect(opts[:model]).to be(SequelSimpleModel)
80
+ selected_column_names = opts[:select].map(&:column)
81
+ expect(selected_column_names).to match_array(expected_select_from_to_query)
82
+ # 2 Eager loaded associations as well
83
+ expect(opts[:eager].keys).to match_array([:other_model, :parent, :tags])
84
+ # We can not introspect those eagers, as they are procs...but at least validate they are
85
+ expect(opts[:eager][:other_model]).to be_a Proc
86
+ expect(opts[:eager][:parent]).to be_a Proc
87
+
88
+ # Also, let's make sure the query actually works by making Sequel attempt to retrieve it and finding the right things.
89
+ result = ds.all
90
+ # 2 simple models
91
+ expect(result.size).to be 2
92
+ # First simple model points to other_model 11 and parent 1
93
+ simple_one = result.find{|i| i.id == 1}
94
+ expect(simple_one.other_model.id).to be 11
95
+ expect(simple_one.parent.id).to be 1
96
+ # also, its' parent in turn has 2 children (1 and 2) linked by its parent_uuid
97
+ expect(simple_one.parent.children.map(&:id)).to match_array([1,2])
98
+ # Has the blue and red tags
99
+ expect(simple_one.tags.map(&:tag_name)).to match_array(['blue','red'])
100
+
101
+ # second simple model points to other_model 22 and parent 2
102
+ simple_two = result.find{|i| i.id == 2}
103
+ expect(simple_two.other_model.id).to be 22
104
+ expect(simple_two.parent.id).to be 2
105
+ # also, its' parent in turn has no children (as no simple models point to it by uuid)
106
+ expect(simple_two.parent.children.map(&:id)).to be_empty
107
+ # Also has the red tag
108
+ expect(simple_two.tags.map(&:tag_name)).to match_array(['red'])
109
+ end
110
+ it 'calls the explain debug method if enabled' do
111
+ suppress_output do
112
+ # Actually make it run all the way...but suppressing the output
113
+ subject.generate
114
+ end
115
+ end
116
+ end
117
+ context 'just mocking the query' do
118
+ let(:query) { Q.new }
119
+
120
+ it 'creates the right recursive lambdas for the eager loading' do
121
+
122
+ ds = subject.generate
123
+ result = ds.dump
124
+ expect(result[:columns]).to match_array(expected_select_from_to_query)
125
+ # 2 eager loads
126
+ expect(result[:eagers].keys).to match_array([:other_model, :parent, :tags])
127
+ # 1 - other model
128
+ other_model_eagers = result[:eagers][:other_model]
129
+ expect(other_model_eagers[:columns]).to match_array([:id])
130
+
131
+ # 2 - parent association
132
+ parent_eagers = result[:eagers][:parent]
133
+ expect(parent_eagers[:columns]).to match_array([:id,:uuid]) # uuid is necessary for the "children" assoc
134
+ expect(parent_eagers[:eagers].keys).to match_array([:children])
135
+ # 2.1 - children association off of the parent
136
+ parent_children_eagers = parent_eagers[:eagers][:children]
137
+ expect(parent_children_eagers[:columns]).to match_array([:id,:parent_uuid]) # parent_uuid is required for the assoc
138
+ expect(parent_children_eagers[:eagers]).to be_empty
139
+
140
+ # 3 - tags association
141
+ tags_eagers = result[:eagers][:tags]
142
+ expect(tags_eagers[:columns]).to match_array([:id, :tag_name]) # uuid is necessary for the "children" assoc
143
+ expect(tags_eagers[:eagers].keys).to be_empty
144
+
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ require_relative '../support/spec_resources_active_model.rb'
4
+ require 'praxis/extensions/pagination'
5
+
6
+ class Book < Praxis::MediaType
7
+ attributes do
8
+ attribute :id, Integer
9
+ attribute :simple_name, String
10
+ attribute :category_uuid, String
11
+ end
12
+ end
13
+ Book.finalize!
14
+ BookPaginationParamsAttribute = Attributor::Attribute.new(Praxis::Types::PaginationParams.for(Book)) do
15
+ max_items 3
16
+ page_size 2
17
+ # disallow :paging
18
+ default by: :id
19
+ end
20
+
21
+ BookOrderingParamsAttribute = Attributor::Attribute.new(Praxis::Types::OrderingParams.for(Book)) do
22
+ enforce_for :all
23
+ end
24
+
25
+ describe Praxis::Extensions::Pagination::ActiveRecordPaginationHandler do
26
+ shared_examples 'sorts_the_same' do |op, expected|
27
+ let(:order_params) { BookOrderingParamsAttribute.load(op) }
28
+ it do
29
+ loaded_ids = subject.all.map(&:id)
30
+ expected_ids = expected.all.map(&:id)
31
+ expect(loaded_ids).to eq(expected_ids)
32
+ end
33
+ end
34
+
35
+ shared_examples 'paginates_the_same' do |par, expected|
36
+ let(:paginator_params) { BookPaginationParamsAttribute.load(par) }
37
+ it do
38
+ loaded_ids = subject.all.map(&:id)
39
+ expected_ids = expected.all.map(&:id)
40
+ expect(loaded_ids).to eq(expected_ids)
41
+ end
42
+ end
43
+
44
+ let(:query) { ActiveBook }
45
+ let(:table) { ActiveBook.table_name }
46
+ let(:paginator_params) { nil }
47
+ let(:order_params) { nil }
48
+ let(:pagination) do
49
+ Praxis::Extensions::Pagination::PaginationStruct.new(paginator_params, order_params)
50
+ end
51
+
52
+
53
+ context '.paginate' do
54
+ subject {described_class.paginate(query, pagination) }
55
+
56
+ context 'empty struct' do
57
+ let(:paginator_params) { nil }
58
+
59
+ it 'does not change the query with an empty struct' do
60
+ expect(subject).to be(query)
61
+ end
62
+ end
63
+
64
+ context 'page-based' do
65
+ it_behaves_like 'paginates_the_same', 'page=1,items=3',
66
+ ::ActiveBook.limit(3)
67
+ it_behaves_like 'paginates_the_same', 'page=2,items=3',
68
+ ::ActiveBook.offset(3).limit(3)
69
+ end
70
+
71
+ context 'page-based with defaults' do
72
+ it_behaves_like 'paginates_the_same', '',
73
+ ::ActiveBook.offset(0).limit(2)
74
+ it_behaves_like 'paginates_the_same', 'page=2',
75
+ ::ActiveBook.offset(2).limit(2)
76
+ end
77
+
78
+ context 'cursor-based' do
79
+ it_behaves_like 'paginates_the_same', 'by=id,items=3',
80
+ ::ActiveBook.limit(3).order(id: :asc)
81
+ it_behaves_like 'paginates_the_same', 'by=id,from=1000,items=3',
82
+ ::ActiveBook.where("id > 1000").limit(3).order(id: :asc)
83
+ it_behaves_like 'paginates_the_same', 'by=simple_name,from=Book1000,items=3',
84
+ ::ActiveBook.where("simple_name > 'Book1000'").limit(3).order(simple_name: :asc)
85
+ end
86
+
87
+ context 'cursor-based with defaults' do
88
+ it_behaves_like 'paginates_the_same', '',
89
+ ::ActiveBook.limit(2).order(id: :asc)
90
+ it_behaves_like 'paginates_the_same', 'by=id,from=1000',
91
+ ::ActiveBook.where("id > 1000").limit(2).order(id: :asc)
92
+ end
93
+
94
+ context 'including order' do
95
+ let(:order_params) { BookOrderingParamsAttribute.load(op_string) }
96
+
97
+ context 'when compatible with cursor' do
98
+ let(:op_string){ 'id'}
99
+ # Compatible cursor field
100
+ it_behaves_like 'paginates_the_same', 'by=id,items=3',
101
+ ::ActiveBook.limit(3).order(id: :asc)
102
+ end
103
+
104
+ context 'when incompatible with cursor' do
105
+ let(:op_string){ 'id'}
106
+ let(:paginator_params) { BookPaginationParamsAttribute.load('by=simple_name,items=3') }
107
+ it do
108
+ expect{subject.all}.to raise_error(described_class::PaginationException, /is incompatible with pagination/)
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ context '.order' do
115
+ subject {described_class.order(query, pagination.order) }
116
+
117
+ it 'does not change the query with an empty struct' do
118
+ expect(subject).to be(query)
119
+ end
120
+
121
+ it_behaves_like 'sorts_the_same', 'simple_name',
122
+ ::ActiveBook.order(simple_name: :asc)
123
+ it_behaves_like 'sorts_the_same', '-simple_name',
124
+ ::ActiveBook.order(simple_name: :desc)
125
+ it_behaves_like 'sorts_the_same', '-simple_name,id',
126
+ ::ActiveBook.order(simple_name: :desc, id: :asc)
127
+ it_behaves_like 'sorts_the_same', '-simple_name,-id',
128
+ ::ActiveBook.order(simple_name: :desc, id: :desc)
129
+ end
130
+ end