test_auto_top_up_repo.rb

 1# frozen_string_literal: true
 2
 3require "test_helper"
 4require "auto_top_up_repo"
 5
 6class AutoTopUpRepoTest < Minitest::Test
 7	def setup
 8		@redis = Minitest::Mock.new
 9		@db = Minitest::Mock.new
10		@repo = AutoTopUpRepo.new(redis: @redis, db: @db)
11	end
12
13	property(:find) { string }
14	def find(customer_id)
15		@redis.expect(
16			:get,
17			nil,
18			["jmp_customer_auto_top_up_amount-#{customer_id}"]
19		)
20		@repo.find(customer_id)
21		assert_mock @redis
22	end
23
24	property(:put_valid_amount) { range(15, 9999999) }
25	def put_valid_amount(amount)
26		@redis.expect(
27			:set,
28			nil,
29			["jmp_customer_auto_top_up_amount-somecustomer", amount]
30		)
31		@redis.expect(
32			:del,
33			nil,
34			["jmp_customer_low_balance-somecustomer"]
35		)
36		@db.expect(
37			:exec_params,
38			nil,
39			["SELECT check_and_notify_low_balance($1)", ["somecustomer"]]
40		)
41		@repo.put("somecustomer", amount)
42		assert_mock @redis
43		assert_mock @db
44	end
45
46	property(:put_invalid_amount) { branch [:range, 1, 14], [:range, -999, -1] }
47	def put_invalid_amount(amount)
48		@repo.put("somecustomer", amount)
49		assert_mock @redis
50	end
51
52	def test_put_zero
53		@redis.expect(
54			:del,
55			nil,
56			["jmp_customer_auto_top_up_amount-somecustomer"]
57		)
58		@repo.put("somecustomer", 0)
59		assert_mock @redis
60	end
61end