spec/integration/read_spec.rb in rom-sql-0.8.0 vs spec/integration/read_spec.rb in rom-sql-0.9.0

- old
+ new

@@ -1,39 +1,28 @@ -require 'virtus' +require 'dry-struct' -RSpec.describe 'Reading relations' do +RSpec.describe 'Reading relations using custom mappers' do include_context 'users and tasks' - # FIXME: one example fails on :mysql - with_adapters(:postgres, :sqlite) do + with_adapters do before :each do - class Goal - include Virtus.value_object(coerce: true) - - values do - attribute :id, Integer - attribute :title, String + module Test + class Goal < Dry::Struct + attribute :id, Types::Strict::Int + attribute :title, Types::Strict::String end - end - class User - include Virtus.value_object(coerce: true) - - values do - attribute :id, Integer - attribute :name, String - attribute :goals, Array[Goal] + class User < Dry::Struct + attribute :id, Types::Strict::Int + attribute :name, Types::Strict::String + attribute :goals, Types::Strict::Array.member(Goal) end - end - class UserGoalCount - include Virtus.value_object(coerce: true) - - values do - attribute :id, Integer - attribute :name, String - attribute :goal_count, Integer + class UserGoalCount < Dry::Struct + attribute :id, Types::Strict::Int + attribute :name, Types::Strict::String + attribute :goal_count, Types::Strict::Int end end conf.relation(:goals) do use :assoc_macros @@ -78,42 +67,45 @@ end end conf.mappers do define(:users) do - model User + model Test::User group :goals do - model Goal + model Test::Goal attribute :id, from: :tasks_id attribute :title end end define(:user_goal_counts) do - model UserGoalCount + model Test::UserGoalCount end end end it 'loads domain objects' do user = container.relation(:users).as(:users).with_goals.by_name('Jane').to_a.first expect(user).to eql( - User.new( - id: 1, name: 'Jane', goals: [Goal.new(id: 2, title: "Jane's task")] + Test::User.new( + id: 1, name: 'Jane', goals: [Test::Goal.new(id: 2, title: "Jane's task")] )) end - it 'works with grouping and aggregates' do - container.relations[:goals].insert(id: 3, user_id: 1, title: 'Get Milk') + # FIXME: on mysql and sqlite + if metadata[:postgres] + it 'works with grouping and aggregates' do + container.relations[:goals].insert(id: 3, user_id: 1, title: 'Get Milk') - users_with_goal_count = container.relation(:user_goal_counts).as(:user_goal_counts).all + users_with_goal_count = container.relation(:user_goal_counts).as(:user_goal_counts).all - expect(users_with_goal_count.to_a).to eq([ - UserGoalCount.new(id: 1, name: "Jane", goal_count: 2), - UserGoalCount.new(id: 2, name: "Joe", goal_count: 1) - ]) + expect(users_with_goal_count.to_a).to eq([ + Test::UserGoalCount.new(id: 1, name: "Jane", goal_count: 2), + Test::UserGoalCount.new(id: 2, name: "Joe", goal_count: 1) + ]) + end end end end