rom-sql 2.0.0.beta2 → 2.0.0.beta3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +66 -0
- data/lib/rom/plugins/relation/sql/postgres/explain.rb +54 -0
- data/lib/rom/sql.rb +1 -1
- data/lib/rom/sql/attribute.rb +17 -18
- data/lib/rom/sql/errors.rb +3 -0
- data/lib/rom/sql/extensions/mysql.rb +1 -1
- data/lib/rom/sql/extensions/mysql/type_builder.rb +28 -0
- data/lib/rom/sql/extensions/postgres.rb +3 -1
- data/lib/rom/sql/extensions/postgres/commands.rb +30 -13
- data/lib/rom/sql/extensions/postgres/{attributes_inferrer.rb → type_builder.rb} +24 -28
- data/lib/rom/sql/extensions/postgres/type_serializer.rb +39 -0
- data/lib/rom/sql/extensions/postgres/types.rb +24 -477
- data/lib/rom/sql/extensions/postgres/types/array.rb +163 -0
- data/lib/rom/sql/extensions/postgres/types/geometric.rb +135 -0
- data/lib/rom/sql/extensions/postgres/types/json.rb +235 -0
- data/lib/rom/sql/extensions/postgres/types/network.rb +15 -0
- data/lib/rom/sql/extensions/sqlite.rb +1 -1
- data/lib/rom/sql/extensions/sqlite/{attributes_inferrer.rb → type_builder.rb} +5 -5
- data/lib/rom/sql/extensions/sqlite/types.rb +8 -3
- data/lib/rom/sql/foreign_key.rb +17 -0
- data/lib/rom/sql/function.rb +86 -8
- data/lib/rom/sql/gateway.rb +26 -26
- data/lib/rom/sql/index.rb +4 -0
- data/lib/rom/sql/migration.rb +3 -3
- data/lib/rom/sql/migration/inline_runner.rb +9 -83
- data/lib/rom/sql/migration/migrator.rb +35 -12
- data/lib/rom/sql/migration/recorder.rb +21 -0
- data/lib/rom/sql/migration/runner.rb +115 -0
- data/lib/rom/sql/migration/schema_diff.rb +108 -53
- data/lib/rom/sql/migration/writer.rb +61 -0
- data/lib/rom/sql/relation.rb +2 -1
- data/lib/rom/sql/relation/reading.rb +63 -3
- data/lib/rom/sql/relation/writing.rb +38 -0
- data/lib/rom/sql/schema.rb +9 -3
- data/lib/rom/sql/schema/attributes_inferrer.rb +3 -119
- data/lib/rom/sql/schema/inferrer.rb +99 -18
- data/lib/rom/sql/schema/type_builder.rb +94 -0
- data/lib/rom/sql/type_dsl.rb +30 -0
- data/lib/rom/sql/type_extensions.rb +11 -6
- data/lib/rom/sql/type_serializer.rb +46 -0
- data/lib/rom/sql/types.rb +12 -0
- data/lib/rom/sql/version.rb +1 -1
- metadata +26 -244
- data/.codeclimate.yml +0 -15
- data/.gitignore +0 -17
- data/.rspec +0 -3
- data/.travis.yml +0 -39
- data/.yardopts +0 -2
- data/Gemfile +0 -33
- data/Guardfile +0 -24
- data/LICENSE.txt +0 -22
- data/Rakefile +0 -19
- data/circle.yml +0 -10
- data/lib/rom/sql/extensions/mysql/attributes_inferrer.rb +0 -10
- data/lib/rom/sql/relation/sequel_api.rb +0 -133
- data/log/.gitkeep +0 -0
- data/rom-sql.gemspec +0 -29
- data/spec/extensions/postgres/attribute_spec.rb +0 -217
- data/spec/extensions/postgres/integration_spec.rb +0 -59
- data/spec/extensions/postgres/types_spec.rb +0 -252
- data/spec/extensions/sqlite/types_spec.rb +0 -11
- data/spec/fixtures/migrations/20150403090603_create_carrots.rb +0 -8
- data/spec/integration/associations/many_to_many/custom_fks_spec.rb +0 -76
- data/spec/integration/associations/many_to_many/from_view_spec.rb +0 -88
- data/spec/integration/associations/many_to_many_spec.rb +0 -162
- data/spec/integration/associations/many_to_one/custom_fks_spec.rb +0 -64
- data/spec/integration/associations/many_to_one/from_view_spec.rb +0 -84
- data/spec/integration/associations/many_to_one/self_ref_spec.rb +0 -53
- data/spec/integration/associations/many_to_one_spec.rb +0 -117
- data/spec/integration/associations/one_to_many/custom_fks_spec.rb +0 -54
- data/spec/integration/associations/one_to_many/from_view_spec.rb +0 -57
- data/spec/integration/associations/one_to_many/self_ref_spec.rb +0 -54
- data/spec/integration/associations/one_to_many_spec.rb +0 -86
- data/spec/integration/associations/one_to_one_spec.rb +0 -69
- data/spec/integration/associations/one_to_one_through_spec.rb +0 -92
- data/spec/integration/auto_migrations/errors_spec.rb +0 -31
- data/spec/integration/auto_migrations/indexes_spec.rb +0 -253
- data/spec/integration/auto_migrations/managing_columns_spec.rb +0 -156
- data/spec/integration/auto_migrations/postgres/column_types_spec.rb +0 -63
- data/spec/integration/combine_with_spec.rb +0 -43
- data/spec/integration/commands/create_spec.rb +0 -304
- data/spec/integration/commands/delete_spec.rb +0 -84
- data/spec/integration/commands/update_spec.rb +0 -90
- data/spec/integration/commands/upsert_spec.rb +0 -83
- data/spec/integration/gateway_spec.rb +0 -107
- data/spec/integration/migration_spec.rb +0 -55
- data/spec/integration/plugins/associates/many_to_many_spec.rb +0 -69
- data/spec/integration/plugins/associates_spec.rb +0 -250
- data/spec/integration/plugins/auto_restrictions_spec.rb +0 -74
- data/spec/integration/relation_schema_spec.rb +0 -271
- data/spec/integration/schema/call_spec.rb +0 -24
- data/spec/integration/schema/inferrer/mysql_spec.rb +0 -45
- data/spec/integration/schema/inferrer/postgres_spec.rb +0 -203
- data/spec/integration/schema/inferrer/sqlite_spec.rb +0 -37
- data/spec/integration/schema/inferrer_spec.rb +0 -390
- data/spec/integration/schema/prefix_spec.rb +0 -16
- data/spec/integration/schema/qualified_spec.rb +0 -16
- data/spec/integration/schema/rename_spec.rb +0 -21
- data/spec/integration/schema/view_spec.rb +0 -29
- data/spec/integration/sequel_api_spec.rb +0 -36
- data/spec/integration/setup_spec.rb +0 -26
- data/spec/integration/support/active_support_notifications_spec.rb +0 -24
- data/spec/integration/support/rails_log_subscriber_spec.rb +0 -30
- data/spec/integration/wrap_spec.rb +0 -91
- data/spec/shared/accounts.rb +0 -48
- data/spec/shared/database_setup.rb +0 -70
- data/spec/shared/notes.rb +0 -23
- data/spec/shared/posts.rb +0 -34
- data/spec/shared/puppies.rb +0 -15
- data/spec/shared/relations.rb +0 -8
- data/spec/shared/users.rb +0 -32
- data/spec/shared/users_and_tasks.rb +0 -50
- data/spec/spec_helper.rb +0 -122
- data/spec/support/env_helper.rb +0 -25
- data/spec/support/helpers.rb +0 -24
- data/spec/support/oracle/create_users.sql +0 -7
- data/spec/support/oracle/set_sys_passwords.sql +0 -2
- data/spec/support/test_configuration.rb +0 -16
- data/spec/unit/attribute_spec.rb +0 -104
- data/spec/unit/function_spec.rb +0 -48
- data/spec/unit/gateway_spec.rb +0 -70
- data/spec/unit/logger_spec.rb +0 -14
- data/spec/unit/migration_tasks_spec.rb +0 -111
- data/spec/unit/migrator_spec.rb +0 -25
- data/spec/unit/order_dsl_spec.rb +0 -43
- data/spec/unit/plugin/associates_spec.rb +0 -94
- data/spec/unit/plugin/pagination_spec.rb +0 -91
- data/spec/unit/plugin/timestamp_spec.rb +0 -117
- data/spec/unit/projection_dsl_spec.rb +0 -110
- data/spec/unit/relation/assoc_spec.rb +0 -87
- data/spec/unit/relation/associations_spec.rb +0 -27
- data/spec/unit/relation/avg_spec.rb +0 -11
- data/spec/unit/relation/by_pk_spec.rb +0 -62
- data/spec/unit/relation/dataset_spec.rb +0 -50
- data/spec/unit/relation/distinct_spec.rb +0 -15
- data/spec/unit/relation/exclude_spec.rb +0 -11
- data/spec/unit/relation/exist_predicate_spec.rb +0 -25
- data/spec/unit/relation/exists_spec.rb +0 -18
- data/spec/unit/relation/fetch_spec.rb +0 -21
- data/spec/unit/relation/group_spec.rb +0 -61
- data/spec/unit/relation/having_spec.rb +0 -22
- data/spec/unit/relation/inner_join_spec.rb +0 -158
- data/spec/unit/relation/inspect_spec.rb +0 -11
- data/spec/unit/relation/instrument_spec.rb +0 -45
- data/spec/unit/relation/invert_spec.rb +0 -11
- data/spec/unit/relation/left_join_spec.rb +0 -55
- data/spec/unit/relation/lock_spec.rb +0 -93
- data/spec/unit/relation/map_spec.rb +0 -16
- data/spec/unit/relation/max_spec.rb +0 -11
- data/spec/unit/relation/min_spec.rb +0 -11
- data/spec/unit/relation/order_spec.rb +0 -51
- data/spec/unit/relation/pluck_spec.rb +0 -11
- data/spec/unit/relation/prefix_spec.rb +0 -29
- data/spec/unit/relation/primary_key_spec.rb +0 -27
- data/spec/unit/relation/project_spec.rb +0 -24
- data/spec/unit/relation/qualified_columns_spec.rb +0 -30
- data/spec/unit/relation/qualified_spec.rb +0 -25
- data/spec/unit/relation/read_spec.rb +0 -25
- data/spec/unit/relation/rename_spec.rb +0 -23
- data/spec/unit/relation/right_join_spec.rb +0 -57
- data/spec/unit/relation/select_append_spec.rb +0 -21
- data/spec/unit/relation/select_spec.rb +0 -40
- data/spec/unit/relation/sum_spec.rb +0 -11
- data/spec/unit/relation/union_spec.rb +0 -19
- data/spec/unit/relation/unique_predicate_spec.rb +0 -18
- data/spec/unit/relation/where_spec.rb +0 -133
- data/spec/unit/restriction_dsl_spec.rb +0 -34
- data/spec/unit/schema_spec.rb +0 -25
- data/spec/unit/types_spec.rb +0 -65
@@ -1,162 +0,0 @@
|
|
1
|
-
RSpec.describe ROM::SQL::Associations::ManyToMany, helpers: true do
|
2
|
-
include_context 'users and tasks'
|
3
|
-
|
4
|
-
with_adapters do
|
5
|
-
context 'through a relation with a composite PK' do
|
6
|
-
subject(:assoc) do
|
7
|
-
build_assoc(:many_to_many, :tasks, :tags, through: :task_tags)
|
8
|
-
end
|
9
|
-
|
10
|
-
let(:tags) { relations[:tags] }
|
11
|
-
|
12
|
-
before do
|
13
|
-
conf.relation(:task_tags) do
|
14
|
-
schema(infer: true) do
|
15
|
-
associations do
|
16
|
-
belongs_to :task
|
17
|
-
belongs_to :tag
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
conf.relation(:tasks) do
|
23
|
-
schema(infer: true) do
|
24
|
-
associations do
|
25
|
-
has_many :task_tags
|
26
|
-
has_many :tags, through: :task_tags
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe '#result' do
|
33
|
-
specify { expect(assoc.result).to be(:many) }
|
34
|
-
end
|
35
|
-
|
36
|
-
describe '#combine_keys' do
|
37
|
-
specify { expect(assoc.combine_keys).to eql(id: :task_id) }
|
38
|
-
end
|
39
|
-
|
40
|
-
describe '#call' do
|
41
|
-
it 'prepares joined relations' do
|
42
|
-
relation = assoc.()
|
43
|
-
|
44
|
-
expect(relation.schema.map(&:to_sql_name)).
|
45
|
-
to eql([Sequel.qualify(:tags, :id),
|
46
|
-
Sequel.qualify(:tags, :name),
|
47
|
-
Sequel.qualify(:task_tags, :task_id)])
|
48
|
-
expect(relation.to_a).to eql([id: 1, name: 'important', task_id: 1])
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe ':through another assoc' do
|
53
|
-
subject(:assoc) do
|
54
|
-
build_assoc(:many_to_many, :users, :tags, through: :tasks)
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'prepares joined relations through other association' do
|
58
|
-
relation = assoc.()
|
59
|
-
|
60
|
-
expect(relation.schema.map(&:to_sql_name)).
|
61
|
-
to eql([Sequel.qualify(:tags, :id),
|
62
|
-
Sequel.qualify(:tags, :name),
|
63
|
-
Sequel.qualify(:tasks, :user_id)])
|
64
|
-
expect(relation.to_a).to eql([id: 1, name: 'important', user_id: 2])
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
describe '#eager_load' do
|
69
|
-
it 'preloads relation based on association' do
|
70
|
-
relation = tags.eager_load(assoc).call(tasks.call)
|
71
|
-
|
72
|
-
expect(relation.to_a).to eql([id: 1, name: 'important', task_id: 1])
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'maintains original relation' do
|
76
|
-
relation = tags.
|
77
|
-
select_append(tags[:name].as(:tag)).
|
78
|
-
eager_load(assoc).call(tasks.call)
|
79
|
-
|
80
|
-
expect(relation.to_a).to eql([id: 1, tag: 'important', name: 'important', task_id: 1])
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'respects custom order' do
|
84
|
-
conn[:tags].insert id: 2, name: 'boring'
|
85
|
-
conn[:task_tags].insert(tag_id: 2, task_id: 1)
|
86
|
-
|
87
|
-
relation = tags.
|
88
|
-
order(tags[:name].qualified).
|
89
|
-
eager_load(assoc).call(tasks.call)
|
90
|
-
|
91
|
-
expect(relation.to_a).
|
92
|
-
to eql([
|
93
|
-
{ id: 2, name: 'boring', task_id: 1 },
|
94
|
-
{ id: 1, name: 'important', task_id: 1 }
|
95
|
-
])
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
context 'with two associations pointing to the same target relation' do
|
101
|
-
before do
|
102
|
-
inferrable_relations.concat %i(users_tasks)
|
103
|
-
end
|
104
|
-
|
105
|
-
before do
|
106
|
-
conn.create_table(:users_tasks) do
|
107
|
-
foreign_key :user_id, :users
|
108
|
-
foreign_key :task_id, :tasks
|
109
|
-
primary_key [:user_id, :task_id]
|
110
|
-
end
|
111
|
-
|
112
|
-
conf.relation(:users) do
|
113
|
-
schema(infer: true) do
|
114
|
-
associations do
|
115
|
-
has_many :users_tasks
|
116
|
-
has_many :tasks, through: :users_tasks
|
117
|
-
has_many :tasks, as: :priv_tasks
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
conf.relation(:users_tasks) do
|
123
|
-
schema(infer: true) do
|
124
|
-
associations do
|
125
|
-
belongs_to :user
|
126
|
-
belongs_to :task
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
conf.relation(:tasks) do
|
132
|
-
schema(infer: true) do
|
133
|
-
associations do
|
134
|
-
has_many :users_tasks
|
135
|
-
has_many :users, through: :users_tasks
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
it 'does not conflict with two FKs' do
|
142
|
-
users = container.relations[:users]
|
143
|
-
tasks = container.relations[:tasks]
|
144
|
-
assoc = users.associations[:tasks]
|
145
|
-
|
146
|
-
relation = tasks.eager_load(assoc).call(users.call)
|
147
|
-
|
148
|
-
expect(relation.to_a).to be_empty
|
149
|
-
end
|
150
|
-
|
151
|
-
it 'preloads using FK' do
|
152
|
-
users = container.relations[:users]
|
153
|
-
tasks = container.relations[:tasks]
|
154
|
-
assoc = users.associations[:priv_tasks]
|
155
|
-
|
156
|
-
relation = tasks.eager_load(assoc).call(users.where(id: 2).call)
|
157
|
-
|
158
|
-
expect(relation.to_a).to eql([id: 1, user_id: 2, title: "Joe's task"])
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
@@ -1,64 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
RSpec.describe ROM::SQL::Associations::ManyToOne, '#call' do
|
4
|
-
include_context 'database setup'
|
5
|
-
|
6
|
-
before do
|
7
|
-
inferrable_relations.concat %i(destinations flights)
|
8
|
-
end
|
9
|
-
|
10
|
-
let(:assoc_from) { relations[:flights].associations[:from] }
|
11
|
-
let(:assoc_to) { relations[:flights].associations[:to] }
|
12
|
-
|
13
|
-
with_adapters do
|
14
|
-
before do
|
15
|
-
conn.create_table(:destinations) do
|
16
|
-
primary_key :id
|
17
|
-
column :name, String, null: false
|
18
|
-
end
|
19
|
-
|
20
|
-
conn.create_table(:flights) do
|
21
|
-
primary_key :id
|
22
|
-
foreign_key :from_id, :destinations, null: false
|
23
|
-
foreign_key :to_id, :destinations, null: false
|
24
|
-
column :code, String, null: false
|
25
|
-
end
|
26
|
-
|
27
|
-
conf.relation(:destinations) { schema(infer: true) }
|
28
|
-
|
29
|
-
conf.relation(:flights) do
|
30
|
-
schema(infer: true) do
|
31
|
-
associations do
|
32
|
-
belongs_to :destination, as: :from, foreign_key: :from_id
|
33
|
-
belongs_to :destination, as: :to, foreign_key: :to_id
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
from_id = relations[:destinations].insert(name: 'FROM')
|
39
|
-
to_id = relations[:destinations].insert(name: 'TO')
|
40
|
-
|
41
|
-
relations[:flights].insert(code: 'F1', from_id: from_id, to_id: to_id)
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'prepares joined relations using correct FKs based on association aliases' do
|
45
|
-
relation = assoc_from.()
|
46
|
-
|
47
|
-
expect(relation.schema.map(&:to_sql_name)).
|
48
|
-
to eql([Sequel.qualify(:destinations, :id),
|
49
|
-
Sequel.qualify(:destinations, :name),
|
50
|
-
Sequel.qualify(:flights, :id).as(:flight_id)])
|
51
|
-
|
52
|
-
expect(relation.first).to eql(id: 1, name: 'FROM', flight_id: 1)
|
53
|
-
|
54
|
-
relation = assoc_to.()
|
55
|
-
|
56
|
-
expect(relation.schema.map(&:to_sql_name)).
|
57
|
-
to eql([Sequel.qualify(:destinations, :id),
|
58
|
-
Sequel.qualify(:destinations, :name),
|
59
|
-
Sequel.qualify(:flights, :id).as(:flight_id)])
|
60
|
-
|
61
|
-
expect(relation.first).to eql(id: 2, name: 'TO', flight_id: 1)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
@@ -1,84 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
RSpec.describe ROM::SQL::Associations::ManyToOne, '#call' do
|
4
|
-
include_context 'database setup'
|
5
|
-
|
6
|
-
before do
|
7
|
-
inferrable_relations.concat %i(destinations flights)
|
8
|
-
end
|
9
|
-
|
10
|
-
let(:assoc_inter) { relations[:flights].associations[:inter_destination] }
|
11
|
-
let(:assoc_final) { relations[:flights].associations[:final_destination] }
|
12
|
-
|
13
|
-
with_adapters do
|
14
|
-
before do
|
15
|
-
conn.create_table(:destinations) do
|
16
|
-
primary_key :id
|
17
|
-
column :name, String, null: false
|
18
|
-
column :intermediate, TrueClass, null: false, default: false
|
19
|
-
end
|
20
|
-
|
21
|
-
conn.create_table(:flights) do
|
22
|
-
primary_key :id
|
23
|
-
foreign_key :destination_id, :destinations, null: false
|
24
|
-
column :code, String, null: false
|
25
|
-
end
|
26
|
-
|
27
|
-
conf.relation(:destinations) do
|
28
|
-
schema(infer: true)
|
29
|
-
|
30
|
-
view(:intermediate, schema) do
|
31
|
-
where(intermediate: true)
|
32
|
-
end
|
33
|
-
|
34
|
-
view(:final, schema) do
|
35
|
-
where(intermediate: false)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
conf.relation(:flights) do
|
40
|
-
schema(infer: true) do
|
41
|
-
associations do
|
42
|
-
belongs_to :destination, as: :inter_destination, view: :intermediate
|
43
|
-
belongs_to :destination, as: :final_destination, view: :final
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
final_id = relations[:destinations].insert(name: 'Final')
|
49
|
-
inter_id = relations[:destinations].insert(name: 'Intermediate', intermediate: true)
|
50
|
-
|
51
|
-
relations[:flights].insert(code: 'F1', destination_id: inter_id)
|
52
|
-
relations[:flights].insert(code: 'F2', destination_id: final_id)
|
53
|
-
end
|
54
|
-
|
55
|
-
after do
|
56
|
-
conn.drop_table(:flights)
|
57
|
-
conn.drop_table(:destinations)
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'prepares joined relations using custom view in target relation' do
|
61
|
-
relation = assoc_inter.()
|
62
|
-
|
63
|
-
expect(relation.schema.map(&:to_sql_name)).
|
64
|
-
to eql([Sequel.qualify(:destinations, :id),
|
65
|
-
Sequel.qualify(:destinations, :name),
|
66
|
-
Sequel.qualify(:destinations, :intermediate),
|
67
|
-
Sequel.qualify(:flights, :id).as(:flight_id)])
|
68
|
-
|
69
|
-
expect(relation.first).to eql(id: 2, intermediate: db_true, name: 'Intermediate', flight_id: 1)
|
70
|
-
expect(relation.count).to be(1)
|
71
|
-
|
72
|
-
relation = assoc_final.()
|
73
|
-
|
74
|
-
expect(relation.schema.map(&:to_sql_name)).
|
75
|
-
to eql([Sequel.qualify(:destinations, :id),
|
76
|
-
Sequel.qualify(:destinations, :name),
|
77
|
-
Sequel.qualify(:destinations, :intermediate),
|
78
|
-
Sequel.qualify(:flights, :id).as(:flight_id)])
|
79
|
-
|
80
|
-
expect(relation.first).to eql(id: 1, intermediate: db_false, name: 'Final', flight_id: 2)
|
81
|
-
expect(relation.count).to be(1)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
RSpec.describe ROM::SQL::Associations::ManyToOne, '#call' do
|
4
|
-
subject(:assoc) do
|
5
|
-
relations[:categories].associations[:parent]
|
6
|
-
end
|
7
|
-
|
8
|
-
include_context 'database setup'
|
9
|
-
|
10
|
-
with_adapters do
|
11
|
-
before do
|
12
|
-
conn.create_table(:categories) do
|
13
|
-
primary_key :id
|
14
|
-
foreign_key :parent_id, :categories, null: true
|
15
|
-
column :name, String, null: false
|
16
|
-
end
|
17
|
-
|
18
|
-
conf.relation(:categories) do
|
19
|
-
schema(infer: true) do
|
20
|
-
associations do
|
21
|
-
belongs_to :categories, as: :parent, foreign_key: :parent_id
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
p1_id = relations[:categories].insert(name: 'P1')
|
27
|
-
p2_id = relations[:categories].insert(name: 'P2')
|
28
|
-
relations[:categories].insert(name: 'C3', parent_id: p2_id)
|
29
|
-
relations[:categories].insert(name: 'C4', parent_id: p1_id)
|
30
|
-
relations[:categories].insert(name: 'C5', parent_id: p1_id)
|
31
|
-
end
|
32
|
-
|
33
|
-
after do
|
34
|
-
conn.drop_table(:categories)
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'prepares joined relations using custom FK for a self-ref association' do
|
38
|
-
relation = assoc.()
|
39
|
-
|
40
|
-
expect(relation.schema.map(&:to_sql_name)).
|
41
|
-
to eql([Sequel.qualify(:categories, :id),
|
42
|
-
Sequel.qualify(:categories, :parent_id),
|
43
|
-
Sequel.qualify(:categories, :name)])
|
44
|
-
|
45
|
-
expect(relation.to_a).
|
46
|
-
to eql([
|
47
|
-
{ id: 1, parent_id: nil, name: 'P1' },
|
48
|
-
{ id: 1, parent_id: nil, name: 'P1' },
|
49
|
-
{ id: 2, parent_id: nil, name: 'P2' }
|
50
|
-
])
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
@@ -1,117 +0,0 @@
|
|
1
|
-
RSpec.describe ROM::SQL::Associations::ManyToOne, helpers: true do
|
2
|
-
with_adapters do
|
3
|
-
context 'common name conventions' do
|
4
|
-
include_context 'users and tasks'
|
5
|
-
include_context 'accounts'
|
6
|
-
|
7
|
-
subject(:assoc) do
|
8
|
-
build_assoc(:many_to_one, :tasks, :users)
|
9
|
-
end
|
10
|
-
|
11
|
-
before do
|
12
|
-
conf.relation(:tasks) do
|
13
|
-
schema(infer: true)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
describe '#name' do
|
18
|
-
it 'uses target by default' do
|
19
|
-
expect(assoc.name).to be(:users)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
describe '#result' do
|
24
|
-
specify { expect(assoc.result).to be(:one) }
|
25
|
-
end
|
26
|
-
|
27
|
-
describe '#combine_keys' do
|
28
|
-
specify { expect(assoc.combine_keys).to eql(user_id: :id) }
|
29
|
-
end
|
30
|
-
|
31
|
-
describe '#call' do
|
32
|
-
it 'prepares joined relations' do
|
33
|
-
relation = assoc.(preload: false)
|
34
|
-
|
35
|
-
expect(relation.schema.map(&:to_sql_name)).
|
36
|
-
to eql([Sequel.qualify(:users, :id),
|
37
|
-
Sequel.qualify(:users, :name),
|
38
|
-
Sequel.qualify(:tasks, :id).as(:task_id)])
|
39
|
-
|
40
|
-
expect(relation.where(user_id: 1).one).to eql(id: 1, task_id: 2, name: 'Jane')
|
41
|
-
|
42
|
-
expect(relation.where(user_id: 2).one).to eql(id: 2, task_id: 1, name: 'Joe')
|
43
|
-
|
44
|
-
expect(relation.to_a).
|
45
|
-
to eql([
|
46
|
-
{ id: 1, task_id: 2, name: 'Jane' },
|
47
|
-
{ id: 2, task_id: 1, name: 'Joe' }
|
48
|
-
])
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe '#eager_load' do
|
53
|
-
it 'preloads relation based on association' do
|
54
|
-
relation = users.eager_load(assoc).call(tasks.call)
|
55
|
-
|
56
|
-
expect(relation.to_a).to eql([{ id: 1, name: 'Jane' }, { id: 2, name: 'Joe' }])
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'maintains original relation' do
|
60
|
-
users.accounts.insert(user_id: 2, number: '31', balance: 0)
|
61
|
-
|
62
|
-
relation = users.
|
63
|
-
join(:accounts, user_id: :id).
|
64
|
-
select_append(users.accounts[:number].as(:account_num)).
|
65
|
-
order(:account_num).
|
66
|
-
eager_load(assoc).call(tasks.call)
|
67
|
-
|
68
|
-
expect(relation.to_a).
|
69
|
-
to eql([{ id: 2, name: 'Joe', account_num: '31' },
|
70
|
-
{ id: 1, name: 'Jane', account_num: '42' }])
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
context 'arbitrary name conventions' do
|
76
|
-
include_context 'users'
|
77
|
-
include_context 'posts'
|
78
|
-
|
79
|
-
let(:articles_name) { ROM::Relation::Name[:articles, :posts] }
|
80
|
-
let(:articles) { container.relations[:articles] }
|
81
|
-
|
82
|
-
subject(:assoc) do
|
83
|
-
build_assoc(:many_to_one, articles_name, :users)
|
84
|
-
end
|
85
|
-
|
86
|
-
before do
|
87
|
-
conf.relation(:articles) do
|
88
|
-
schema(:posts, infer: true)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
describe '#call' do
|
93
|
-
it 'prepares joined relations' do
|
94
|
-
relation = assoc.()
|
95
|
-
|
96
|
-
expect(relation.schema.map(&:to_sql_name)).
|
97
|
-
to eql([Sequel.qualify(:users, :id),
|
98
|
-
Sequel.qualify(:users, :name),
|
99
|
-
Sequel.qualify(:posts, :post_id)])
|
100
|
-
|
101
|
-
expect(relation.order(:id).to_a).to eql([
|
102
|
-
{ id: 1, name: 'Jane', post_id: 2 },
|
103
|
-
{ id: 2, name: 'Joe', post_id: 1 }
|
104
|
-
])
|
105
|
-
|
106
|
-
expect(relation.where(author_id: 1).to_a).to eql(
|
107
|
-
[id: 1, name: 'Jane', post_id: 2]
|
108
|
-
)
|
109
|
-
|
110
|
-
expect(relation.where(author_id: 2).to_a).to eql(
|
111
|
-
[id: 2, name: 'Joe', post_id: 1]
|
112
|
-
)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|