Sha256: f71aa5d78161878fc8497da012a53c10c532dcc13fe9182d4c0dd00204eed266

Contents?: true

Size: 1.43 KB

Versions: 4

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

RSpec.describe "DB / Relations", :app_integration do
  before do
    @env = ENV.to_h
    allow(Hanami::Env).to receive(:loaded?).and_return(false)
  end

  after do
    ENV.replace(@env)
  end

  it "registers nested relations" do
    with_tmp_directory(@dir = Dir.mktmpdir) do
      write "config/app.rb", <<~RUBY
        require "hanami"

        module TestApp
          class App < Hanami::App
            config.logger.stream = File::NULL
          end
        end
      RUBY

      write "app/relations/nested/posts.rb", <<~RUBY
        module TestApp
          module Relations
            module Nested
              class Posts < Hanami::DB::Relation
                schema :posts, infer: true
              end
            end
          end
        end
      RUBY

      ENV["DATABASE_URL"] = "sqlite::memory"

      require "hanami/prepare"

      Hanami.app.prepare :db

      # Manually run a migration and add a test record
      gateway = TestApp::App["db.gateway"]
      migration = gateway.migration do
        change do
          create_table :posts do
            primary_key :id
            column :title, :text
          end
        end
      end
      migration.apply(gateway, :up)
      gateway.connection.execute("INSERT INTO posts (title) VALUES ('Hi from nested relation')")

      post = TestApp::App["relations.posts"].to_a[0]
      expect(post[:title]).to eq "Hi from nested relation"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hanami-2.2.1 spec/integration/db/relations_spec.rb
hanami-2.2.0 spec/integration/db/relations_spec.rb
hanami-2.2.0.rc1 spec/integration/db/relations_spec.rb
hanami-2.2.0.beta2 spec/integration/db/relations_spec.rb