Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 86 lines (76 sloc) 2.78 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
# -- encoding: utf-8 --
require 'digest/md5'
require 'fileutils'
require 'tempfile'
require 'helpers_for_test'

class TestSave < TestCase

  include TempfileTest

  def setup
    super
    @org_filename = @data_dir + '/test.jpg'
    FileUtils.cp(@org_filename, @temp_filename)
    @mini_exiftool = MiniExiftool.new @temp_filename
    @mini_exiftool_num = MiniExiftool.new @temp_filename, :numerical => true
    @org_md5 = Digest::MD5.hexdigest(File.read(@org_filename))
  end

  def test_allowed_value
    @mini_exiftool_num['Orientation'] = 2
    result = @mini_exiftool_num.save
    assert_equal true, result
    assert_equal @org_md5, Digest::MD5.hexdigest(File.read(@org_filename))
    assert_not_equal @org_md5, Digest::MD5.hexdigest(File.read(@temp_filename))
    assert_equal false, @mini_exiftool_num.changed?
    result = @mini_exiftool_num.save
    assert_equal false, result
  end

  def test_non_allowed_value
    @mini_exiftool['Orientation'] = 'some string'
    result = @mini_exiftool.save
    assert_equal false, result
    assert_equal 1, @mini_exiftool.errors.size
    assert_match(/Can't convert IFD0:Orientation \(not in PrintConv\)/,
                 @mini_exiftool.errors['Orientation'])
    assert @mini_exiftool.changed?
    assert @mini_exiftool.changed_tags.include?('Orientation')
  end

  def test_no_changing_of_file_when_error
    @mini_exiftool['ISO'] = 800
    @mini_exiftool['Orientation'] = 'some value'
    @mini_exiftool['ExposureTime'] = '1/30'
    result = @mini_exiftool.save
    assert_equal false, result
    assert_equal @org_md5, Digest::MD5.hexdigest(File.read(@org_filename))
    assert_equal @org_md5, Digest::MD5.hexdigest(File.read(@temp_filename))
  end

  def test_encoding_conversion
    special_string = 'äöü'
    if special_string.respond_to?(:encode)
     special_string_latin1 = special_string.encode('ISO-8859-1')
    else
      special_string_latin1 = "\xE4\xF6\xFC"
    end
    @mini_exiftool.title = special_string
    assert @mini_exiftool.save
    assert_equal false, @mini_exiftool.convert_encoding
    assert_equal special_string, @mini_exiftool.title
    @mini_exiftool.convert_encoding = true
    @mini_exiftool.title = special_string_latin1
    assert @mini_exiftool.save
    assert_equal true, @mini_exiftool.convert_encoding
    if special_string_latin1.respond_to?(:encode)
      special_string_latin1.encode!('ISO-8859-1')
    end
    assert_equal special_string_latin1, @mini_exiftool.title
  end

  def test_save_bang
    @mini_exiftool.orientation = 'some value'
    exception = false
    begin
      @mini_exiftool.save!
    rescue MiniExiftool::Error => e
      assert_match /Orientation/, e.message
      exception = true
    end
    assert exception, "No exception when save! with error."
  end

end
Something went wrong with that request. Please try again.