require File.dirname(__FILE__) + '/test_helper.rb' class TestRedfinApiBase < Test::Unit::TestCase context "with property page containing parcel-id-number" do setup do @url = "http://www.redfin.com/IL/Roselle/999-Main-St-60606/home/99999999" RedfinApi::Base.stubs(:http_get).with(@url).returns(File.read(File.dirname(__FILE__) + "/data/property_page.html")) end should "extract PIN information" do address = RedfinApi::Base.fetch_address(@url) assert_equal("07343000300000", address[:pin]) end end should "fetch address from redfin url" do url = "http://www.redfin.com/IL/Roselle/999-Main-St-60606/home/99999999" RedfinApi::Base.stubs(:http_get).with(url).returns(File.read(File.dirname(__FILE__) + "/data/property_page.html")) address = RedfinApi::Base.fetch_address(url) assert_equal("640 WASHINGTON Ct", address[:address_line_1]) assert_equal("ROSELLE", address[:city]) assert_equal("IL", address[:state]) assert_equal("60172", address[:zip]) end context "with query location returning pastSalesBuildings" do setup do @address = "123 Main ST City ST" # address that is not perfect (query should still find it) query_location_url = "http://www.redfin.com/stingray/do/query-location?location=#{URI.encode(@address)}" RedfinApi::Base.stubs(:http_get).with(query_location_url).returns(File.read(File.dirname(__FILE__) + "/data/query_location_pastSalesBuildings.json")) end should "return query location by address string" do location = RedfinApi::Base.query_location(@address) assert_equal("41.777641", location[:lat]) assert_equal("-87.98056", location[:lon]) assert_equal("225 Carlisle Ave", location[:address_line_1]) assert_equal("Westmont", location[:city]) assert_equal("IL", location[:state]) assert_equal("60559", location[:zip]) assert_equal("18016252", location[:property_id]) assert_equal("http://www.redfin.com/IL/Westmont/225-Carlisle-Ave-60559/home/18016252", location[:url]) end end context "with query location returning listings" do setup do @address = "123 Main ST City ST" # address that is not perfect (query should still find it) query_location_url = "http://www.redfin.com/stingray/do/query-location?location=#{URI.encode(@address)}" RedfinApi::Base.stubs(:http_get).with(query_location_url).returns(File.read(File.dirname(__FILE__) + "/data/query_location_listings.json")) listing_id = "3467651" gis_url ="http://www.redfin.com/stingray/do/gis-id-search?listing_id=#{listing_id}" RedfinApi::Base.stubs(:http_get).with(gis_url).returns(File.read(File.dirname(__FILE__) + "/data/gis_id_search.json")) end should "return query location by address string" do address = "123 Main ST City ST" # address that is not perfect (query should still find it) location = RedfinApi::Base.query_location(address) assert_equal("42.021069", location[:lat]) assert_equal("-88.115638", location[:lon]) assert_equal("1420", location[:number]) assert_equal("Arlington", location[:street_name]) assert_equal("Ln", location[:street_type]) assert_equal("A", location[:unit]) assert_equal("1420 Arlington Ln", location[:address_line_1]) assert_equal("Schaumburg", location[:city]) assert_equal("IL", location[:state]) assert_equal("60193", location[:zip]) assert_equal("3467651", location[:listing_id]) # the 'id' returned with "listings" is not the correct redfin_id assert_equal("21620838", location[:property_id]) assert_equal("http://www.redfin.com/IL/Schaumburg/1930-Keystone-Pl-60193/unit-A/home/21620838", location[:url]) end end context "with gis_id_search returning json" do setup do RedfinApi::Base.stubs(:http_get).returns(File.read(File.dirname(__FILE__) + "/data/gis_id_search.json")) end should "return query location by address string" do listing_id = "12345" resp = RedfinApi::Base.gis_id_search(listing_id) assert_equal("21620838", resp[:property_id]) assert_equal("http://www.redfin.com/IL/Schaumburg/1930-Keystone-Pl-60193/unit-A/home/21620838", resp[:url]) end end context "with location hash containing address information" do setup do @location = { :state => "IL", :city => "Hoffman Estates", :number => "123", :street_name => "Popular Creek", :street_type => "Dr", :zip => "12345", :property_id => "1239812" } end should "generate proper redfin url by replacing spaces with dashes in address components" do assert_equal("http://www.redfin.com/IL/Hoffman-Estates/123-Popular-Creek-Dr-12345/home/1239812", RedfinApi::Base.construct_redfin_url_from_address(@location)) end end end