Skip to content

Commit f9d3fbc

Browse files
oschwaldclaude
andcommitted
Add tests for absent anonymizer and residential data
horgh's review of the anonymizer.residential addition flagged two untested paths: - Insights#initialize unconditionally calls Anonymizer.new(record['anonymizer']), and Anonymizer#initialize unconditionally calls AnonymizerFeed.new(record['residential']). The common case has neither key present, so Anonymizer.new(nil) and AnonymizerFeed.new(nil) are the hot paths, guarded only by `record.nil? ? nil : record['residential']`. Add coverage for a Model::Insights built from a raw hash with no "anonymizer" key at all, and for an "anonymizer" present without a nested "residential" key, asserting every getter returns nil with no NoMethodError. - network_last_seen's `if !date_string` guard, which prevents Date.parse(nil) from raising when the date key is absent, was untested in all three places it is duplicated: AnonymizerFeed, Anonymizer, and Model::AnonymousPlus. Add a test per class asserting network_last_seen is nil when the date key is absent. Verified the AnonymizerFeed test actually catches a regression: with the guard temporarily removed (network_last_seen calling Date.parse(date_string) unconditionally), 3 of the 5 new tests failed with "TypeError: no implicit conversion of nil into String" as expected. Restored the guard afterward. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 39e1d6f commit f9d3fbc

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

test/test_record.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# frozen_string_literal: true
2+
3+
require 'date'
4+
require 'maxmind/geoip2'
5+
require 'minitest/autorun'
6+
7+
class RecordTest < Minitest::Test
8+
# This covers the common case where a web service response has no
9+
# "anonymizer" key at all. Insights#initialize always calls
10+
# Anonymizer.new(record['anonymizer']), so this exercises
11+
# Anonymizer.new(nil) and, in turn, AnonymizerFeed.new(nil).
12+
def test_insights_no_anonymizer
13+
model = MaxMind::GeoIP2::Model::Insights.new({}, ['en'])
14+
15+
assert_nil(model.anonymizer.confidence)
16+
refute(model.anonymizer.anonymous?)
17+
refute(model.anonymizer.anonymous_vpn?)
18+
refute(model.anonymizer.hosting_provider?)
19+
refute(model.anonymizer.public_proxy?)
20+
refute(model.anonymizer.residential_proxy?)
21+
refute(model.anonymizer.tor_exit_node?)
22+
assert_nil(model.anonymizer.network_last_seen)
23+
assert_nil(model.anonymizer.provider_name)
24+
25+
assert_nil(model.anonymizer.residential.confidence)
26+
assert_nil(model.anonymizer.residential.network_last_seen)
27+
assert_nil(model.anonymizer.residential.provider_name)
28+
end
29+
30+
# This covers an "anonymizer" object being present without a nested
31+
# "residential" key, which is the common case when the network is not
32+
# part of the residential proxy feed.
33+
def test_insights_anonymizer_without_residential
34+
model = MaxMind::GeoIP2::Model::Insights.new(
35+
{
36+
'anonymizer' => {
37+
'confidence' => 85,
38+
},
39+
},
40+
['en'],
41+
)
42+
43+
assert_equal(85, model.anonymizer.confidence)
44+
45+
assert_nil(model.anonymizer.residential.confidence)
46+
assert_nil(model.anonymizer.residential.network_last_seen)
47+
assert_nil(model.anonymizer.residential.provider_name)
48+
end
49+
50+
# network_last_seen is parsed lazily and guards against a missing date
51+
# string. This asserts the guard is in place rather than an unconditional
52+
# Date.parse(nil), which would raise.
53+
def test_anonymizer_feed_network_last_seen_absent
54+
feed = MaxMind::GeoIP2::Record::AnonymizerFeed.new(
55+
{ 'confidence' => 82 },
56+
)
57+
58+
assert_nil(feed.network_last_seen)
59+
end
60+
61+
def test_anonymizer_network_last_seen_absent
62+
anonymizer = MaxMind::GeoIP2::Record::Anonymizer.new(
63+
{ 'confidence' => 85 },
64+
)
65+
66+
assert_nil(anonymizer.network_last_seen)
67+
end
68+
69+
def test_anonymous_plus_network_last_seen_absent
70+
model = MaxMind::GeoIP2::Model::AnonymousPlus.new(
71+
{
72+
'ip_address' => '1.2.3.4',
73+
'prefix_length' => 24,
74+
},
75+
)
76+
77+
assert_nil(model.network_last_seen)
78+
end
79+
end

0 commit comments

Comments
 (0)