Skip to content

Commit ad286fd

Browse files
committed
Address PR review findings
- Fix COLLATE NOCASE placement on both indexes (apply to abbr/name columns, not country_id) - Add nil guard for country_id in StateTable#write - Add nil guard for state_id in ZipcodeTable#write - Strengthen fallback tests to verify correct state ID returned - Add test for nil country input to get_state_id - Add test verifying row mutation for downstream Kiba destinations
1 parent 880b051 commit ad286fd

4 files changed

Lines changed: 22 additions & 6 deletions

File tree

lib/free_zipcode_data/state_table.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ def build
1717

1818
ndx = <<-SQL
1919
CREATE UNIQUE INDEX "main"."unique_state"
20-
ON #{tablename} (abbr, country_id COLLATE NOCASE ASC);
20+
ON #{tablename} (abbr COLLATE NOCASE ASC, country_id);
2121
SQL
2222
database.execute_batch(ndx)
2323

2424
ndx = <<-SQL
2525
CREATE UNIQUE INDEX "main"."state_name"
26-
ON #{tablename} (name, country_id COLLATE NOCASE ASC);
26+
ON #{tablename} (name COLLATE NOCASE ASC, country_id);
2727
SQL
2828
database.execute_batch(ndx)
2929
end
@@ -33,6 +33,8 @@ def write(row)
3333

3434
row[:state] = 'Marshall Islands' if row[:short_state] == 'MH' && row[:state].nil?
3535
country_id = get_country_id(row[:country])
36+
return nil unless country_id
37+
3638
sql = <<-SQL
3739
INSERT INTO states (abbr, name, country_id)
3840
VALUES ('#{row[:short_state]}',

lib/free_zipcode_data/zipcode_table.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def write(row)
3030
return nil unless row[:postal_code]
3131

3232
state_id = get_state_id(row[:country], row[:short_state], row[:state])
33+
return nil unless state_id
34+
3335
city_name = escape_single_quotes(row[:city])
3436

3537
sql = <<-SQL

spec/free_zipcode_data/db_table_spec.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,33 @@ def build; end
9090

9191
describe 'private #get_state_id' do
9292
it 'finds a state by exact match (abbr + name + country)' do
93+
expected_id = db.execute("SELECT id FROM states WHERE abbr = 'NY'")[0][0]
9394
id = table.send(:get_state_id, 'US', 'NY', 'New York')
94-
expect(id).to be_a(Integer)
95+
expect(id).to eq(expected_id)
9596
end
9697

9798
it 'falls back to abbr + country when name does not match' do
99+
expected_id = db.execute("SELECT id FROM states WHERE abbr = 'NY'")[0][0]
98100
id = table.send(:get_state_id, 'US', 'NY', 'Wrong Name')
99-
expect(id).to be_a(Integer)
101+
expect(id).to eq(expected_id)
100102
end
101103

102104
it 'falls back to name + country when abbr does not match' do
105+
expected_id = db.execute("SELECT id FROM states WHERE name = 'New York'")[0][0]
103106
id = table.send(:get_state_id, 'US', 'XX', 'New York')
104-
expect(id).to be_a(Integer)
107+
expect(id).to eq(expected_id)
105108
end
106109

107110
it 'returns nil for an unknown state' do
108111
id = table.send(:get_state_id, 'US', 'ZZ', 'Nonexistent')
109112
expect(id).to be_nil
110113
end
111114

115+
it 'returns nil when country is nil' do
116+
id = table.send(:get_state_id, nil, 'NY', 'New York')
117+
expect(id).to be_nil
118+
end
119+
112120
it 'scopes lookup by country' do
113121
# Seed a Canadian state with the same abbr as a US state
114122
ca_state_table = FreeZipcodeData::StateTable.new(database: db, tablename: 'states')

spec/free_zipcode_data/state_table_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@
5151
end
5252

5353
it 'creates a state from country lookup when short_state is nil' do
54-
table.write({ country: 'US', short_state: nil, state: 'Unknown' })
54+
row = { country: 'US', short_state: nil, state: 'Unknown' }
55+
table.write(row)
5556
rows = db.execute("SELECT abbr, name FROM states WHERE abbr = 'US'")
5657
expect(rows.length).to eq(1)
5758
expect(rows[0]).to eq(['US', 'United States of America'])
59+
# Verify row mutation for downstream Kiba destinations
60+
expect(row[:short_state]).to eq('US')
61+
expect(row[:state]).to eq('United States of America')
5862
end
5963

6064
it 'creates a state from country lookup when short_state is empty' do

0 commit comments

Comments
 (0)