spec/unit/relation_spec.rb in rom-sql-0.6.1 vs spec/unit/relation_spec.rb in rom-sql-0.7.0.beta1

- old
+ new

@@ -1,21 +1,65 @@ require 'spec_helper' describe ROM::Relation do include_context 'users and tasks' - let(:users) { rom.relations.users } - let(:tasks) { rom.relations.tasks } + let(:users) { container.relations.users } + let(:tasks) { container.relations.tasks } before do - setup.relation(:users) do + configuration.relation(:users) do def sorted order(:id) end end + + configuration.relation(:tasks) end + describe '#dataset' do + it 'selects all qualified columns and sorts by pk' do + expect(users.dataset).to eql( + users.select(*users.columns).order(:users__id).dataset + ) + end + end + + describe '#sum' do + it 'delegates to dataset and return value' do + expect(users.dataset).to receive(:sum).with(:id).and_call_original + expect(users.sum(:id)).to eql(1) + end + end + + describe '#min' do + it 'delegates to dataset and return value' do + users.insert id: 2, name: 'Oskar' + + expect(users.dataset).to receive(:min).with(:id).and_call_original + expect(users.min(:id)).to eql(1) + end + end + + describe '#max' do + it 'delegates to dataset and return value' do + users.insert id: 2, name: 'Oskar' + + expect(users.dataset).to receive(:max).with(:id).and_call_original + expect(users.max(:id)).to eql(2) + end + end + + describe '#avg' do + it 'delegates to dataset and return value' do + users.insert id: 2, name: 'Oskar' + + expect(users.dataset).to receive(:avg).with(:id).and_call_original + expect(users.avg(:id)).to eql(1.5) + end + end + describe '#distinct' do it 'delegates to dataset and returns a new relation' do expect(users.dataset).to receive(:distinct).with(:name).and_call_original expect(users.distinct(:name)).to_not eq(users) end @@ -51,10 +95,16 @@ expect(result.to_a).to match_array([ { name: 'Piotr', title: 'Finish ROM' } ]) end + + it 'raises error when column names are ambiguous' do + expect { + users.inner_join(:tasks, user_id: :id).to_a + }.to raise_error(Sequel::DatabaseError, /column reference "id" is ambiguous/) + end end describe '#left_join' do it 'joins relations using left outer join' do conn[:users].insert(id: 2, name: 'Jane') @@ -127,8 +177,24 @@ end it 'returns true when there are more than one tuple matching criteria' do tasks.insert(title: 'Task One') expect(tasks.unique?(title: 'Task One')).to be(false) + end + end + + describe '#union' do + let(:relation1) { users.where(id: 1).select(:id, :name) } + let(:relation2) { users.where(id: 2).select(:id, :name) } + + it 'unions two relations and returns a new relation' do + conn[:users].insert(id: 2, name: 'Jane') + + result = relation1.union(relation2) + + expect(result.to_a).to match_array([ + { id: 1, name: 'Piotr' }, + { id: 2, name: 'Jane' } + ]) end end end