praxis 0.22.pre.2 → 2.0.pre.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +5 -20
- data/CHANGELOG.md +333 -324
- data/lib/praxis.rb +14 -9
- data/lib/praxis/action_definition.rb +8 -10
- data/lib/praxis/action_definition/headers_dsl_compiler.rb +1 -1
- data/lib/praxis/api_definition.rb +27 -44
- data/lib/praxis/api_general_info.rb +23 -3
- data/lib/praxis/application.rb +15 -142
- data/lib/praxis/bootloader.rb +1 -2
- data/lib/praxis/bootloader_stages/environment.rb +13 -0
- data/lib/praxis/config.rb +1 -1
- data/lib/praxis/controller.rb +0 -2
- data/lib/praxis/dispatcher.rb +4 -6
- data/lib/praxis/docs/generator.rb +19 -24
- data/lib/praxis/docs/link_builder.rb +1 -1
- data/lib/praxis/docs/open_api_generator.rb +255 -0
- data/lib/praxis/docs/openapi/info_object.rb +31 -0
- data/lib/praxis/docs/openapi/media_type_object.rb +59 -0
- data/lib/praxis/docs/openapi/operation_object.rb +40 -0
- data/lib/praxis/docs/openapi/parameter_object.rb +69 -0
- data/lib/praxis/docs/openapi/paths_object.rb +58 -0
- data/lib/praxis/docs/openapi/request_body_object.rb +51 -0
- data/lib/praxis/docs/openapi/response_object.rb +63 -0
- data/lib/praxis/docs/openapi/responses_object.rb +44 -0
- data/lib/praxis/docs/openapi/schema_object.rb +87 -0
- data/lib/praxis/docs/openapi/server_object.rb +24 -0
- data/lib/praxis/docs/openapi/tag_object.rb +21 -0
- data/lib/praxis/error_handler.rb +5 -5
- data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +1 -1
- data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +4 -0
- data/lib/praxis/extensions/attribute_filtering/sequel_filter_query_builder.rb +125 -0
- data/lib/praxis/extensions/field_selection.rb +1 -12
- data/lib/praxis/extensions/field_selection/active_record_query_selector.rb +28 -34
- data/lib/praxis/extensions/field_selection/field_selector.rb +4 -0
- data/lib/praxis/extensions/field_selection/sequel_query_selector.rb +35 -39
- data/lib/praxis/extensions/rendering.rb +1 -1
- data/lib/praxis/file_group.rb +1 -1
- data/lib/praxis/handlers/xml.rb +1 -1
- data/lib/praxis/links.rb +4 -0
- data/lib/praxis/mapper/active_model_compat.rb +98 -0
- data/lib/praxis/mapper/resource.rb +242 -0
- data/lib/praxis/mapper/selector_generator.rb +150 -0
- data/lib/praxis/mapper/sequel_compat.rb +76 -0
- data/lib/praxis/media_type_identifier.rb +2 -1
- data/lib/praxis/middleware_app.rb +13 -15
- data/lib/praxis/multipart/part.rb +8 -7
- data/lib/praxis/notifications.rb +1 -1
- data/lib/praxis/plugins/mapper_plugin.rb +64 -0
- data/lib/praxis/request.rb +14 -7
- data/lib/praxis/request_stages/response.rb +2 -3
- data/lib/praxis/resource_definition.rb +15 -19
- data/lib/praxis/response.rb +6 -5
- data/lib/praxis/response_definition.rb +6 -8
- data/lib/praxis/response_template.rb +3 -4
- data/lib/praxis/responses/http.rb +36 -0
- data/lib/praxis/responses/internal_server_error.rb +12 -3
- data/lib/praxis/responses/multipart_ok.rb +11 -4
- data/lib/praxis/responses/validation_error.rb +10 -1
- data/lib/praxis/route.rb +1 -1
- data/lib/praxis/router.rb +3 -3
- data/lib/praxis/routing_config.rb +1 -1
- data/lib/praxis/tasks/api_docs.rb +24 -9
- data/lib/praxis/tasks/routes.rb +0 -1
- data/lib/praxis/trait.rb +1 -1
- data/lib/praxis/types/media_type_common.rb +12 -2
- data/lib/praxis/types/multipart.rb +1 -1
- data/lib/praxis/types/multipart_array.rb +64 -2
- data/lib/praxis/types/multipart_array/part_definition.rb +1 -1
- data/lib/praxis/version.rb +1 -1
- data/praxis.gemspec +11 -9
- data/spec/functional_spec.rb +0 -1
- data/spec/praxis/action_definition_spec.rb +16 -27
- data/spec/praxis/api_definition_spec.rb +8 -13
- data/spec/praxis/api_general_info_spec.rb +8 -3
- data/spec/praxis/application_spec.rb +8 -14
- data/spec/praxis/collection_spec.rb +3 -2
- data/spec/praxis/config_spec.rb +2 -2
- data/spec/praxis/extensions/field_selection/active_record_query_selector_spec.rb +106 -0
- data/spec/praxis/extensions/field_selection/sequel_query_selector_spec.rb +147 -0
- data/spec/praxis/extensions/field_selection/support/spec_resources_active_model.rb +130 -0
- data/spec/praxis/extensions/field_selection/support/spec_resources_sequel.rb +106 -0
- data/spec/praxis/handlers/xml_spec.rb +2 -2
- data/spec/praxis/mapper/resource_spec.rb +169 -0
- data/spec/praxis/mapper/selector_generator_spec.rb +325 -0
- data/spec/praxis/media_type_spec.rb +0 -10
- data/spec/praxis/middleware_app_spec.rb +16 -10
- data/spec/praxis/request_spec.rb +7 -17
- data/spec/praxis/request_stages/action_spec.rb +8 -1
- data/spec/praxis/request_stages/validate_spec.rb +1 -1
- data/spec/praxis/resource_definition_spec.rb +10 -12
- data/spec/praxis/response_definition_spec.rb +19 -30
- data/spec/praxis/response_spec.rb +6 -13
- data/spec/praxis/responses/internal_server_error_spec.rb +5 -2
- data/spec/praxis/router_spec.rb +5 -9
- data/spec/spec_app/app/controllers/instances.rb +1 -1
- data/spec/spec_app/config.ru +6 -1
- data/spec/spec_app/config/environment.rb +3 -21
- data/spec/spec_app/design/api.rb +6 -0
- data/spec/spec_helper.rb +13 -17
- data/spec/support/be_deep_equal_matcher.rb +39 -0
- data/spec/support/spec_resources.rb +124 -0
- metadata +86 -53
- data/lib/praxis/extensions/attribute_filtering.rb +0 -28
- data/lib/praxis/extensions/attribute_filtering/query_builder.rb +0 -39
- data/lib/praxis/extensions/mapper_selectors.rb +0 -16
- data/lib/praxis/media_type_collection.rb +0 -127
- data/lib/praxis/plugins/praxis_mapper_plugin.rb +0 -246
- data/spec/praxis/media_type_collection_spec.rb +0 -157
- data/spec/praxis/plugins/praxis_mapper_plugin_spec.rb +0 -142
- data/spec/spec_app/app/models/person.rb +0 -3
@@ -14,7 +14,7 @@ module Praxis
|
|
14
14
|
|
15
15
|
def update_attribute(attribute, options, block)
|
16
16
|
attribute.options.merge!(options)
|
17
|
-
attribute.type.attributes(options, &block)
|
17
|
+
attribute.type.attributes(**options, &block)
|
18
18
|
end
|
19
19
|
|
20
20
|
def create_attribute(type=Attributor::Struct, **opts, &block)
|
data/lib/praxis/version.rb
CHANGED
data/praxis.gemspec
CHANGED
@@ -21,22 +21,21 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.executables << 'praxis'
|
22
22
|
|
23
23
|
spec.add_dependency 'rack', '>= 1'
|
24
|
-
spec.add_dependency 'mustermann', '>=
|
24
|
+
spec.add_dependency 'mustermann', '>=1.1', '<=2'
|
25
25
|
spec.add_dependency 'activesupport', '>= 3'
|
26
26
|
spec.add_dependency 'mime', '~> 0'
|
27
|
-
spec.add_dependency 'praxis-
|
28
|
-
spec.add_dependency '
|
29
|
-
spec.add_dependency '
|
30
|
-
spec.add_dependency 'thor', '~> 0.18'
|
27
|
+
spec.add_dependency 'praxis-blueprints', '>= 3.5'
|
28
|
+
spec.add_dependency 'attributor', '>= 5.5'
|
29
|
+
spec.add_dependency 'thor'
|
31
30
|
spec.add_dependency 'terminal-table', '~> 1.4'
|
32
31
|
|
33
32
|
spec.add_development_dependency 'bundler'
|
34
33
|
spec.add_development_dependency 'rake', '~> 0.9'
|
35
34
|
spec.add_development_dependency 'rake-notes', '~> 0'
|
36
35
|
if RUBY_PLATFORM !~ /java/
|
37
|
-
spec.add_development_dependency 'pry'
|
38
|
-
spec.add_development_dependency 'pry-byebug'
|
39
|
-
spec.add_development_dependency 'pry-stack_explorer'
|
36
|
+
spec.add_development_dependency 'pry'
|
37
|
+
spec.add_development_dependency 'pry-byebug'
|
38
|
+
spec.add_development_dependency 'pry-stack_explorer'
|
40
39
|
spec.add_development_dependency 'sqlite3', '~> 1'
|
41
40
|
else
|
42
41
|
spec.add_development_dependency 'jdbc-sqlite3'
|
@@ -50,6 +49,9 @@ Gem::Specification.new do |spec|
|
|
50
49
|
spec.add_development_dependency 'rack-test', '~> 0'
|
51
50
|
spec.add_development_dependency 'simplecov', '~> 0'
|
52
51
|
spec.add_development_dependency 'fuubar', '~> 2'
|
53
|
-
spec.add_development_dependency 'yard',
|
52
|
+
spec.add_development_dependency 'yard', ">= 0.9.20"
|
54
53
|
spec.add_development_dependency 'coveralls'
|
54
|
+
# Just for the query selector extensions etc...
|
55
|
+
spec.add_development_dependency 'sequel', '~> 5'
|
56
|
+
spec.add_development_dependency 'activerecord', '> 4'
|
55
57
|
end
|
data/spec/functional_spec.rb
CHANGED
@@ -136,7 +136,6 @@ describe 'Functional specs' do
|
|
136
136
|
headers = last_response.headers
|
137
137
|
expect(headers['Content-Type']).to eq('application/json')
|
138
138
|
expect(headers['Spec-Middleware']).to eq('used')
|
139
|
-
expect(headers['Content-Length']).to eq(last_response.body.size.to_s)
|
140
139
|
end
|
141
140
|
|
142
141
|
it 'returns early when making the before filter break' do
|
@@ -1,15 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Praxis::ActionDefinition do
|
4
|
-
|
5
|
-
before(:context) do
|
6
|
-
Thread.current[:praxis_instance] = Praxis::Application.new(name: 'action_definition_spec', skip_registration: true)
|
7
|
-
end
|
8
|
-
|
9
|
-
let(:praxis_instance) do
|
10
|
-
Thread.current[:praxis_instance]
|
11
|
-
end
|
12
|
-
|
13
4
|
class SpecMediaType < Praxis::MediaType
|
14
5
|
identifier 'application/json'
|
15
6
|
|
@@ -40,8 +31,10 @@ describe Praxis::ActionDefinition do
|
|
40
31
|
end
|
41
32
|
end
|
42
33
|
end
|
34
|
+
|
43
35
|
subject(:action) do
|
44
|
-
|
36
|
+
|
37
|
+
Praxis::ApiDefinition.define do |api|
|
45
38
|
api.response_template :ok do |media_type: , location: nil, headers: nil, description: nil |
|
46
39
|
status 200
|
47
40
|
|
@@ -49,10 +42,6 @@ describe Praxis::ActionDefinition do
|
|
49
42
|
location location
|
50
43
|
headers headers if headers
|
51
44
|
end
|
52
|
-
|
53
|
-
api.info do # applies to all API infos
|
54
|
-
base_path "/pref"
|
55
|
-
end
|
56
45
|
end
|
57
46
|
Praxis::ActionDefinition.new(:foo, resource_definition) do
|
58
47
|
routing { get '/:one' }
|
@@ -122,11 +111,11 @@ describe Praxis::ActionDefinition do
|
|
122
111
|
let(:traits) { {test: trait} }
|
123
112
|
|
124
113
|
before do
|
125
|
-
allow(
|
114
|
+
allow(Praxis::ApiDefinition.instance).to receive(:traits).and_return(traits)
|
126
115
|
end
|
127
116
|
|
128
117
|
its('params.attributes.keys') { should eq [:inherited, :app_name, :name, :one]}
|
129
|
-
its('routes.first.path.to_s') { should eq '/
|
118
|
+
its('routes.first.path.to_s') { should eq '/api/foobars/hello_world/test_trait/:app_name/:one' }
|
130
119
|
its(:traits) { should eq [:test] }
|
131
120
|
|
132
121
|
it 'is reflected in the describe output' do
|
@@ -292,7 +281,7 @@ describe Praxis::ActionDefinition do
|
|
292
281
|
subject(:action) { resource_definition.actions[:show] }
|
293
282
|
|
294
283
|
it 'works' do
|
295
|
-
expansion = action.primary_route.path.expand(cloud_id:232, id: 2)
|
284
|
+
expansion = action.primary_route.path.expand(cloud_id:'232', id: '2')
|
296
285
|
expect(expansion).to eq "/api/clouds/232/instances/2"
|
297
286
|
end
|
298
287
|
|
@@ -326,9 +315,11 @@ describe Praxis::ActionDefinition do
|
|
326
315
|
end
|
327
316
|
|
328
317
|
context 'with a base_path and base_params on ApiDefinition' do
|
329
|
-
|
330
|
-
|
331
|
-
|
318
|
+
# Without getting a fresh new ApiDefinition it is very difficult to test stuff using the Singleton
|
319
|
+
# So for some tests we're gonna create a new instance and work with it to avoid the singleton issues
|
320
|
+
let(:non_singleton_api) do
|
321
|
+
api_def=Praxis::ApiDefinition.__send__(:new)
|
322
|
+
api_def.instance_eval do |api|
|
332
323
|
|
333
324
|
api.info do
|
334
325
|
base_path '/apps/:app_name'
|
@@ -341,13 +332,11 @@ describe Praxis::ActionDefinition do
|
|
341
332
|
end
|
342
333
|
|
343
334
|
end
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
params { attribute :one, String }
|
350
|
-
end
|
335
|
+
api_def
|
336
|
+
end
|
337
|
+
|
338
|
+
before do
|
339
|
+
allow(Praxis::ApiDefinition).to receive(:instance).and_return(non_singleton_api)
|
351
340
|
end
|
352
341
|
|
353
342
|
its('routes.first.path.to_s') { should eq '/apps/:app_name/foobars/hello_world/:one' }
|
@@ -2,18 +2,12 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Praxis::ApiDefinition do
|
4
4
|
|
5
|
-
|
6
|
-
subject(:api){ Praxis::ApiDefinition.instance }
|
5
|
+
subject(:api){ Praxis::ApiDefinition.instance }
|
7
6
|
|
8
7
|
# Without getting a fresh new ApiDefinition it is very difficult to test stuff using the Singleton
|
9
8
|
# So for some tests we're gonna create a new instance and work with it to avoid the singleton issues
|
10
|
-
let(:app_instance){ double("AppInstance") }
|
11
|
-
before do
|
12
|
-
allow(app_instance).to receive(:versioning_scheme=).with([:header, :params])
|
13
|
-
end
|
14
|
-
|
15
9
|
let(:non_singleton_api) do
|
16
|
-
api_def=Praxis::ApiDefinition.new
|
10
|
+
api_def=Praxis::ApiDefinition.__send__(:new)
|
17
11
|
api_def.instance_eval do |api|
|
18
12
|
api.response_template :template1, &Proc.new {}
|
19
13
|
api.trait :trait1, &Proc.new {}
|
@@ -38,7 +32,12 @@ describe Praxis::ApiDefinition do
|
|
38
32
|
end
|
39
33
|
end
|
40
34
|
|
41
|
-
context '
|
35
|
+
context 'singleton' do
|
36
|
+
it 'should be a Singleton' do
|
37
|
+
expect(Praxis::ApiDefinition.ancestors).to include( Singleton )
|
38
|
+
expect(subject).to eq(Praxis::ApiDefinition.instance )
|
39
|
+
end
|
40
|
+
|
42
41
|
it 'has the :ok and :created response templates registered' do
|
43
42
|
expect(api.responses.keys).to include(:ok)
|
44
43
|
expect(api.responses.keys).to include(:created)
|
@@ -59,10 +58,6 @@ describe Praxis::ApiDefinition do
|
|
59
58
|
expect(api.responses.keys).to include(:foobar)
|
60
59
|
expect(api.response(:foobar)).to be_kind_of(Praxis::ResponseTemplate)
|
61
60
|
end
|
62
|
-
it 'stores the app instance' do
|
63
|
-
template = api.response(:template1)
|
64
|
-
expect(template.application).to be(app_instance)
|
65
|
-
end
|
66
61
|
end
|
67
62
|
|
68
63
|
context '.response' do
|
@@ -2,7 +2,12 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Praxis::ApiGeneralInfo do
|
4
4
|
|
5
|
-
subject(:info){ Praxis::ApiGeneralInfo.new
|
5
|
+
subject(:info){ Praxis::ApiGeneralInfo.new }
|
6
|
+
|
7
|
+
before do
|
8
|
+
allow(Praxis::Application.instance).to receive(:versioning_scheme=).with([:header, :params])
|
9
|
+
end
|
10
|
+
|
6
11
|
|
7
12
|
let(:info_block) do
|
8
13
|
Proc.new do
|
@@ -59,8 +64,8 @@ describe Praxis::ApiGeneralInfo do
|
|
59
64
|
end
|
60
65
|
|
61
66
|
context 'base_path with versioning' do
|
62
|
-
let(:global_info){ Praxis::ApiGeneralInfo.new
|
63
|
-
subject(:info){ Praxis::ApiGeneralInfo.new(global_info,
|
67
|
+
let(:global_info){ Praxis::ApiGeneralInfo.new }
|
68
|
+
subject(:info){ Praxis::ApiGeneralInfo.new(global_info, version: '1.0') }
|
64
69
|
|
65
70
|
before do
|
66
71
|
global_info
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Praxis::Application do
|
4
4
|
context 'configuration' do
|
5
5
|
subject(:app) do
|
6
|
-
app = Class.new(Praxis::Application).
|
6
|
+
app = Class.new(Praxis::Application).instance
|
7
7
|
|
8
8
|
config = Object.new
|
9
9
|
def config.define(key=nil, type=Attributor::Struct, **opts, &block)
|
@@ -27,7 +27,7 @@ describe Praxis::Application do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'passes the params and block to config' do
|
30
|
-
ret = app.config(:key, Attributor::Hash, {option: :one}, &myblock)
|
30
|
+
ret = app.config(:key, Attributor::Hash, **{option: :one}, &myblock)
|
31
31
|
expect(ret).to eq([:key, Attributor::Hash, {option: :one}, myblock])
|
32
32
|
end
|
33
33
|
|
@@ -45,7 +45,7 @@ describe Praxis::Application do
|
|
45
45
|
end
|
46
46
|
|
47
47
|
context 'media type handlers' do
|
48
|
-
subject { Class.new(Praxis::Application).
|
48
|
+
subject { Class.new(Praxis::Application).instance }
|
49
49
|
|
50
50
|
before do
|
51
51
|
# don't actually bootload; we're merely running specs
|
@@ -94,14 +94,12 @@ describe Praxis::Application do
|
|
94
94
|
end
|
95
95
|
|
96
96
|
describe '#setup' do
|
97
|
-
subject { Praxis::Application.
|
98
|
-
|
99
|
-
let(:builder) { double("Builder", to_app: double('Rack app'), run: true) }
|
100
|
-
|
97
|
+
subject { Class.new(Praxis::Application).instance }
|
98
|
+
|
101
99
|
before do
|
102
|
-
# don't actually bootload; we're merely running specs
|
103
|
-
allow(subject).to receive(:
|
104
|
-
allow(subject).to receive(:
|
100
|
+
# don't actually bootload; we're merely running specs
|
101
|
+
allow(subject.bootloader).to receive(:setup!).and_return(true)
|
102
|
+
allow(subject.builder).to receive(:to_app).and_return(double('Rack app'))
|
105
103
|
end
|
106
104
|
|
107
105
|
it 'is idempotent' do
|
@@ -114,9 +112,5 @@ describe Praxis::Application do
|
|
114
112
|
expect(subject.setup).to eq(subject)
|
115
113
|
expect(subject.setup).to eq(subject)
|
116
114
|
end
|
117
|
-
|
118
|
-
it 'creates an ApiDefinition that has a back-reference' do
|
119
|
-
expect(subject.api_definition.application).to eq(subject)
|
120
|
-
end
|
121
115
|
end
|
122
116
|
end
|
@@ -8,6 +8,7 @@ describe Praxis::Collection do
|
|
8
8
|
subject!(:collection) do
|
9
9
|
Praxis::Collection.of(member_type)
|
10
10
|
end
|
11
|
+
let(:identifier_string) { subject.identifier.to_s }
|
11
12
|
|
12
13
|
context '.of' do
|
13
14
|
let(:member_type) do
|
@@ -16,7 +17,7 @@ describe Praxis::Collection do
|
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
19
|
-
|
20
|
+
it { expect(identifier_string).to eq('application/an-awesome-type; type=collection') }
|
20
21
|
|
21
22
|
it 'sets the collection on the media type' do
|
22
23
|
expect(member_type::Collection).to be(collection)
|
@@ -34,7 +35,7 @@ describe Praxis::Collection do
|
|
34
35
|
context 'defined explicitly' do
|
35
36
|
subject(:type) { Volume::Collection }
|
36
37
|
its(:member_type) { should be Volume }
|
37
|
-
|
38
|
+
it { expect(identifier_string).to eq('application/vnd.acme.volumes') }
|
38
39
|
end
|
39
40
|
|
40
41
|
context '.member_type' do
|
data/spec/praxis/config_spec.rb
CHANGED
@@ -72,12 +72,12 @@ describe Praxis::Config do
|
|
72
72
|
|
73
73
|
it 'it is not allowed if its for the top key' do
|
74
74
|
expect{
|
75
|
-
config.define nil, config_type, config_opts
|
75
|
+
config.define nil, config_type, **config_opts
|
76
76
|
}.to raise_error(/You cannot define the top level configuration with a non-Struct type/)
|
77
77
|
end
|
78
78
|
|
79
79
|
before do
|
80
|
-
config.define config_key, config_type, config_opts
|
80
|
+
config.define config_key, config_type, **config_opts
|
81
81
|
end
|
82
82
|
|
83
83
|
it 'sets the attribute to the defined type' do
|
@@ -0,0 +1,106 @@
|
|
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
|
+
|
34
|
+
subject(:selector) {described_class.new(query: query, selectors: selector_node) }
|
35
|
+
context '#generate with a mocked' do
|
36
|
+
let(:query) { double("Query") }
|
37
|
+
it 'calls the select columns for the top level, and includes the right association hashes' do
|
38
|
+
expect(query).to receive(:select).with(*expected_select_from_to_query).and_return(query)
|
39
|
+
expected_includes = {
|
40
|
+
author: {
|
41
|
+
books: {}
|
42
|
+
},
|
43
|
+
category: {
|
44
|
+
books: {}
|
45
|
+
},
|
46
|
+
tags: {}
|
47
|
+
}
|
48
|
+
expect(query).to receive(:includes).with(expected_includes).and_return(query)
|
49
|
+
expect(subject).to_not receive(:explain_query)
|
50
|
+
subject.generate
|
51
|
+
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
|
58
|
+
end
|
59
|
+
|
60
|
+
context '#generate with a real AR model' do
|
61
|
+
let(:query) { ActiveBook }
|
62
|
+
|
63
|
+
it 'calls the select columns for the top level, and includes the right association hashes' do
|
64
|
+
expected_includes = {
|
65
|
+
author: {
|
66
|
+
books: {}
|
67
|
+
},
|
68
|
+
category: {
|
69
|
+
books: {}
|
70
|
+
},
|
71
|
+
tags: {}
|
72
|
+
}
|
73
|
+
#expect(query).to receive(:includes).with(expected_includes).and_return(query)
|
74
|
+
expect(subject).to_not receive(:explain_query)
|
75
|
+
final_query = subject.generate
|
76
|
+
expect(final_query.select_values).to match_array(expected_select_from_to_query)
|
77
|
+
# Our query selector always uses a single hash tree from the top, not an array of things
|
78
|
+
includes_hash = final_query.includes_values.first
|
79
|
+
expect(includes_hash).to match(expected_includes)
|
80
|
+
# Also, make AR do the actual query to make sure everything is wired up correctly
|
81
|
+
result = final_query.to_a
|
82
|
+
expect(result.size).to be 2
|
83
|
+
book1 = result[0]
|
84
|
+
book2 = result[1]
|
85
|
+
expect(book1.author.id).to eq 11
|
86
|
+
expect(book1.author.books.size).to eq 1
|
87
|
+
expect(book1.author.books.map(&:simple_name)).to eq(['Book1'])
|
88
|
+
expect(book1.category.name).to eq 'cat1'
|
89
|
+
expect(book1.tags.map(&:name)).to match_array(['blue','red'])
|
90
|
+
|
91
|
+
expect(book2.author.id).to eq 22
|
92
|
+
expect(book2.author.books.size).to eq 1
|
93
|
+
expect(book2.author.books.map(&:simple_name)).to eq(['Book2'])
|
94
|
+
expect(book2.category.name).to eq 'cat2'
|
95
|
+
expect(book2.tags.map(&:name)).to match_array(['red'])
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'calls the explain debug method if enabled' do
|
99
|
+
suppress_output do
|
100
|
+
# Actually make it run all the way...but suppressing the output
|
101
|
+
subject.generate(debug: true)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
@@ -0,0 +1,147 @@
|
|
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) }
|
69
|
+
|
70
|
+
context 'generate' do
|
71
|
+
context 'using the real models and DB' do
|
72
|
+
let(:query) { SequelSimpleModel }
|
73
|
+
|
74
|
+
it 'calls the select columns for the top level, and includes the right association hashes' do
|
75
|
+
ds = subject.generate
|
76
|
+
opts = ds.opts
|
77
|
+
# Top model is our simplemodel
|
78
|
+
expect(opts[:model]).to be(SequelSimpleModel)
|
79
|
+
selected_column_names = opts[:select].map(&:column)
|
80
|
+
expect(selected_column_names).to match_array(expected_select_from_to_query)
|
81
|
+
# 2 Eager loaded associations as well
|
82
|
+
expect(opts[:eager].keys).to match_array([:other_model, :parent, :tags])
|
83
|
+
# We can not introspect those eagers, as they are procs...but at least validate they are
|
84
|
+
expect(opts[:eager][:other_model]).to be_a Proc
|
85
|
+
expect(opts[:eager][:parent]).to be_a Proc
|
86
|
+
|
87
|
+
# Also, let's make sure the query actually works by making Sequel attempt to retrieve it and finding the right things.
|
88
|
+
result = ds.all
|
89
|
+
# 2 simple models
|
90
|
+
expect(result.size).to be 2
|
91
|
+
# First simple model points to other_model 11 and parent 1
|
92
|
+
simple_one = result.find{|i| i.id == 1}
|
93
|
+
expect(simple_one.other_model.id).to be 11
|
94
|
+
expect(simple_one.parent.id).to be 1
|
95
|
+
# also, its' parent in turn has 2 children (1 and 2) linked by its parent_uuid
|
96
|
+
expect(simple_one.parent.children.map(&:id)).to match_array([1,2])
|
97
|
+
# Has the blue and red tags
|
98
|
+
expect(simple_one.tags.map(&:tag_name)).to match_array(['blue','red'])
|
99
|
+
|
100
|
+
# second simple model points to other_model 22 and parent 2
|
101
|
+
simple_two = result.find{|i| i.id == 2}
|
102
|
+
expect(simple_two.other_model.id).to be 22
|
103
|
+
expect(simple_two.parent.id).to be 2
|
104
|
+
# also, its' parent in turn has no children (as no simple models point to it by uuid)
|
105
|
+
expect(simple_two.parent.children.map(&:id)).to be_empty
|
106
|
+
# Also has the red tag
|
107
|
+
expect(simple_two.tags.map(&:tag_name)).to match_array(['red'])
|
108
|
+
end
|
109
|
+
it 'calls the explain debug method if enabled' do
|
110
|
+
suppress_output do
|
111
|
+
# Actually make it run all the way...but suppressing the output
|
112
|
+
subject.generate(debug: true)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
context 'just mocking the query' do
|
117
|
+
let(:query) { Q.new }
|
118
|
+
|
119
|
+
it 'creates the right recursive lambdas for the eager loading' do
|
120
|
+
|
121
|
+
ds = subject.generate
|
122
|
+
result = ds.dump
|
123
|
+
expect(result[:columns]).to match_array(expected_select_from_to_query)
|
124
|
+
# 2 eager loads
|
125
|
+
expect(result[:eagers].keys).to match_array([:other_model, :parent, :tags])
|
126
|
+
# 1 - other model
|
127
|
+
other_model_eagers = result[:eagers][:other_model]
|
128
|
+
expect(other_model_eagers[:columns]).to match_array([:id])
|
129
|
+
|
130
|
+
# 2 - parent association
|
131
|
+
parent_eagers = result[:eagers][:parent]
|
132
|
+
expect(parent_eagers[:columns]).to match_array([:id,:uuid]) # uuid is necessary for the "children" assoc
|
133
|
+
expect(parent_eagers[:eagers].keys).to match_array([:children])
|
134
|
+
# 2.1 - children association off of the parent
|
135
|
+
parent_children_eagers = parent_eagers[:eagers][:children]
|
136
|
+
expect(parent_children_eagers[:columns]).to match_array([:id,:parent_uuid]) # parent_uuid is required for the assoc
|
137
|
+
expect(parent_children_eagers[:eagers]).to be_empty
|
138
|
+
|
139
|
+
# 3 - tags association
|
140
|
+
tags_eagers = result[:eagers][:tags]
|
141
|
+
expect(tags_eagers[:columns]).to match_array([:id, :tag_name]) # uuid is necessary for the "children" assoc
|
142
|
+
expect(tags_eagers[:eagers].keys).to be_empty
|
143
|
+
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|