aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/test/analyzer/image_analyzer_test.rb
blob: 73438c15ab9c27505f445605676fd0d32fcb0a8c (plain) (blame)
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
# frozen_string_literal: true

require "test_helper"
require "database/setup"

require "active_storage/analyzer/image_analyzer"

class ActiveStorage::Analyzer::ImageAnalyzerTest < ActiveSupport::TestCase
  test "analyzing a JPEG image" do
    blob = create_file_blob(filename: "racecar.jpg", content_type: "image/jpeg")
    metadata = extract_metadata_from(blob)

    assert_equal 4104, metadata[:width]
    assert_equal 2736, metadata[:height]
  end

  test "analyzing a rotated JPEG image" do
    blob = create_file_blob(filename: "racecar_rotated.jpg", content_type: "image/jpeg")
    metadata = extract_metadata_from(blob)

    assert_equal 2736, metadata[:width]
    assert_equal 4104, metadata[:height]
  end

  test "analyzing an SVG image without an XML declaration" do
    blob = create_file_blob(filename: "icon.svg", content_type: "image/svg+xml")
    metadata = extract_metadata_from(blob)

    assert_equal 792, metadata[:width]
    assert_equal 584, metadata[:height]
  end

  test "analyzing an unsupported image type" do
    blob = create_blob(data: "bad", filename: "bad_file.bad", content_type: "image/bad_type")
    metadata = extract_metadata_from(blob)

    assert_nil metadata[:width]
    assert_nil metadata[:heigh]
  end
end