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