praxis 2.0.pre.3 → 2.0.pre.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +5 -5
  2. data/.rspec +0 -1
  3. data/.ruby-version +1 -0
  4. data/CHANGELOG.md +26 -0
  5. data/Gemfile +1 -1
  6. data/Guardfile +2 -1
  7. data/Rakefile +1 -7
  8. data/TODO.md +28 -0
  9. data/lib/api_browser/package-lock.json +7110 -0
  10. data/lib/praxis.rb +7 -4
  11. data/lib/praxis/action_definition.rb +9 -16
  12. data/lib/praxis/api_general_info.rb +21 -0
  13. data/lib/praxis/application.rb +1 -2
  14. data/lib/praxis/bootloader_stages/routing.rb +2 -4
  15. data/lib/praxis/docs/generator.rb +11 -6
  16. data/lib/praxis/docs/open_api_generator.rb +255 -0
  17. data/lib/praxis/docs/openapi/info_object.rb +31 -0
  18. data/lib/praxis/docs/openapi/media_type_object.rb +59 -0
  19. data/lib/praxis/docs/openapi/operation_object.rb +40 -0
  20. data/lib/praxis/docs/openapi/parameter_object.rb +69 -0
  21. data/lib/praxis/docs/openapi/paths_object.rb +55 -0
  22. data/lib/praxis/docs/openapi/request_body_object.rb +51 -0
  23. data/lib/praxis/docs/openapi/response_object.rb +63 -0
  24. data/lib/praxis/docs/openapi/responses_object.rb +44 -0
  25. data/lib/praxis/docs/openapi/schema_object.rb +87 -0
  26. data/lib/praxis/docs/openapi/server_object.rb +24 -0
  27. data/lib/praxis/docs/openapi/tag_object.rb +21 -0
  28. data/lib/praxis/extensions/attribute_filtering.rb +2 -0
  29. data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +148 -157
  30. data/lib/praxis/extensions/attribute_filtering/active_record_patches.rb +15 -0
  31. data/lib/praxis/extensions/attribute_filtering/active_record_patches/5x.rb +90 -0
  32. data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_0.rb +68 -0
  33. data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_1_plus.rb +58 -0
  34. data/lib/praxis/extensions/attribute_filtering/filter_tree_node.rb +35 -0
  35. data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +13 -12
  36. data/lib/praxis/extensions/attribute_filtering/sequel_filter_query_builder.rb +3 -2
  37. data/lib/praxis/extensions/field_selection/active_record_query_selector.rb +7 -9
  38. data/lib/praxis/extensions/field_selection/field_selector.rb +4 -0
  39. data/lib/praxis/extensions/field_selection/sequel_query_selector.rb +6 -9
  40. data/lib/praxis/extensions/pagination.rb +130 -0
  41. data/lib/praxis/extensions/pagination/active_record_pagination_handler.rb +42 -0
  42. data/lib/praxis/extensions/pagination/header_generator.rb +70 -0
  43. data/lib/praxis/extensions/pagination/ordering_params.rb +238 -0
  44. data/lib/praxis/extensions/pagination/pagination_handler.rb +68 -0
  45. data/lib/praxis/extensions/pagination/pagination_params.rb +378 -0
  46. data/lib/praxis/extensions/pagination/sequel_pagination_handler.rb +45 -0
  47. data/lib/praxis/handlers/json.rb +2 -0
  48. data/lib/praxis/handlers/www_form.rb +5 -0
  49. data/lib/praxis/handlers/{xml.rb → xml-sample.rb} +6 -0
  50. data/lib/praxis/links.rb +4 -0
  51. data/lib/praxis/mapper/active_model_compat.rb +23 -5
  52. data/lib/praxis/mapper/resource.rb +16 -9
  53. data/lib/praxis/mapper/selector_generator.rb +11 -10
  54. data/lib/praxis/mapper/sequel_compat.rb +1 -0
  55. data/lib/praxis/media_type.rb +1 -56
  56. data/lib/praxis/multipart/part.rb +5 -2
  57. data/lib/praxis/plugins/mapper_plugin.rb +1 -1
  58. data/lib/praxis/plugins/pagination_plugin.rb +71 -0
  59. data/lib/praxis/resource_definition.rb +4 -12
  60. data/lib/praxis/response_definition.rb +1 -1
  61. data/lib/praxis/route.rb +2 -4
  62. data/lib/praxis/routing_config.rb +4 -8
  63. data/lib/praxis/tasks/api_docs.rb +23 -0
  64. data/lib/praxis/tasks/routes.rb +10 -15
  65. data/lib/praxis/types/media_type_common.rb +10 -0
  66. data/lib/praxis/types/multipart_array.rb +62 -0
  67. data/lib/praxis/validation_handler.rb +1 -2
  68. data/lib/praxis/version.rb +1 -1
  69. data/praxis.gemspec +4 -5
  70. data/spec/functional_spec.rb +9 -6
  71. data/spec/praxis/action_definition_spec.rb +4 -16
  72. data/spec/praxis/api_general_info_spec.rb +6 -6
  73. data/spec/praxis/extensions/attribute_filtering/active_record_filter_query_builder_spec.rb +304 -0
  74. data/spec/praxis/extensions/attribute_filtering/filter_tree_node_spec.rb +39 -0
  75. data/spec/praxis/extensions/attribute_filtering/filtering_params_spec.rb +34 -0
  76. data/spec/praxis/extensions/field_expansion_spec.rb +6 -24
  77. data/spec/praxis/extensions/field_selection/active_record_query_selector_spec.rb +15 -11
  78. data/spec/praxis/extensions/field_selection/sequel_query_selector_spec.rb +4 -3
  79. data/spec/praxis/extensions/pagination/active_record_pagination_handler_spec.rb +130 -0
  80. data/spec/praxis/extensions/{field_selection/support → support}/spec_resources_active_model.rb +45 -2
  81. data/spec/praxis/extensions/{field_selection/support → support}/spec_resources_sequel.rb +0 -0
  82. data/spec/praxis/mapper/selector_generator_spec.rb +32 -0
  83. data/spec/praxis/media_type_spec.rb +5 -129
  84. data/spec/praxis/request_spec.rb +3 -22
  85. data/spec/praxis/resource_definition_spec.rb +1 -1
  86. data/spec/praxis/response_definition_spec.rb +8 -9
  87. data/spec/praxis/route_spec.rb +2 -9
  88. data/spec/praxis/routing_config_spec.rb +4 -13
  89. data/spec/praxis/types/multipart_array_spec.rb +4 -21
  90. data/spec/spec_app/config/environment.rb +0 -2
  91. data/spec/spec_app/design/api.rb +7 -1
  92. data/spec/spec_app/design/media_types/instance.rb +0 -8
  93. data/spec/spec_app/design/media_types/volume.rb +0 -12
  94. data/spec/spec_app/design/resources/instances.rb +1 -2
  95. data/spec/spec_helper.rb +6 -0
  96. data/spec/support/spec_media_types.rb +0 -73
  97. metadata +51 -49
  98. data/spec/praxis/handlers/xml_spec.rb +0 -177
  99. data/spec/praxis/links_spec.rb +0 -68
@@ -0,0 +1,304 @@
1
+ require 'spec_helper'
2
+
3
+ require_relative '../support/spec_resources_active_model.rb'
4
+ require 'praxis/extensions/attribute_filtering'
5
+ require 'praxis/extensions/attribute_filtering/active_record_filter_query_builder'
6
+
7
+ describe Praxis::Extensions::AttributeFiltering::ActiveRecordFilterQueryBuilder do
8
+ let(:root_resource) { ActiveBookResource }
9
+ let(:filters_map) { root_resource.instance_variable_get(:@_filters_map)}
10
+ let(:base_model) { root_resource.model }
11
+ let(:base_query) { base_model }
12
+ let(:instance) { described_class.new(query: base_query, model: base_model, filters_map: filters_map) }
13
+
14
+ shared_examples 'subject_equivalent_to' do |expected_result|
15
+ it do
16
+ loaded_ids = subject.all.map(&:id).sort
17
+ expected_ids = expected_result.all.map(&:id).sort
18
+ expect(loaded_ids).to_not be_empty
19
+ expect(loaded_ids).to eq(expected_ids)
20
+ end
21
+ end
22
+
23
+ # Poorman's way to compare SQL queries...
24
+ shared_examples 'subject_matches_sql' do |expected_sql|
25
+ it do
26
+ # Remove parenthesis as our queries have WHERE clauses using them...
27
+ gen_sql = subject.all.to_sql.gsub(/[()]/,'')
28
+ # Strip blank at the beggining (and end) of every line
29
+ # ...and recompose it by adding an extra space at the beginning of each one instead
30
+ exp = expected_sql.split(/\n/).map do |line|
31
+ " " + line.strip.gsub(/[()]/,'')
32
+ end.join.strip
33
+ expect(gen_sql).to eq(exp)
34
+ end
35
+ end
36
+
37
+ context 'initialize' do
38
+ it 'sets the right things to the instance' do
39
+ instance
40
+ expect(instance.query).to eq(base_query)
41
+ expect(instance.model).to eq(base_model)
42
+ expect(instance.attr_to_column).to eq(filters_map)
43
+ end
44
+ end
45
+ context 'generate' do
46
+ subject { instance.generate(filters) }
47
+ let(:filters) { Praxis::Types::FilteringParams.load(filters_string)}
48
+
49
+ context 'with no filters' do
50
+ let(:filters_string) { '' }
51
+ it 'does not modify the query' do
52
+ expect(subject).to be(base_query)
53
+ end
54
+ end
55
+ context 'by a simple field' do
56
+ context 'that maps to the same name' do
57
+ let(:filters_string) { 'category_uuid=deadbeef1' }
58
+ it_behaves_like 'subject_equivalent_to', ActiveBook.where(category_uuid: 'deadbeef1')
59
+ end
60
+ context 'that maps to a different name' do
61
+ let(:filters_string) { 'name=Book1'}
62
+ it_behaves_like 'subject_equivalent_to', ActiveBook.where(simple_name: 'Book1')
63
+ end
64
+ context 'that is mapped as a nested struct' do
65
+ let(:filters_string) { 'fake_nested.name=Book1'}
66
+ it_behaves_like 'subject_equivalent_to', ActiveBook.where(simple_name: 'Book1')
67
+ end
68
+ end
69
+
70
+ context 'by a field or a related model' do
71
+ context 'for a belongs_to association' do
72
+ let(:filters_string) { 'author.name=author2'}
73
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.name' => 'author2')
74
+ end
75
+ context 'for a has_many association' do
76
+ let(:filters_string) { 'taggings.label=primary' }
77
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:taggings).where('active_taggings.label' => 'primary')
78
+ end
79
+ context 'for a has_many through association' do
80
+ let(:filters_string) { 'tags.name=blue' }
81
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:tags).where('active_tags.name' => 'blue')
82
+ end
83
+ end
84
+
85
+ context 'by using all supported operators' do
86
+ PREF = Praxis::Extensions::AttributeFiltering::ALIAS_TABLE_PREFIX
87
+ COMMON_SQL_PREFIX = <<~SQL
88
+ SELECT "active_books".* FROM "active_books"
89
+ INNER JOIN
90
+ "active_authors" "#{PREF}/author" ON "#{PREF}/author"."id" = "active_books"."author_id"
91
+ SQL
92
+ context '=' do
93
+ let(:filters_string) { 'author.id=11'}
94
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.id = 11')
95
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
96
+ WHERE "#{PREF}/author"."id" = 11
97
+ SQL
98
+ end
99
+ context '= (with array)' do
100
+ let(:filters_string) { 'author.id=11,22'}
101
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.id IN (11,22)')
102
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
103
+ WHERE "#{PREF}/author"."id" IN (11,22)
104
+ SQL
105
+ end
106
+ context '!=' do
107
+ let(:filters_string) { 'author.id!=11'}
108
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.id <> 11')
109
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
110
+ WHERE "#{PREF}/author"."id" <> 11
111
+ SQL
112
+ end
113
+ context '!= (with array)' do
114
+ let(:filters_string) { 'author.id!=11,888'}
115
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.id NOT IN (11,888)')
116
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
117
+ WHERE "#{PREF}/author"."id" NOT IN (11,888)
118
+ SQL
119
+ end
120
+ context '>' do
121
+ let(:filters_string) { 'author.id>1'}
122
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.id > 1')
123
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
124
+ WHERE "#{PREF}/author"."id" > 1
125
+ SQL
126
+ end
127
+ context '<' do
128
+ let(:filters_string) { 'author.id<22'}
129
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.id < 22')
130
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
131
+ WHERE "#{PREF}/author"."id" < 22
132
+ SQL
133
+ end
134
+ context '>=' do
135
+ let(:filters_string) { 'author.id>=22'}
136
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.id >= 22')
137
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
138
+ WHERE "#{PREF}/author"."id" >= 22
139
+ SQL
140
+ end
141
+ context '<=' do
142
+ let(:filters_string) { 'author.id<=22'}
143
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.id <= 22')
144
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
145
+ WHERE "#{PREF}/author"."id" <= 22
146
+ SQL
147
+ end
148
+ context '!' do
149
+ let(:filters_string) { 'author.id!'}
150
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.id IS NOT NULL')
151
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
152
+ WHERE "#{PREF}/author"."id" IS NOT NULL
153
+ SQL
154
+ end
155
+ context '!!' do
156
+ let(:filters_string) { 'author.name!!'}
157
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.name IS NULL')
158
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
159
+ WHERE "#{PREF}/author"."name" IS NULL
160
+ SQL
161
+ end
162
+ context 'including LIKE fuzzy queries' do
163
+ context 'LIKE' do
164
+ let(:filters_string) { 'author.name=author*'}
165
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.name LIKE "author%"')
166
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
167
+ WHERE "#{PREF}/author"."name" LIKE 'author%'
168
+ SQL
169
+ end
170
+ context 'NOT LIKE' do
171
+ let(:filters_string) { 'author.name!=foobar*'}
172
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:author).where('active_authors.name NOT LIKE "foobar%"')
173
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
174
+ WHERE "#{PREF}/author"."name" NOT LIKE 'foobar%'
175
+ SQL
176
+ end
177
+ end
178
+ end
179
+
180
+ context 'with a field mapping using a proc' do
181
+ let(:filters_string) { 'name_is_not=Book1' }
182
+ it_behaves_like 'subject_equivalent_to', ActiveBook.where.not(simple_name: 'Book1')
183
+ end
184
+
185
+ context 'with a deeply nested chains' do
186
+ context 'of depth 2' do
187
+ let(:filters_string) { 'category.books.name=Book2' }
188
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(category: :books).where('books_active_categories.simple_name': 'Book2')
189
+ end
190
+ context 'multiple conditions on a nested relationship' do
191
+ let(:filters_string) { 'category.books.taggings.tag_id=1&category.books.taggings.label=primary' }
192
+ it_behaves_like 'subject_equivalent_to',
193
+ ActiveBook.joins(category: { books: :taggings }).where('active_taggings.tag_id': 1).where('active_taggings.label': 'primary')
194
+ it_behaves_like 'subject_matches_sql', <<~SQL
195
+ SELECT "active_books".* FROM "active_books"
196
+ INNER JOIN "active_categories" ON "active_categories"."uuid" = "active_books"."category_uuid"
197
+ INNER JOIN "active_books" "books_active_categories" ON "books_active_categories"."category_uuid" = "active_categories"."uuid"
198
+ INNER JOIN "active_taggings" "#{PREF}/category/books/taggings" ON "/category/books/taggings"."book_id" = "books_active_categories"."id"
199
+ WHERE ("#{PREF}/category/books/taggings"."tag_id" = 1)
200
+ AND ("#{PREF}/category/books/taggings"."label" = 'primary')
201
+ SQL
202
+ end
203
+ context 'that contain multiple joins to the same table' do
204
+ let(:filters_string) { 'taggings.tag.taggings.tag_id=1' }
205
+ it_behaves_like 'subject_equivalent_to',
206
+ ActiveBook.joins(taggings: {tag: :taggings}).where('taggings_active_tags.tag_id=1')
207
+ end
208
+ end
209
+
210
+ context 'by multiple fields' do
211
+ context 'adds the where clauses for the top model if fields belong to it' do
212
+ let(:filters_string) { 'category_uuid=deadbeef1&name=Book1' }
213
+ it_behaves_like 'subject_equivalent_to', ActiveBook.where(category_uuid: 'deadbeef1', simple_name: 'Book1')
214
+ end
215
+ context 'adds multiple where clauses for same nested relationship join (instead of multiple joins with 1 clause each)' do
216
+ let(:filters_string) { 'taggings.label=primary&taggings.tag_id=2' }
217
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(:taggings).where('active_taggings.label' => 'primary', 'active_taggings.tag_id' => 2)
218
+ end
219
+ end
220
+
221
+ context 'uses fully qualified names for conditions (disambiguate fields)' do
222
+ context 'when we have a join table condition that has the same field' do
223
+ COMMON_SQL_PREFIX = <<~SQL
224
+ SELECT "active_books".* FROM "active_books"
225
+ INNER JOIN "active_categories" ON "active_categories"."uuid" = "active_books"."category_uuid"
226
+ INNER JOIN "active_books" "#{PREF}/category/books" ON "#{PREF}/category/books"."category_uuid" = "active_categories"."uuid"
227
+ SQL
228
+ let(:filters_string) { 'name=Book1&category.books.name=Book3' }
229
+ it_behaves_like 'subject_equivalent_to', ActiveBook.joins(category: :books)
230
+ .where('simple_name': 'Book1')
231
+ .where('books_active_categories.simple_name': 'Book3')
232
+ it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
233
+ WHERE ("#{PREF}/category/books"."simple_name" = 'Book3')
234
+ AND ("active_books"."simple_name" = 'Book1')
235
+ SQL
236
+ end
237
+
238
+ context 'it qualifis them even if there are no joined tables/conditions at all' do
239
+ let(:filters_string) { 'id=11'}
240
+ it_behaves_like 'subject_matches_sql', <<~SQL
241
+ SELECT "active_books".* FROM "active_books"
242
+ WHERE "active_books"."id" = 11
243
+ SQL
244
+ end
245
+
246
+ end
247
+
248
+ context 'ActiveRecord continues to work as expected (with our patches)' do
249
+ context 'using a deep join with repeated tables' do
250
+ subject{ ActiveBook.joins(taggings: {tag: :taggings}).where('taggings_active_tags.tag_id=1') }
251
+ it 'performs query' do
252
+ expect(subject.to_a).to_not be_empty
253
+ end
254
+ it_behaves_like 'subject_matches_sql', <<~SQL
255
+ SELECT "active_books".* FROM "active_books"
256
+ INNER JOIN "active_taggings" ON "active_taggings"."book_id" = "active_books"."id"
257
+ INNER JOIN "active_tags" ON "active_tags"."id" = "active_taggings"."tag_id"
258
+ INNER JOIN "active_taggings" "taggings_active_tags" ON "taggings_active_tags"."tag_id" = "active_tags"."id"
259
+ WHERE (taggings_active_tags.tag_id=1)
260
+ SQL
261
+ end
262
+ context 'a deep join with repeated tables with the root AND the join, along with :through joins as well' do
263
+ subject!{ ActiveBook.joins(tags: {books: {taggings: :book}}).where('books_active_taggings.simple_name="Book2"') }
264
+ it 'performs query' do
265
+ expect(subject.to_a).to_not be_empty
266
+ end
267
+ it_behaves_like 'subject_matches_sql', <<~SQL
268
+ SELECT "active_books".* FROM "active_books"
269
+ INNER JOIN "active_taggings" ON "active_taggings"."book_id" = "active_books"."id"
270
+ INNER JOIN "active_tags" ON "active_tags"."id" = "active_taggings"."tag_id"
271
+ INNER JOIN "active_taggings" "taggings_active_tags_join" ON "taggings_active_tags_join"."tag_id" = "active_tags"."id"
272
+ INNER JOIN "active_books" "books_active_tags" ON "books_active_tags"."id" = "taggings_active_tags_join"."book_id"
273
+ INNER JOIN "active_taggings" "taggings_active_books" ON "taggings_active_books"."book_id" = "books_active_tags"."id"
274
+ INNER JOIN "active_books" "books_active_taggings" ON "books_active_taggings"."id" = "taggings_active_books"."book_id"
275
+ WHERE (books_active_taggings.simple_name="Book2")
276
+ SQL
277
+ end
278
+ end
279
+
280
+ context 'respects scopes' do
281
+ context 'for a has_many through association' do
282
+ let(:filters_string) { 'primary_tags.name=blue' }
283
+ it_behaves_like 'subject_equivalent_to',
284
+ ActiveBook.joins(:primary_tags).where('active_tags.name="blue"')
285
+
286
+ it 'adds the association scope clause to the join' do
287
+ inner_join_pieces = subject.to_sql.split('INNER')
288
+ found = inner_join_pieces.any? do |line|
289
+ line =~ /\s+JOIN "active_taggings".+ON.+\."label" = 'primary'/
290
+ end
291
+ expect(found).to be_truthy
292
+ end
293
+ # This is slightly incorrect in AR 6.1+ (since the picked aliases for active_taggings tables vary)
294
+ # it_behaves_like 'subject_matches_sql', <<~SQL
295
+ # SELECT "active_books".* FROM "active_books"
296
+ # INNER JOIN "active_taggings" ON "active_taggings"."label" = 'primary'
297
+ # AND "active_taggings"."book_id" = "active_books"."id"
298
+ # INNER JOIN "active_tags" "/primary_tags" ON "/primary_tags"."id" = "active_taggings"."tag_id"
299
+ # WHERE ("/primary_tags"."name" = 'blue')
300
+ # SQL
301
+ end
302
+ end
303
+ end
304
+ end
@@ -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
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- require_relative 'support/spec_resources_active_model.rb'
3
+ require_relative '../support/spec_resources_active_model.rb'
4
4
 
5
5
  describe Praxis::Extensions::FieldSelection::ActiveRecordQuerySelector do
6
6
  let(:selector_fields) do
@@ -30,8 +30,9 @@ describe Praxis::Extensions::FieldSelection::ActiveRecordQuerySelector do
30
30
  ]
31
31
  end
32
32
  let(:selector_node) { Praxis::Mapper::SelectorGenerator.new.add(ActiveBookResource,selector_fields) }
33
+ let(:debug){ false }
33
34
 
34
- subject(:selector) {described_class.new(query: query, selectors: selector_node) }
35
+ subject(:selector) {described_class.new(query: query, selectors: selector_node, debug: debug) }
35
36
  context '#generate with a mocked' do
36
37
  let(:query) { double("Query") }
37
38
  it 'calls the select columns for the top level, and includes the right association hashes' do
@@ -49,12 +50,15 @@ describe Praxis::Extensions::FieldSelection::ActiveRecordQuerySelector do
49
50
  expect(subject).to_not receive(:explain_query)
50
51
  subject.generate
51
52
  end
52
- it 'calls the explain debug method if enabled' do
53
- expect(query).to receive(:select).and_return(query)
54
- expect(query).to receive(:includes).and_return(query)
55
- expect(subject).to receive(:explain_query)
56
- subject.generate(debug: true)
57
- 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
58
62
  end
59
63
 
60
64
  context '#generate with a real AR model' do
@@ -79,14 +83,14 @@ describe Praxis::Extensions::FieldSelection::ActiveRecordQuerySelector do
79
83
  expect(includes_hash).to match(expected_includes)
80
84
  # Also, make AR do the actual query to make sure everything is wired up correctly
81
85
  result = final_query.to_a
82
- expect(result.size).to be 2
86
+ expect(result.size).to be > 2 # We are using 2 but we've seeded more
83
87
  book1 = result[0]
84
88
  book2 = result[1]
85
89
  expect(book1.author.id).to eq 11
86
90
  expect(book1.author.books.size).to eq 1
87
91
  expect(book1.author.books.map(&:simple_name)).to eq(['Book1'])
88
92
  expect(book1.category.name).to eq 'cat1'
89
- expect(book1.tags.map(&:name)).to match_array(['blue','red'])
93
+ expect(book1.tags.map(&:name)).to match_array(['blue','red','green'])
90
94
 
91
95
  expect(book2.author.id).to eq 22
92
96
  expect(book2.author.books.size).to eq 1
@@ -98,7 +102,7 @@ describe Praxis::Extensions::FieldSelection::ActiveRecordQuerySelector do
98
102
  it 'calls the explain debug method if enabled' do
99
103
  suppress_output do
100
104
  # Actually make it run all the way...but suppressing the output
101
- subject.generate(debug: true)
105
+ subject.generate
102
106
  end
103
107
  end
104
108
  end