rom-sql 1.3.5 → 2.0.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -7
  3. data/Gemfile +7 -5
  4. data/lib/rom/plugins/relation/sql/auto_restrictions.rb +11 -17
  5. data/lib/rom/sql.rb +3 -2
  6. data/lib/rom/sql/associations.rb +5 -0
  7. data/lib/rom/sql/associations/core.rb +20 -0
  8. data/lib/rom/sql/associations/many_to_many.rb +83 -0
  9. data/lib/rom/sql/associations/many_to_one.rb +55 -0
  10. data/lib/rom/sql/associations/one_to_many.rb +31 -0
  11. data/lib/rom/sql/{association → associations}/one_to_one.rb +3 -2
  12. data/lib/rom/sql/{association → associations}/one_to_one_through.rb +3 -2
  13. data/lib/rom/sql/associations/self_ref.rb +39 -0
  14. data/lib/rom/sql/attribute.rb +44 -54
  15. data/lib/rom/sql/errors.rb +2 -0
  16. data/lib/rom/sql/extensions/mysql.rb +1 -1
  17. data/lib/rom/sql/extensions/mysql/attributes_inferrer.rb +10 -0
  18. data/lib/rom/sql/extensions/postgres.rb +1 -1
  19. data/lib/rom/sql/extensions/postgres/{inferrer.rb → attributes_inferrer.rb} +4 -4
  20. data/lib/rom/sql/extensions/postgres/types.rb +9 -19
  21. data/lib/rom/sql/extensions/sqlite.rb +1 -1
  22. data/lib/rom/sql/extensions/sqlite/{inferrer.rb → attributes_inferrer.rb} +2 -2
  23. data/lib/rom/sql/gateway.rb +29 -30
  24. data/lib/rom/sql/index.rb +13 -0
  25. data/lib/rom/sql/migration.rb +10 -0
  26. data/lib/rom/sql/migration/inline_runner.rb +86 -0
  27. data/lib/rom/sql/migration/migrator.rb +17 -0
  28. data/lib/rom/sql/migration/schema_diff.rb +177 -0
  29. data/lib/rom/sql/plugin/associates.rb +11 -45
  30. data/lib/rom/sql/plugin/pagination.rb +4 -4
  31. data/lib/rom/sql/relation.rb +22 -42
  32. data/lib/rom/sql/relation/reading.rb +3 -3
  33. data/lib/rom/sql/schema.rb +14 -21
  34. data/lib/rom/sql/schema/associations_dsl.rb +7 -6
  35. data/lib/rom/sql/schema/attributes_inferrer.rb +164 -0
  36. data/lib/rom/sql/schema/inferrer.rb +40 -141
  37. data/lib/rom/sql/type_extensions.rb +44 -0
  38. data/lib/rom/sql/version.rb +1 -1
  39. data/lib/rom/sql/wrap.rb +25 -0
  40. data/rom-sql.gemspec +2 -2
  41. data/spec/integration/{association → associations}/many_to_many/custom_fks_spec.rb +4 -2
  42. data/spec/integration/{association → associations}/many_to_many/from_view_spec.rb +2 -2
  43. data/spec/integration/{association → associations}/many_to_many_spec.rb +25 -30
  44. data/spec/integration/{association → associations}/many_to_one/custom_fks_spec.rb +5 -3
  45. data/spec/integration/{association → associations}/many_to_one/from_view_spec.rb +3 -3
  46. data/spec/integration/{association → associations}/many_to_one/self_ref_spec.rb +2 -2
  47. data/spec/integration/{association → associations}/many_to_one_spec.rb +20 -38
  48. data/spec/integration/{association → associations}/one_to_many/custom_fks_spec.rb +4 -2
  49. data/spec/integration/{association → associations}/one_to_many/from_view_spec.rb +2 -2
  50. data/spec/integration/{association → associations}/one_to_many/self_ref_spec.rb +2 -2
  51. data/spec/integration/{association → associations}/one_to_many_spec.rb +24 -11
  52. data/spec/integration/{association → associations}/one_to_one_spec.rb +13 -9
  53. data/spec/integration/{association → associations}/one_to_one_through_spec.rb +15 -11
  54. data/spec/integration/auto_migrations/errors_spec.rb +31 -0
  55. data/spec/integration/auto_migrations/indexes_spec.rb +109 -0
  56. data/spec/integration/auto_migrations/managing_columns_spec.rb +156 -0
  57. data/spec/integration/auto_migrations/postgres/column_types_spec.rb +63 -0
  58. data/spec/integration/commands/create_spec.rb +2 -4
  59. data/spec/integration/commands/delete_spec.rb +2 -2
  60. data/spec/integration/commands/update_spec.rb +2 -0
  61. data/spec/integration/graph_spec.rb +9 -3
  62. data/spec/integration/plugins/associates_spec.rb +16 -55
  63. data/spec/integration/plugins/auto_restrictions_spec.rb +0 -11
  64. data/spec/integration/relation_schema_spec.rb +49 -25
  65. data/spec/integration/schema/inferrer/postgres_spec.rb +1 -1
  66. data/spec/integration/schema/inferrer_spec.rb +7 -18
  67. data/spec/integration/setup_spec.rb +4 -0
  68. data/spec/integration/{plugins/auto_wrap_spec.rb → wrap_spec.rb} +13 -36
  69. data/spec/shared/accounts.rb +4 -0
  70. data/spec/shared/database_setup.rb +2 -1
  71. data/spec/shared/notes.rb +2 -0
  72. data/spec/shared/posts.rb +2 -0
  73. data/spec/shared/puppies.rb +2 -0
  74. data/spec/shared/relations.rb +2 -2
  75. data/spec/shared/users.rb +2 -0
  76. data/spec/shared/users_and_tasks.rb +4 -0
  77. data/spec/spec_helper.rb +3 -6
  78. data/spec/support/helpers.rb +11 -8
  79. data/spec/support/test_configuration.rb +16 -0
  80. data/spec/unit/plugin/associates_spec.rb +5 -10
  81. data/spec/unit/plugin/pagination_spec.rb +9 -9
  82. data/spec/unit/plugin/timestamp_spec.rb +9 -9
  83. data/spec/unit/relation/dataset_spec.rb +7 -5
  84. data/spec/unit/relation/inner_join_spec.rb +2 -15
  85. data/spec/unit/relation/primary_key_spec.rb +1 -1
  86. data/spec/unit/schema_spec.rb +6 -4
  87. metadata +65 -70
  88. data/lib/rom/plugins/relation/sql/auto_combine.rb +0 -71
  89. data/lib/rom/plugins/relation/sql/auto_wrap.rb +0 -62
  90. data/lib/rom/sql/association.rb +0 -103
  91. data/lib/rom/sql/association/many_to_many.rb +0 -119
  92. data/lib/rom/sql/association/many_to_one.rb +0 -73
  93. data/lib/rom/sql/association/name.rb +0 -78
  94. data/lib/rom/sql/association/one_to_many.rb +0 -60
  95. data/lib/rom/sql/extensions/mysql/inferrer.rb +0 -10
  96. data/lib/rom/sql/qualified_attribute.rb +0 -53
  97. data/lib/rom/sql/schema/dsl.rb +0 -75
  98. data/spec/unit/association/many_to_many_spec.rb +0 -89
  99. data/spec/unit/association/many_to_one_spec.rb +0 -81
  100. data/spec/unit/association/name_spec.rb +0 -68
  101. data/spec/unit/association/one_to_many_spec.rb +0 -82
  102. data/spec/unit/association/one_to_one_spec.rb +0 -83
  103. data/spec/unit/association/one_to_one_through_spec.rb +0 -69
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe ROM::SQL::Association::ManyToOne, '#call' do
3
+ RSpec.describe ROM::SQL::Associations::ManyToOne, '#call' do
4
4
  include_context 'database setup'
5
5
 
6
6
  before do
@@ -24,6 +24,8 @@ RSpec.describe ROM::SQL::Association::ManyToOne, '#call' do
24
24
  column :code, String, null: false
25
25
  end
26
26
 
27
+ conf.relation(:destinations) { schema(infer: true) }
28
+
27
29
  conf.relation(:flights) do
28
30
  schema(infer: true) do
29
31
  associations do
@@ -40,7 +42,7 @@ RSpec.describe ROM::SQL::Association::ManyToOne, '#call' do
40
42
  end
41
43
 
42
44
  it 'prepares joined relations using correct FKs based on association aliases' do
43
- relation = assoc_from.call(relations)
45
+ relation = assoc_from.()
44
46
 
45
47
  expect(relation.schema.map(&:to_sql_name)).
46
48
  to eql([Sequel.qualify(:destinations, :id),
@@ -49,7 +51,7 @@ RSpec.describe ROM::SQL::Association::ManyToOne, '#call' do
49
51
 
50
52
  expect(relation.first).to eql(id: 1, name: 'FROM', flight_id: 1)
51
53
 
52
- relation = assoc_to.call(relations)
54
+ relation = assoc_to.()
53
55
 
54
56
  expect(relation.schema.map(&:to_sql_name)).
55
57
  to eql([Sequel.qualify(:destinations, :id),
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe ROM::SQL::Association::ManyToOne, '#call' do
3
+ RSpec.describe ROM::SQL::Associations::ManyToOne, '#call' do
4
4
  include_context 'database setup'
5
5
 
6
6
  before do
@@ -58,7 +58,7 @@ RSpec.describe ROM::SQL::Association::ManyToOne, '#call' do
58
58
  end
59
59
 
60
60
  it 'prepares joined relations using custom view in target relation' do
61
- relation = assoc_inter.call(relations)
61
+ relation = assoc_inter.()
62
62
 
63
63
  expect(relation.schema.map(&:to_sql_name)).
64
64
  to eql([Sequel.qualify(:destinations, :id),
@@ -69,7 +69,7 @@ RSpec.describe ROM::SQL::Association::ManyToOne, '#call' do
69
69
  expect(relation.first).to eql(id: 2, intermediate: db_true, name: 'Intermediate', flight_id: 1)
70
70
  expect(relation.count).to be(1)
71
71
 
72
- relation = assoc_final.call(relations)
72
+ relation = assoc_final.()
73
73
 
74
74
  expect(relation.schema.map(&:to_sql_name)).
75
75
  to eql([Sequel.qualify(:destinations, :id),
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe ROM::SQL::Association::OneToMany, '#call' do
3
+ RSpec.describe ROM::SQL::Associations::ManyToOne, '#call' do
4
4
  subject(:assoc) do
5
5
  relations[:categories].associations[:parent]
6
6
  end
@@ -35,7 +35,7 @@ RSpec.describe ROM::SQL::Association::OneToMany, '#call' do
35
35
  end
36
36
 
37
37
  it 'prepares joined relations using custom FK for a self-ref association' do
38
- relation = assoc.call(relations)
38
+ relation = assoc.()
39
39
 
40
40
  expect(relation.schema.map(&:to_sql_name)).
41
41
  to eql([Sequel.qualify(:categories, :id),
@@ -1,45 +1,36 @@
1
- RSpec.describe ROM::SQL::Association::ManyToOne, helpers: true do
1
+ RSpec.describe ROM::SQL::Associations::ManyToOne, helpers: true do
2
2
  with_adapters do
3
3
  context 'common name conventions' do
4
4
  include_context 'users and tasks'
5
5
  include_context 'accounts'
6
6
 
7
- subject(:assoc) {
8
- ROM::SQL::Association::ManyToOne.new(:tasks, :users)
9
- }
7
+ subject(:assoc) do
8
+ build_assoc(:many_to_one, :tasks, :users)
9
+ end
10
10
 
11
11
  before do
12
12
  conf.relation(:tasks) do
13
- schema do
14
- attribute :id, ROM::SQL::Types::Serial
15
- attribute :user_id, ROM::SQL::Types::ForeignKey(:users)
16
- attribute :title, ROM::SQL::Types::String
17
- end
13
+ schema(infer: true)
18
14
  end
19
15
  end
20
16
 
21
- describe '#result' do
22
- specify { expect(ROM::SQL::Association::ManyToOne.result).to be(:one) }
23
- end
24
-
25
17
  describe '#name' do
26
18
  it 'uses target by default' do
27
19
  expect(assoc.name).to be(:users)
28
20
  end
29
21
  end
30
22
 
31
- describe '#target' do
32
- it 'builds full relation name' do
33
- assoc = ROM::SQL::Association::ManyToOne.new(:users, :tasks, relation: :foo)
23
+ describe '#result' do
24
+ specify { expect(assoc.result).to be(:one) }
25
+ end
34
26
 
35
- expect(assoc.name).to be(:tasks)
36
- expect(assoc.target).to eql(ROM::SQL::Association::Name[:foo, :tasks])
37
- end
27
+ describe '#combine_keys' do
28
+ specify { expect(assoc.combine_keys).to eql(user_id: :id) }
38
29
  end
39
30
 
40
31
  describe '#call' do
41
32
  it 'prepares joined relations' do
42
- relation = assoc.call(container.relations)
33
+ relation = assoc.(preload: false)
43
34
 
44
35
  expect(relation.schema.map(&:to_sql_name)).
45
36
  to eql([Sequel.qualify(:users, :id),
@@ -58,15 +49,11 @@ RSpec.describe ROM::SQL::Association::ManyToOne, helpers: true do
58
49
  end
59
50
  end
60
51
 
61
- describe ROM::Plugins::Relation::SQL::AutoCombine, '#for_combine' do
52
+ describe '#eager_load' do
62
53
  it 'preloads relation based on association' do
63
- relation = users.for_combine(assoc).call(tasks.call)
54
+ relation = users.eager_load(assoc).call(tasks.call)
64
55
 
65
- expect(relation.to_a).
66
- to eql([
67
- { id: 1, task_id: 2, name: 'Jane' },
68
- { id: 2, task_id: 1, name: 'Joe' }
69
- ])
56
+ expect(relation.to_a).to eql([{ id: 1, name: 'Jane' }, { id: 2, name: 'Joe' }])
70
57
  end
71
58
 
72
59
  it 'maintains original relation' do
@@ -76,11 +63,11 @@ RSpec.describe ROM::SQL::Association::ManyToOne, helpers: true do
76
63
  join(:accounts, user_id: :id).
77
64
  select_append(users.accounts[:number].as(:account_num)).
78
65
  order(:account_num).
79
- for_combine(assoc).call(tasks.call)
66
+ eager_load(assoc).call(tasks.call)
80
67
 
81
68
  expect(relation.to_a).
82
- to eql([{ id: 2, task_id: 1, name: 'Joe', account_num: '31' },
83
- { id: 1, task_id: 2, name: 'Jane', account_num: '42' }])
69
+ to eql([{ id: 2, name: 'Joe', account_num: '31' },
70
+ { id: 1, name: 'Jane', account_num: '42' }])
84
71
  end
85
72
  end
86
73
  end
@@ -93,23 +80,18 @@ RSpec.describe ROM::SQL::Association::ManyToOne, helpers: true do
93
80
  let(:articles) { container.relations[:articles] }
94
81
 
95
82
  subject(:assoc) do
96
- ROM::SQL::Association::ManyToOne.new(articles_name, :users)
83
+ build_assoc(:many_to_one, articles_name, :users)
97
84
  end
98
85
 
99
86
  before do
100
87
  conf.relation(:articles) do
101
- schema(:posts) do
102
- attribute :post_id, ROM::SQL::Types::Serial
103
- attribute :author_id, ROM::SQL::Types::ForeignKey(:users)
104
- attribute :title, ROM::SQL::Types::Strict::String
105
- attribute :body, ROM::SQL::Types::Strict::String
106
- end
88
+ schema(:posts, infer: true)
107
89
  end
108
90
  end
109
91
 
110
92
  describe '#call' do
111
93
  it 'prepares joined relations' do
112
- relation = assoc.call(container.relations)
94
+ relation = assoc.()
113
95
 
114
96
  expect(relation.schema.map(&:to_sql_name)).
115
97
  to eql([Sequel.qualify(:users, :id),
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe ROM::SQL::Association::OneToMany, '#call' do
3
+ RSpec.describe ROM::SQL::Associations::OneToMany, '#call' do
4
4
  include_context 'users'
5
5
 
6
6
  before do
@@ -20,6 +20,8 @@ RSpec.describe ROM::SQL::Association::OneToMany, '#call' do
20
20
  column :text, String, null: false
21
21
  end
22
22
 
23
+ conf.relation(:puzzles) { schema(infer: true) }
24
+
23
25
  conf.relation(:users) do
24
26
  schema(infer: true) do
25
27
  associations do
@@ -38,7 +40,7 @@ RSpec.describe ROM::SQL::Association::OneToMany, '#call' do
38
40
  end
39
41
 
40
42
  it 'prepares joined relations using custom FK' do
41
- relation = assoc.call(relations)
43
+ relation = assoc.()
42
44
 
43
45
  expect(relation.schema.map(&:to_sql_name)).
44
46
  to eql([Sequel.qualify(:puzzles, :id),
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe ROM::SQL::Association::OneToMany, '#call' do
3
+ RSpec.describe ROM::SQL::Associations::OneToMany, '#call' do
4
4
  include_context 'users'
5
5
 
6
6
  before do
@@ -42,7 +42,7 @@ RSpec.describe ROM::SQL::Association::OneToMany, '#call' do
42
42
  end
43
43
 
44
44
  it 'prepares joined relations using custom view' do
45
- relation = assoc.call(relations)
45
+ relation = assoc.()
46
46
 
47
47
  expect(relation.schema.map(&:to_sql_name)).
48
48
  to eql([Sequel.qualify(:puzzles, :id),
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe ROM::SQL::Association::OneToMany, '#call' do
3
+ RSpec.describe ROM::SQL::Associations::OneToMany, '#call' do
4
4
  include_context 'database setup'
5
5
 
6
6
  before do
@@ -36,7 +36,7 @@ RSpec.describe ROM::SQL::Association::OneToMany, '#call' do
36
36
  end
37
37
 
38
38
  it 'prepares joined relations using custom FK for a self-ref association' do
39
- relation = assoc.call(relations)
39
+ relation = assoc.()
40
40
 
41
41
  expect(relation.schema.map(&:to_sql_name)).
42
42
  to eql([Sequel.qualify(:categories, :id),
@@ -1,10 +1,10 @@
1
- RSpec.describe ROM::SQL::Association::OneToMany do
2
- subject(:assoc) {
3
- ROM::SQL::Association::OneToMany.new(:users, :tasks)
4
- }
5
-
1
+ RSpec.describe ROM::SQL::Associations::OneToMany, helpers: true do
6
2
  include_context 'users and tasks'
7
3
 
4
+ subject(:assoc) do
5
+ build_assoc(:one_to_many, :users, :tasks)
6
+ end
7
+
8
8
  with_adapters do
9
9
  before do
10
10
  conf.relation(:tasks) do
@@ -17,12 +17,25 @@ RSpec.describe ROM::SQL::Association::OneToMany do
17
17
  end
18
18
 
19
19
  describe '#result' do
20
- specify { expect(ROM::SQL::Association::OneToMany.result).to be(:many) }
20
+ specify { expect(assoc.result).to be(:many) }
21
+ end
22
+
23
+ describe '#combine_keys' do
24
+ specify { expect(assoc.combine_keys).to eql(id: :user_id) }
25
+ end
26
+
27
+ describe '#associate' do
28
+ it 'merges FKs into tuples' do
29
+ child = { name: 'Child' }
30
+ parent = { id: 312, name: 'Parent '}
31
+
32
+ expect(assoc.associate(child, parent)).to eql(user_id: 312, name: 'Child')
33
+ end
21
34
  end
22
35
 
23
36
  describe '#call' do
24
37
  it 'prepares joined relations' do
25
- relation = assoc.call(container.relations)
38
+ relation = assoc.()
26
39
 
27
40
  expect(relation.schema.map(&:name)).to eql(%i[id user_id title])
28
41
 
@@ -41,9 +54,9 @@ RSpec.describe ROM::SQL::Association::OneToMany do
41
54
  end
42
55
  end
43
56
 
44
- describe ROM::Plugins::Relation::SQL::AutoCombine, '#for_combine' do
57
+ describe '#eager_load' do
45
58
  it 'preloads relation based on association' do
46
- relation = tasks.for_combine(assoc).call(users.call)
59
+ relation = tasks.eager_load(assoc).call(users.call)
47
60
 
48
61
  expect(relation.to_a).to eql([
49
62
  { id: 1, user_id: 2, title: "Joe's task" },
@@ -55,7 +68,7 @@ RSpec.describe ROM::SQL::Association::OneToMany do
55
68
  relation = tasks.
56
69
  join(:task_tags, tag_id: :id).
57
70
  select_append(tasks.task_tags[:tag_id].qualified).
58
- for_combine(assoc).call(users.call)
71
+ eager_load(assoc).call(users.call)
59
72
 
60
73
  expect(relation.to_a).to eql([{ id: 1, user_id: 2, title: "Joe's task", tag_id: 1 }])
61
74
  end
@@ -63,7 +76,7 @@ RSpec.describe ROM::SQL::Association::OneToMany do
63
76
  it 'respects custom order' do
64
77
  relation = tasks.
65
78
  order(tasks[:title].qualified).
66
- for_combine(assoc).call(users.call)
79
+ eager_load(assoc).call(users.call)
67
80
 
68
81
  expect(relation.to_a).
69
82
  to eql([{ id: 2, user_id: 1, title: "Jane's task" }, { id: 1, user_id: 2, title: "Joe's task" }])
@@ -1,10 +1,10 @@
1
- RSpec.describe ROM::SQL::Association::OneToOne do
1
+ RSpec.describe ROM::SQL::Associations::OneToOne, helpers: true do
2
2
  include_context 'users'
3
3
  include_context 'accounts'
4
4
 
5
- subject(:assoc) {
6
- ROM::SQL::Association::OneToOne.new(:users, :accounts)
7
- }
5
+ subject(:assoc) do
6
+ build_assoc(:one_to_one, :users, :accounts)
7
+ end
8
8
 
9
9
  with_adapters do
10
10
  before do
@@ -21,16 +21,20 @@ RSpec.describe ROM::SQL::Association::OneToOne do
21
21
  end
22
22
 
23
23
  describe '#result' do
24
- specify { expect(ROM::SQL::Association::OneToOne.result).to be(:one) }
24
+ specify { expect(assoc.result).to be(:one) }
25
+ end
26
+
27
+ describe '#combine_keys' do
28
+ specify { expect(assoc.combine_keys).to eql(id: :user_id) }
25
29
  end
26
30
 
27
31
  describe '#call' do
28
32
  it 'prepares joined relations' do |example|
29
- relation = assoc.call(container.relations)
33
+ relation = assoc.()
30
34
 
31
35
  expect(relation.schema.map(&:name)).to eql(%i[id user_id number balance])
32
36
 
33
- # TODO: this if caluse should be removed when (and if) https://github.com/xerial/sqlite-jdbc/issues/112
37
+ # TODO: this if clause should be removed when (and if) https://github.com/xerial/sqlite-jdbc/issues/112
34
38
  # will be resolved. See https://github.com/rom-rb/rom-sql/issues/49 for details
35
39
  if jruby? && sqlite?(example)
36
40
  expect(relation.to_a).
@@ -44,9 +48,9 @@ RSpec.describe ROM::SQL::Association::OneToOne do
44
48
  end
45
49
  end
46
50
 
47
- describe ROM::Plugins::Relation::SQL::AutoCombine, '#for_combine' do
51
+ describe '#eager_load' do
48
52
  it 'preloads relation based on association' do |example|
49
- relation = accounts.for_combine(assoc).call(users.call)
53
+ relation = accounts.eager_load(assoc).call(users.call)
50
54
 
51
55
  # TODO: this if caluse should be removed when (and if) https://github.com/xerial/sqlite-jdbc/issues/112
52
56
  # will be resolved. See https://github.com/rom-rb/rom-sql/issues/49 for details
@@ -1,10 +1,10 @@
1
- RSpec.describe ROM::SQL::Association::OneToOneThrough do
1
+ RSpec.describe ROM::SQL::Associations::OneToOneThrough, helpers: true do
2
2
  include_context 'users'
3
3
  include_context 'accounts'
4
4
 
5
- subject(:assoc) {
6
- ROM::SQL::Association::OneToOneThrough.new(:users, :cards, through: :accounts)
7
- }
5
+ subject(:assoc) do
6
+ build_assoc(:one_to_one_through, :users, :cards, through: :accounts)
7
+ end
8
8
 
9
9
  with_adapters do
10
10
  before do
@@ -48,12 +48,16 @@ RSpec.describe ROM::SQL::Association::OneToOneThrough do
48
48
  end
49
49
 
50
50
  describe '#result' do
51
- specify { expect(ROM::SQL::Association::OneToOneThrough.result).to be(:one) }
51
+ specify { expect(assoc.result).to be(:one) }
52
+ end
53
+
54
+ describe '#combine_keys' do
55
+ specify { expect(assoc.combine_keys).to eql(id: :user_id) }
52
56
  end
53
57
 
54
58
  describe '#call' do
55
59
  it 'prepares joined relations' do
56
- relation = assoc.call(container.relations)
60
+ relation = assoc.()
57
61
 
58
62
  expect(relation.schema.map(&:name)).to eql(%i[id account_id pan user_id])
59
63
  expect(relation.to_a).to eql([id: 1, account_id: 1, pan: '*6789', user_id: 1])
@@ -62,24 +66,24 @@ RSpec.describe ROM::SQL::Association::OneToOneThrough do
62
66
 
63
67
  describe ':through another assoc' do
64
68
  subject(:assoc) do
65
- ROM::SQL::Association::OneToOneThrough.new(:users, :subscriptions, through: :accounts)
69
+ build_assoc(:one_to_one_through, :users, :subscriptions, through: :accounts)
66
70
  end
67
71
 
68
72
  let(:account_assoc) do
69
- ROM::SQL::Association::OneToOneThrough.new(:accounts, :subscriptions, through: :cards)
73
+ build_assoc(:one_to_one_through, :accounts, :subscriptions, through: :cards)
70
74
  end
71
75
 
72
76
  it 'prepares joined relations through other association' do
73
- relation = assoc.call(container.relations)
77
+ relation = assoc.()
74
78
 
75
79
  expect(relation.schema.map(&:name)).to eql(%i[id card_id service user_id])
76
80
  expect(relation.to_a).to eql([id: 1, card_id: 1, service: 'aws', user_id: 1])
77
81
  end
78
82
  end
79
83
 
80
- describe ROM::Plugins::Relation::SQL::AutoCombine, '#for_combine' do
84
+ describe '#eager_load' do
81
85
  it 'preloads relation based on association' do
82
- relation = cards.for_combine(assoc).call(users.call)
86
+ relation = cards.eager_load(assoc).call(users.call)
83
87
 
84
88
  expect(relation.to_a).to eql([id: 1, account_id: 1, pan: '*6789', user_id: 1])
85
89
  end
@@ -0,0 +1,31 @@
1
+ RSpec.describe ROM::SQL::Gateway, :postgres do
2
+ include_context 'database setup'
3
+
4
+ subject(:gateway) { container.gateways[:default] }
5
+
6
+ before do
7
+ conn.drop_table?(:users)
8
+ end
9
+
10
+ describe 'unsupported conversions' do
11
+ before do
12
+ conf.relation(:users) do
13
+ schema do
14
+ attribute :id, ROM::SQL::Types::Serial
15
+ attribute :name, ROM::SQL::Types::String
16
+ end
17
+ end
18
+ end
19
+
20
+ it 'raises an error' do
21
+ conn.create_table :users do
22
+ primary_key :id
23
+ column :name, Integer, null: false
24
+ end
25
+
26
+ expect {
27
+ gateway.auto_migrate!(conf)
28
+ }.to raise_error(ROM::SQL::UnsupportedConversion, /Don't know how to convert/)
29
+ end
30
+ end
31
+ end