Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ledermann committed Dec 17, 2023
1 parent d9c9914 commit b0a43e0
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 82 deletions.
39 changes: 39 additions & 0 deletions test/cassettes/forecast_setup.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions test/cassettes/forecast_success.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 3 additions & 39 deletions test/cassettes/loop_success.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/cassettes/prices_blank.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions test/cassettes/prices_setup.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions test/cassettes/prices_success.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions test/cassettes/senec_success.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 59 additions & 3 deletions test/forecast_provider_test.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,71 @@
require 'test_helper'

class ForecastProviderTest < Minitest::Test
def setup
VCR.use_cassette('forecast_setup') { fill_forecast }
end

TIME = Time.parse('2023-12-01 10:30:00 +0100').freeze

def test_success_request
VCR.use_cassette('forecast_success') do
refute_predicate forecast_provider, :sunshine_ahead?
Timecop.travel(TIME) do
VCR.use_cassette('forecast_success') do
assert_predicate forecast_provider, :sunshine_ahead?
end
end
end

private

def forecast_provider
@forecast_provider ||= ForecastProvider.new(config: Config.from_env)
@forecast_provider ||= ForecastProvider.new(config:)
end

def config
@config ||= Config.from_env
end

def fake_forecast
# Forecast from 10:00 to 13:00 for TIME
[
{ time: '2023-12-01 10:00:00 +0100', watt: 5500 },
{ time: '2023-12-01 11:00:00 +0100', watt: 9600 },
{ time: '2023-12-01 12:00:00 +0100', watt: 3700 },
{ time: '2023-12-01 13:00:00 +0100', watt: 8800 },
]
end

def forecast_points
fake_forecast.map do |forecast|
InfluxDB2::Point.new(
name: config.influx_measurement_forecast,
time: Time.parse(forecast[:time]).to_i,
fields: {
watt: forecast[:watt],
},
)
end
end

def fill_forecast
write_api.write(
data: forecast_points,
bucket: config.influx_bucket,
org: config.influx_org,
)
end

def write_api
@write_api ||= influx_client.create_write_api
end

def influx_client
@influx_client ||=
InfluxDB2::Client.new(
config.influx_url,
config.influx_token,
use_ssl: config.influx_schema == 'https',
precision: InfluxDB2::WritePrecision::SECOND,
)
end
end
Loading

0 comments on commit b0a43e0

Please sign in to comment.