spec/unit/relation_spec.rb in rom-sql-0.3.2 vs spec/unit/relation_spec.rb in rom-sql-0.4.0.beta1

- old
+ new

@@ -1,20 +1,54 @@ require 'spec_helper' describe ROM::Relation do include_context 'users and tasks' - subject(:users) { rom.relations.users } + let(:users) { rom.relations.users } + let(:tasks) { rom.relations.tasks } before do setup.relation(:users) do def sorted order(:id) end end end + describe '#map' do + it 'yields tuples' do + result = [] + users.map { |tuple| result << tuple } + expect(result).to eql([{ id: 1, name: 'Piotr' }]) + end + end + + describe '#inner_join' do + it 'joins relations using inner join' do + conn[:users].insert(id: 2, name: 'Jane') + + result = users.inner_join(:tasks, user_id: :id).select(:name, :title) + + expect(result.to_a).to match_array([ + { name: 'Piotr', title: 'Finish ROM' } + ]) + end + end + + describe '#left_join' do + it 'joins relations using left outer join' do + conn[:users].insert(id: 2, name: 'Jane') + + result = users.left_join(:tasks, user_id: :id).select(:name, :title) + + expect(result.to_a).to match_array([ + { name: 'Piotr', title: 'Finish ROM' }, + { name: 'Jane', title: nil } + ]) + end + end + describe '#project' do it 'projects the dataset using new column names' do projected = users.sorted.project(:name) expect(projected.header).to match_array([:name]) @@ -57,11 +91,23 @@ expect(columns).to eql([:users__id___user_id]) end end describe '#inspect' do - it 'includes dataset and header' do - expect(users.inspect).to include('header') + it 'includes dataset' do expect(users.inspect).to include('dataset') + end + end + + describe '#unique?' do + before { tasks.delete } + + it 'returns true when there is only one tuple matching criteria' do + expect(tasks.unique?(title: 'Task One')).to be(true) + 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 end