Sha256: 332964cd9f15db1616e88f96f3d57db9305ecade1be9e5074f8ecc52af17d629

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

require "test_helper"

include Grip

class TestAttachment < Test::Unit::TestCase
  context "An Attachment" do
    setup do
      @attachment   = Attachment.new( :name => "image", :owner_type => "Mock", :owner_id => Mongo::ObjectID.new )
      @dir          = File.dirname(__FILE__) + '/fixtures'
      @image        = File.open("#{@dir}/cthulhu.png", 'r')
      @file_system  = MongoMapper.database['fs.files']
    end

    teardown do
      #@file_system.drop
      @image.close
    end

    should "be an Attachment" do
      assert @attachment.is_a?(Attachment)
    end

    should "belong to :owner" do
      name, assoc = Attachment.associations.first
      #assert_equal(:owner, assoc.name)
      #assert_equal(:belongs_to, assoc.type)
    end

    context "with a valid file" do
      setup do
        @attachment.file = @image
      end

      should "have correct :content_type" do
        assert_equal("image/png", @attachment.content_type)
      end

      should "have correct :file_size" do
        assert_equal(27582, @attachment.file_size)
      end

      should "have correct :file_name" do
        assert_equal("cthulhu.png", @attachment.file_name)
      end

      should "read file from grid store" do
        assert_equal "image/png", @file_system.find_one(:filename => @attachment.grid_key)['contentType']
      end

      should "return a GridStore" do
        assert_equal(GridFS::GridStore, @attachment.file.class)
      end

    end

    context "with an invalid file" do
      should "raise Grip::InvalidFile" do
        assert_raise(InvalidFile) { @attachment.file = Hash.new }
      end
    end

		context "with an empty string or nil" do
			
			should "set image to nil" do
				@attachment.file = ""
				assert_nil @attachment.file
			end
			
		end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
grip-0.7.3 test/grip_attachment_test.rb
grip-0.7.2 test/grip_attachment_test.rb