<commit_before>require './test/helper'

class IntegrationTest < Test::Unit::TestCase
  context "Many models at once" do
    setup do
      rebuild_model
      @file      = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
      300.times do |i|
        Dummy.create! :avatar => @file
      end
    end

    should "not exceed the open file limit" do
       assert_nothing_raised do
         dummies = Dummy.find(:all)
         dummies.each { |dummy| dummy.avatar }
       end
    end
  end

  context "An attachment" do
    setup do
      rebuild_model :styles => { :thumb => "50x50#" }
      @dummy = Dummy.new
      @file = File.new(File.join(File.dirname(__FILE__),
                                 "fixtures",
                                 "5k.png"), 'rb')
      @dummy.avatar = @file
      assert @dummy.save
    end

    teardown { @file.close }

    should "create its thumbnails properly" do
      assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
    end

    context 'reprocessing with unreadable original' do
      setup { File.chmod(0000, @dummy.avatar.path) }

      should "not raise an error" do
        assert_nothing_raised do
          @dummy.avatar.reprocess!
        end
      end

      should "return false" do
        assert ! @dummy.avatar.reprocess!
      end

      teardown { File.chmod(0644, @dummy.avatar.path) }
    end

    context "redefining its attachment styles" do
      setup do
        Dummy.class_eval do
          has_attached_file :avatar, :styles => { :thumb => "150x25#" }
          has_attached_file :avatar, :styles => { :thumb => "150x25#", :dynamic => lambda { |a| '50x50#' } }
        end
        @d2 = Dummy.find(@dummy.id)
        @d2.avatar.reprocess!
        @d2.save
      end

      should "create its thumbnails properly" do
        assert_match /\b150x25\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
        assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:dynamic)}"`
      end
    end
  end

  context "Attachment" do
    setup do
      @thumb_path = "./test/../public/system/avatars/1/thumb/5k.png"
      File.delete(@thumb_path) if File.exists?(@thumb_path)
      rebuild_model :styles => { :thumb => "50x50#" }
      @dummy = Dummy.new
      @file = File.new(File.join(File.dirname(__FILE__),
                                 "fixtures",
                                 "5k.png"), 'rb')

    end

    teardown { @file.close }

    should "not create the thumbnails upon saving when post-processing is disabled" do
      @dummy.avatar.post_processing = false
      @dummy.avatar = @file
      assert @dummy.save
      assert !File.exists?(@thumb_path)
    end

    should "create the thumbnails upon saving when post_processing is enabled" do
      @dummy.avatar.post_processing = true
      @dummy.avatar = @file
      assert @dummy.save
      assert File.exists?(@thumb_path)
    end
  end

  context "Attachment with no generated thumbnails" do
    setup do
      @thumb_small_path = "./test/../public/system/avatars/1/thumb_small/5k.png"
      @thumb_large_path = "./test/../public/system/avatars/1/thumb_large/5k.png"
      File.delete(@thumb_small_path) if File.exists?(@thumb_small_path)
      File.delete(@thumb_large_path) if File.exists?(@thumb_large_path)
      rebuild_model :styles => { :thumb_small => "50x50#", :thumb_large => "60x60#" }
      @dummy = Dummy.new
      @file = File.new(File.join(File.dirname(__FILE__),
                                 "fixtures",
                                 "5k.png"), 'rb')

      @dummy.avatar.post_processing = false
      @dummy.avatar = @file
      assert @dummy.save
      @dummy.avatar.post_processing = true<commit_msg>Added an additional test around disabling post-processing.<commit_after>require './test/helper'

class IntegrationTest < Test::Unit::TestCase
  context "Many models at once" do
    setup do
      rebuild_model
      @file      = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
      300.times do |i|
        Dummy.create! :avatar => @file
      end
    end

    should "not exceed the open file limit" do
       assert_nothing_raised do
         dummies = Dummy.find(:all)
         dummies.each { |dummy| dummy.avatar }
       end
    end
  end

  context "An attachment" do
    setup do
      rebuild_model :styles => { :thumb => "50x50#" }
      @dummy = Dummy.new
      @file = File.new(File.join(File.dirname(__FILE__),
                                 "fixtures",
                                 "5k.png"), 'rb')
      @dummy.avatar = @file
      assert @dummy.save
    end

    teardown { @file.close }

    should "create its thumbnails properly" do
      assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
    end

    context 'reprocessing with unreadable original' do
      setup { File.chmod(0000, @dummy.avatar.path) }

      should "not raise an error" do
        assert_nothing_raised do
          @dummy.avatar.reprocess!
        end
      end

      should "return false" do
        assert ! @dummy.avatar.reprocess!
      end

      teardown { File.chmod(0644, @dummy.avatar.path) }
    end

    context "redefining its attachment styles" do
      setup do
        Dummy.class_eval do
          has_attached_file :avatar, :styles => { :thumb => "150x25#" }
          has_attached_file :avatar, :styles => { :thumb => "150x25#", :dynamic => lambda { |a| '50x50#' } }
        end
        @d2 = Dummy.find(@dummy.id)
        @d2.avatar.reprocess!
        @d2.save
      end

      should "create its thumbnails properly" do
        assert_match /\b150x25\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
        assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:dynamic)}"`
      end
    end
  end

  context "Attachment" do
    setup do
      @thumb_path = "./test/../public/system/avatars/1/thumb/5k.png"
      File.delete(@thumb_path) if File.exists?(@thumb_path)
      rebuild_model :styles => { :thumb => "50x50#" }
      @dummy = Dummy.new
      @file = File.new(File.join(File.dirname(__FILE__),
                                 "fixtures",
                                 "5k.png"), 'rb')

    end

    teardown { @file.close }

    context "when post-processing is disabled" do
      setup do
        @dummy.avatar.post_processing = false
        @dummy.avatar = @file
        assert @dummy.save
      end

      should "not create the thumbnails upon save" do
        assert !File.exists?(@thumb_path)
      end

      should "create the thumbnails when reprocess! is called" do
        @dummy.avatar.reprocess!
        assert File.exists?(@thumb_path)
      end
    end

    should "create the thumbnails upon saving when post_processing is enabled" do
      @dummy.avatar.post_processing = true
      @dummy.avatar = @file
      assert @dummy.save
      assert File.exists?(@thumb_path)
    end
  end

  context "Attachment with no generated thumbnails" do
    setup do
      @thumb_small_path = "./test/../public/system/avatars/1/thumb_small/5k.png"
      @thumb_large_path = "./test/../public/system/avatars/1/thumb_large/5k.png"
      File.delete(@thumb_small_path) if File.exists?(@thumb_small_path)
      File.delete(@thumb_large_path) if File.exists?(@thumb_large_path)
      rebuild_model :styles => { :thumb_small => "50x50#", :thumb_large => "60x60#" }
      @dummy = Dummy.new
      @file = File.new(File.join(File.dirname(__FILE__),
                                 "fixtures",
                                 "5k.png"), 'rb')

      @dummy.avatar.post_processing = false
      @dummy.avatar = @file
      assert @dummy.save
      @dummy.avatar.post_processing = true
{'fraction_non_alphanumeric': 0.12114045460517672, 'fraction_numerical': 0.017606096439364078, 'mean_word_length': 2.115841178878428, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1172711', 'n_tokens_mistral': 2525, 'n_tokens_neox': 2403, 'n_words': 550}