cirandas.net

ref: master

test/unit/create_thumbnails_job_test.rb


 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
41
42
43
44
45
46
47
48
49
require_relative "../test_helper"

class CreateThumbnailsJobTest < ActiveSupport::TestCase

  should 'create thumbnails to uploaded files' do
    person = create_user('test_user').person
    file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => person)

    assert_equal [], file.thumbnails
    job = CreateThumbnailsJob.new(file.class.name, file.id)
    job.perform
    file.reload
    assert_not_equal [], file.thumbnails
  end

  should 'set thumbnails_processed to true after finished' do
    person = create_user('test_user').person
    file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => person)

    job = CreateThumbnailsJob.new(file.class.name, file.id)
    job.perform

    file.reload
    assert file.thumbnails_processed
  end

  should 'not create thumbnails from deleted files' do
    person = create_user('test_user').person
    file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => person)
    job = CreateThumbnailsJob.new(file.class.name, file.id)
    file.destroy
    assert_nothing_raised do
      job.perform
    end
  end

  should 'expire cache of articles that use an image that just got a thumbnail' do
    person = create_user('test_user').person
    file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :profile => person)
    article = create(Article, :name => 'test', :image_builder => {
       :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
    }, :profile_id => person.id)
    old_cache_key = article.cache_key
    job = CreateThumbnailsJob.new(file.class.name, file.id)
    job.perform
    process_delayed_job_queue
    assert_not_equal old_cache_key, Article.find(article.id).reload.cache_key
  end
end