Back to Repositories

Testing Image Geometry Detection and EXIF Handling in Paperclip

This test suite validates the GeometryDetector functionality in Paperclip, focusing on image dimension extraction and orientation handling. The tests ensure accurate parsing of image metadata and proper handling of EXIF orientation data.

Test Coverage Overview

The test suite covers essential image geometry detection capabilities in Paperclip.

Key areas tested include:
  • Basic image dimension extraction
  • EXIF orientation detection
  • Configurable EXIF orientation handling
Edge cases focus on different image formats and orientation scenarios, with integration points to the GeometryParser component.

Implementation Analysis

The testing approach uses RSpec’s stubbing capabilities to isolate GeometryDetector functionality from GeometryParser dependencies.

Implementation patterns include:
  • Fixture-based test data
  • Stub method chaining
  • Configuration state management
  • Clean test setup and teardown

Technical Details

Testing tools and setup:
  • RSpec testing framework
  • Stub/mock functionality
  • Fixture file handling
  • Configuration management through Paperclip.options

Best Practices Demonstrated

The test suite demonstrates strong testing practices through isolated unit tests and proper test organization.

Notable practices include:
  • Clear test case separation
  • Proper setup/teardown patterns
  • Configuration state restoration
  • Focused test scenarios

thoughtbot/paperclip

spec/paperclip/geometry_detector_spec.rb

            
require 'spec_helper'

describe Paperclip::GeometryDetector do
  it 'identifies an image and extract its dimensions' do
    Paperclip::GeometryParser.stubs(:new).with("434x66,").returns(stub(make: :correct))
    file = fixture_file("5k.png")
    factory = Paperclip::GeometryDetector.new(file)

    output = factory.make

    expect(output).to eq :correct
  end

  it 'identifies an image and extract its dimensions and orientation' do
    Paperclip::GeometryParser.stubs(:new).with("300x200,6").returns(stub(make: :correct))
    file = fixture_file("rotated.jpg")
    factory = Paperclip::GeometryDetector.new(file)

    output = factory.make

    expect(output).to eq :correct
  end

  it 'avoids reading EXIF orientation if so configured' do
    begin
      Paperclip.options[:use_exif_orientation] = false
      Paperclip::GeometryParser.stubs(:new).with("300x200,1").returns(stub(make: :correct))
      file = fixture_file("rotated.jpg")
      factory = Paperclip::GeometryDetector.new(file)

      output = factory.make

      expect(output).to eq :correct
    ensure
      Paperclip.options[:use_exif_orientation] = true
    end
  end
end