spec/integration/read_spec.rb in rom-sql-0.3.2 vs spec/integration/read_spec.rb in rom-sql-0.4.0.beta1

- old
+ new

@@ -2,11 +2,11 @@ require 'virtus' describe 'Reading relations' do include_context 'users and tasks' - it 'loads domain objects' do + before :each do class Task include Virtus.value_object(coerce: true) values do attribute :id, Integer @@ -22,10 +22,20 @@ attribute :name, String attribute :tasks, Array[Task] end end + class UserTaskCount + include Virtus.value_object(coerce: true) + + values do + attribute :id, Integer + attribute :name, String + attribute :task_count, Integer + end + end + setup.relation(:tasks) setup.relation(:users) do one_to_many :tasks, key: :user_id @@ -40,10 +50,26 @@ def all select(:id, :name) end end + setup.relation(:user_task_counts) do + dataset :users + register_as :user_task_counts + one_to_many :tasks, key: :user_id + + def all + with_tasks.select_group(:users__id, :users__name).select_append { + count(:tasks).as(:task_count) + } + end + + def with_tasks + association_left_join(:tasks, select: [:id, :title]) + end + end + setup.mappers do define(:users) do model User group :tasks do @@ -51,15 +77,30 @@ attribute :id, from: :tasks_id attribute :title end end + + define(:user_task_counts) do + model UserTaskCount + end end + end + it 'loads domain objects' do user = rom.read(:users).with_tasks.by_name('Piotr').to_a.first expect(user).to eql( User.new( id: 1, name: 'Piotr', tasks: [Task.new(id: 1, title: 'Finish ROM')] )) + end + + it 'works with grouping and aggregates' do + rom.relations[:tasks].insert(id: 2, user_id: 1, title: 'Get Milk') + + users_with_task_count = rom.read(:user_task_counts).all + expect(users_with_task_count.to_a).to eq([ + UserTaskCount.new(id: 1, name: "Piotr", task_count: 2) + ]) end end