68 lines
2.4 KiB
Ruby
68 lines
2.4 KiB
Ruby
require "test_helper"
|
|
|
|
class PromotionCodeTest < ActiveSupport::TestCase
|
|
# Test valid promotion code creation
|
|
def test_valid_promotion_code
|
|
promotion_code = PromotionCode.create(
|
|
code: "DISCOUNT10",
|
|
discount_amount_cents: 1000, # €10.00
|
|
expires_at: 1.month.from_now,
|
|
active: true
|
|
)
|
|
|
|
assert promotion_code.valid?
|
|
assert_equal "DISCOUNT10", promotion_code.code
|
|
assert_equal 1000, promotion_code.discount_amount_cents
|
|
assert promotion_code.active?
|
|
end
|
|
|
|
# Test validation for required fields
|
|
def test_validation_for_required_fields
|
|
promotion_code = PromotionCode.new
|
|
refute promotion_code.valid?
|
|
assert_not_nil promotion_code.errors[:code]
|
|
end
|
|
|
|
# Test unique code validation
|
|
def test_unique_code_validation
|
|
PromotionCode.create(code: "UNIQUE123", discount_amount_cents: 500)
|
|
duplicate_code = PromotionCode.new(code: "UNIQUE123", discount_amount_cents: 500)
|
|
refute duplicate_code.valid?
|
|
assert_not_nil duplicate_code.errors[:code]
|
|
end
|
|
|
|
# Test discount amount validation
|
|
def test_discount_amount_validation
|
|
promotion_code = PromotionCode.new(code: "VALID123", discount_amount_cents: -100)
|
|
refute promotion_code.valid?
|
|
assert_not_nil promotion_code.errors[:discount_amount_cents]
|
|
end
|
|
|
|
# Test active scope
|
|
def test_active_scope
|
|
active_code = PromotionCode.create(code: "ACTIVE123", discount_amount_cents: 500, active: true)
|
|
inactive_code = PromotionCode.create(code: "INACTIVE123", discount_amount_cents: 500, active: false)
|
|
|
|
assert_includes PromotionCode.active, active_code
|
|
refute_includes PromotionCode.active, inactive_code
|
|
end
|
|
|
|
# Test expired scope
|
|
def test_expired_scope
|
|
expired_code = PromotionCode.create(code: "EXPIRED123", discount_amount_cents: 500, expires_at: 1.day.ago)
|
|
future_code = PromotionCode.create(code: "FUTURE123", discount_amount_cents: 500, expires_at: 1.month.from_now)
|
|
|
|
assert_includes PromotionCode.expired, expired_code
|
|
refute_includes PromotionCode.expired, future_code
|
|
end
|
|
|
|
# Test valid scope
|
|
def test_valid_scope
|
|
valid_code = PromotionCode.create(code: "VALID123", discount_amount_cents: 500, active: true, expires_at: 1.month.from_now)
|
|
invalid_code = PromotionCode.create(code: "INVALID123", discount_amount_cents: 500, active: false, expires_at: 1.day.ago)
|
|
|
|
assert_includes PromotionCode.valid, valid_code
|
|
refute_includes PromotionCode.valid, invalid_code
|
|
end
|
|
end
|