1# frozen_string_literal: true
2
3require "test_helper"
4require "local_calling_guide_repo"
5
6class LocalCallingGuideRepoTest < Minitest::Test
7 def test_find_returns_lat_lon
8 body = Nokogiri::XML::Builder.new { |xml|
9 xml.root do
10 xml.prefixdata do
11 xml.npa "917"
12 xml.nxx "727"
13 xml.x "0"
14 xml.rc "NEW YORK"
15 xml.region "NY"
16 xml.send(:"rc-lat", "40.739362")
17 xml.send(:"rc-lon", "-73.991043")
18 end
19 xml.prefixdata do
20 xml.npa "917"
21 xml.nxx "727"
22 xml.x "1"
23 xml.rc "NEW YORK"
24 xml.region "NY"
25 xml.send(:"rc-lat", "40.739362")
26 xml.send(:"rc-lon", "-73.991043")
27 end
28 end
29 }.to_xml
30
31 stub_request(
32 :get,
33 "https://localcallingguide.com/xmlprefix.php?npa=917&nxx=727"
34 ).to_return(status: 200, body: body)
35
36 prefix = LocalCallingGuideRepo.new.find("917", "727").sync
37 assert_equal(40.739362, prefix.lat)
38 assert_equal(-73.991043, prefix.lon)
39 end
40 em :test_find_returns_lat_lon
41
42 def test_prefix_to_h
43 body = Nokogiri::XML::Builder.new { |xml|
44 xml.root do
45 xml.prefixdata do
46 xml.rc "NEW YORK"
47 xml.region "NY"
48 xml.send(:"rc-lat", "40.739362")
49 xml.send(:"rc-lon", "-73.991043")
50 end
51 end
52 }.to_xml
53
54 stub_request(
55 :get,
56 "https://localcallingguide.com/xmlprefix.php?npa=917&nxx=727"
57 ).to_return(status: 200, body: body)
58
59 prefix = LocalCallingGuideRepo.new.find("917", "727").sync
60 assert_equal(
61 { "region" => "NY", "locality" => "NEW YORK" },
62 prefix.to_h
63 )
64 end
65 em :test_prefix_to_h
66
67 def test_find_raises_on_missing_data
68 # Applying Style/SymbolProc causes Nokogiri
69 # to raise with "Cannot creating bind from C-level Proc"
70 body = Nokogiri::XML::Builder.new { |xml| # rubocop:disable Style/SymbolProc
71 xml.root
72 }.to_xml
73
74 stub_request(
75 :get,
76 "https://localcallingguide.com/xmlprefix.php?npa=000&nxx=000"
77 ).to_return(status: 200, body: body)
78
79 assert_raises(RuntimeError) do
80 LocalCallingGuideRepo.new.find("000", "000").sync
81 end
82 end
83 em :test_find_raises_on_missing_data
84end