test_customer_info_form.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "forwardable"
  5require "customer_info_form"
  6require "customer_repo"
  7
  8class FakeRepo
  9	def initialize(customers)
 10		@customers = customers
 11	end
 12
 13	def find_by(k, v)
 14		@customers.find { |cust| cust.public_send(k).to_s == v.to_s }
 15	end
 16
 17	def find_by_format(s)
 18		EMPromise.resolve(nil).then do
 19			key = CustomerRepo::QueryKey.for(s)
 20			case key
 21			when CustomerRepo::QueryKey::ID
 22				find_by(:customer_id, key.customer_id)
 23			when CustomerRepo::QueryKey::JID
 24				find_by(:jid, key.jid)
 25			when CustomerRepo::QueryKey::Tel
 26				find_by(:tel, key.tel)
 27			else
 28				raise "Un-faked format: #{s}"
 29			end || raise(CustomerRepo::NotFound, "No Customer")
 30		end
 31	end
 32end
 33
 34class CustomerInfoFormTest < Minitest::Test
 35	def setup
 36		@customer_test = OpenStruct.new(
 37			customer_id: "test",
 38			jid: "test\\40example.com@example.net",
 39			tel: "+13334445555"
 40		)
 41		@customer_v2 = OpenStruct.new(
 42			customer_id: "test_v2",
 43			jid: "test_v2\\40example.com@example.net",
 44			tel: "+14445556666"
 45		)
 46		@repo = FakeRepo.new([@customer_test, @customer_v2])
 47		@info_form = CustomerInfoForm.new(@repo)
 48	end
 49
 50	def test_nothing
 51		assert_nil(@info_form.parse_something("").sync)
 52	end
 53	em :test_nothing
 54
 55	def test_find_customer_id
 56		result = @info_form.parse_something("test").sync
 57		assert_equal @customer_test, result
 58	end
 59	em :test_find_customer_id
 60
 61	def test_find_real_jid
 62		result = @info_form.parse_something("test@example.com").sync
 63		assert_equal @customer_test, result
 64	end
 65	em :test_find_real_jid
 66
 67	def test_find_cheo_jid
 68		result = @info_form.parse_something(
 69			"test\\40example.com@example.net"
 70		).sync
 71		assert_equal @customer_test, result
 72	end
 73	em :test_find_cheo_jid
 74
 75	def test_find_sgx_jmp_customer_by_phone
 76		result = @info_form.parse_something("+13334445555").sync
 77		assert_equal @customer_test, result
 78	end
 79	em :test_find_sgx_jmp_customer_by_phone
 80
 81	def test_find_sgx_jmp_customer_by_phone_friendly_format
 82		result = @info_form.parse_something("13334445555").sync
 83		assert_equal @customer_test, result
 84
 85		result = @info_form.parse_something("3334445555").sync
 86		assert_equal @customer_test, result
 87
 88		result = @info_form.parse_something("(333) 444-5555").sync
 89		assert_equal @customer_test, result
 90	end
 91	em :test_find_sgx_jmp_customer_by_phone_friendly_format
 92
 93	def test_find_v2_customer_by_phone
 94		result = @info_form.parse_something("+14445556666").sync
 95		assert_equal @customer_v2, result
 96	end
 97	em :test_find_v2_customer_by_phone
 98
 99	def test_missing_customer_by_phone
100		result = @info_form.parse_something("+17778889999").sync
101		assert_nil(result)
102	end
103	em :test_missing_customer_by_phone
104
105	def test_garbage
106		result = @info_form.parse_something("garbage").sync
107		assert_nil(result)
108	end
109	em :test_garbage
110
111	def test_route
112		result = @info_form.parse_something("route").sync
113		assert_nil(result)
114	end
115	em :test_route
116end