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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
require 'test_helper'
# Test Models:
#
# Answer - :scope => :question_id
# Comment - :scope => :question_id (with an AR default scope)
# Invoice - :scope => :account_id, :start_at => 1000
# Product - :scope => :account_id, :start_at => lambda { |r| r.computed_start_value }
# Order - :scope => :non_existent_column
# User - :scope => :account_id, :column => :custom_sequential_id
# Address - :scope => :account_id ('sequential_id' does not exist)
# Email - :scope => [:emailable_id, :emailable_type]
# Subscription - no options
# Rating - :scope => :comment_id, skip: { |r| r.score == 0 }
# Monster - no options
# Zombie - STI, inherits from Monster
# Werewolf - STI, inherits from Monster
class ActsAsSequencedTest < ActiveSupport::TestCase
test "default start_at" do
question = Question.create
answer = question.answers.create
assert_equal 1, answer.sequential_id
end
test "custom start_at" do
account = Account.create
invoice = account.invoices.create
assert_equal 1000, invoice.sequential_id
another_invoice = account.invoices.create
assert_equal 1001, another_invoice.sequential_id
end
test "lambda start_at" do
account = Account.create
product = Product.create(:account_id => account.id)
assert_equal 3, product.sequential_id
another_product = Product.create(:account_id => account.id)
assert_equal 4, another_product.sequential_id
end
test "custom start_at with populated table" do
account = Account.create
account.invoices.create(:sequential_id => 1)
invoice = account.invoices.create
assert_equal 1000, invoice.sequential_id
end
test "sequential id increment" do
question = Question.create
question.answers.create(:sequential_id => 10)
another_answer = question.answers.create
assert_equal 11, another_answer.sequential_id
end
test "default scope" do
Subscription.create(:sequential_id => 1)
subscription = Subscription.create
assert_equal 2, subscription.sequential_id
end
test "undefined scope method" do
account = Account.create
order = account.orders.build
assert_raises(NoMethodError) { order.save }
end
test "custom sequential id column" do
account = Account.create
user = account.users.create
assert_equal 1, user.custom_sequential_id
end
test "sequential id remains on save" do
question = Question.create
answer = question.answers.create
assert_equal 1, answer.sequential_id
answer.reload
answer.body = "Updated body"
answer.save
assert_equal 1, answer.sequential_id
end
test "undefined sequential id column" do
account = Account.create
address = account.addresses.build
assert_raises(NoMethodError) { address.save }
end
test "manually setting sequential id" do
question = Question.create
answer = question.answers.build(:sequential_id => 10)
another_answer = question.answers.build(:sequential_id => 10)
answer.save
another_answer.save
assert_equal 10, answer.sequential_id
assert_equal 10, another_answer.sequential_id
end
test "model with a default scope for sorting" do
question = Question.create
(1..3).each { |id| question.comments.create(:sequential_id => id) }
comment = question.comments.create
assert_equal 4, comment.sequential_id
end
test "multi-column scopes" do
Email.create(:emailable_id => 1, :emailable_type => "User", :sequential_id => 2)
Email.create(:emailable_id => 1, :emailable_type => "Question", :sequential_id => 3)
email = Email.create(:emailable_id => 1, :emailable_type => "User")
assert_equal 3, email.sequential_id
end
test "skip option" do
rating = Rating.create(:comment_id => 1, :score => 1)
assert_equal 1, rating.sequential_id
rating = Rating.create(:comment_id => 1, :score => 0)
assert_equal nil, rating.sequential_id
end
test "STI" do
zombie = Zombie.create
werewolf = Werewolf.create
assert_equal 1, zombie.sequential_id
assert_equal 2, werewolf.sequential_id
end
end
|