summaryrefslogtreecommitdiff
path: root/test/models/gallery_test.rb
blob: 603c0752f4cc3a011e76fadc18cea95f9b5fbe2c (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

# == Schema Information
#
# Table name: galleries
#
#  id                 :integer          not null, primary key
#  description        :string           default(""), not null
#  image_count        :integer          default(0), not null
#  order_position_asc :boolean          default(FALSE), not null
#  spoiler_warning    :string           default(""), not null
#  title              :string           not null
#  watcher_count      :integer          default(0), not null
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#  creator_id         :integer          not null
#  thumbnail_id       :integer          not null
#
# Indexes
#
#  index_galleries_on_creator_id    (creator_id)
#  index_galleries_on_thumbnail_id  (thumbnail_id)
#
# Foreign Keys
#
#  fk_rails_...  (creator_id => users.id) ON DELETE => cascade ON UPDATE => cascade
#
require 'test_helper'
require 'post_loader'

class GalleryTest < ActiveSupport::TestCase
  test 'inserting images creates interactions and updates indexes' do
    images = gallery_images
    assert_equal images.size, @gallery.gallery_interactions.size

    search = search_gallery_images
    assert_equal images.size, search.size
    images.each_with_index do |image, i|
      assert_equal image.id, search.records[i].id
    end
  end

  test 'images can be removed' do
    images = gallery_images

    @gallery.remove(images[0]).reload
    images.delete images[0]

    assert_equal images.size, @gallery.gallery_interactions.size
    assert_equal images.size, search_gallery_images.size
  end

  test 'gallery is destroyable' do
    gallery_images
    @gallery.destroy

    assert_equal 0, @gallery.gallery_interactions.count
    assert_equal 0, search_gallery_images.size
  end

  def search_gallery_images
    refresh_index Image
    PostLoader.new.gallery(@gallery.id).records
  end

  def gallery_images
    @gallery = create(:gallery)

    images = create_list(:post_skips_validation, 4)

    images.each { |i| @gallery.add(i) }
    images.reverse # images are ordered by time added, descending
  end
end