mirror of
https://github.com/indentlabs/notebook.git
synced 2025-10-26 11:19:22 +00:00
add pin/unpin image tests
This commit is contained in:
parent
de2feb0122
commit
8e329a59eb
200
test/controllers/content_controller_pin_test.rb
Normal file
200
test/controllers/content_controller_pin_test.rb
Normal file
@ -0,0 +1,200 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ContentControllerPinTest < ActionDispatch::IntegrationTest
|
||||
include Devise::Test::IntegrationHelpers
|
||||
|
||||
setup do
|
||||
@user = users(:one)
|
||||
@character = characters(:one)
|
||||
@image_upload = image_uploads(:regular)
|
||||
@pinned_image = image_uploads(:pinned)
|
||||
|
||||
# Create a test basil commission
|
||||
@basil_commission = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Test prompt',
|
||||
job_id: 'test_job_123',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
sign_in @user
|
||||
end
|
||||
|
||||
test "should pin an unpinned image upload" do
|
||||
assert_not @image_upload.pinned, "Image should start unpinned"
|
||||
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @image_upload.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
assert_equal @image_upload.id, json_response['id']
|
||||
assert_equal 'image_upload', json_response['type']
|
||||
assert json_response['pinned'], "Response should indicate image is now pinned"
|
||||
|
||||
@image_upload.reload
|
||||
assert @image_upload.pinned, "Image should be pinned in database"
|
||||
end
|
||||
|
||||
test "should unpin a pinned image upload" do
|
||||
assert @pinned_image.pinned, "Image should start pinned"
|
||||
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @pinned_image.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
assert_equal @pinned_image.id, json_response['id']
|
||||
assert_equal 'image_upload', json_response['type']
|
||||
assert_not json_response['pinned'], "Response should indicate image is now unpinned"
|
||||
|
||||
@pinned_image.reload
|
||||
assert_not @pinned_image.pinned, "Image should be unpinned in database"
|
||||
end
|
||||
|
||||
test "should pin a basil commission" do
|
||||
assert_not @basil_commission.pinned, "Basil commission should start unpinned"
|
||||
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @basil_commission.id, image_type: 'basil_commission' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
assert_equal @basil_commission.id, json_response['id']
|
||||
assert_equal 'basil_commission', json_response['type']
|
||||
assert json_response['pinned'], "Response should indicate basil commission is now pinned"
|
||||
|
||||
@basil_commission.reload
|
||||
assert @basil_commission.pinned, "Basil commission should be pinned in database"
|
||||
end
|
||||
|
||||
test "should unpin previously pinned basil commission" do
|
||||
@basil_commission.update!(pinned: true)
|
||||
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @basil_commission.id, image_type: 'basil_commission' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
assert_not json_response['pinned'], "Response should indicate basil commission is now unpinned"
|
||||
|
||||
@basil_commission.reload
|
||||
assert_not @basil_commission.pinned, "Basil commission should be unpinned in database"
|
||||
end
|
||||
|
||||
test "pinning an image should unpin other images for same content" do
|
||||
# Ensure we have a pinned image and an unpinned image for the same character
|
||||
@pinned_image.update!(pinned: true)
|
||||
@image_upload.update!(pinned: false)
|
||||
|
||||
# Pin the unpinned image
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @image_upload.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
|
||||
# Check that the previously pinned image is now unpinned
|
||||
@pinned_image.reload
|
||||
@image_upload.reload
|
||||
|
||||
assert_not @pinned_image.pinned, "Previously pinned image should be unpinned"
|
||||
assert @image_upload.pinned, "Newly pinned image should be pinned"
|
||||
end
|
||||
|
||||
test "pinning an image should unpin basil commissions for same content" do
|
||||
@basil_commission.update!(pinned: true)
|
||||
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @image_upload.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
|
||||
@basil_commission.reload
|
||||
@image_upload.reload
|
||||
|
||||
assert_not @basil_commission.pinned, "Basil commission should be unpinned"
|
||||
assert @image_upload.pinned, "Image upload should be pinned"
|
||||
end
|
||||
|
||||
test "pinning a basil commission should unpin image uploads for same content" do
|
||||
@pinned_image.update!(pinned: true)
|
||||
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @basil_commission.id, image_type: 'basil_commission' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
|
||||
@pinned_image.reload
|
||||
@basil_commission.reload
|
||||
|
||||
assert_not @pinned_image.pinned, "Image upload should be unpinned"
|
||||
assert @basil_commission.pinned, "Basil commission should be pinned"
|
||||
end
|
||||
|
||||
test "should return error for non-existent image" do
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: 99999, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :not_found
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
assert_equal 'Image not found', json_response['error']
|
||||
end
|
||||
|
||||
test "should return error for invalid image type" do
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @image_upload.id, image_type: 'invalid_type' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :bad_request
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
assert_equal 'Invalid image type', json_response['error']
|
||||
end
|
||||
|
||||
test "should return unauthorized for other user's content" do
|
||||
other_user = users(:two)
|
||||
other_character = characters(:two)
|
||||
other_image = ImageUpload.create!(
|
||||
user: other_user,
|
||||
content_type: 'Character',
|
||||
content_id: other_character.id,
|
||||
privacy: 'public'
|
||||
)
|
||||
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: other_image.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :forbidden
|
||||
|
||||
json_response = JSON.parse(response.body)
|
||||
assert_equal 'Unauthorized', json_response['error']
|
||||
end
|
||||
|
||||
test "should require authentication" do
|
||||
sign_out @user
|
||||
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @image_upload.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :unauthorized
|
||||
end
|
||||
|
||||
# Using Devise::Test::IntegrationHelpers for sign_in/sign_out
|
||||
end
|
||||
226
test/integration/pin_workflow_test.rb
Normal file
226
test/integration/pin_workflow_test.rb
Normal file
@ -0,0 +1,226 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PinWorkflowTest < ActionDispatch::IntegrationTest
|
||||
include Devise::Test::IntegrationHelpers
|
||||
|
||||
setup do
|
||||
@user = users(:one)
|
||||
@character = characters(:one)
|
||||
|
||||
# Create multiple images for testing
|
||||
@image1 = ImageUpload.create!(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
@image2 = ImageUpload.create!(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
@basil1 = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Test basil 1',
|
||||
job_id: 'basil_job_1',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
@basil2 = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Test basil 2',
|
||||
job_id: 'basil_job_2',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
sign_in @user
|
||||
end
|
||||
|
||||
test "complete pin workflow: pin, switch, unpin" do
|
||||
# Step 1: Pin first image
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @image1.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
assert JSON.parse(response.body)['pinned']
|
||||
|
||||
# Verify state
|
||||
@image1.reload
|
||||
assert @image1.pinned, "Image 1 should be pinned"
|
||||
assert_not @image2.reload.pinned, "Image 2 should remain unpinned"
|
||||
assert_not @basil1.reload.pinned, "Basil 1 should remain unpinned"
|
||||
assert_not @basil2.reload.pinned, "Basil 2 should remain unpinned"
|
||||
|
||||
# Step 2: Pin second image (should unpin first)
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @image2.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
assert JSON.parse(response.body)['pinned']
|
||||
|
||||
# Verify state
|
||||
@image1.reload
|
||||
@image2.reload
|
||||
assert_not @image1.pinned, "Image 1 should be unpinned"
|
||||
assert @image2.pinned, "Image 2 should be pinned"
|
||||
assert_not @basil1.reload.pinned, "Basil 1 should remain unpinned"
|
||||
assert_not @basil2.reload.pinned, "Basil 2 should remain unpinned"
|
||||
|
||||
# Step 3: Pin basil commission (should unpin image)
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @basil1.id, image_type: 'basil_commission' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
assert JSON.parse(response.body)['pinned']
|
||||
|
||||
# Verify state
|
||||
assert_not @image1.reload.pinned, "Image 1 should remain unpinned"
|
||||
assert_not @image2.reload.pinned, "Image 2 should be unpinned"
|
||||
assert @basil1.reload.pinned, "Basil 1 should be pinned"
|
||||
assert_not @basil2.reload.pinned, "Basil 2 should remain unpinned"
|
||||
|
||||
# Step 4: Pin second basil (should unpin first basil)
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @basil2.id, image_type: 'basil_commission' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
assert JSON.parse(response.body)['pinned']
|
||||
|
||||
# Verify state
|
||||
assert_not @image1.reload.pinned, "Image 1 should remain unpinned"
|
||||
assert_not @image2.reload.pinned, "Image 2 should remain unpinned"
|
||||
assert_not @basil1.reload.pinned, "Basil 1 should be unpinned"
|
||||
assert @basil2.reload.pinned, "Basil 2 should be pinned"
|
||||
|
||||
# Step 5: Unpin the currently pinned basil (go back to 0 pins)
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @basil2.id, image_type: 'basil_commission' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
assert_not JSON.parse(response.body)['pinned']
|
||||
|
||||
# Verify final state - nothing should be pinned
|
||||
assert_not @image1.reload.pinned, "Image 1 should remain unpinned"
|
||||
assert_not @image2.reload.pinned, "Image 2 should remain unpinned"
|
||||
assert_not @basil1.reload.pinned, "Basil 1 should remain unpinned"
|
||||
assert_not @basil2.reload.pinned, "Basil 2 should be unpinned"
|
||||
end
|
||||
|
||||
test "pin workflow across different content types" do
|
||||
# Create another character with images
|
||||
character2 = Character.create!(
|
||||
name: 'Test Character 2',
|
||||
user: @user,
|
||||
privacy: 'public'
|
||||
)
|
||||
|
||||
image_char2 = ImageUpload.create!(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: character2.id,
|
||||
privacy: 'public',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
# Pin image for character 1
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @image1.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
assert @image1.reload.pinned, "Character 1 image should be pinned"
|
||||
|
||||
# Pin image for character 2 - should NOT affect character 1 pins
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: image_char2.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
assert_response :success
|
||||
|
||||
# Both should remain pinned (different content)
|
||||
assert @image1.reload.pinned, "Character 1 image should remain pinned"
|
||||
assert image_char2.reload.pinned, "Character 2 image should be pinned"
|
||||
assert_not @image2.reload.pinned, "Character 1 other image should remain unpinned"
|
||||
end
|
||||
|
||||
test "pin workflow with rapid requests should handle gracefully" do
|
||||
# Simulate rapid clicking by making multiple requests quickly
|
||||
threads = []
|
||||
results = []
|
||||
|
||||
5.times do |i|
|
||||
threads << Thread.new do
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: @image1.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
results << {
|
||||
status: response.status,
|
||||
body: response.body.present? ? JSON.parse(response.body) : nil
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
threads.each(&:join)
|
||||
|
||||
# At least one request should succeed
|
||||
successful_requests = results.select { |r| r[:status] == 200 }
|
||||
assert successful_requests.any?, "At least one request should succeed"
|
||||
|
||||
# Final state should be consistent
|
||||
@image1.reload
|
||||
assert [@image1.pinned, !@image1.pinned].include?(true), "Image should have a consistent final state"
|
||||
end
|
||||
|
||||
test "database performance with many images" do
|
||||
# Create many images to test performance
|
||||
images = []
|
||||
20.times do |i|
|
||||
images << ImageUpload.create!(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: false
|
||||
)
|
||||
end
|
||||
|
||||
# Time the pin operation
|
||||
start_time = Time.current
|
||||
|
||||
post toggle_image_pin_path,
|
||||
params: { image_id: images.first.id, image_type: 'image_upload' },
|
||||
headers: { 'Accept' => 'application/json' }
|
||||
|
||||
end_time = Time.current
|
||||
duration = end_time - start_time
|
||||
|
||||
assert_response :success
|
||||
assert duration < 1.0, "Pin operation should complete in under 1 second even with many images"
|
||||
|
||||
# Verify only one image is pinned
|
||||
pinned_count = ImageUpload.where(
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
pinned: true
|
||||
).count
|
||||
|
||||
assert_equal 1, pinned_count, "Only one image should be pinned"
|
||||
end
|
||||
|
||||
# Using Devise::Test::IntegrationHelpers for sign_in
|
||||
end
|
||||
206
test/models/basil_commission_pin_test.rb
Normal file
206
test/models/basil_commission_pin_test.rb
Normal file
@ -0,0 +1,206 @@
|
||||
require 'test_helper'
|
||||
|
||||
class BasilCommissionPinTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@user = users(:one)
|
||||
@character = characters(:one)
|
||||
end
|
||||
|
||||
test "should allow setting pinned to true" do
|
||||
commission = BasilCommission.new(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Test prompt',
|
||||
job_id: 'test_job_123',
|
||||
pinned: true
|
||||
)
|
||||
|
||||
assert commission.valid?, "Commission with pinned=true should be valid"
|
||||
assert commission.pinned, "Commission should be pinned"
|
||||
end
|
||||
|
||||
test "should allow setting pinned to false" do
|
||||
commission = BasilCommission.new(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Test prompt',
|
||||
job_id: 'test_job_456',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
assert commission.valid?, "Commission with pinned=false should be valid"
|
||||
assert_not commission.pinned, "Commission should not be pinned"
|
||||
end
|
||||
|
||||
test "should default pinned to false if not specified" do
|
||||
commission = BasilCommission.new(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Test prompt',
|
||||
job_id: 'test_job_789'
|
||||
)
|
||||
|
||||
assert_not commission.pinned, "Commission should default to not pinned"
|
||||
end
|
||||
|
||||
test "pinned scope should return only pinned commissions" do
|
||||
pinned_commission = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Pinned test',
|
||||
job_id: 'pinned_job_123',
|
||||
pinned: true
|
||||
)
|
||||
|
||||
unpinned_commission = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Unpinned test',
|
||||
job_id: 'unpinned_job_456',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
pinned_commissions = BasilCommission.pinned
|
||||
|
||||
assert_includes pinned_commissions, pinned_commission
|
||||
assert_not_includes pinned_commissions, unpinned_commission
|
||||
end
|
||||
|
||||
test "should update pinned status without triggering callbacks" do
|
||||
commission = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Test prompt',
|
||||
job_id: 'test_job_update',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
# This simulates what the controller does
|
||||
commission.update_column(:pinned, true)
|
||||
|
||||
assert commission.reload.pinned, "Commission should be pinned after update_column"
|
||||
end
|
||||
|
||||
test "multiple commissions can exist for same entity with different pin status" do
|
||||
commission1 = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'First commission',
|
||||
job_id: 'job_1',
|
||||
pinned: true
|
||||
)
|
||||
|
||||
commission2 = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Second commission',
|
||||
job_id: 'job_2',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
assert commission1.valid?
|
||||
assert commission2.valid?
|
||||
assert commission1.pinned
|
||||
assert_not commission2.pinned
|
||||
end
|
||||
|
||||
test "should be able to query pinned commissions for specific entity" do
|
||||
# Create commissions for character 1
|
||||
char1_pinned = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Character 1 pinned',
|
||||
job_id: 'char1_pinned_job',
|
||||
pinned: true
|
||||
)
|
||||
|
||||
char1_unpinned = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Character 1 unpinned',
|
||||
job_id: 'char1_unpinned_job',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
# Create commission for character 2
|
||||
char2 = characters(:two)
|
||||
char2_pinned = BasilCommission.create!(
|
||||
user: users(:two),
|
||||
entity: char2,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Character 2 pinned',
|
||||
job_id: 'char2_pinned_job',
|
||||
pinned: true
|
||||
)
|
||||
|
||||
# Query pinned commissions for character 1 only
|
||||
char1_pinned_commissions = BasilCommission.where(
|
||||
entity_type: 'Character',
|
||||
entity_id: @character.id,
|
||||
pinned: true
|
||||
)
|
||||
|
||||
assert_equal 1, char1_pinned_commissions.count
|
||||
assert_includes char1_pinned_commissions, char1_pinned
|
||||
assert_not_includes char1_pinned_commissions, char1_unpinned
|
||||
assert_not_includes char1_pinned_commissions, char2_pinned
|
||||
end
|
||||
|
||||
test "ordered scope should work with pinned commissions" do
|
||||
# Create commissions with different positions
|
||||
commission1 = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'First commission',
|
||||
job_id: 'job_1',
|
||||
pinned: true,
|
||||
position: 2
|
||||
)
|
||||
|
||||
commission2 = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Second commission',
|
||||
job_id: 'job_2',
|
||||
pinned: false,
|
||||
position: 1
|
||||
)
|
||||
|
||||
ordered_commissions = BasilCommission.where(entity: @character).ordered
|
||||
|
||||
assert_equal commission2, ordered_commissions.first, "Commission with position 1 should be first"
|
||||
assert_equal commission1, ordered_commissions.second, "Commission with position 2 should be second"
|
||||
end
|
||||
|
||||
test "should handle acts_as_paranoid for pinned commissions" do
|
||||
commission = BasilCommission.create!(
|
||||
user: @user,
|
||||
entity: @character,
|
||||
entity_type: 'Character',
|
||||
prompt: 'Test deletion',
|
||||
job_id: 'deletion_test_job',
|
||||
pinned: true
|
||||
)
|
||||
|
||||
# Soft delete the commission
|
||||
commission.destroy
|
||||
|
||||
# Should not appear in normal queries
|
||||
assert_not_includes BasilCommission.pinned, commission
|
||||
|
||||
# Should appear in with_deleted queries
|
||||
assert_includes BasilCommission.with_deleted.pinned, commission
|
||||
end
|
||||
end
|
||||
148
test/models/image_upload_pin_test.rb
Normal file
148
test/models/image_upload_pin_test.rb
Normal file
@ -0,0 +1,148 @@
|
||||
require 'test_helper'
|
||||
|
||||
class ImageUploadPinTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@user = users(:one)
|
||||
@character = characters(:one)
|
||||
end
|
||||
|
||||
test "should allow setting pinned to true" do
|
||||
image = ImageUpload.new(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: true
|
||||
)
|
||||
|
||||
assert image.valid?, "Image with pinned=true should be valid"
|
||||
assert image.pinned, "Image should be pinned"
|
||||
end
|
||||
|
||||
test "should allow setting pinned to false" do
|
||||
image = ImageUpload.new(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
assert image.valid?, "Image with pinned=false should be valid"
|
||||
assert_not image.pinned, "Image should not be pinned"
|
||||
end
|
||||
|
||||
test "should default pinned to false if not specified" do
|
||||
image = ImageUpload.new(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public'
|
||||
)
|
||||
|
||||
assert_not image.pinned, "Image should default to not pinned"
|
||||
end
|
||||
|
||||
test "pinned scope should return only pinned images" do
|
||||
pinned_image = ImageUpload.create!(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: true
|
||||
)
|
||||
|
||||
unpinned_image = ImageUpload.create!(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
pinned_images = ImageUpload.pinned
|
||||
|
||||
assert_includes pinned_images, pinned_image
|
||||
assert_not_includes pinned_images, unpinned_image
|
||||
end
|
||||
|
||||
test "should update pinned status without triggering callbacks" do
|
||||
image = ImageUpload.create!(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
# This simulates what the controller does
|
||||
image.update_column(:pinned, true)
|
||||
|
||||
assert image.reload.pinned, "Image should be pinned after update_column"
|
||||
end
|
||||
|
||||
test "multiple images can exist for same content with different pin status" do
|
||||
image1 = ImageUpload.create!(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: true
|
||||
)
|
||||
|
||||
image2 = ImageUpload.create!(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
assert image1.valid?
|
||||
assert image2.valid?
|
||||
assert image1.pinned
|
||||
assert_not image2.pinned
|
||||
end
|
||||
|
||||
test "should be able to query pinned images for specific content" do
|
||||
# Create images for character 1
|
||||
char1_pinned = ImageUpload.create!(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: true
|
||||
)
|
||||
|
||||
char1_unpinned = ImageUpload.create!(
|
||||
user: @user,
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
privacy: 'public',
|
||||
pinned: false
|
||||
)
|
||||
|
||||
# Create image for character 2
|
||||
char2 = characters(:two)
|
||||
char2_pinned = ImageUpload.create!(
|
||||
user: users(:two),
|
||||
content_type: 'Character',
|
||||
content_id: char2.id,
|
||||
privacy: 'public',
|
||||
pinned: true
|
||||
)
|
||||
|
||||
# Query pinned images for character 1 only
|
||||
char1_pinned_images = ImageUpload.where(
|
||||
content_type: 'Character',
|
||||
content_id: @character.id,
|
||||
pinned: true
|
||||
)
|
||||
|
||||
# Should include our newly created image plus any fixture images
|
||||
assert char1_pinned_images.count >= 1, "Should have at least one pinned image"
|
||||
assert_includes char1_pinned_images, char1_pinned
|
||||
assert_not_includes char1_pinned_images, char1_unpinned
|
||||
assert_not_includes char1_pinned_images, char2_pinned
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user