这些是我的 Cashout 模型的一些代码。我可以使用 rspec 测试所有模型,但我不知道如何测试这些范围。
提现.rb
class Cashout < ActiveRecord::Base
belongs_to :partner
scope :add_virtual_columns, select(
"cashouts.*,
( SELECT SUM(c.amount) FROM cashouts c WHERE c.partner_id = cashouts.partner_id) as total_paid_amount,
(
(
CASE
WHEN (SELECT partner_type FROM partners WHERE cashouts.partner_id = partners.id) = 'administrator'
THEN
(
CASE
WHEN ( SELECT SUM(cr.partner_profit) FROM contact_records cr WHERE cr.partner_id in (SELECT id FROM partners WHERE company_id in (SELECT company_id FROM partners WHERE id = cashouts.partner_id))) IS NULL
THEN 0
ELSE ( SELECT SUM(cr.partner_profit + cr.company_profit) FROM contact_records cr WHERE cr.partner_id in (SELECT id FROM partners WHERE company_id in (SELECT company_id FROM partners WHERE id = cashouts.partner_id)))
END
)
ELSE
(
CASE
WHEN ( SELECT SUM(cr.partner_profit) FROM contact_records cr WHERE cr.partner_id = cashouts.partner_id) IS NULL
THEN 0
ELSE ( SELECT SUM(cr.partner_profit) FROM contact_records cr WHERE cr.partner_id = cashouts.partner_id)
END
)
END
)
-
( SELECT SUM(c.amount) FROM cashouts c WHERE c.partner_id = cashouts.partner_id)
) as unpaid_amount"
).group('cashouts.id')
scope :sort_by_total_paid_amount_asc, order("total_paid_amount ASC")
scope :sort_by_total_paid_amount_desc, order("total_paid_amount DESC")
scope :sort_by_unpaid_amount_asc, order("unpaid_amount ASC")
scope :sort_by_unpaid_amount_desc, order("unpaid_amount DESC")
end
您知道如何使用 rspec 和 factorygirl 测试这些示波器吗?