From fc83f046e5707c0e276b65ebd9ab245fd6b43e29 Mon Sep 17 00:00:00 2001 From: Laurent Farcy Date: Mon, 20 Jul 2015 12:41:14 +0200 Subject: [PATCH] Add a unit test for rules management o externalize the PowerTrack configuration in a test/powertrack.yml file loaded when required, on demand --- .gitignore | 2 ++ lib/powertrack/streaming/stream.rb | 3 ++- test/minitest_helper.rb | 33 +++++++++++++++++++++++++++--- test/test_stream_add_rules.rb | 28 ++++++++++++++++--------- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 263c9e5..20da68d 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,5 @@ build/ # ignore Eclipse config .project +# ignore tests-related config file +test/powertrack.yml diff --git a/lib/powertrack/streaming/stream.rb b/lib/powertrack/streaming/stream.rb index eec75a4..53f17e6 100644 --- a/lib/powertrack/streaming/stream.rb +++ b/lib/powertrack/streaming/stream.rb @@ -59,7 +59,8 @@ def delete_rules(*rules) # See http://support.gnip.com/apis/powertrack/api_reference.html#ListRules def list_rules(compressed=true) headers = compressed ? { 'accept-encoding' => 'gzip, compressed' } : {} - make_rules_request(:get, headers: headers) + res = make_rules_request(:get, headers: headers) + res.is_a?(Hash) && res.key?('rules') ? res['rules'] : res end # Establishes a persistent connection to the PowerTrack data stream, diff --git a/test/minitest_helper.rb b/test/minitest_helper.rb index 3883ad6..87664de 100644 --- a/test/minitest_helper.rb +++ b/test/minitest_helper.rb @@ -1,4 +1,31 @@ -$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) - -gem "minitest" +gem 'minitest' require 'minitest/autorun' +require 'yaml' + +class Minitest::Test + + POWERTRACK_CONFIG_FILEPATH = File.join(File.dirname(__FILE__), "powertrack.yml") + + # Returns the PowerTrack configuration as defined in test/powertrack.yml. + def powertrack_config + unless defined?(@loaded) && @loaded + begin + if File.exist?(POWERTRACK_CONFIG_FILEPATH) + @pwtk_config = (YAML.load_file(POWERTRACK_CONFIG_FILEPATH) || {}) + else + $stderr.puts "No PowerTrack config file found at '#{POWERTRACK_CONFIG_FILEPATH}'" + end + rescue Exception + $stderr.puts "Exception while loading PowerTrack config file: #{$!.message}" + ensure + @pwtk_config ||= {} + end + + # symbolize keys + @pwtk_config = Hash[@pwtk_config.map{ |k, v| [k.to_sym, v] }] + @loaded = true + end + + @pwtk_config + end +end diff --git a/test/test_stream_add_rules.rb b/test/test_stream_add_rules.rb index 2fe9058..befa69d 100644 --- a/test/test_stream_add_rules.rb +++ b/test/test_stream_add_rules.rb @@ -6,17 +6,25 @@ class TestRule < Minitest::Test def test_add_a_single_rule stream = PowerTrack::Stream.new( - 'laurent.farcy@ecairn.com', - 'piodv-717', - 'eCairn', - 'twitter', - 'prod') + powertrack_config[:username], + powertrack_config[:password], + powertrack_config[:account_name], + powertrack_config[:data_source], + powertrack_config[:stream_label]) rule = PowerTrack::Rule.new('coke') - p stream.list_rules - p stream.add_rule(rule) - p stream.list_rules(false) - p stream.delete_rules(rule) - p stream.list_rules + assert rule.valid? + pre_existing_rules = stream.list_rules + assert pre_existing_rules.is_a?(Array) + assert_nil stream.add_rule(rule) + rules_after_addition = stream.list_rules(false) + assert rules_after_addition.is_a?(Array) + assert_equal pre_existing_rules.size + 1, rules_after_addition.size + assert [ rule ], rules_after_addition - pre_existing_rules + assert_nil stream.delete_rules(rule) + rules_after_removal = stream.list_rules + assert rules_after_removal.is_a?(Array) + assert_equal rules_after_addition.size - 1, rules_after_removal.size + assert_equal [], rules_after_removal - rules_after_addition end end