praxis 0.22.pre.1 → 2.0.pre.4
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 +328 -323
- data/lib/praxis.rb +13 -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 +2 -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 +8 -18
- data/lib/praxis/docs/link_builder.rb +1 -1
- 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 +1 -1
- 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/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/mapper/active_model_compat.rb +98 -0
- data/lib/praxis/mapper/resource.rb +242 -0
- data/lib/praxis/mapper/selector_generator.rb +154 -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 +3 -5
- 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 +5 -7
- 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 +2 -10
- data/lib/praxis/tasks/routes.rb +0 -1
- data/lib/praxis/trait.rb +1 -1
- data/lib/praxis/types/media_type_common.rb +2 -2
- data/lib/praxis/types/multipart.rb +1 -1
- data/lib/praxis/types/multipart_array.rb +2 -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 +12 -26
- 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_helper.rb +13 -17
- data/spec/support/be_deep_equal_matcher.rb +39 -0
- data/spec/support/spec_resources.rb +124 -0
- metadata +74 -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
@@ -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
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
require 'praxis/mapper/active_model_compat'
|
4
|
+
|
5
|
+
# Creates a new in-memory DB, and the necessary tables (and mini-seeds) for the models in this file
|
6
|
+
def create_tables
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection(
|
9
|
+
adapter: 'sqlite3',
|
10
|
+
dbfile: ':memory:',
|
11
|
+
database: ':memory:'
|
12
|
+
)
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define do
|
15
|
+
ActiveRecord::Migration.suppress_messages do
|
16
|
+
create_table :active_books do |table|
|
17
|
+
table.column :simple_name, :string
|
18
|
+
table.column :added_column, :string
|
19
|
+
table.column :category_uuid, :string
|
20
|
+
table.column :author_id, :integer
|
21
|
+
end
|
22
|
+
|
23
|
+
create_table :active_authors do |table|
|
24
|
+
table.column :name, :string
|
25
|
+
end
|
26
|
+
|
27
|
+
create_table :active_categories do |table|
|
28
|
+
table.column :uuid, :string
|
29
|
+
table.column :name, :string
|
30
|
+
end
|
31
|
+
|
32
|
+
create_table :active_tags do |table|
|
33
|
+
table.column :name, :string
|
34
|
+
end
|
35
|
+
|
36
|
+
create_table :active_taggings do |table|
|
37
|
+
table.column :book_id, :integer
|
38
|
+
table.column :tag_id, :integer
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
create_tables
|
45
|
+
|
46
|
+
class ActiveBook < ActiveRecord::Base
|
47
|
+
include Praxis::Mapper::ActiveModelCompat
|
48
|
+
|
49
|
+
belongs_to :category, class_name: 'ActiveCategory', foreign_key: :category_uuid, primary_key: :uuid
|
50
|
+
belongs_to :author, class_name: 'ActiveAuthor'
|
51
|
+
has_many :taggings, class_name: 'ActiveTagging', foreign_key: :book_id
|
52
|
+
has_many :tags, class_name: 'ActiveTag', through: :taggings
|
53
|
+
end
|
54
|
+
|
55
|
+
class ActiveAuthor < ActiveRecord::Base
|
56
|
+
include Praxis::Mapper::ActiveModelCompat
|
57
|
+
has_many :books, class_name: 'ActiveBook', foreign_key: :author_id
|
58
|
+
end
|
59
|
+
|
60
|
+
class ActiveCategory < ActiveRecord::Base
|
61
|
+
include Praxis::Mapper::ActiveModelCompat
|
62
|
+
has_many :books, class_name: 'ActiveBook', primary_key: :uuid, foreign_key: :category_uuid
|
63
|
+
end
|
64
|
+
|
65
|
+
class ActiveTag < ActiveRecord::Base
|
66
|
+
include Praxis::Mapper::ActiveModelCompat
|
67
|
+
end
|
68
|
+
|
69
|
+
class ActiveTagging < ActiveRecord::Base
|
70
|
+
include Praxis::Mapper::ActiveModelCompat
|
71
|
+
belongs_to :book, class_name: 'ActiveBook', foreign_key: :book_id
|
72
|
+
belongs_to :tag, class_name: 'ActiveTag', foreign_key: :tag_id
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
# A set of resource classes for use in specs
|
77
|
+
class ActiveBaseResource < Praxis::Mapper::Resource
|
78
|
+
end
|
79
|
+
|
80
|
+
class ActiveAuthorResource < ActiveBaseResource
|
81
|
+
model ActiveAuthor
|
82
|
+
|
83
|
+
property :display_name, dependencies: [:name]
|
84
|
+
end
|
85
|
+
|
86
|
+
class ActiveCategoryResource < ActiveBaseResource
|
87
|
+
model ActiveCategory
|
88
|
+
end
|
89
|
+
|
90
|
+
class ActiveTagResource < ActiveBaseResource
|
91
|
+
model ActiveTag
|
92
|
+
end
|
93
|
+
|
94
|
+
class ActiveBookResource < ActiveBaseResource
|
95
|
+
model ActiveBook
|
96
|
+
|
97
|
+
# Forces to add an extra column (added_column)...and yet another (author_id) that will serve
|
98
|
+
# to check that if that's already automatically added due to an association, it won't interfere or duplicate
|
99
|
+
property :author, dependencies: [:author, :added_column, :author_id]
|
100
|
+
|
101
|
+
property :name, dependencies: [:simple_name]
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
def seed_data
|
106
|
+
cat1 = ActiveCategory.create( id: 1 , uuid: 'deadbeef1', name: 'cat1' )
|
107
|
+
cat2 = ActiveCategory.create( id: 2 , uuid: 'deadbeef2', name: 'cat2' )
|
108
|
+
|
109
|
+
author1 = ActiveAuthor.create( id: 11, name: 'author1' )
|
110
|
+
author2 = ActiveAuthor.create( id: 22, name: 'author2' )
|
111
|
+
|
112
|
+
tag_blue = ActiveTag.create(id: 1 , name: 'blue' )
|
113
|
+
tag_red = ActiveTag.create(id: 2 , name: 'red' )
|
114
|
+
|
115
|
+
book1 = ActiveBook.create( id: 1 , simple_name: 'Book1', category_uuid: 'deadbeef1')
|
116
|
+
book1.author = author1
|
117
|
+
book1.category = cat1
|
118
|
+
book1.save
|
119
|
+
ActiveTagging.create(book: book1, tag: tag_blue)
|
120
|
+
ActiveTagging.create(book: book1, tag: tag_red)
|
121
|
+
|
122
|
+
|
123
|
+
book2 = ActiveBook.create( id: 2 , simple_name: 'Book2', category_uuid: 'deadbeef1')
|
124
|
+
book2.author = author2
|
125
|
+
book2.category = cat2
|
126
|
+
book2.save
|
127
|
+
ActiveTagging.create(book: book2, tag: tag_red)
|
128
|
+
end
|
129
|
+
|
130
|
+
seed_data
|