rom-sql 0.6.1 → 0.7.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -5
  3. data/.rubocop_todo.yml +12 -6
  4. data/.travis.yml +3 -2
  5. data/CHANGELOG.md +27 -0
  6. data/Gemfile +1 -0
  7. data/lib/rom/plugins/relation/sql/auto_combine.rb +45 -0
  8. data/lib/rom/plugins/relation/sql/auto_wrap.rb +48 -0
  9. data/lib/rom/plugins/relation/sql/base_view.rb +31 -0
  10. data/lib/rom/sql.rb +17 -10
  11. data/lib/rom/sql/commands/error_wrapper.rb +1 -1
  12. data/lib/rom/sql/commands/update.rb +1 -1
  13. data/lib/rom/sql/gateway.rb +8 -2
  14. data/lib/rom/sql/header.rb +1 -1
  15. data/lib/rom/sql/migration.rb +11 -20
  16. data/lib/rom/sql/migration/migrator.rb +4 -0
  17. data/lib/rom/sql/{relation/associations.rb → plugin/assoc_macros.rb} +17 -2
  18. data/lib/rom/sql/plugin/assoc_macros/class_interface.rb +128 -0
  19. data/lib/rom/sql/plugin/associates.rb +2 -4
  20. data/lib/rom/sql/plugin/pagination.rb +1 -1
  21. data/lib/rom/sql/plugins.rb +4 -2
  22. data/lib/rom/sql/relation.rb +46 -6
  23. data/lib/rom/sql/relation/reading.rb +63 -0
  24. data/lib/rom/sql/tasks/migration_tasks.rake +37 -16
  25. data/lib/rom/sql/version.rb +1 -1
  26. data/rom-sql.gemspec +2 -2
  27. data/spec/fixtures/migrations/20150403090603_create_carrots.rb +1 -1
  28. data/spec/integration/combine_spec.rb +6 -6
  29. data/spec/integration/commands/create_spec.rb +25 -25
  30. data/spec/integration/commands/delete_spec.rb +6 -6
  31. data/spec/integration/commands/update_spec.rb +5 -5
  32. data/spec/integration/{repository_spec.rb → gateway_spec.rb} +34 -21
  33. data/spec/integration/migration_spec.rb +3 -3
  34. data/spec/integration/read_spec.rb +13 -7
  35. data/spec/shared/database_setup.rb +3 -5
  36. data/spec/spec_helper.rb +3 -6
  37. data/spec/support/active_support_notifications_spec.rb +1 -1
  38. data/spec/support/rails_log_subscriber_spec.rb +1 -1
  39. data/spec/unit/association_errors_spec.rb +4 -3
  40. data/spec/unit/combined_associations_spec.rb +7 -5
  41. data/spec/unit/gateway_spec.rb +2 -2
  42. data/spec/unit/logger_spec.rb +1 -1
  43. data/spec/unit/many_to_many_spec.rb +7 -4
  44. data/spec/unit/many_to_one_spec.rb +14 -8
  45. data/spec/unit/migration_tasks_spec.rb +7 -6
  46. data/spec/unit/one_to_many_spec.rb +16 -10
  47. data/spec/unit/plugin/base_view_spec.rb +18 -0
  48. data/spec/unit/plugin/pagination_spec.rb +10 -10
  49. data/spec/unit/relation_spec.rb +69 -3
  50. data/spec/unit/schema_spec.rb +5 -3
  51. metadata +19 -26
  52. data/lib/rom/sql/relation/class_methods.rb +0 -116
  53. data/lib/rom/sql/relation/inspection.rb +0 -16
@@ -1,19 +1,20 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  namespace :db do
4
- task :setup do
5
- # noop
4
+ task :setup do
5
+ #noop
6
6
  end
7
7
  end
8
8
 
9
9
  describe 'MigrationTasks' do
10
+ let(:configuration) { ROM::Configuration.new(:sql, DB_URI) }
11
+ let!(:container) { ROM.container(configuration) }
12
+ let(:migrator) { container.gateways[:default].migrator }
13
+
10
14
  before do
11
- ROM.setup(:sql, ['postgres://localhost/rom'])
12
- ROM.finalize
15
+ ROM::SQL::RakeSupport.stub(:env) { configuration }
13
16
  end
14
17
 
15
- let(:migrator) { ROM.env.gateways[:default].migrator }
16
-
17
18
  context 'db:reset' do
18
19
  it 'calls proper commands' do
19
20
  expect(migrator).to receive(:run).with(target: 0)
@@ -7,17 +7,21 @@ describe 'Defining one-to-many association' do
7
7
  conn[:users].insert id: 2, name: 'Jane'
8
8
  conn[:tasks].insert id: 2, user_id: 2, title: 'Task one'
9
9
 
10
- setup.mappers do
10
+ configuration.mappers do
11
11
  define(:users)
12
12
 
13
13
  define(:with_tasks, parent: :users) do
14
14
  group tasks: [:tasks_id, :title]
15
15
  end
16
16
  end
17
+
18
+ configuration.relation(:tasks) { use :assoc_macros }
17
19
  end
18
20
 
19
21
  it 'extends relation with association methods' do
20
- setup.relation(:users) do
22
+ configuration.relation(:users) do
23
+ use :assoc_macros
24
+
21
25
  one_to_many :tasks, key: :user_id, on: { title: 'Finish ROM' }
22
26
 
23
27
  def by_name(name)
@@ -33,14 +37,14 @@ describe 'Defining one-to-many association' do
33
37
  end
34
38
  end
35
39
 
36
- users = rom.relations.users
40
+ users = container.relations.users
37
41
 
38
42
  expect(users.with_tasks.by_name("Piotr").to_a).to eql(
39
43
  [{ id: 1, name: 'Piotr', tasks_id: 1, title: 'Finish ROM' }]
40
44
  )
41
45
 
42
- result = rom.relation(:users).map_with(:with_tasks)
43
- .all.with_tasks.by_name("Piotr").to_a
46
+ result = container.relation(:users).map_with(:with_tasks)
47
+ .all.with_tasks.by_name("Piotr").to_a
44
48
 
45
49
  expect(result).to eql(
46
50
  [{ id: 1, name: 'Piotr', tasks: [{ tasks_id: 1, title: 'Finish ROM' }] }]
@@ -48,9 +52,11 @@ describe 'Defining one-to-many association' do
48
52
  end
49
53
 
50
54
  it 'allows setting :conditions' do
51
- setup.relation(:users) do
55
+ configuration.relation(:users) do
56
+ use :assoc_macros
57
+
52
58
  one_to_many :piotrs_tasks, relation: :tasks, key: :user_id,
53
- conditions: { name: 'Piotr' }
59
+ conditions: { name: 'Piotr' }
54
60
 
55
61
  def with_piotrs_tasks
56
62
  association_left_join(:piotrs_tasks, select: [:id, :title])
@@ -61,14 +67,14 @@ describe 'Defining one-to-many association' do
61
67
  end
62
68
  end
63
69
 
64
- users = rom.relations.users
70
+ users = container.relations.users
65
71
 
66
72
  expect(users.with_piotrs_tasks.to_a).to eql(
67
73
  [{ id: 1, name: 'Piotr', tasks_id: 1, title: 'Finish ROM' }]
68
74
  )
69
75
 
70
- result = rom.relation(:users).map_with(:with_tasks)
71
- .all.with_piotrs_tasks.to_a
76
+ result = container.relation(:users).map_with(:with_tasks)
77
+ .all.with_piotrs_tasks.to_a
72
78
 
73
79
  expect(result).to eql(
74
80
  [{ id: 1, name: 'Piotr', tasks: [{ tasks_id: 1, title: 'Finish ROM' }] }]
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Plugin / Base View' do
4
+ include_context 'database setup'
5
+
6
+ it 'defines base view' do
7
+ module Test
8
+ class Users < ROM::Relation[:sql]
9
+ dataset :users
10
+ register_as :users
11
+ end
12
+ end
13
+
14
+ configuration.register_relation(Test::Users)
15
+
16
+ expect(container.relation(:users).base.header).to match_array([:id, :name])
17
+ end
18
+ end
@@ -8,7 +8,7 @@ describe 'Plugin / Pagination' do
8
8
  before do
9
9
  9.times { |i| conn[:users].insert(name: "User #{i}") }
10
10
 
11
- setup.relation(:users) do
11
+ configuration.relation(:users) do
12
12
  use :pagination
13
13
 
14
14
  per_page 4
@@ -18,13 +18,13 @@ describe 'Plugin / Pagination' do
18
18
  describe '#page' do
19
19
  it 'allow to call with stringify number' do
20
20
  expect {
21
- rom.relation(:users).page('5')
21
+ container.relation(:users).page('5')
22
22
  }.to_not raise_error
23
23
  end
24
24
 
25
25
  it 'preserves existing modifiers' do
26
26
  expect(
27
- rom.relation(:users).send(:where, name: 'User 2').page(1).to_a.size
27
+ container.relation(:users).send(:where, name: 'User 2').page(1).to_a.size
28
28
  ).to be(1)
29
29
  end
30
30
  end
@@ -32,12 +32,12 @@ describe 'Plugin / Pagination' do
32
32
  describe '#per_page' do
33
33
  it 'allow to call with stringify number' do
34
34
  expect {
35
- rom.relation(:users).per_page('5')
35
+ container.relation(:users).per_page('5')
36
36
  }.to_not raise_error
37
37
  end
38
38
 
39
39
  it 'returns paginated relation with provided limit' do
40
- users = rom.relation(:users).page(2).per_page(5)
40
+ users = container.relation(:users).page(2).per_page(5)
41
41
 
42
42
  expect(users.dataset.opts[:offset]).to eql(5)
43
43
  expect(users.dataset.opts[:limit]).to eql(5)
@@ -55,19 +55,19 @@ describe 'Plugin / Pagination' do
55
55
 
56
56
  describe '#total_pages' do
57
57
  it 'returns a single page when elements are a perfect fit' do
58
- users = rom.relation(:users).page(1).per_page(3)
58
+ users = container.relation(:users).page(1).per_page(3)
59
59
  expect(users.pager.total_pages).to eql(3)
60
60
  end
61
61
 
62
62
  it 'returns the exact number of pages to accommodate all elements' do
63
- users = rom.relation(:users).per_page(9)
63
+ users = container.relation(:users).per_page(9)
64
64
  expect(users.pager.total_pages).to eql(1)
65
65
  end
66
66
  end
67
67
 
68
68
  describe '#pager' do
69
69
  it 'returns a pager with pagination meta-info' do
70
- users = rom.relation(:users).page(1)
70
+ users = container.relation(:users).page(1)
71
71
 
72
72
  expect(users.pager.total).to be(9)
73
73
  expect(users.pager.total_pages).to be(3)
@@ -76,13 +76,13 @@ describe 'Plugin / Pagination' do
76
76
  expect(users.pager.next_page).to be(2)
77
77
  expect(users.pager.prev_page).to be(nil)
78
78
 
79
- users = rom.relation(:users).page(2)
79
+ users = container.relation(:users).page(2)
80
80
 
81
81
  expect(users.pager.current_page).to be(2)
82
82
  expect(users.pager.next_page).to be(3)
83
83
  expect(users.pager.prev_page).to be(1)
84
84
 
85
- users = rom.relation(:users).page(3)
85
+ users = container.relation(:users).page(3)
86
86
 
87
87
  expect(users.pager.next_page).to be(nil)
88
88
  expect(users.pager.prev_page).to be(2)
@@ -3,15 +3,59 @@ require 'spec_helper'
3
3
  describe ROM::Relation do
4
4
  include_context 'users and tasks'
5
5
 
6
- let(:users) { rom.relations.users }
7
- let(:tasks) { rom.relations.tasks }
6
+ let(:users) { container.relations.users }
7
+ let(:tasks) { container.relations.tasks }
8
8
 
9
9
  before do
10
- setup.relation(:users) do
10
+ configuration.relation(:users) do
11
11
  def sorted
12
12
  order(:id)
13
13
  end
14
14
  end
15
+
16
+ configuration.relation(:tasks)
17
+ end
18
+
19
+ describe '#dataset' do
20
+ it 'selects all qualified columns and sorts by pk' do
21
+ expect(users.dataset).to eql(
22
+ users.select(*users.columns).order(:users__id).dataset
23
+ )
24
+ end
25
+ end
26
+
27
+ describe '#sum' do
28
+ it 'delegates to dataset and return value' do
29
+ expect(users.dataset).to receive(:sum).with(:id).and_call_original
30
+ expect(users.sum(:id)).to eql(1)
31
+ end
32
+ end
33
+
34
+ describe '#min' do
35
+ it 'delegates to dataset and return value' do
36
+ users.insert id: 2, name: 'Oskar'
37
+
38
+ expect(users.dataset).to receive(:min).with(:id).and_call_original
39
+ expect(users.min(:id)).to eql(1)
40
+ end
41
+ end
42
+
43
+ describe '#max' do
44
+ it 'delegates to dataset and return value' do
45
+ users.insert id: 2, name: 'Oskar'
46
+
47
+ expect(users.dataset).to receive(:max).with(:id).and_call_original
48
+ expect(users.max(:id)).to eql(2)
49
+ end
50
+ end
51
+
52
+ describe '#avg' do
53
+ it 'delegates to dataset and return value' do
54
+ users.insert id: 2, name: 'Oskar'
55
+
56
+ expect(users.dataset).to receive(:avg).with(:id).and_call_original
57
+ expect(users.avg(:id)).to eql(1.5)
58
+ end
15
59
  end
16
60
 
17
61
  describe '#distinct' do
@@ -53,6 +97,12 @@ describe ROM::Relation do
53
97
  { name: 'Piotr', title: 'Finish ROM' }
54
98
  ])
55
99
  end
100
+
101
+ it 'raises error when column names are ambiguous' do
102
+ expect {
103
+ users.inner_join(:tasks, user_id: :id).to_a
104
+ }.to raise_error(Sequel::DatabaseError, /column reference "id" is ambiguous/)
105
+ end
56
106
  end
57
107
 
58
108
  describe '#left_join' do
@@ -131,4 +181,20 @@ describe ROM::Relation do
131
181
  expect(tasks.unique?(title: 'Task One')).to be(false)
132
182
  end
133
183
  end
184
+
185
+ describe '#union' do
186
+ let(:relation1) { users.where(id: 1).select(:id, :name) }
187
+ let(:relation2) { users.where(id: 2).select(:id, :name) }
188
+
189
+ it 'unions two relations and returns a new relation' do
190
+ conn[:users].insert(id: 2, name: 'Jane')
191
+
192
+ result = relation1.union(relation2)
193
+
194
+ expect(result.to_a).to match_array([
195
+ { id: 1, name: 'Piotr' },
196
+ { id: 2, name: 'Jane' }
197
+ ])
198
+ end
199
+ end
134
200
  end
@@ -5,8 +5,10 @@ describe 'Inferring schema from database' do
5
5
 
6
6
  context "when database schema exists" do
7
7
  it "infers the schema from the database relations" do
8
- expect(rom.relations.users.to_a)
9
- .to eql(rom.gateways[:default][:users].to_a)
8
+ configuration.relation(:users)
9
+
10
+ expect(container.relations.users.to_a)
11
+ .to eql(container.gateways[:default][:users].to_a)
10
12
  end
11
13
  end
12
14
 
@@ -14,7 +16,7 @@ describe 'Inferring schema from database' do
14
16
  it "returns an empty schema" do
15
17
  drop_tables
16
18
 
17
- expect { rom.not_here }.to raise_error(NoMethodError)
19
+ expect { container.not_here }.to raise_error(NoMethodError)
18
20
  end
19
21
  end
20
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rom-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-23 00:00:00.000000000 Z
11
+ date: 2015-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -25,45 +25,33 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.18'
27
27
  - !ruby/object:Gem::Dependency
28
- name: equalizer
28
+ name: dry-equalizer
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.0'
34
- - - ">="
35
- - !ruby/object:Gem::Version
36
- version: 0.0.9
33
+ version: '0.2'
37
34
  type: :runtime
38
35
  prerelease: false
39
36
  version_requirements: !ruby/object:Gem::Requirement
40
37
  requirements:
41
38
  - - "~>"
42
39
  - !ruby/object:Gem::Version
43
- version: '0.0'
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: 0.0.9
40
+ version: '0.2'
47
41
  - !ruby/object:Gem::Dependency
48
42
  name: rom
49
43
  requirement: !ruby/object:Gem::Requirement
50
44
  requirements:
51
45
  - - "~>"
52
46
  - !ruby/object:Gem::Version
53
- version: '0.9'
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: 0.9.0
47
+ version: 1.0.0.beta1
57
48
  type: :runtime
58
49
  prerelease: false
59
50
  version_requirements: !ruby/object:Gem::Requirement
60
51
  requirements:
61
52
  - - "~>"
62
53
  - !ruby/object:Gem::Version
63
- version: '0.9'
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- version: 0.9.0
54
+ version: 1.0.0.beta1
67
55
  - !ruby/object:Gem::Dependency
68
56
  name: bundler
69
57
  requirement: !ruby/object:Gem::Requirement
@@ -111,6 +99,9 @@ files:
111
99
  - README.md
112
100
  - Rakefile
113
101
  - lib/rom-sql.rb
102
+ - lib/rom/plugins/relation/sql/auto_combine.rb
103
+ - lib/rom/plugins/relation/sql/auto_wrap.rb
104
+ - lib/rom/plugins/relation/sql/base_view.rb
114
105
  - lib/rom/sql.rb
115
106
  - lib/rom/sql/commands.rb
116
107
  - lib/rom/sql/commands/create.rb
@@ -126,14 +117,13 @@ files:
126
117
  - lib/rom/sql/migration.rb
127
118
  - lib/rom/sql/migration/migrator.rb
128
119
  - lib/rom/sql/migration/template.rb
120
+ - lib/rom/sql/plugin/assoc_macros.rb
121
+ - lib/rom/sql/plugin/assoc_macros/class_interface.rb
129
122
  - lib/rom/sql/plugin/associates.rb
130
123
  - lib/rom/sql/plugin/pagination.rb
131
124
  - lib/rom/sql/plugins.rb
132
125
  - lib/rom/sql/rake_task.rb
133
126
  - lib/rom/sql/relation.rb
134
- - lib/rom/sql/relation/associations.rb
135
- - lib/rom/sql/relation/class_methods.rb
136
- - lib/rom/sql/relation/inspection.rb
137
127
  - lib/rom/sql/relation/reading.rb
138
128
  - lib/rom/sql/relation/writing.rb
139
129
  - lib/rom/sql/spec/support.rb
@@ -148,9 +138,9 @@ files:
148
138
  - spec/integration/commands/create_spec.rb
149
139
  - spec/integration/commands/delete_spec.rb
150
140
  - spec/integration/commands/update_spec.rb
141
+ - spec/integration/gateway_spec.rb
151
142
  - spec/integration/migration_spec.rb
152
143
  - spec/integration/read_spec.rb
153
- - spec/integration/repository_spec.rb
154
144
  - spec/shared/database_setup.rb
155
145
  - spec/shared/users_and_tasks.rb
156
146
  - spec/spec_helper.rb
@@ -165,6 +155,7 @@ files:
165
155
  - spec/unit/migration_tasks_spec.rb
166
156
  - spec/unit/migrator_spec.rb
167
157
  - spec/unit/one_to_many_spec.rb
158
+ - spec/unit/plugin/base_view_spec.rb
168
159
  - spec/unit/plugin/pagination_spec.rb
169
160
  - spec/unit/relation_spec.rb
170
161
  - spec/unit/schema_spec.rb
@@ -183,9 +174,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
183
174
  version: '0'
184
175
  required_rubygems_version: !ruby/object:Gem::Requirement
185
176
  requirements:
186
- - - ">="
177
+ - - ">"
187
178
  - !ruby/object:Gem::Version
188
- version: '0'
179
+ version: 1.3.1
189
180
  requirements: []
190
181
  rubyforge_project:
191
182
  rubygems_version: 2.4.5
@@ -198,9 +189,9 @@ test_files:
198
189
  - spec/integration/commands/create_spec.rb
199
190
  - spec/integration/commands/delete_spec.rb
200
191
  - spec/integration/commands/update_spec.rb
192
+ - spec/integration/gateway_spec.rb
201
193
  - spec/integration/migration_spec.rb
202
194
  - spec/integration/read_spec.rb
203
- - spec/integration/repository_spec.rb
204
195
  - spec/shared/database_setup.rb
205
196
  - spec/shared/users_and_tasks.rb
206
197
  - spec/spec_helper.rb
@@ -215,6 +206,8 @@ test_files:
215
206
  - spec/unit/migration_tasks_spec.rb
216
207
  - spec/unit/migrator_spec.rb
217
208
  - spec/unit/one_to_many_spec.rb
209
+ - spec/unit/plugin/base_view_spec.rb
218
210
  - spec/unit/plugin/pagination_spec.rb
219
211
  - spec/unit/relation_spec.rb
220
212
  - spec/unit/schema_spec.rb
213
+ has_rdoc: