test_bch_sell_prices.rb

 1# frozen_string_literal: true
 2
 3require "em-hiredis"
 4require "test_helper"
 5require "btc_sell_prices"
 6
 7class BCHSellPricesTest < Minitest::Test
 8	def setup
 9		@redis = Minitest::Mock.new
10		@bull = Minitest::Mock.new
11		@simple_swap = Minitest::Mock.new
12		@subject = BCHSellPrices.new(
13			@redis, "", simple_swap: @simple_swap, bull: @bull
14		)
15	end
16
17	def test_cad
18		@bull.expect(
19			:fetch_btc_cad,
20			EMPromise.resolve(BigDecimal("100"))
21		)
22		@simple_swap.expect(
23			:fetch_rate,
24			EMPromise.resolve(BigDecimal("20")),
25			["btc"]
26		)
27		assert_equal BigDecimal("5"), @subject.cad.sync
28		assert_mock @bull
29		assert_mock @simple_swap
30	end
31	em :test_cad
32
33	def test_cad_raises_on_zero_bch_rate
34		@bull.expect(
35			:fetch_btc_cad,
36			EMPromise.resolve(BigDecimal("100"))
37		)
38		@simple_swap.expect(
39			:fetch_rate,
40			EMPromise.resolve(BigDecimal("0")),
41			["btc"]
42		)
43
44		error = assert_raises(CryptoSellPrices::PriceError) { @subject.cad.sync }
45		assert_equal "SimpleSwap BTC to BCH rate was not positive", error.message
46		assert_mock @bull
47		assert_mock @simple_swap
48	end
49	em :test_cad_raises_on_zero_bch_rate
50
51	def test_usd
52		@bull.expect(
53			:fetch_btc_cad,
54			EMPromise.resolve(BigDecimal("100"))
55		)
56		@simple_swap.expect(
57			:fetch_rate,
58			EMPromise.resolve(BigDecimal("20")),
59			["btc"]
60		)
61		@redis.expect(:get, EMPromise.resolve("0.5"), ["cad_to_usd"])
62		assert_equal BigDecimal("2.5"), @subject.usd.sync
63		assert_mock @bull
64		assert_mock @simple_swap
65		assert_mock @redis
66	end
67	em :test_usd
68end