Skip to content

Commit

Permalink
Add a unit test for rules management
Browse files Browse the repository at this point in the history
o externalize the PowerTrack configuration in a test/powertrack.yml file
  loaded when required, on demand
  • Loading branch information
Laurent Farcy committed Jul 20, 2015
1 parent 6db8982 commit fc83f04
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ build/
# ignore Eclipse config
.project

# ignore tests-related config file
test/powertrack.yml
3 changes: 2 additions & 1 deletion lib/powertrack/streaming/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 30 additions & 3 deletions test/minitest_helper.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 18 additions & 10 deletions test/test_stream_add_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fc83f04

Please sign in to comment.