Created
December 15, 2020 09:29
-
-
Save aaripurna/84b8e752ce8a8274c4d0330eee67b47c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I am assuming that the modles will look like this | |
class Customer < ApplicationRecord | |
has_many :purcases | |
# Rest of the code | |
end | |
class Purcase < ApplicationRecord | |
has_one :discount_coupon_usage | |
# Rest of the code | |
end | |
class DiscountCouponUsage < ApplicationRecord | |
belongs_to :purcase | |
belongs_to :discount_coupon | |
# Rest of the code | |
end | |
class DiscountCoupon < ApplicationRecord | |
has_many :discount_coupon_usages | |
# Rest of the code | |
end | |
And here is the test | |
class CustomerTest << ActiveSupport::TestCase | |
test "a new customer will get a 15% discount on all your purchases today" do | |
customer = customer(:one) | |
purcases = purcase(:three) | |
customer << purcases | |
customer.purcases.each do |purcase| | |
assert_equal(purcase.total_paid, purcase.total_purcase - (purcase.total_purcase * 15 / 100)) | |
end | |
end | |
test "existing customer and has hold a loyalty card, will get a 10% discount" do | |
customer = customer(:one) | |
customer.update(created_at: 3.months.ago, own_loyalty_card: true) | |
customer.purcase = purcase(:one) | |
assert_equal(customer.purcase.total_paid, purcase.total_purcase - (purcase.total_purcase * 15 / 100)) | |
end | |
test "existing customer and without a loyalty card, will not get a discount" do | |
customer = customer(:one) | |
customer.update(created_at: 3.months.ago, own_loyalty_card: true) | |
customer.purcase = purcase(:one) | |
assert_equal(customer.purcase.total_paid, purcase.total_purcase) | |
end | |
test "a new customer with discout coupon will get 20% off" do | |
customer = customer(:one) | |
customer.purcase = purcase(:one) | |
discoun_coupon = discount_coupon(:one) | |
discoun_coupn_usage = DiscountCouponUsage.create(purcase_id: customer.purcase.id, discoun_coupon_id: discoun_coupon.id) | |
assert_equal(customer.purcase.total_paid, purcase.total_purcase - (purcase.total_purcase * 20 / 100)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment