~singpolyma/sgx-jmp

ref: c08d6eaa188fae91ec236e04707a1faa43166878 sgx-jmp/test/test_customer.rb -rw-r--r-- 2.2 KiB
c08d6eaaStephen Paul Weber Method to bill the plan of a Customer 1 year, 4 months ago
                                                                                
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
# frozen_string_literal: true

require "test_helper"
require "customer"

Customer::REDIS = Minitest::Mock.new
Customer::DB = Minitest::Mock.new

class CustomerTest < Minitest::Test
	def test_for_jid
		Customer::REDIS.expect(
			:get,
			EMPromise.resolve(1),
			["jmp_customer_id-test@example.com"]
		)
		Customer::DB.expect(
			:query_defer,
			EMPromise.resolve([{ balance: 1234, plan_name: "test_usd" }]),
			[String, [1]]
		)
		customer = Customer.for_jid("test@example.com").sync
		assert_kind_of Customer, customer
		assert_equal 1234, customer.balance
		assert_equal "merchant_usd", customer.merchant_account
	end
	em :test_for_jid

	def test_for_jid_not_found
		Customer::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["jmp_customer_id-test2@example.com"]
		)
		assert_raises do
			Customer.for_jid("test2@example.com").sync
		end
	end
	em :test_for_jid_not_found

	def test_for_customer_id_not_found
		Customer::DB.expect(
			:query_defer,
			EMPromise.resolve([]),
			[String, [7357]]
		)
		customer = Customer.for_customer_id(7357).sync
		assert_equal BigDecimal.new(0), customer.balance
	end
	em :test_for_customer_id_not_found

	def test_bill_plan_activate
		Customer::DB.expect(:transaction, nil) do |&block|
			block.call
			true
		end
		Customer::DB.expect(
			:exec,
			nil,
			[
				String,
				Matching.new do |params|
					params[0] == "test" &&
					params[1].is_a?(String) &&
					BigDecimal.new(-1) == params[2]
				end
			]
		)
		Customer::DB.expect(
			:exec,
			OpenStruct.new(cmd_tuples: 1),
			[String, ["test", "test_usd"]]
		)
		Customer.new("test", plan_name: "test_usd").bill_plan.sync
		Customer::DB.verify
	end
	em :test_bill_plan_activate

	def test_bill_plan_update
		Customer::DB.expect(:transaction, nil) do |&block|
			block.call
			true
		end
		Customer::DB.expect(
			:exec,
			nil,
			[
				String,
				Matching.new do |params|
					params[0] == "test" &&
					params[1].is_a?(String) &&
					BigDecimal.new(-1) == params[2]
				end
			]
		)
		Customer::DB.expect(
			:exec,
			OpenStruct.new(cmd_tuples: 0),
			[String, ["test", "test_usd"]]
		)
		Customer::DB.expect(:exec, nil, [String, ["test"]])
		Customer.new("test", plan_name: "test_usd").bill_plan.sync
		Customer::DB.verify
	end
	em :test_bill_plan_update
end