1
0

weather_agent_spec.rb 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. require 'rails_helper'
  2. describe Agents::WeatherAgent do
  3. let(:agent) do
  4. Agents::WeatherAgent.create(
  5. name: 'weather',
  6. options: {
  7. :location => "37.77550,-122.41292",
  8. :api_key => 'test',
  9. :which_day => 1,
  10. }
  11. ).tap do |agent|
  12. agent.user = users(:bob)
  13. agent.save!
  14. end
  15. end
  16. let :pirate_weather_agent do
  17. Agents::WeatherAgent.create(
  18. name: "weather from Pirate Weather",
  19. options: {
  20. :location => "37.779329,-122.41915",
  21. :service => "pirateweather",
  22. :which_day => 1,
  23. :api_key => "test"
  24. }
  25. ).tap do |agent|
  26. agent.user = users(:bob)
  27. agent.save!
  28. end
  29. end
  30. it "creates a valid agent" do
  31. expect(agent).to be_valid
  32. end
  33. it "is valid with put-your-key-here or your-key" do
  34. agent.options['api_key'] = 'put-your-key-here'
  35. expect(agent).to be_valid
  36. expect(agent.working?).to be_falsey
  37. agent.options['api_key'] = 'your-key'
  38. expect(agent).to be_valid
  39. expect(agent.working?).to be_falsey
  40. end
  41. context "pirate weather" do
  42. it "validates the location properly" do
  43. expect(pirate_weather_agent.options["location"]).to eq "37.779329,-122.41915"
  44. expect(pirate_weather_agent).to be_valid
  45. pirate_weather_agent.options["location"] = "37.779329, -122.41915" # with a space
  46. expect(pirate_weather_agent).to be_valid
  47. pirate_weather_agent.options["location"] = "94103" # a zip code
  48. expect(pirate_weather_agent).to_not be_valid
  49. pirate_weather_agent.options["location"] = "37.779329,-122.41915"
  50. expect(pirate_weather_agent.options["location"]).to eq "37.779329,-122.41915"
  51. expect(pirate_weather_agent).to be_valid
  52. end
  53. it "fails cases that pass the first test but are invalid" do
  54. pirate_weather_agent.options["location"] = "137.779329, -122.41915" # too high latitude
  55. expect(pirate_weather_agent).to_not be_valid
  56. pirate_weather_agent.options["location"] = "37.779329, -522.41915" # too low longitude
  57. expect(pirate_weather_agent).to_not be_valid
  58. end
  59. end
  60. describe "#service" do
  61. it "doesn't have a Service object attached" do
  62. expect(agent.service).to be_nil
  63. end
  64. end
  65. describe "Agents::WeatherAgent::VALID_COORDS_REGEX" do
  66. it "matches 37.779329,-122.41915" do
  67. expect(
  68. "37.779329,-122.41915" =~ Agents::WeatherAgent::VALID_COORDS_REGEX
  69. ).to be_truthy
  70. end
  71. it "matches a dozen random valid values" do
  72. valid_longitude_range = -180.0..180.0
  73. valid_latitude_range = -90.0..90.0
  74. 12.times do
  75. expect(
  76. "#{rand valid_latitude_range},#{rand valid_longitude_range}" =~ Agents::WeatherAgent::VALID_COORDS_REGEX
  77. ).not_to be_nil
  78. end
  79. end
  80. end
  81. end