Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
160 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
spec/models/agents/twitter_stream_agent_spec/check_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'spec_helper' | ||
|
||
describe Agents::TwitterStreamAgent do | ||
before do | ||
@opts = { | ||
:consumer_key => "---", | ||
:consumer_secret => "---", | ||
:oauth_token => "---", | ||
:oauth_token_secret => "---", | ||
:filters => %w[keyword1 keyword2], | ||
:expected_update_period_in_days => "2", | ||
:generate => "events" | ||
} | ||
|
||
@agent = Agents::TwitterStreamAgent.new(:name => "HuginnBot", :options => @opts) | ||
@agent.user = users(:bob) | ||
@agent.save! | ||
end | ||
|
||
describe '#check' do | ||
context "when generate is set to 'counts'" do | ||
before do | ||
@agent.options[:generate] = 'counts' | ||
@agent.save! | ||
end | ||
|
||
it 'emits events' do | ||
@agent.process_tweet('keyword1', {:text => "something", :user => {:name => "Mr. Someone"}}) | ||
@agent.process_tweet('keyword2', {:text => "something", :user => {:name => "Mr. Someone"}}) | ||
@agent.process_tweet('keyword1', {:text => "something", :user => {:name => "Mr. Someone"}}) | ||
|
||
lambda { | ||
@agent.reload.check | ||
}.should change { @agent.events.count }.by(2) | ||
|
||
@agent.events[-1].payload[:filter].should == 'keyword1' | ||
@agent.events[-1].payload[:count].should == 2 | ||
|
||
@agent.events[-2].payload[:filter].should == 'keyword2' | ||
@agent.events[-2].payload[:count].should == 1 | ||
|
||
@agent.memory[:filter_counts].should == {} | ||
end | ||
end | ||
|
||
context "when generate is not set to 'counts'" do | ||
it 'does nothing' do | ||
@agent.memory[:filter_counts] = { :keyword1 => 2 } | ||
@agent.save! | ||
lambda { | ||
@agent.reload.check | ||
}.should_not change { Event.count } | ||
@agent.memory[:filter_counts].should == {} | ||
end | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
spec/models/agents/twitter_stream_agent_spec/credentials_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'spec_helper' | ||
|
||
describe Agents::TwitterStreamAgent do | ||
describe "#valid_credentials" do | ||
let(:stream) { Agents::TwitterStreamAgent.new } | ||
let(:invalid_error_message) { "Twitter credentials are invalid, a connection could not be established" } | ||
let(:user) { User.new } | ||
before do | ||
stream.user = user | ||
end | ||
context "invalid credentials" do | ||
it 'includes invalid credential errors' do | ||
mock.any_instance_of(Twitter::REST::Client).verify_credentials { raise Twitter::Error::Unauthorized.new("invalid") } | ||
stream.valid? | ||
expect(stream.errors.full_messages).to include(invalid_error_message) | ||
end | ||
end | ||
|
||
context "valid credentials" do | ||
it 'includes invalid credential errors' do | ||
mock.any_instance_of(Twitter::REST::Client).verify_credentials { true } | ||
stream.valid? | ||
expect(stream.errors.full_messages).to_not include(invalid_error_message) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
module SpecHelpers | ||
def build_events(options = {}) | ||
options[:values].map.with_index do |tuple, index| | ||
event = Event.new | ||
event.agent = agents(:bob_weather_agent) | ||
event.payload = (options[:pattern] || {}).dup.merge((options[:keys].zip(tuple)).inject({}) { |memo, (key, value)| memo[key] = value; memo }) | ||
event.created_at = (100 - index).hours.ago | ||
event.updated_at = (100 - index).hours.ago | ||
event | ||
def self.included(klass) | ||
klass.class_eval do | ||
include BuildEvents | ||
include TwitterCredentials | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module SpecHelpers | ||
module BuildEvents | ||
def build_events(options = {}) | ||
options[:values].map.with_index do |tuple, index| | ||
event = Event.new | ||
event.agent = agents(:bob_weather_agent) | ||
event.payload = (options[:pattern] || {}).dup.merge((options[:keys].zip(tuple)).inject({}) { |memo, (key, value)| memo[key] = value; memo }) | ||
event.created_at = (100 - index).hours.ago | ||
event.updated_at = (100 - index).hours.ago | ||
event | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module SpecHelpers | ||
module TwitterCredentials | ||
def self.included(klass) | ||
klass.class_eval do | ||
RSpec.configure do |config| | ||
config.before(:each) do | ||
any_instance_of(Twitter::REST::Client) do |klass| | ||
stub(klass).verify_credentials { 'true' } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |