|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'kiba' |
| 4 | +require 'etl/free_zipcode_data_job' |
| 5 | + |
| 6 | +RSpec.describe ETL::FreeZipcodeDataJob do |
| 7 | + let(:db) { create_test_database(line_count: 5) } |
| 8 | + let(:fixture_csv) { File.join(FreeZipcodeData.root, 'spec', 'fixtures', 'test_data.csv') } |
| 9 | + let(:logger) { FreeZipcodeData::Logger.instance } |
| 10 | + let(:string_io) { StringIO.new } |
| 11 | + let(:options) do |
| 12 | + OpenStruct.new( |
| 13 | + country_tablename: 'countries', |
| 14 | + state_tablename: 'states', |
| 15 | + county_tablename: 'counties', |
| 16 | + zipcode_tablename: 'zipcodes', |
| 17 | + verbose: false |
| 18 | + ) |
| 19 | + end |
| 20 | + |
| 21 | + before do |
| 22 | + FreeZipcodeData::Options.instance.initialize_hash(options) |
| 23 | + logger.log_provider = ::Logger.new(string_io) |
| 24 | + end |
| 25 | + |
| 26 | + describe '.setup' do |
| 27 | + it 'returns a Kiba job definition' do |
| 28 | + job = described_class.setup(fixture_csv, db, logger, options) |
| 29 | + expect(job).not_to be_nil |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + describe 'full ETL pipeline' do |
| 34 | + before do |
| 35 | + # Build all tables |
| 36 | + FreeZipcodeData::CountryTable.new(database: db, tablename: 'countries').build |
| 37 | + FreeZipcodeData::StateTable.new(database: db, tablename: 'states').build |
| 38 | + FreeZipcodeData::CountyTable.new(database: db, tablename: 'counties').build |
| 39 | + FreeZipcodeData::ZipcodeTable.new(database: db, tablename: 'zipcodes').build |
| 40 | + |
| 41 | + job = described_class.setup(fixture_csv, db, logger, options) |
| 42 | + Kiba.run(job) |
| 43 | + end |
| 44 | + |
| 45 | + it 'populates the countries table' do |
| 46 | + rows = db.execute('SELECT alpha2 FROM countries ORDER BY alpha2') |
| 47 | + expect(rows.flatten).to include('CA', 'GB', 'US') |
| 48 | + end |
| 49 | + |
| 50 | + it 'populates the states table' do |
| 51 | + rows = db.execute('SELECT abbr FROM states ORDER BY abbr') |
| 52 | + abbrs = rows.flatten |
| 53 | + expect(abbrs).to include('CA', 'IL', 'NY') |
| 54 | + end |
| 55 | + |
| 56 | + it 'populates the counties table' do |
| 57 | + rows = db.execute('SELECT name FROM counties ORDER BY name') |
| 58 | + names = rows.flatten |
| 59 | + expect(names).to include('Cook', 'Los Angeles', 'New York') |
| 60 | + end |
| 61 | + |
| 62 | + it 'populates the zipcodes table' do |
| 63 | + rows = db.execute('SELECT code FROM zipcodes ORDER BY code') |
| 64 | + codes = rows.flatten |
| 65 | + expect(codes).to include('10001', '60601', '90210') |
| 66 | + end |
| 67 | + |
| 68 | + it 'links zipcodes to states' do |
| 69 | + rows = db.execute(<<-SQL) |
| 70 | + SELECT z.code, s.abbr |
| 71 | + FROM zipcodes z |
| 72 | + JOIN states s ON CAST(z.state_id AS INTEGER) = s.id |
| 73 | + WHERE z.code = '60601' |
| 74 | + SQL |
| 75 | + expect(rows[0]).to eq(['60601', 'IL']) |
| 76 | + end |
| 77 | + |
| 78 | + it 'links states to countries' do |
| 79 | + rows = db.execute(<<-SQL) |
| 80 | + SELECT s.abbr, c.alpha2 |
| 81 | + FROM states s |
| 82 | + JOIN countries c ON s.country_id = c.id |
| 83 | + WHERE s.abbr = 'NY' |
| 84 | + SQL |
| 85 | + expect(rows[0]).to eq(['NY', 'US']) |
| 86 | + end |
| 87 | + |
| 88 | + it 'stores geocode data for zipcodes' do |
| 89 | + rows = db.execute("SELECT lat, lon FROM zipcodes WHERE code = '10001'") |
| 90 | + lat = rows[0][0].to_f |
| 91 | + lon = rows[0][1].to_f |
| 92 | + expect(lat).to be_within(0.01).of(40.7484) |
| 93 | + expect(lon).to be_within(0.01).of(-73.9967) |
| 94 | + end |
| 95 | + end |
| 96 | +end |
0 commit comments