~singpolyma/sgx-jmp

ref: b5ae16166ab0bfbb2d360c7987396fd127c2264e sgx-jmp/test/test_customer_info_form.rb -rw-r--r-- 2.8 KiB
b5ae1616Stephen Paul Weber Refactor CustomerInfoForm to use find_by_format 1 year, 2 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# frozen_string_literal: true

require "test_helper"
require "forwardable"
require "customer_info_form"
require "customer_repo"

class FakeRepo
	def initialize(customers)
		@customers = customers
	end

	def find_by(k, v)
		@customers.find { |cust| cust.public_send(k).to_s == v.to_s }
	end

	def find_by_format(s)
		EMPromise.resolve(nil).then do
			key = CustomerRepo::QueryKey.for(s)
			case key
			when CustomerRepo::QueryKey::ID
				find_by(:customer_id, key.customer_id)
			when CustomerRepo::QueryKey::JID
				find_by(:jid, key.jid)
			when CustomerRepo::QueryKey::Tel
				find_by(:tel, key.tel)
			else
				raise "Un-faked format: #{s}"
			end || raise("No Customer")
		end
	end
end

class CustomerInfoFormTest < Minitest::Test
	def setup
		@customer_test = OpenStruct.new(
			customer_id: "test",
			jid: "test\\40example.com@example.net",
			tel: "+13334445555"
		)
		@customer_v2 = OpenStruct.new(
			customer_id: "test_v2",
			jid: "test_v2\\40example.com@example.net",
			tel: "+14445556666"
		)
		@repo = FakeRepo.new([@customer_test, @customer_v2])
		@info_form = CustomerInfoForm.new(@repo)
	end

	def test_nothing
		assert_kind_of(
			CustomerInfoForm::NoCustomer,
			@info_form.parse_something("").sync
		)
	end
	em :test_nothing

	def test_find_customer_id
		result = @info_form.parse_something("test").sync
		assert_equal @customer_test, result
	end
	em :test_find_customer_id

	def test_find_real_jid
		result = @info_form.parse_something("test@example.com").sync
		assert_equal @customer_test, result
	end
	em :test_find_real_jid

	def test_find_cheo_jid
		result = @info_form.parse_something(
			"test\\40example.com@example.net"
		).sync
		assert_equal @customer_test, result
	end
	em :test_find_cheo_jid

	def test_find_sgx_jmp_customer_by_phone
		result = @info_form.parse_something("+13334445555").sync
		assert_equal @customer_test, result
	end
	em :test_find_sgx_jmp_customer_by_phone

	def test_find_sgx_jmp_customer_by_phone_friendly_format
		result = @info_form.parse_something("13334445555").sync
		assert_equal @customer_test, result

		result = @info_form.parse_something("3334445555").sync
		assert_equal @customer_test, result

		result = @info_form.parse_something("(333) 444-5555").sync
		assert_equal @customer_test, result
	end
	em :test_find_sgx_jmp_customer_by_phone_friendly_format

	def test_find_v2_customer_by_phone
		result = @info_form.parse_something("+14445556666").sync
		assert_equal @customer_v2, result
	end
	em :test_find_v2_customer_by_phone

	def test_missing_customer_by_phone
		result = @info_form.parse_something("+17778889999").sync
		assert_kind_of(
			CustomerInfoForm::NoCustomer,
			result
		)
	end
	em :test_missing_customer_by_phone

	def test_garbage
		result = @info_form.parse_something("garbage").sync
		assert_kind_of(
			CustomerInfoForm::NoCustomer,
			result
		)
	end
	em :test_garbage
end