Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 201 lines (169 sloc) 6.832 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
require './test/helper'
require 'paperclip/schema'
require 'active_support/testing/deprecation'

class SchemaTest < Test::Unit::TestCase
  include ActiveSupport::Testing::Deprecation

  def setup
    rebuild_class
  end

  def teardown
    Dummy.connection.drop_table :dummies rescue nil
  end

  context "within table definition" do
    context "using #has_attached_file" do
      should "create attachment columns" do
        Dummy.connection.create_table :dummies, :force => true do |t|
          ActiveSupport::Deprecation.silence do
            t.has_attached_file :avatar
          end
        end
        rebuild_class

        columns = Dummy.columns.map{ |column| [column.name, column.type] }

        assert_includes columns, ['avatar_file_name', :string]
        assert_includes columns, ['avatar_content_type', :string]
        assert_includes columns, ['avatar_file_size', :integer]
        assert_includes columns, ['avatar_updated_at', :datetime]
      end

      should "display deprecation warning" do
        Dummy.connection.create_table :dummies, :force => true do |t|
          assert_deprecated do
            t.has_attached_file :avatar
          end
        end
      end
    end

    context "using #attachment" do
      setup do
        Dummy.connection.create_table :dummies, :force => true do |t|
          t.attachment :avatar
        end
        rebuild_class
      end

      should "create attachment columns" do
        columns = Dummy.columns.map{ |column| [column.name, column.type] }

        assert_includes columns, ['avatar_file_name', :string]
        assert_includes columns, ['avatar_content_type', :string]
        assert_includes columns, ['avatar_file_size', :integer]
        assert_includes columns, ['avatar_updated_at', :datetime]
      end
    end
  end

  context "within schema statement" do
    setup do
      Dummy.connection.create_table :dummies, :force => true
    end

    context "migrating up" do
      context "with single attachment" do
        setup do
          Dummy.connection.add_attachment :dummies, :avatar
          rebuild_class
        end

        should "create attachment columns" do
          columns = Dummy.columns.map{ |column| [column.name, column.type] }

          assert_includes columns, ['avatar_file_name', :string]
          assert_includes columns, ['avatar_content_type', :string]
          assert_includes columns, ['avatar_file_size', :integer]
          assert_includes columns, ['avatar_updated_at', :datetime]
        end
      end

      context "with multiple attachments" do
        setup do
          Dummy.connection.add_attachment :dummies, :avatar, :photo
          rebuild_class
        end

        should "create attachment columns" do
          columns = Dummy.columns.map{ |column| [column.name, column.type] }

          assert_includes columns, ['avatar_file_name', :string]
          assert_includes columns, ['avatar_content_type', :string]
          assert_includes columns, ['avatar_file_size', :integer]
          assert_includes columns, ['avatar_updated_at', :datetime]
          assert_includes columns, ['photo_file_name', :string]
          assert_includes columns, ['photo_content_type', :string]
          assert_includes columns, ['photo_file_size', :integer]
          assert_includes columns, ['photo_updated_at', :datetime]
        end
      end

      context "with no attachment" do
        should "raise an error" do
          assert_raise ArgumentError do
            Dummy.connection.add_attachment :dummies
          rebuild_class
          end
        end
      end
    end

    context "migrating down" do
      setup do
        Dummy.connection.change_table :dummies do |t|
          t.column :avatar_file_name, :string
          t.column :avatar_content_type, :string
          t.column :avatar_file_size, :integer
          t.column :avatar_updated_at, :datetime
        end
      end

      context "using #drop_attached_file" do
        should "remove the attachment columns" do
          ActiveSupport::Deprecation.silence do
            Dummy.connection.drop_attached_file :dummies, :avatar
          end
          rebuild_class

          columns = Dummy.columns.map{ |column| [column.name, column.type] }

          assert_not_includes columns, ['avatar_file_name', :string]
          assert_not_includes columns, ['avatar_content_type', :string]
          assert_not_includes columns, ['avatar_file_size', :integer]
          assert_not_includes columns, ['avatar_updated_at', :datetime]
        end

        should "display a deprecation warning" do
          assert_deprecated do
            Dummy.connection.drop_attached_file :dummies, :avatar
          end
        end
      end

      context "using #remove_attachment" do
        context "with single attachment" do
          setup do
            Dummy.connection.remove_attachment :dummies, :avatar
            rebuild_class
          end

          should "remove the attachment columns" do
            columns = Dummy.columns.map{ |column| [column.name, column.type] }

            assert_not_includes columns, ['avatar_file_name', :string]
            assert_not_includes columns, ['avatar_content_type', :string]
            assert_not_includes columns, ['avatar_file_size', :integer]
            assert_not_includes columns, ['avatar_updated_at', :datetime]
          end
        end

        context "with multiple attachments" do
          setup do
            Dummy.connection.change_table :dummies do |t|
              t.column :photo_file_name, :string
              t.column :photo_content_type, :string
              t.column :photo_file_size, :integer
              t.column :photo_updated_at, :datetime
            end

            Dummy.connection.remove_attachment :dummies, :avatar, :photo
            rebuild_class
          end

          should "remove the attachment columns" do
            columns = Dummy.columns.map{ |column| [column.name, column.type] }

            assert_not_includes columns, ['avatar_file_name', :string]
            assert_not_includes columns, ['avatar_content_type', :string]
            assert_not_includes columns, ['avatar_file_size', :integer]
            assert_not_includes columns, ['avatar_updated_at', :datetime]
            assert_not_includes columns, ['photo_file_name', :string]
            assert_not_includes columns, ['photo_content_type', :string]
            assert_not_includes columns, ['photo_file_size', :integer]
            assert_not_includes columns, ['photo_updated_at', :datetime]
          end
        end

        context "with no attachment" do
          should "raise an error" do
            assert_raise ArgumentError do
              Dummy.connection.remove_attachment :dummies
            end
          end
        end
      end
    end
  end
end
Something went wrong with that request. Please try again.