test_customer.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "customer"
  5
  6Customer::REDIS = Minitest::Mock.new
  7Customer::DB = Minitest::Mock.new
  8CustomerPlan::DB = Minitest::Mock.new
  9
 10class CustomerTest < Minitest::Test
 11	def test_for_jid
 12		Customer::REDIS.expect(
 13			:get,
 14			EMPromise.resolve(1),
 15			["jmp_customer_id-test@example.com"]
 16		)
 17		Customer::DB.expect(
 18			:query_defer,
 19			EMPromise.resolve([{ balance: 1234, plan_name: "test_usd" }]),
 20			[String, [1]]
 21		)
 22		customer = Customer.for_jid("test@example.com").sync
 23		assert_kind_of Customer, customer
 24		assert_equal 1234, customer.balance
 25		assert_equal "merchant_usd", customer.merchant_account
 26	end
 27	em :test_for_jid
 28
 29	def test_for_jid_not_found
 30		Customer::REDIS.expect(
 31			:get,
 32			EMPromise.resolve(nil),
 33			["jmp_customer_id-test2@example.com"]
 34		)
 35		assert_raises do
 36			Customer.for_jid("test2@example.com").sync
 37		end
 38	end
 39	em :test_for_jid_not_found
 40
 41	def test_for_customer_id_not_found
 42		Customer::DB.expect(
 43			:query_defer,
 44			EMPromise.resolve([]),
 45			[String, [7357]]
 46		)
 47		customer = Customer.for_customer_id(7357).sync
 48		assert_equal BigDecimal.new(0), customer.balance
 49	end
 50	em :test_for_customer_id_not_found
 51
 52	def test_bill_plan_activate
 53		CustomerPlan::DB.expect(:transaction, nil) do |&block|
 54			block.call
 55			true
 56		end
 57		CustomerPlan::DB.expect(
 58			:exec,
 59			nil,
 60			[
 61				String,
 62				Matching.new do |params|
 63					params[0] == "test" &&
 64					params[1].is_a?(String) &&
 65					BigDecimal.new(-1) == params[2]
 66				end
 67			]
 68		)
 69		CustomerPlan::DB.expect(
 70			:exec,
 71			OpenStruct.new(cmd_tuples: 1),
 72			[String, ["test", "test_usd"]]
 73		)
 74		Customer.new("test", plan_name: "test_usd").bill_plan.sync
 75		CustomerPlan::DB.verify
 76	end
 77	em :test_bill_plan_activate
 78
 79	def test_bill_plan_update
 80		CustomerPlan::DB.expect(:transaction, nil) do |&block|
 81			block.call
 82			true
 83		end
 84		CustomerPlan::DB.expect(
 85			:exec,
 86			nil,
 87			[
 88				String,
 89				Matching.new do |params|
 90					params[0] == "test" &&
 91					params[1].is_a?(String) &&
 92					BigDecimal.new(-1) == params[2]
 93				end
 94			]
 95		)
 96		CustomerPlan::DB.expect(
 97			:exec,
 98			OpenStruct.new(cmd_tuples: 0),
 99			[String, ["test", "test_usd"]]
100		)
101		CustomerPlan::DB.expect(:exec, nil, [String, ["test"]])
102		Customer.new("test", plan_name: "test_usd").bill_plan.sync
103		CustomerPlan::DB.verify
104	end
105	em :test_bill_plan_update
106end