~singpolyma/sgx-jmp

ref: 023c87a11bbaa6bd3eaeb3977330b8ea80ded0cd sgx-jmp/test/test_low_balance.rb -rw-r--r-- 2.0 KiB
023c87a1Stephen Paul Weber Quieter voicemail beep 7 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_customer_low_balance-test"]
		)
		assert_kind_of LowBalance::Locked, LowBalance.for(customer).sync
	end
	em :test_for_locked

	def test_for_no_auto_top_up
		ExpiringLock::REDIS.expect(
			:exists,
			EMPromise.resolve(0),
			["jmp_customer_low_balance-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_customer_low_balance-test", 60 * 60 * 24 * 7, Time]
		)
		assert_kind_of(
			LowBalance,
			LowBalance.for(customer).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_customer_low_balance-test"]
		)
		CustomerPlan::REDIS.expect(
			:get,
			EMPromise.resolve("15"),
			["jmp_customer_auto_top_up_amount-test"]
		)
		ExpiringLock::REDIS.expect(
			:setex,
			EMPromise.resolve(nil),
			["jmp_customer_low_balance-test", 60 * 60 * 24 * 7, Time]
		)
		assert_kind_of(
			LowBalance::AutoTopUp,
			LowBalance.for(customer).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
			@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