Sha256: 883371e07c2fc1ee24c68b7d410ebad415e19bfc1a108ef6f84e482229b1774a

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

# encoding: utf-8

RSpec.describe 'Repository with multi-adapters configuration' do
  include_context 'database'

  let(:configuration) {
    ROM::Configuration.new(default: [:sql, uri], memory: [:memory])
  }

  let(:users) { rom.relation(:sql_users) }
  let(:tasks) { rom.relation(:memory_tasks) }

  let(:repo) { Test::Repository.new(rom) }

  before do
    module Test
      class Users < ROM::Relation[:sql]
        dataset :users
        register_as :sql_users
        gateway :default
      end

      class Tasks < ROM::Relation[:memory]
        dataset :tasks
        register_as :memory_tasks
        gateway :memory

        use :view
        use :key_inference

        view(:base, [:user_id, :title]) do
          project(:user_id, :title)
        end

        def for_users(users)
          restrict(user_id: users.map { |u| u[:id] })
        end
      end

      class Repository < ROM::Repository
        relations :sql_users, :memory_tasks

        def users_with_tasks(id)
          sql_users.where(id: id).combine_children(many: { tasks: memory_tasks })
        end
      end
    end

    configuration.register_relation(Test::Users)
    configuration.register_relation(Test::Tasks)

    user_id = configuration.gateways[:default].dataset(:users).insert(name: 'Jane')
    configuration.gateways[:memory].dataset(:tasks).insert(user_id: user_id, title: 'Jane Task')
  end

  specify 'ᕕ⁞ ᵒ̌ 〜 ᵒ̌ ⁞ᕗ' do
    user = repo.users_with_tasks(users.last[:id]).first

    expect(user.name).to eql('Jane')

    expect(user.tasks[0].user_id).to eql(user.id)
    expect(user.tasks[0].title).to eql('Jane Task')
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-repository-0.2.0 spec/integration/multi_adapter_spec.rb
rom-repository-0.2.0.rc1 spec/integration/multi_adapter_spec.rb
rom-repository-0.2.0.beta1 spec/integration/multi_adapter_spec.rb