~singpolyma/sgx-jmp

ref: 1b9bf2959540f16f493665453383978f6497c79a sgx-jmp/test/test_low_balance.rb -rw-r--r-- 2.1 KiB
1b9bf295Stephen Paul Weber On low balance, top-up or notify 1 year, 2 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
# frozen_string_literal: true

require "test_helper"
require "low_balance"

ExpiringLock::REDIS = Minitest::Mock.new
CustomerPlan::REDIS = Minitest::Mock.new
Customer::REDIS = Minitest::Mock.new

class LowBalanceTest < Minitest::Test
	def test_for_locked
		ExpiringLock::REDIS.expect(
			:exists,
			EMPromise.resolve(1),
			["jmp_low_balance_notify-test"]
		)
		assert_kind_of LowBalance::Locked, LowBalance.for(Customer.new("test")).sync
	end
	em :test_for_locked

	def test_for_no_auto_top_up
		ExpiringLock::REDIS.expect(
			:exists,
			EMPromise.resolve(0),
			["jmp_low_balance_notify-test"]
		)
		CustomerPlan::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["jmp_customer_auto_top_up_amount-test"]
		)
		Customer::REDIS.expect(
			:smembers,
			EMPromise.resolve([]),
			["jmp_customer_btc_addresses-test"]
		)
		ExpiringLock::REDIS.expect(
			:setex,
			EMPromise.resolve(nil),
			["jmp_low_balance_notify-test", 60 * 60 * 24, ""]
		)
		assert_kind_of(
			LowBalance,
			LowBalance.for(Customer.new("test")).sync
		)
		assert_mock ExpiringLock::REDIS
	end
	em :test_for_no_auto_top_up

	def test_for_auto_top_up
		ExpiringLock::REDIS.expect(
			:exists,
			EMPromise.resolve(0),
			["jmp_low_balance_notify-test"]
		)
		CustomerPlan::REDIS.expect(
			:get,
			EMPromise.resolve("15"),
			["jmp_customer_auto_top_up_amount-test"]
		)
		ExpiringLock::REDIS.expect(
			:setex,
			EMPromise.resolve(nil),
			["jmp_low_balance_notify-test", 60 * 60 * 24, ""]
		)
		assert_kind_of(
			LowBalance::AutoTopUp,
			LowBalance.for(Customer.new("test")).sync
		)
		assert_mock ExpiringLock::REDIS
	end
	em :test_for_auto_top_up

	class AutoTopUpTest < Minitest::Test
		LowBalance::AutoTopUp::Transaction = Minitest::Mock.new

		def setup
			@customer = Customer.new("test")
			@auto_top_up = LowBalance::AutoTopUp.new(@customer, 100)
		end

		def test_notify!
			tx = PromiseMock.new
			tx.expect(:insert, EMPromise.resolve(nil))
			LowBalance::AutoTopUp::Transaction.expect(
				:sale,
				tx,
				[@customer, amount: 100]
			)
			@auto_top_up.notify!
			assert_mock tx
		end
		em :test_notify!
	end
end