spec/weather_services/noaa_spec.rb in barometer-0.8.0 vs spec/weather_services/noaa_spec.rb in barometer-0.9.0

- old
+ new

@@ -1,179 +1,81 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -include Barometer +require_relative '../spec_helper' -describe Barometer::WeatherService::Noaa, :vcr => { - :cassette_name => "WeatherService::Noaa" +describe Barometer::WeatherService::Noaa, vcr: { + cassette_name: "WeatherService::Noaa" } do - before(:each) do - @accepted_formats = [:zipcode, :coordinates] + + it "auto-registers this weather service as :noaa" do + Barometer::WeatherService.source(:noaa).should == Barometer::WeatherService::Noaa end - describe "the class methods" do - it "defines accepted_formats" do - WeatherService::Noaa._accepted_formats.should == @accepted_formats - end + describe ".call" do + let(:query) { build_query.tap{|q|q.stub(add_conversion: nil)} } - it "defines source_name" do - WeatherService::Noaa._source_name.should == :noaa - end + subject { Barometer::WeatherService::Noaa.call(query) } - it "defines fetch_current" do - WeatherService::Noaa.respond_to?("_fetch_current").should be_true - end - - it "defines fetch_forecast" do - WeatherService::Noaa.respond_to?("_fetch_forecast").should be_true - end - - it "defines get_all" do - WeatherService::Noaa.respond_to?("_fetch").should be_true - end - - describe "acceptable countries" do - before(:each) do - @query = Barometer::Query.new("90210") - @measurement = Barometer::Measurement.new + before do + query.stub(:convert!).and_return do |*formats| + if formats.include?(:noaa_station_id) + Barometer::ConvertedQuery.new("KSMO", :station_id) + elsif formats.include?(:zipcode) + Barometer::ConvertedQuery.new("90210", :zipcode) + end end - - it "accepts nil" do - @query.country_code = nil - WeatherService::Noaa._supports_country?(@query).should be_true - end - - it "accepts blank" do - @query.country_code = "" - WeatherService::Noaa._supports_country?(@query).should be_true - end - - it "accepts US" do - @query.country_code = "US" - WeatherService::Noaa._supports_country?(@query).should be_true - end - - it "rejects other" do - @query.country_code = "CA" - WeatherService::Noaa._supports_country?(@query).should be_false - end end - end - describe "building the current data" do - it "defines the build method" do - WeatherService::Noaa.respond_to?("_build_current").should be_true + it "asks the query to convert to accepted formats" do + query.should_receive(:convert!).with(:zipcode, :coordinates) + subject end - it "requires Hash input" do - lambda { WeatherService::Noaa._build_current }.should raise_error(ArgumentError) - lambda { WeatherService::Noaa._build_current({}) }.should_not raise_error(ArgumentError) + it "adds a coordinate conversion to the query" do + query.should_receive(:add_conversion).with(:coordinates, '34.1,-118.41') + subject end - it "returns Barometer::CurrentMeasurement object" do - current = WeatherService::Noaa._build_current({}) - current.is_a?(Measurement::Result).should be_true - end - end + it "includes the expected data" do + subject.query.should == '90210' + subject.format.should == :zipcode + subject.should be_metric - describe "building the forecast data" do - it "defines the build method" do - WeatherService::Noaa.respond_to?("_build_forecast").should be_true - end + should have_data(:current, :observed_at).as_format(:time) + should have_data(:current, :stale_at).as_format(:time) - it "requires Hash input" do - lambda { WeatherService::Noaa._build_forecast }.should raise_error(ArgumentError) - lambda { WeatherService::Noaa._build_forecast({}) }.should_not raise_error(ArgumentError) - end + should have_data(:current, :humidity).as_format(:float) + should have_data(:current, :condition).as_format(:string) + should have_data(:current, :icon).as_format(:string) + should have_data(:current, :temperature).as_format(:temperature) + # should have_data(:current, :wind_chill).as_format(:temperature) + should have_data(:current, :dew_point).as_format(:temperature) + should have_data(:current, :wind).as_format(:vector) + should have_data(:current, :pressure).as_format(:pressure) + should have_data(:current, :visibility).as_format(:distance) - it "returns Array object" do - current = WeatherService::Noaa._build_forecast({}) - current.is_a?(Array).should be_true - end - end + should have_data(:location, :name).as_value("Santa Monica Muni, CA") + should have_data(:location, :city).as_value("Santa Monica Muni") + should have_data(:location, :state_code).as_value("CA") + should have_data(:location, :country_code).as_value("US") + should have_data(:location, :latitude).as_value(34.10) + should have_data(:location, :longitude).as_value(-118.41) - describe "when measuring" do - before(:each) do - @query = Barometer::Query.new("90210") - @measurement = Barometer::Measurement.new - end + should have_data(:station, :id).as_value("KSMO") + should have_data(:station, :name).as_value("Santa Monica Muni, CA") + should have_data(:station, :city).as_value("Santa Monica Muni") + should have_data(:station, :state_code).as_value("CA") + should have_data(:station, :country_code).as_value("US") + should have_data(:station, :latitude).as_value(34.10) + should have_data(:station, :longitude).as_value(-118.41) - describe "all" do - it "responds to _measure" do - Barometer::WeatherService::Noaa.respond_to?("_measure").should be_true - end + should have_data(:timezone, :to_s).as_format(/^P[DS]T$/i) - it "requires a Barometer::Measurement object" do - lambda { Barometer::WeatherService::Noaa._measure(nil, @query) }.should raise_error(ArgumentError) - lambda { Barometer::WeatherService::Noaa._measure("invalid", @query) }.should raise_error(ArgumentError) - - lambda { Barometer::WeatherService::Noaa._measure(@measurement, @query) }.should_not raise_error(ArgumentError) - end - - it "requires a Barometer::Query query" do - lambda { Barometer::WeatherService::Noaa._measure }.should raise_error(ArgumentError) - lambda { Barometer::WeatherService::Noaa._measure(@measurement, 1) }.should raise_error(ArgumentError) - - lambda { Barometer::WeatherService::Noaa._measure(@measurement, @query) }.should_not raise_error(ArgumentError) - end - - it "returns a Barometer::Measurement object" do - result = Barometer::WeatherService::Noaa._measure(@measurement, @query) - result.is_a?(Barometer::Measurement).should be_true - result.current.is_a?(Barometer::Measurement::Result).should be_true - result.forecast.is_a?(Barometer::Measurement::ResultArray).should be_true - end - end - end - - describe "overall data correctness" do - before(:each) do - @query = Barometer::Query.new("90210") - @measurement = Barometer::Measurement.new - end - - it "should correctly build the data" do - result = WeatherService::Noaa._measure(@measurement, @query) - - # build current - @measurement.current.humidity.to_s.should match(/^\d{0,2}$/i) - @measurement.current.condition.should match(/^[\w ]{2,}$/i) - @measurement.current.icon.to_s.should match(/^\w{1,4}$/i) - @measurement.current.temperature.to_s.should match(/^\d{1,3}[ ]?[cfCF]?$/i) - @measurement.current.dew_point.to_s.should match(/^\d{1,3}[ ]?[cfCF]?$/i) - @measurement.current.wind_chill.to_s.should match(/^\d{0,3}[ ]?[cfCF]?$/i) - @measurement.current.wind.to_s.should match(/^\d{1,3}[ ]?[a-zA-Z]{0,3}$/i) - @measurement.current.wind.direction.to_s.should match(/^[neswNESW][\w ]{3,}$/i) - @measurement.current.wind.degrees.to_s.should match(/^\d{1,3}$/i) - @measurement.current.pressure.to_s.should match(/^\d{1,4}[ ]?[a-zA-Z]{0,3}$/i) - - # build station - @measurement.station.id.should == "KSMO" - @measurement.station.name.should == "Santa Monica Muni, CA" - @measurement.station.city.should == "Santa Monica Muni" - @measurement.station.state_code.should == "CA" - @measurement.station.country_code.should == "US" - @measurement.station.latitude.to_f.should == 34.03 - @measurement.station.longitude.to_f.should == -118.45 - - # builds location - @measurement.location.city.should == "Santa Monica Muni" - @measurement.location.state_code.should == "CA" - @measurement.location.country_code.should == "US" - - # builds forecasts - @measurement.forecast.size.should == 7 - - @measurement.forecast[0].valid_start_date.to_s.should match(/^\d{2,4}-\d{1,2}-\d{1,2}/i) - @measurement.forecast[0].valid_end_date.to_s.should match(/^\d{2,4}-\d{1,2}-\d{1,2}/i) - @measurement.forecast[0].condition.should match(/^[\w ]+$/i) - @measurement.forecast[0].icon.should match(/^[\w ]+$/i) - @measurement.forecast[0].high.f.to_s.should match(/^\d{1,3}[ ]?[cfCF]?$/i) - @measurement.forecast[0].low.f.to_s.should match(/^\d{1,3}[ ]?[cfCF]?$/i) - - # builds local time - @measurement.measured_at.to_s.should match(/^\d{1,2}:\d{1,2}[ ]?[apmAPM]{0,2}$/i) - @measurement.current.current_at.to_s.should match(/^\d{1,2}:\d{1,2}[ ]?[apmAPM]{0,2}$/i) - - # builds timezone - @measurement.timezone.code.should match(/(PDT|PST)/i) + subject.forecast.size.should == 14 + should have_forecast(:starts_at).as_format(:time) + should have_forecast(:ends_at).as_format(:time) + should have_forecast(:icon).as_format(:string) + should have_forecast(:condition).as_format(:string) + should have_forecast(:pop).as_format(:float) + should have_forecast(:high).as_format(:temperature) + should have_forecast(:low).as_format(:temperature) end end end