Skip to content
Permalink
Browse files

MINITEST! And some refactoring...

  • Loading branch information...
fteem committed Jul 11, 2015
1 parent e6dca33 commit 39264e419dc932f4562d622a293d920634218af6
@@ -1 +1,9 @@
require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new do |t|
t.libs.push 'test'
t.pattern = 'test/**/*_test.rb'
end

task default: :test
@@ -20,6 +20,8 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "minitest"
spec.add_development_dependency "m"
spec.add_development_dependency "minitest-reporters"
spec.add_development_dependency "webmock"
end
@@ -1,7 +1,8 @@
require "forecastr/version"
require 'forecastr/radar'
require 'forecastr/forecast'
require 'forecastr/wind'
require 'forecastr/temperature'
require 'forecastr/data_container'
require 'forecastr/client'
require "forecastr/radar"
require "forecastr/forecast"
require "forecastr/wind"
require "forecastr/temperature"
require "forecastr/data_container"
require "forecastr/client"
require "forecastr/temperature_converter"
@@ -6,11 +6,11 @@ def initialize(kelvin)
end

def to_celsius
(@kelvin - 273.15).round(2)
TemperatureConverter.to_celsius(@kelvin)
end

def to_farenheit
(1.8 * (@kelvin - 273.15) + 32).round(2)
TemperatureConverter.to_farenheit(@kelvin)
end

def to_s
@@ -0,0 +1,13 @@
module Forecastr
class TemperatureConverter
class << self
def to_celsius kelvins
(kelvins - 273.15).round(2)
end

def to_farenheit kelvins
(1.8 * (kelvins - 273.15) + 32).round(2)
end
end
end
end
@@ -2,8 +2,6 @@ module Forecastr
class Wind
DIRECTIONS = ["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]

This comment has been minimized.

Copy link
@zenspider

zenspider Jul 14, 2015

DIRECTIONS = %w[N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW]

attr_reader :speed, :direction

def initialize(speed, angle)
@speed = speed
@angle = angle
@@ -0,0 +1 @@
{"coord":{"lon":-0.13,"lat":51.51},"sys":{"message":0.0846,"country":"GB","sunrise":1395640357,"sunset":1395685239},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"base":"cmc stations","main":{"temp":279.64,"humidity":55,"pressure":1003,"temp_min":278.71,"temp_max":280.37},"wind":{"speed":2.06,"gust":3.6,"deg":350},"clouds":{"all":92},"dt":1395699452,"id":2643743,"name":"London","cod":200}
@@ -0,0 +1 @@
{"coord":{"lon":-0.13,"lat":51.51},"sys":{"message":0.047,"country":"GB","sunrise":1395381569,"sunset":1395425736},"weather":[{"id":520,"main":"Rain","description":"light intensity shower rain","icon":"09n"}],"base":"cmc stations","main":{"temp":280.79,"pressure":1001,"humidity":70,"temp_min":279.26,"temp_max":282.15},"wind":{"speed":5.1,"deg":180,"var_beg":150,"var_end":210},"rain":{"1h":5.08},"clouds":{"all":92},"dt":1395441145,"id":2643743,"name":"London","cod":200}
@@ -0,0 +1 @@
{"coord":{"lon":21.43,"lat":42},"sys":{"message":0.0427,"country":"MK","sunrise":1395376499,"sunset":1395420461},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01n"}],"base":"cmc stations","main":{"temp":283.85,"pressure":1021,"humidity":70,"temp_min":280.15,"temp_max":285.93},"wind":{"speed":1.16,"deg":128.501},"clouds":{"all":0},"dt":1395441000,"id":863860,"name":"Opština Karpoš","cod":200}
@@ -0,0 +1,18 @@
require 'test_helper'

class Forecastr::ClientTest < Minitest::Test
def test_fetching_city_data_from_api
stub_get("http://api.openweathermap.org/data/2.5/weather?q=London,UK").to_return(:body => fixture('london.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
results = Forecastr::Client.search_by_city('London,UK')

assert_equal results.city, "London"
end

def test_fetching_coordinates_data_from_api
stub_get("http://api.openweathermap.org/data/2.5/weather?lat=42.0&lon=21.4333").to_return(:body => fixture('skopje.json'), :headers => {:content_type => 'application/json; charset=utf-8'})
results = Forecastr::Client.search_by_coordinates(42.0, 21.4333)

This comment has been minimized.

Copy link
@zenspider

zenspider Jul 14, 2015

Because of float imprecision, you MAY want to make these strings.

ETA: because you're schlepping it off to another service (and this isn't money), it probably doesn't matter.


assert_equal results.city, "Opština Karpoš"
end

end
@@ -0,0 +1,25 @@
require 'test_helper'

class Forecastr::DataContainerTest < Minitest::Test

def setup
json = JSON.parse(fixture('skopje.json').read)
@container = Forecastr::DataContainer.new(json)
end

def test_mapping_from_json
assert_equal @container.city, "Opština Karpoš"
assert_equal @container.longitude, 21.43
assert_equal @container.latitude, 42
assert_equal @container.temperature, 283.85
assert_equal @container.pressure, 1021
assert_equal @container.humidity, 70
assert_equal @container.min_temperature, 280.15
assert_equal @container.max_temperature, 285.93
assert_equal @container.clouds, 0
assert_equal @container.wind_speed, 1.16
assert_equal @container.wind_angle, 128.501
assert_equal @container.sunrise, '1395376499'
assert_equal @container.sunset, '1395420461'
end
end
@@ -0,0 +1,58 @@
require 'test_helper'

class Forecastr::ForecastTest < Minitest::Test
def setup
json = JSON.parse(fixture('london.json').read)
@data = Forecastr::DataContainer.new(json)
@forecast = Forecastr::Forecast.new(@data)
end

def test_it_has_a_city
assert_equal @forecast.city, @data.city
end

def test_it_has_time_of_sunrise
assert_equal @forecast.sunrise, DateTime.strptime(@data.sunrise, '%s')
end

def test_time_of_sunset
assert_equal @forecast.sunset, DateTime.strptime(@data.sunset, '%s')

This comment has been minimized.

Copy link
@zenspider

zenspider Jul 14, 2015

assert_equal(expected, actual)

end

def test_longitude
assert_equal @forecast.longitude, @data.longitude
end

def test_latitude
assert_equal @forecast.latitude, @data.latitude
end

def test_current_temperature
assert_equal @forecast.temperature.to_celsius, Forecastr::TemperatureConverter.to_celsius(@data.temperature)
end

def test_minimal_temperature
assert_equal @forecast.min_temperature.to_celsius, Forecastr::TemperatureConverter.to_celsius(@data.min_temperature)
end

def test_maximal_temperature
assert_equal @forecast.max_temperature.to_celsius, Forecastr::TemperatureConverter.to_celsius(@data.max_temperature)
end

def test_pressure
assert_equal @forecast.pressure, 1001

This comment has been minimized.

Copy link
@zenspider

zenspider Jul 14, 2015

ditto on all of these

end

def test_humidity
assert_equal @forecast.humidity, 70
end

def test_cloud_coverage
assert_equal @forecast.clouds, 92
end

def test_wind
assert_respond_to @forecast, :wind
assert_kind_of Forecastr::Wind, @forecast.wind
end
end
@@ -0,0 +1,31 @@
require 'test_helper'

class Forecastr::RadarTest < Minitest::Test

def setup
@radar = Forecastr::Radar.new
end

def test_find_forecast_for_city
stub_get("http://api.openweathermap.org/data/2.5/weather?q=London,UK").to_return(:body => fixture('london.json'), :headers => {:content_type => 'application/json; charset=utf-8'})

forecast = @radar.find_by_city("London,UK")

assert_equal forecast.city, "London"
assert_equal forecast.temperature.to_celsius, 7.64
assert_equal forecast.humidity, 70
assert_equal forecast.pressure, 1001
end


def test_find_forecast_by_coordinates
stub_get("http://api.openweathermap.org/data/2.5/weather?lat=42.0&lon=21.4333").to_return(:body => fixture('skopje.json'), :headers => {:content_type => 'application/json; charset=utf-8'})

forecast = @radar.find_by_coordinates(42.000, 21.4333)

assert_equal forecast.city, "Opština Karpoš"
assert_equal forecast.temperature.to_celsius, 10.7
assert_equal forecast.humidity, 70
assert_equal forecast.pressure, 1021
end
end
@@ -0,0 +1,12 @@
require 'test_helper'

class Forecastr::TemperatureConverterTest < Minitest::Test

def test_celsius_conversion
assert_equal Forecastr::TemperatureConverter.to_celsius(273.15), 0
end

def test_farenheit_conversion
assert_equal Forecastr::TemperatureConverter.to_farenheit(278), 40.73
end
end
@@ -0,0 +1,16 @@
require 'test_helper'

class Forecastr::TemperatureTest < Minitest::Test

def setup
@temperature = Forecastr::Temperature.new(306.11)
end

def test_to_celsius
assert_equal @temperature.to_celsius, 32.96

This comment has been minimized.

Copy link
@zenspider

zenspider Jul 14, 2015

Don't ever use assert_equal with floats. Use assert_in_delta or assert_in_epsilon (auto-scaling delta).

end

def test_to_farenheit
assert_equal @temperature.to_farenheit, 91.33
end
end
@@ -0,0 +1,16 @@
require 'test_helper'

class Forecastr::WindTest < Minitest::Test

def setup
@wind = Forecastr::Wind.new(2.5, -37)
end

def test_speed_in_ms
assert_equal @wind.speed, "2.5 m/s"
end

def test_direction
assert_equal @wind.direction, "NNW"
end
end
@@ -0,0 +1,22 @@
require 'forecastr'
require 'minitest/autorun'
require 'minitest/unit'
require "minitest/reporters"
require 'webmock'

include WebMock::API

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

def fixture_path
File.expand_path('../fixtures', __FILE__)

This comment has been minimized.

Copy link
@zenspider

zenspider Jul 14, 2015

If you run tests via rake, there's no reason to expand_path. You'll always be in the root directory.

end

def fixture(file)
File.new(fixture_path + '/' + file)

This comment has been minimized.

Copy link
@zenspider

zenspider Jul 14, 2015

Use string interpolation or, better, File.join.

end

def stub_get(path)
stub_request(:get, path)
end

3 comments on commit 39264e4

@fteem

This comment has been minimized.

Copy link
Owner Author

replied Jul 14, 2015

I blogged about this migration @ https://ieftimov.com/migrate-rspec-to-minitest

@zenspider

This comment has been minimized.

Copy link

replied Jul 14, 2015

Couple simple mistakes, but looks good!

@fteem

This comment has been minimized.

Copy link
Owner Author

replied Jul 14, 2015

@zenspider thanks so much! :-)

Please sign in to comment.
You can’t perform that action at this time.