# frozen_string_literal: true

require "test_helper"
require "auto_top_up_repo"

class AutoTopUpRepoTest < Minitest::Test
	def setup
		@redis = Minitest::Mock.new
		@db = Minitest::Mock.new
		@repo = AutoTopUpRepo.new(redis: @redis, db: @db)
	end

	property(:find) { string }
	def find(customer_id)
		@redis.expect(
			:get,
			nil,
			["jmp_customer_auto_top_up_amount-#{customer_id}"]
		)
		@repo.find(customer_id)
		assert_mock @redis
	end

	property(:put_valid_amount) { range(15, 9999999) }
	def put_valid_amount(amount)
		@redis.expect(
			:set,
			nil,
			["jmp_customer_auto_top_up_amount-somecustomer", amount]
		)
		@redis.expect(
			:del,
			nil,
			["jmp_customer_low_balance-somecustomer"]
		)
		@db.expect(
			:exec_params,
			nil,
			["SELECT check_and_notify_low_balance($1)", ["somecustomer"]]
		)
		@repo.put("somecustomer", amount)
		assert_mock @redis
		assert_mock @db
	end

	property(:put_invalid_amount) { branch [:range, 1, 14], [:range, -999, -1] }
	def put_invalid_amount(amount)
		@repo.put("somecustomer", amount)
		assert_mock @redis
	end

	def test_put_zero
		@redis.expect(
			:del,
			nil,
			["jmp_customer_auto_top_up_amount-somecustomer"]
		)
		@repo.put("somecustomer", 0)
		assert_mock @redis
	end
end
