Browse Source

test: add some basic unit tests for generating thumbnails

Lukasz Badura 3 months ago
parent
commit
5ea5c64bf5
3 changed files with 25 additions and 2 deletions
  1. 1 0
      lib/fotos/asset.rb
  2. 5 2
      lib/fotos/thumbnail.rb
  3. 19 0
      test/test_thumbnail.rb

+ 1 - 0
lib/fotos/asset.rb

@@ -49,6 +49,7 @@ module Fotos
 
 
     def exif_date
     def exif_date
       field_name = 'exif-ifd2-DateTimeOriginal'
       field_name = 'exif-ifd2-DateTimeOriginal'
+      return unless image.get_fields.include?(field_name)
       exif_date_data = image.get_value(field_name).split(' ')[0..1].join(':')
       exif_date_data = image.get_value(field_name).split(' ')[0..1].join(':')
       dt_attrs = exif_date_data.split(':').map(&:to_i)
       dt_attrs = exif_date_data.split(':').map(&:to_i)
       # get date from file's exif data
       # get date from file's exif data

+ 5 - 2
lib/fotos/thumbnail.rb

@@ -1,18 +1,21 @@
 require 'image_science'
 require 'image_science'
 module Fotos
 module Fotos
   class Thumbnail
   class Thumbnail
+    WIDTH = 250
+
     def initialize(asset)
     def initialize(asset)
       @asset = asset
       @asset = asset
     end
     end
 
 
     def create(destination_path = ".")
     def create(destination_path = ".")
       target_path = @asset.thumbnail_path(destination_path)
       target_path = @asset.thumbnail_path(destination_path)
-      return if File.exist?(target_path)
+      return target_path if File.exist?(target_path)
       ImageScience.with_image(@asset.file_path) do |img|
       ImageScience.with_image(@asset.file_path) do |img|
-        img.thumbnail(250) do |thumb|
+        img.thumbnail(WIDTH) do |thumb|
           thumb.save(target_path)
           thumb.save(target_path)
         end
         end
       end
       end
+      target_path
     end
     end
   end
   end
 end
 end

+ 19 - 0
test/test_thumbnail.rb

@@ -0,0 +1,19 @@
+require "test_helper"
+
+class TestThumbnail < Minitest::Test
+  def setup
+    @jpg_path = File.expand_path("../fixtures/cockapoo.jpg", __FILE__)
+    @heic_path = File.expand_path("../fixtures/mammoth.heic", __FILE__)
+    @jpg_asset = Fotos::Asset.new(@jpg_path)
+    @heic_asset = Fotos::Asset.new(@heic_path)
+  end
+
+  def test_create_thumbnail_from_jpg
+    target_path = Fotos::Thumbnail.new(@jpg_asset).create()
+    thumbnail = Fotos::Asset.new(target_path)
+    assert_equal Fotos::Thumbnail::WIDTH, thumbnail.image.get('width')
+
+    # clean up previously created thumbnail
+    File.unlink thumbnail.path
+  end
+end