Sha256: 5e90095c4fa7736dc25c2628ceca210d7e3258bce280db72a6a6ffd33157a607

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

require 'spec_helper'

describe ActiveRecordViews do
  describe '.create_view' do
    let(:connection) { ActiveRecord::Base.connection }

    self.use_transactional_fixtures = false
    after do
      connection.execute 'DROP VIEW IF EXISTS test'
      connection.execute 'DROP TABLE IF EXISTS active_record_views'
    end

    def create_test_view(sql)
      ActiveRecordViews.create_view connection, 'test', sql
    end

    def test_view_sql
      connection.select_value <<-SQL
        SELECT view_definition
        FROM information_schema.views
        WHERE table_name = 'test'
      SQL
    end

    it 'creates database view' do
      expect(test_view_sql).to be_nil
      create_test_view 'select 1 as id'
      expect(test_view_sql).to eq 'SELECT 1 AS id;'
    end

    context 'with existing view' do
      before do
        create_test_view 'select 1 as id'
        expect(test_view_sql).to eq 'SELECT 1 AS id;'
      end

      it 'updates view with compatible change' do
        create_test_view 'select 2 as id'
        expect(test_view_sql).to eq 'SELECT 2 AS id;'
      end

      it 'recreates view with incompatible change' do
        create_test_view "select 'foo'::text as name"
        expect(test_view_sql).to eq "SELECT 'foo'::text AS name;"
      end

      context 'having a dependant view' do
        before do
          connection.execute 'CREATE VIEW dependency AS SELECT * FROM test'
        end

        after do
          connection.execute 'DROP VIEW dependency'
        end

        it 'updates view with compatible change' do
          create_test_view 'select 2 as id'
          expect(test_view_sql).to eq 'SELECT 2 AS id;'
        end

        it 'fails to update view with incompatible signature change' do
          expect {
            create_test_view "select 'foo'::text as name"
          }.to raise_error ActiveRecord::StatementInvalid, /cannot change name of view column/
          expect(test_view_sql).to eq 'SELECT 1 AS id;'
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord_views-0.0.1 spec/active_record_views_spec.rb