~singpolyma/sgx-jmp

ref: 6ead87605043d167e477aa8d7959d6e4b7af7d03 sgx-jmp/test/test_customer_repo.rb -rw-r--r-- 2.2 KiB
6ead8760Christopher Vollick Remove BigDecimal.new 1 year, 5 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
96
# frozen_string_literal: true

require "test_helper"
require "customer_repo"

class CustomerRepoTest < Minitest::Test
	def mkrepo(
		redis: Minitest::Mock.new,
		db: Minitest::Mock.new,
		braintree: Minitest::Mock.new
	)
		CustomerRepo.new(redis: redis, db: db, braintree: braintree)
	end

	def test_find_by_jid
		redis = Minitest::Mock.new
		db = Minitest::Mock.new
		repo = mkrepo(redis: redis, db: db)
		redis.expect(
			:get,
			EMPromise.resolve(1),
			["jmp_customer_id-test@example.com"]
		)
		db.expect(
			:query_defer,
			EMPromise.resolve([{ balance: 1234, plan_name: "test_usd" }]),
			[String, [1]]
		)
		customer = repo.find_by_jid("test@example.com").sync
		assert_kind_of Customer, customer
		assert_equal 1234, customer.balance
		assert_equal "merchant_usd", customer.merchant_account
		assert_mock redis
		assert_mock db
	end
	em :test_find_by_jid

	def test_find_by_jid_not_found
		redis = Minitest::Mock.new
		repo = mkrepo(redis: redis)
		redis.expect(
			:get,
			EMPromise.resolve(nil),
			["jmp_customer_id-test2@example.com"]
		)
		assert_raises do
			repo.find_by_jid("test2@example.com").sync
		end
		assert_mock redis
	end
	em :test_find_by_jid_not_found

	def test_find_db_empty
		db = Minitest::Mock.new
		redis = Minitest::Mock.new
		redis.expect(
			:get,
			EMPromise.resolve("test@example.net"),
			["jmp_customer_jid-7357"]
		)
		repo = mkrepo(db: db, redis: redis)
		db.expect(
			:query_defer,
			EMPromise.resolve([]),
			[String, [7357]]
		)
		customer = repo.find(7357).sync
		assert_equal BigDecimal(0), customer.balance
		assert_mock db
	end
	em :test_find_db_empty

	def test_create
		redis = Minitest::Mock.new
		braintree = Minitest::Mock.new
		repo = mkrepo(redis: redis, braintree: braintree)
		braintree_customer = Minitest::Mock.new
		braintree.expect(:customer, braintree_customer)
		braintree_customer.expect(:create, EMPromise.resolve(
			OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
		))
		redis.expect(
			:msetnx,
			EMPromise.resolve(1),
			[
				"jmp_customer_id-test@example.com", "test",
				"jmp_customer_jid-test", "test@example.com"
			]
		)
		assert_kind_of Customer, repo.create("test@example.com").sync
		assert_mock braintree
		assert_mock braintree_customer
		assert_mock redis
	end
	em :test_create
end