diff --git a/Gemfile b/Gemfile index c4da66a..9581350 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' group :test do - gem 'yajl-ruby', '~> 1.0', require: 'yajl' + gem 'yajl-ruby', '~> 1.3.1', require: 'yajl' end # Specify your gem's dependencies in powertrack-rb.gemspec diff --git a/Gemfile.lock b/Gemfile.lock index a931382..2c8c961 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - powertrack (1.3.1) + powertrack (2.0.0) em-http-request (~> 1.1) eventmachine (~> 1.0) exponential-backoff (~> 0.0.2) @@ -29,7 +29,7 @@ GEM rake (10.4.2) ruby-prof (0.15.8) void_logger (0.1) - yajl-ruby (1.2.1) + yajl-ruby (1.3.1) PLATFORMS ruby @@ -40,4 +40,7 @@ DEPENDENCIES powertrack! rake (~> 10.3) ruby-prof (~> 0.15) - yajl-ruby (~> 1.0) + yajl-ruby (~> 1.3.1) + +BUNDLED WITH + 1.10.6 diff --git a/History.txt b/History.txt index dfdfb84..75986da 100644 --- a/History.txt +++ b/History.txt @@ -1,4 +1,10 @@ -v1.3.0 +v2.0.0 +------ + + * Remove support for PowerTrack v1 since this version was sunset in early + January 2017 + +v1.3.1 ------ * Add the PowerTrack::Stream.v2? instance method diff --git a/README.md b/README.md index 6f49fc7..79d3585 100644 --- a/README.md +++ b/README.md @@ -128,16 +128,20 @@ replaying the same timeframe again and again when GNIP is unstable. All the errors that come from PowerTrack are defined through an ad-hoc exception class hierarchy. See ```lib/powertrack/errors.rb```. +## PowerTrack v1 + +The library was originally designed for PowerTrack v1. But this version of the +PowerTrack API was sunset in early January 2017. + +Consequently, since v2.0, the library does not support v1 anymore. + ## PowerTrack v2 -The library provides early support for PowerTrack API version 2. Please read +The library provides support for PowerTrack API version 2. Please read [PowerTrack API v2](http://support.gnip.com/apis/powertrack2.0/index.html) and the [Migration Guide](http://support.gnip.com/apis/powertrack2.0/transition.html) for details about this new major release. -Set the ```:v2```option to ```true``` when building a ```PowerTrack::Stream``` -object to enable this feature. The library uses v1 by default. - Everything should work the same for v2 as for v1 except o ```PowerTrack::Stream.add_rule``` and ```PowerTrack::Stream.delete_rule``` diff --git a/lib/powertrack/rules/rule.rb b/lib/powertrack/rules/rule.rb index aded089..1123d29 100644 --- a/lib/powertrack/rules/rule.rb +++ b/lib/powertrack/rules/rule.rb @@ -7,11 +7,8 @@ class Rule # The maximum length of a rule tag. MAX_TAG_LENGTH = 255 - # The maximum lengh of the value of a standard rule - MAX_STD_RULE_VALUE_LENGTH = 1024 - - # The maximum lengh of the value of a long rule - MAX_LONG_RULE_VALUE_LENGTH = 2048 + # The maximum lengh of the value of a rule + MAX_RULE_VALUE_LENGTH = 2048 # The maximum number of positive terms in a single rule value MAX_POSITIVE_TERMS = 30 @@ -20,58 +17,29 @@ class Rule MAX_NEGATIVE_TERMS = 50 # The maximum size of the HTTP body accepted by PowerTrack /rules calls (in bytes) - # 1 MB for v1, 5MB for v2 - MAX_RULES_BODY_SIZE = { - v1: 1024**2, - v2: 5*1024**2 - } + # 5MB since v2 + MAX_RULES_BODY_SIZE = 5*1024**2 # The default rule features DEFAULT_RULE_FEATURES = { # no id by default id: nil, # no tag by default - tag: nil, - # long determined by value length - long: nil, - # v1 by default - v2: false + tag: nil }.freeze attr_reader :value, :id, :tag, :error # Builds a new rule based on a value and some optional features - # (:id, :tag, :long, :v2). - # - # By default, the constructor assesses if it's a long rule or not - # based on the length of the value. But the 'long' feature can be - # explicitly specified with the :long feature. Finally, if :v2 is - # true the rule is always considered long. + # (:id, :tag). def initialize(value, features=nil) @value = value || '' features = DEFAULT_RULE_FEATURES.merge(features || {}) @tag = features[:tag] @id = features[:id] - # only accept boolean values - _v2 = features[:v2] - @v2 = (_v2 == !!_v2) ? _v2 : false - # check if long is a boolean - _long = features[:long] - # v2 rules are always long - @long = (@v2 ? true : (_long == !!_long ? _long : @value.size > MAX_STD_RULE_VALUE_LENGTH)) @error = nil end - # Returns true if the rule is long. - def long? - @long - end - - # Returns true if the rule is v2. - def v2? - @v2 - end - # Returns true if the rule is valid, false otherwise. The validation error # can be through the error method. def valid? @@ -82,23 +50,12 @@ def valid? :too_long_value?, :contains_empty_source?, :contains_negated_or?, - :too_long_tag? + :too_long_tag?, + :contains_explicit_and?, + :contains_lowercase_or?, + :contains_explicit_not? ] - if @v2 - validation_rules += [ - :contains_explicit_and?, - :contains_lowercase_or?, - :contains_explicit_not? - ] - else - # no more restriction on the number of positive and negative terms in v2 - validation_rules += [ - :too_many_positive_terms?, - :too_many_negative_terms? - ] - end - validation_rules.each do |validator| # stop when 1 validator fails if self.send(validator) @@ -132,8 +89,7 @@ def to_s def ==(other) other.class == self.class && other.value == @value && - other.tag == @tag && - other.long? == self.long? + other.tag == @tag end alias eql? == @@ -142,13 +98,12 @@ def ==(other) # rules as Hash keys. def hash # let's assume a nil value for @value or @tag is not different from the empty value - "v:#{@value},t:#{@tag},l:#{@long}".hash + "v:#{@value},t:#{@tag}".hash end - # Returns the maximum length of the rule value according to the type of the - # rule (long or standard). + # Returns the maximum length of the rule value. def max_value_length - long? ? MAX_LONG_RULE_VALUE_LENGTH : MAX_STD_RULE_VALUE_LENGTH + MAX_RULE_VALUE_LENGTH end protected @@ -178,20 +133,6 @@ def contains_explicit_not? !@value[/(^| )NOT /].nil? end - # Does the rule value contain too many positive terms ? - def too_many_positive_terms? - return false if long? - # negative look-behind; see http://www.rexegg.com/regex-disambiguation.html - # exclude the OR operator from the terms being counted - @value.scan(/(? MAX_POSITIVE_TERMS - end - - # Does the rule value contain too many negative terms ? - def too_many_negative_terms? - return false if long? - @value.scan(/(^| )\-(\w|\([^(]*\)|\"[^"]*\")/).size > MAX_NEGATIVE_TERMS - end - # Does the rule value contain an empty source ? def contains_empty_source? !@value[/source\:\s/].nil? diff --git a/lib/powertrack/streaming/stream.rb b/lib/powertrack/streaming/stream.rb index 8cd468b..3414ab8 100644 --- a/lib/powertrack/streaming/stream.rb +++ b/lib/powertrack/streaming/stream.rb @@ -17,13 +17,9 @@ class Stream # Includes a logger, void by default include VoidLogger::LoggerMixin - # The format of the URLs to connect to the various stream services - FEATURE_URL_FORMAT = { - # [ hostname, account, source, mode, label, feature ] - v1: "https://%s.gnip.com/accounts/%s/publishers/%s/%s/track/%s%s.json".freeze, - # [ hostname, domain, feature, stream type, account, source, label, sub-feature ] - v2: "https://gnip-%s.%s.com/%s/%s/accounts/%s/publishers/%s/%s%s.json".freeze - }.freeze + # The format of the URL to connect to the stream service + # [ hostname, domain, feature, stream type, account, source, label, sub-feature ] + FEATURE_URL_FORMAT = 'https://gnip-%s.%s.com/%s/%s/accounts/%s/publishers/%s/%s%s.json'.freeze # The default timeout on a connection to PowerTrack. Can be overriden per call. DEFAULT_CONNECTION_TIMEOUT = 30 @@ -34,14 +30,10 @@ class Stream # The default options for using the stream. DEFAULT_STREAM_OPTIONS = { - # enable PowerTrack v2 API (using v1 by default) - v2: false, # override the default connection timeout connect_timeout: DEFAULT_CONNECTION_TIMEOUT, # override the default inactivity timeout inactivity_timeout: DEFAULT_INACTIVITY_TIMEOUT, - # use a client id if you want to leverage the Backfill feature in v1 - client_id: nil, # enable the replay mode to get activities over the last 5 days # see http://support.gnip.com/apis/replay/api_reference.html replay: false @@ -67,13 +59,6 @@ def initialize(username, password, account_name, data_source, label, options=nil @label = label @options = DEFAULT_STREAM_OPTIONS.merge(options || {}) @replay = !!@options[:replay] - @client_id = @options[:client_id] - @v2 = !!@options[:v2] - end - - # Returns true if the stream uses PowerTrack v2 - def v2? - @v2 end # Adds many rules to your PowerTrack stream’s ruleset. @@ -95,11 +80,11 @@ def add_rules(*rules) # See http://support.gnip.com/apis/powertrack/api_reference.html#DeleteRules def delete_rules(*rules) # v2 does not use DELETE anymore - delete_verb = @v2 ? :post : :delete + delete_verb = :post # flatten the rules in case it was provided as an array delete_options = { body: MultiJson.encode('rules' => rules.flatten) } # v2 uses a query parameter - delete_options[:query] = { '_method' => 'delete' } if @v2 + delete_options[:query] = { '_method' => 'delete' } make_rules_request(delete_verb, delete_options) end @@ -149,7 +134,7 @@ def list_rules(options=nil) from: nil, # the ending date to which the activities will be recovered (replay mode only) to: nil, - # specify a number of minutes to leverage the Backfill feature (v2 only) + # specify a number of minutes to leverage the Backfill feature backfill_minutes: nil, # called for each message received, except heartbeats on_message: nil, @@ -181,39 +166,21 @@ def track(options=nil) # Returns the URL of the stream for a given feature. def feature_url(hostname, feature=nil, sub_feature=nil) - _url = nil - if @v2 - feature ||= @replay ? 'replay' : hostname - sub_feature = sub_feature ? "/#{sub_feature}" : '' - stream_type = (feature == 'rules' && @replay ? 'powertrack-replay' : 'powertrack') - # replay streaming is on gnip.com while replay rules are on twitter.com... - domain = (feature == 'replay' && @replay ? 'gnip' : 'twitter') - - _url = FEATURE_URL_FORMAT[:v2] % - [ hostname, - domain, - feature, - stream_type, - @account_name, - @data_source, - @label, - sub_feature ] - else - feature = feature ? "/#{feature}" : '' - mode = @replay ? 'replay' : 'streams' - - _url = FEATURE_URL_FORMAT[:v1] % - [ hostname, - @account_name, - @data_source, - mode, - @label, - feature ] - - _url += "?client=#{@client_id}" if @client_id - end - - _url + feature ||= @replay ? 'replay' : hostname + sub_feature = sub_feature ? "/#{sub_feature}" : '' + stream_type = (feature == 'rules' && @replay ? 'powertrack-replay' : 'powertrack') + # replay streaming is on gnip.com while replay rules are on twitter.com... + domain = (feature == 'replay' && @replay ? 'gnip' : 'twitter') + + FEATURE_URL_FORMAT % + [ hostname, + domain, + feature, + stream_type, + @account_name, + @data_source, + @label, + sub_feature ] end # Returns the HTTP header that turns on GZip-based compression if required. @@ -396,7 +363,7 @@ def track_once(options, retrier) logger.info "Replay mode enabled from '#{from}' to '#{to}'" end - if @v2 && backfill_minutes + if backfill_minutes get_opts[:query]['backfillMinutes'] = backfill_minutes end diff --git a/lib/powertrack/version.rb b/lib/powertrack/version.rb index 88abd13..c9038a0 100644 --- a/lib/powertrack/version.rb +++ b/lib/powertrack/version.rb @@ -1,3 +1,3 @@ module PowerTrack - VERSION = '1.3.1'.freeze + VERSION = '2.0.0'.freeze end diff --git a/test/minitest_helper.rb b/test/minitest_helper.rb index a717a8f..dca7a3f 100644 --- a/test/minitest_helper.rb +++ b/test/minitest_helper.rb @@ -30,14 +30,13 @@ def powertrack_config end # Returns a brand-new stream based on the config found in test/powertrack.yml. - def new_stream(v2=false, replay=false) + def new_stream(replay=false) PowerTrack::Stream.new( powertrack_config[:username], powertrack_config[:password], powertrack_config[:account_name], powertrack_config[:data_source], replay ? 'prod' : powertrack_config[:stream_label], - replay: replay, - v2: v2) + replay: replay) end end diff --git a/test/test_manage_rules.rb b/test/test_manage_rules.rb index 3ce6711..cbe828b 100644 --- a/test/test_manage_rules.rb +++ b/test/test_manage_rules.rb @@ -4,24 +4,16 @@ class TestManageRules < Minitest::Test - def test_add_then_delete_a_single_rule_v1 - add_then_delete_a_single_rule(false, false) + def test_add_then_delete_a_single_rule + add_then_delete_a_single_rule(false) end - def test_add_then_delete_a_single_rule_v2 - add_then_delete_a_single_rule(true, false) + def test_add_then_delete_a_single_rule_in_replay_mode + add_then_delete_a_single_rule(true) end - def test_add_then_delete_a_single_rule_in_replay_mode_v1 - add_then_delete_a_single_rule(false, true) - end - - def test_add_then_delete_a_single_rule_in_replay_mode_v2 - add_then_delete_a_single_rule(true, true) - end - - def add_then_delete_a_single_rule(v2, replay) - stream = new_stream(v2, replay) + def add_then_delete_a_single_rule(replay) + stream = new_stream(replay) # add a logger stream.logger = Logger.new(STDERR) @@ -32,30 +24,26 @@ def add_then_delete_a_single_rule(v2, replay) pre_existing_rules = stream.list_rules $stderr.puts pre_existing_rules.inspect assert pre_existing_rules.is_a?(Array) - assert pre_existing_rules.all? { |rule| !rule.id.nil? } if v2 + assert pre_existing_rules.all? { |rule| !rule.id.nil? } already_in = pre_existing_rules.any? { |rule| new_rule == rule } res = stream.add_rule(new_rule) - if v2 - assert res.is_a?(Hash) - assert res['summary'].is_a?(Hash) - - if already_in - assert_equal 0, res['summary']['created'] - assert_equal 1, res['summary']['not_created'] - else - assert_equal 1, res['summary']['created'] - assert_equal 0, res['summary']['not_created'] - end + assert res.is_a?(Hash) + assert res['summary'].is_a?(Hash) + + if already_in + assert_equal 0, res['summary']['created'] + assert_equal 1, res['summary']['not_created'] else - assert_nil res + assert_equal 1, res['summary']['created'] + assert_equal 0, res['summary']['not_created'] end rules_after_addition = stream.list_rules assert rules_after_addition.is_a?(Array) - assert rules_after_addition.all? { |rule| !rule.id.nil? } if v2 + assert rules_after_addition.all? { |rule| !rule.id.nil? } if already_in assert_equal pre_existing_rules.size, rules_after_addition.size @@ -67,19 +55,15 @@ def add_then_delete_a_single_rule(v2, replay) res = stream.delete_rules(new_rule) - if v2 - assert res.is_a?(Hash) - assert res['summary'].is_a?(Hash) - assert_equal 1, res['summary']['deleted'] - assert_equal 0, res['summary']['not_deleted'] - else - assert_nil res - end + assert res.is_a?(Hash) + assert res['summary'].is_a?(Hash) + assert_equal 1, res['summary']['deleted'] + assert_equal 0, res['summary']['not_deleted'] 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 - assert rules_after_removal.all? { |rule| !rule.id.nil? } if v2 + assert rules_after_removal.all? { |rule| !rule.id.nil? } end end diff --git a/test/test_rule.rb b/test/test_rule.rb index 83baf37..1804bb1 100644 --- a/test/test_rule.rb +++ b/test/test_rule.rb @@ -8,220 +8,71 @@ def test_valid_rule rule = PowerTrack::Rule.new('coke') assert_equal 'coke', rule.value assert_nil rule.tag - assert !rule.long? assert rule.valid? assert_nil rule.error - long_rule = PowerTrack::Rule.new('pepsi', tag: 'soda', long: true) - assert_equal 'pepsi', long_rule.value - assert_equal 'soda', long_rule.tag - assert long_rule.long? - assert long_rule.valid? - assert_nil long_rule.error - - v2_rule = PowerTrack::Rule.new('dr pepper', tag: 'soda', v2: true) - assert v2_rule.v2? - assert_equal 'dr pepper', v2_rule.value - assert_equal 'soda', v2_rule.tag - assert v2_rule.long? - assert v2_rule.valid? - assert_nil v2_rule.error + tagged_rule = PowerTrack::Rule.new('dr pepper', tag: 'soda') + assert_equal 'dr pepper', tagged_rule.value + assert_equal 'soda', tagged_rule.tag + assert tagged_rule.valid? + assert_nil tagged_rule.error end def test_too_long_tag long_tag = 'a' * PowerTrack::Rule::MAX_TAG_LENGTH - rule = PowerTrack::Rule.new('coke', tag: long_tag, long: false) + rule = PowerTrack::Rule.new('coke', tag: long_tag) assert rule.valid? assert_nil rule.error long_tag = 'b' * 2 * PowerTrack::Rule::MAX_TAG_LENGTH - rule = PowerTrack::Rule.new('coke', tag: long_tag, long: true) + rule = PowerTrack::Rule.new('coke', tag: long_tag) assert !rule.valid? assert_match /too long tag/i, rule.error end def test_too_long_value - long_val = 'a' * PowerTrack::Rule::MAX_STD_RULE_VALUE_LENGTH - # v1 + long_val = 'a' * PowerTrack::Rule::MAX_RULE_VALUE_LENGTH + rule = PowerTrack::Rule.new(long_val) assert rule.valid? + assert_nil rule.error - # v2 - v2_rule = PowerTrack::Rule.new(long_val, v2: true) - assert v2_rule.v2? - assert v2_rule.valid? - assert_nil v2_rule.error - - long_val = 'c' * PowerTrack::Rule::MAX_LONG_RULE_VALUE_LENGTH - # v1 - rule = long_val.to_pwtk_rule(long: false) - assert !rule.valid? - assert_match /too long value/i, rule.error - + long_val = 'c' * PowerTrack::Rule::MAX_RULE_VALUE_LENGTH assert long_val.to_pwtk_rule.valid? - assert long_val.to_pwtk_rule(long: true).valid? - - # v2 - assert long_val.to_pwtk_rule(v2: true).valid? - assert long_val.to_pwtk_rule(long: false, v2: true).valid? - very_long_val = 'rrr' * PowerTrack::Rule::MAX_LONG_RULE_VALUE_LENGTH - # v1 + very_long_val = 'rrr' * PowerTrack::Rule::MAX_RULE_VALUE_LENGTH rule = very_long_val.to_pwtk_rule assert !rule.valid? assert_match /too long value/i, rule.error - - # v2 - v2_rule = very_long_val.to_pwtk_rule(v2: true) - assert v2_rule.v2? - assert !v2_rule.valid? - assert_match /too long value/i, v2_rule.error - end - - def test_too_many_positive_terms - phrase = ([ 'coke' ] * PowerTrack::Rule::MAX_POSITIVE_TERMS).join(' ') - rule = PowerTrack::Rule.new(phrase) - assert !rule.long? - assert rule.valid? - assert_nil rule.error - - long_rule = PowerTrack::Rule.new(phrase, long: true) - assert long_rule.long? - assert long_rule.valid? - assert_nil long_rule.error - - # v2 - v2_rule = PowerTrack::Rule.new(phrase, v2: true) - assert v2_rule.v2? - assert v2_rule.long? - assert v2_rule.valid? - assert_nil v2_rule.error - - phrase = ([ 'coke' ] * (2 * PowerTrack::Rule::MAX_POSITIVE_TERMS)).join(' ') - # v1 - rule = PowerTrack::Rule.new(phrase, long: false) - assert !rule.long? - assert !rule.valid? - assert_match /too many positive terms/i, rule.error - # v2 - v2_rule = PowerTrack::Rule.new(phrase, v2: true) - assert v2_rule.v2? - assert v2_rule.long? - assert v2_rule.valid? - assert_nil v2_rule.error - - long_rule = PowerTrack::Rule.new(phrase, long: true) - assert long_rule.long? - assert long_rule.valid? - assert_nil long_rule.error - - phrase = "from:lkv1csayp OR from:u42vf OR from:y OR from:groj OR from:69iqciuxlxerqq OR from:4 OR from:9832xjrqi1ncrs OR from:7kfss6jxtl0oj OR from:b31m9qf0u3tc OR from:0 OR from:abo59n OR from:3lma3kl OR from:5 OR from:ovw7bgov OR from:ubp OR from:gc9a6b OR from:jo7ootfvy4 OR from:sg7oohj OR from:349ankku OR from:9b72n OR from:qz7offt5019u OR from:gkd OR from:cc31p3 OR from:xws9 OR from:bjzbatm OR from:rwjm78cgre3j5 OR from:f1obak7w3w OR from:nontf OR from:4aeas6kgb7nia OR from:dzqy7" - long_rule = PowerTrack::Rule.new(phrase) - assert !long_rule.long? - assert long_rule.valid?, long_rule.error - assert_nil long_rule.error - - long_rule = PowerTrack::Rule.new(phrase + " OR from:michel") - assert !rule.valid? - assert_match /too many positive terms/i, rule.error - - v2_rule = PowerTrack::Rule.new(phrase + " OR from:michel", v2: true) - assert v2_rule.v2? - assert v2_rule.valid? - assert_nil v2_rule.error - end - - def test_too_many_negative_terms - phrase = ([ '-pepsi' ] * PowerTrack::Rule::MAX_POSITIVE_TERMS).join(' ') - rule = PowerTrack::Rule.new(phrase) - assert !rule.long? - assert rule.valid? - assert_nil rule.error - - long_rule = PowerTrack::Rule.new(phrase, long: true) - assert long_rule.long? - assert long_rule.valid? - assert_nil long_rule.error - - v2_rule = PowerTrack::Rule.new(phrase, v2: true) - assert v2_rule.v2? - assert v2_rule.long? - assert v2_rule.valid? - assert_nil v2_rule.error - - phrase = ([ '-pepsi' ] * (2 * PowerTrack::Rule::MAX_POSITIVE_TERMS)).join(' ') - rule = PowerTrack::Rule.new(phrase) - assert !rule.long? - assert !rule.valid? - assert_match /too many negative terms/i, rule.error - - long_rule = PowerTrack::Rule.new(phrase, long: true) - assert long_rule.long? - assert long_rule.valid? - assert_nil long_rule.error - - v2_rule = PowerTrack::Rule.new(phrase, v2: true) - assert v2_rule.v2? - assert v2_rule.long? - assert v2_rule.valid? - assert_nil v2_rule.error end def test_contains_negated_or phrase = 'coke OR -pepsi' rule = PowerTrack::Rule.new(phrase) - assert !rule.long? assert !rule.valid? assert_match /contains negated or/i, rule.error - - v2_rule = PowerTrack::Rule.new(phrase, v2: true) - assert v2_rule.v2? - assert v2_rule.long? - assert !v2_rule.valid? - assert_match /contains negated or/i, v2_rule.error end def test_contains_explicit_and phrase = 'coke AND pepsi' rule = PowerTrack::Rule.new(phrase) - assert !rule.long? - assert rule.valid? - assert_nil rule.error - - v2_rule = PowerTrack::Rule.new(phrase, v2: true) - assert v2_rule.v2? - assert v2_rule.long? - assert !v2_rule.valid? - assert_match /contains explicit and/i, v2_rule.error + assert !rule.valid? + assert_match /contains explicit and/i, rule.error end def test_contains_explicit_not [ 'coke NOT pepsi', 'NOT (pepsi OR "dr pepper")' ].each do |phrase| rule = PowerTrack::Rule.new(phrase) - assert !rule.long? - assert rule.valid? - assert_nil rule.error - - v2_rule = PowerTrack::Rule.new(phrase, v2: true) - assert v2_rule.v2? - assert v2_rule.long? - assert !v2_rule.valid? - assert_match /contains explicit not/i, v2_rule.error + assert !rule.valid? + assert_match /contains explicit not/i, rule.error end end def test_contains_lowercase_or phrase = 'coke or pepsi' rule = PowerTrack::Rule.new(phrase) - assert !rule.long? - assert rule.valid? - assert_nil rule.error - - v2_rule = PowerTrack::Rule.new(phrase, v2: true) - assert v2_rule.v2? - assert v2_rule.long? - assert !v2_rule.valid? - assert_match /contains lowercase or/i, v2_rule.error + assert !rule.valid? + assert_match /contains lowercase or/i, rule.error end def test_to_hash_and_json @@ -231,7 +82,7 @@ def test_to_hash_and_json assert_equal MultiJson.encode(res), rule.to_json res[:tag] = 'soda' - rule = PowerTrack::Rule.new(res[:value], tag: res[:tag], long: true) + rule = PowerTrack::Rule.new(res[:value], tag: res[:tag]) assert_equal res, rule.to_hash assert_equal MultiJson.encode(res), rule.to_json end @@ -247,22 +98,12 @@ def test_double_quote_jsonification def test_hash short_rule = PowerTrack::Rule.new('coke') - not_long_rule = PowerTrack::Rule.new('coke', long: false) - false_long_rule = PowerTrack::Rule.new('coke', long: true) short_rule_with_tag = PowerTrack::Rule.new('coke', tag: 'soda') - assert short_rule == not_long_rule - assert_equal short_rule, not_long_rule - assert_equal short_rule.hash, not_long_rule.hash - - assert short_rule != false_long_rule h = { short_rule => 1 } - h[not_long_rule] = 2 - h[false_long_rule] = 3 h[short_rule_with_tag] = 4 - assert_equal 2, h[short_rule] - assert_equal h[short_rule], h[not_long_rule] + assert_equal 1, h[short_rule] assert_equal 4, h[short_rule_with_tag] assert_nil h[PowerTrack::Rule.new('pepsi', tag: 'soda')] end diff --git a/test/test_track_stream.rb b/test/test_track_stream.rb index bcb1235..f564fa9 100644 --- a/test/test_track_stream.rb +++ b/test/test_track_stream.rb @@ -4,25 +4,16 @@ class TestTrackStream < Minitest::Test - def test_track_realtime_stream_v1 - track_simple_stream(false, false) + def test_track_realtime_stream + track_simple_stream(false) end - def test_track_realtime_stream_v2 - track_simple_stream(true, false) + def test_track_replay_stream + track_simple_stream(true) end - def test_track_replay_stream_v1 - track_simple_stream(false, true) - end - - def test_track_replay_stream_v2 - track_simple_stream(true, true) - end - - def track_simple_stream(v2, replay) - stream = new_stream(v2, replay) - assert_equal !!v2, stream.v2? + def track_simple_stream(replay) + stream = new_stream(replay) # add a logger stream.logger = Logger.new(STDERR) @@ -33,18 +24,14 @@ def track_simple_stream(v2, replay) begin res = stream.add_rule(new_rule) - if v2 - assert res.is_a?(Hash) - assert res['summary'].is_a?(Hash) - else - assert_nil res - end + assert res.is_a?(Hash) + assert res['summary'].is_a?(Hash) rules_after_addition = stream.list_rules assert rules_after_addition.is_a?(Array) assert rules_after_addition.size > 0 assert rules_after_addition.any? { |rule| rule == new_rule } - assert rules_after_addition.all? { |rule| !rule.id.nil? } if v2 + assert rules_after_addition.all? { |rule| !rule.id.nil? } heartbeats = 0 received = 0 @@ -106,8 +93,12 @@ def track_simple_stream(v2, replay) end # heartbeats only sent every 10 minutes in v2... - assert heartbeats > 0, 'No heartbeat received' unless v2 - puts "#{heartbeats} heartbeats received" + unless replay + assert_equal 0, heartbeats, "Unexpected #{heartbeats} heartbeats received" + else + assert heartbeats > 0, 'No heartbeats received so far' + puts "#{heartbeats} heartbeats received" + end assert received > 0, 'No message received so far' puts "#{received} messages received" @@ -119,14 +110,10 @@ def track_simple_stream(v2, replay) ensure res = stream.delete_rules(new_rule) - if v2 - assert res.is_a?(Hash) - assert res['summary'].is_a?(Hash) - assert_equal 1, res['summary']['deleted'] - assert_equal 0, res['summary']['not_deleted'] - else - assert_nil res - end + assert res.is_a?(Hash) + assert res['summary'].is_a?(Hash) + assert_equal 1, res['summary']['deleted'] + assert_equal 0, res['summary']['not_deleted'] end end end