Browse Source

Add basic tests for Fotos::Asset

Lukasz Badura 4 months ago
parent
commit
cb16fbf660
4 changed files with 65 additions and 2 deletions
  1. 2 2
      lib/fotos/asset.rb
  2. BIN
      test/fixtures/cockapoo.jpg
  3. BIN
      test/fixtures/mammoth.heic
  4. 63 0
      test/test_asset.rb

+ 2 - 2
lib/fotos/asset.rb

@@ -33,8 +33,8 @@ module Fotos
       File.join(dirname, file_name)
       File.join(dirname, file_name)
     end
     end
 
 
-    def thumbnail_path
-      File.join(dirname, thumbnail_name)
+    def thumbnail_path(target_path = dirname)
+      File.join(target_path, thumbnail_name)
     end
     end
 
 
     def is_thumbnail?
     def is_thumbnail?

BIN
test/fixtures/cockapoo.jpg


BIN
test/fixtures/mammoth.heic


+ 63 - 0
test/test_asset.rb

@@ -0,0 +1,63 @@
+require "test_helper"
+
+class TestAsset < 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_initializes_with_file_path
+    assert_equal @jpg_path, @jpg_asset.file_path
+    assert_equal @heic_path, @heic_asset.file_path
+  end
+
+  def test_dirname_returns_parent_directory_name
+    assert_equal "fixtures", @jpg_asset.dirname
+    assert_equal "fixtures", @heic_asset.dirname
+  end
+
+  def test_file_name_returns_basename
+    assert_equal "cockapoo.jpg", @jpg_asset.file_name
+    assert_equal "mammoth.heic", @heic_asset.file_name
+  end
+
+  def test_ext_name_returns_file_extension
+    assert_equal ".jpg", @jpg_asset.ext_name
+    assert_equal ".heic", @heic_asset.ext_name
+  end
+
+  def test_supported_returns_true_for_jpeg_jpg
+    assert @jpg_asset.supported?
+    refute @heic_asset.supported?
+  end
+
+  def test_thumbnail_name_adds_prefix
+    assert_equal "TH_cockapoo.jpg", @jpg_asset.thumbnail_name
+    assert_equal "TH_mammoth.heic", @heic_asset.thumbnail_name
+  end
+
+  def test_path_returns_relative_path
+    assert_equal "fixtures/cockapoo.jpg", @jpg_asset.path
+    assert_equal "fixtures/mammoth.heic", @heic_asset.path
+  end
+
+  def test_thumbnail_path_with_default_target
+    assert_equal "fixtures/TH_cockapoo.jpg", @jpg_asset.thumbnail_path
+    assert_equal "fixtures/TH_mammoth.heic", @heic_asset.thumbnail_path
+  end
+
+  def test_thumbnail_path_with_custom_target
+    assert_equal "/tmp/TH_cockapoo.jpg", @jpg_asset.thumbnail_path("/tmp")
+    assert_equal "output/TH_mammoth.heic", @heic_asset.thumbnail_path("output")
+  end
+
+  def test_is_thumbnail_detects_thumbnail_prefix
+    thumbnail_asset = Fotos::Asset.new("/path/TH_image.jpg")
+    regular_asset = Fotos::Asset.new("/path/image.jpg")
+
+    assert thumbnail_asset.is_thumbnail?
+    refute regular_asset.is_thumbnail?
+  end
+end