Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
splitting test suite into multiple test files along functional lines
  • Loading branch information
Dan Croak committed May 16, 2009
1 parent f4d27a3 commit 46d742c
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 367 deletions.
4 changes: 2 additions & 2 deletions Rakefile
@@ -1,7 +1,7 @@
require 'rake'
require 'rake/testtask'

test_files_pattern = 'test/twitter_*_test.rb'
test_files_pattern = 'test/*_test.rb'
Rake::TestTask.new do |t|
t.libs << 'lib'
t.pattern = test_files_pattern
Expand Down Expand Up @@ -64,7 +64,7 @@ end
def write_yaml(opts = {})
@client = TwitterSearch::Client.new 'twitter-search'
tweets = @client.query(opts[:tweets])
File.open(File.join(File.dirname(__FILE__), 'test', 'yaml', "#{opts[:file]}.yaml"), 'w+') do |file|
File.open(File.join(File.dirname(__FILE__), 'test', 'yaml', "#{opts[:file]}.yaml"), 'w+') do |file|
file.puts tweets.to_yaml
end
end
27 changes: 27 additions & 0 deletions test/foreign_languages_test.rb
@@ -0,0 +1,27 @@
require File.join(File.dirname(__FILE__), 'test_helper')

class ForeignLanguagesTest < Test::Unit::TestCase # :nodoc:
context "@client.query :q => 'congratulations', :lang => 'en'" do
setup do
@tweets = read_yaml :file => 'english'
end

should_have_default_search_behaviors

should 'find tweets containing "congratulations" and are in English' do
assert @tweets.all?{ |t| t.text =~ /congratulation/i && t.language == 'en' }
end
end

context "@client.query :q => 'با', :lang => 'ar'" do
setup do
@tweets = read_yaml :file => 'arabic'
end

should_have_default_search_behaviors

should 'find tweets containing "با" and are in Arabic' do
assert @tweets.all?{ |t| t.text.include?('با') && t.language == 'ar' }
end
end
end
15 changes: 15 additions & 0 deletions test/hash_interface_test.rb
@@ -0,0 +1,15 @@
require File.join(File.dirname(__FILE__), 'test_helper')

class HashInterfaceTest < Test::Unit::TestCase # :nodoc:
context "@tweets[2]" do
setup do
@tweets = read_yaml :file => 'reference_mashable'
end

should_have_default_search_behaviors

should 'return the third tweet' do
assert_equal 859152168, @tweets[2].id
end
end
end
197 changes: 197 additions & 0 deletions test/operators_test.rb
@@ -0,0 +1,197 @@
require File.join(File.dirname(__FILE__), 'test_helper')

class OperatorsTest < Test::Unit::TestCase # :nodoc:
context '@client.query :q => \'"happy hour"\'' do
setup do
@tweets = read_yaml :file => 'happy_hour_exact'
end

should_have_default_search_behaviors

should 'find tweets containing the exact phrase "happy hour"' do
assert @tweets.all?{ |t| t.text =~ /happy hour/i }
end
end

context "@client.query :q => 'obama OR hillary'" do
setup do
@tweets = read_yaml :file => 'obama_or_hillary'
end

should_have_default_search_behaviors

should 'find tweets containing either "obama" or "hillary" (or both)' do
assert @tweets.all?{ |t| t.text =~ /obama/i || t.text =~ /hillary/i }
end
end

context "@client.query :q => 'beer -root'" do
setup do
@tweets = read_yaml :file => 'beer_minus_root'
end

should_have_default_search_behaviors

should 'find tweets containing "beer" but not "root"' do
assert @tweets.all?{ |t| t.text =~ /beer/i || t.text !~ /root/i }
end
end

context "@client.query :q => '#haiku'" do
setup do
@tweets = read_yaml :file => 'hashtag_haiku'
end

should_have_default_search_behaviors

should 'find tweets containing the hashtag "haiku"' do
assert @tweets.all?{ |t| t.text =~ /#haiku/i }
end
end

context "@client.query :q => 'from: alexiskold'" do
setup do
@tweets = read_yaml :file => 'from_alexiskold'
end

should_have_default_search_behaviors

should 'find tweets sent from person "alexiskold"' do
assert @tweets.all?{ |t| t.from_user == 'alexiskold' }
end
end

context "@client.query :q => 'to:techcrunch'" do
setup do
@tweets = read_yaml :file => 'to_techcrunch'
end

should_have_default_search_behaviors

should 'find tweets sent to person "techcrunch"' do
assert @tweets.all?{ |t| t.text =~ /^@techcrunch/i }
end
end

context "@client.query :q => '@mashable'" do
setup do
@tweets = read_yaml :file => 'reference_mashable'
end

should_have_default_search_behaviors

should 'find tweets referencing person "mashable"' do
assert @tweets.all?{ |t| t.text =~ /@mashable/i }
end
end

context "@client.query :q => '\"happy hour\" near:\"san francisco\"'" do
setup do
@tweets = read_yaml :file => 'happy_hour_near_sf'
end

# Twitter Search API requires the geocode parameter for location searching
should 'not find tweets using the near operator' do
assert ! @tweets.any?
end
end

context "@client.query :q => 'near:NYC within:15mi'" do
setup do
@tweets = read_yaml :file => 'within_15mi_nyc'
end

# Twitter Search API requires the geocode parameter for location searching
should 'not find tweets using the near operator' do
assert ! @tweets.any?
end
end

context "@client.query :q => 'superhero since:2008-05-01'" do
setup do
@tweets = read_yaml :file => 'superhero_since'
end

should_have_default_search_behaviors

should "find tweets containing superhero" do
@tweets.each do |tweet|
assert_match /superhero/i, tweet.text
end
end

should 'find tweets sent since date "2008-05-01" (year-month-day)' do
@tweets.each do |tweet|
assert convert_date(tweet.created_at) > DateTime.new(2008, 5, 1)
end
end
end

context "@client.query :q => 'ftw until:2008-05-03'" do
setup do
@tweets = read_yaml :file => 'ftw_until'
end

should_have_default_search_behaviors

should "find tweets containing ftw" do
@tweets.each do |tweet|
assert_match /ftw/i, tweet.text
end
end

should "find tweets sent up to date 2008-05-03" do
@tweets.each do |tweet|
assert convert_date(tweet.created_at) < DateTime.new(2008, 5, 3, 11, 59)
end
end
end

context "@client.query :q => 'movie -scary :)'" do
setup do
@tweets = read_yaml :file => 'movie_positive_tude'
end

should_have_default_search_behaviors

should 'find tweets containing "movie", but not "scary", and with a positive attitude' do
assert @tweets.all?{ |t| t.text =~ /movie/i && t.text !~ /scary/i && positive_attitude?(t.text) }
end
end

context "@client.query :q => 'flight :('" do
setup do
@tweets = read_yaml :file => 'flight_negative_tude'
end

should_have_default_search_behaviors

should 'find tweets containing "flight" and with a negative attitude' do
assert @tweets.all?{ |t| t.text =~ /flight/i && negative_attitude?(t.text) }
end
end

context "@client.query :q => 'traffic ?'" do
setup do
@tweets = read_yaml :file => 'traffic_question'
end

should_have_default_search_behaviors

should 'find tweets containing "traffic" and asking a question' do
assert @tweets.all?{ |t| t.text =~ /traffic/i && t.text.include?('?') }
end
end

context "@client.query :q => 'hilarious filter:links'" do
setup do
@tweets = read_yaml :file => 'hilarious_links'
end

should_have_default_search_behaviors

should 'find tweets containing "hilarious" and linking to URLs' do
assert @tweets.all?{ |t| t.text =~ /hilarious/i && hyperlinks?(t.text) }
end
end
end
43 changes: 43 additions & 0 deletions test/pagination_test.rb
@@ -0,0 +1,43 @@
require File.join(File.dirname(__FILE__), 'test_helper')

class PaginationTest < Test::Unit::TestCase # :nodoc:
context "@client.query :q => 'Boston Celtics', :rpp => '30'" do
setup do
@tweets = read_yaml :file => 'results_per_page'
end

should_find_tweets
should_have_text_for_all_tweets
should_return_page 1
should_return_tweets_in_sets_of 30
end

context "@client.query :q => 'a Google(or Twitter)whack', :rpp => '2'" do
setup do
@tweets = read_yaml :file => 'only_one_result'
end

should 'not be able to get next page of @tweets' do
assert ! @tweets.has_next_page?
end
end

context "@client.query :q => 'almost Google(or Twitter)whack', :rpp => '1'" do
setup do
@page_one = read_yaml :file => 'only_two_results'
@page_two = read_yaml :file => 'only_two_results_page_2'
end

should 'be able to get next page of @tweets' do
assert @page_one.has_next_page?

FakeWeb.register_uri( :get,
"#{TwitterSearch::Client::TWITTER_SEARCH_API_URL}?max_id=100&q=almost+a+Google%28or+is+it+Twitter%29whack&rpp=1&page=2",
:string => '{"results":[{"text":"Boston Celtics-Los Angeles Lakers, Halftime http://tinyurl.com/673s24","from_user":"nbatube","id":858836387,"language":"en","created_at":"Tue, 15 Jul 2008 09:27:57 +0000"}],"since_id":0,"max_id":100,"results_per_page":1,"page":2,"query":"almost+a+Google%28or+is+it+Twitter%29whack"}'
)
next_page = @page_one.get_next_page
assert_equal @page_two[0].created_at, next_page[0].created_at
assert_equal @page_two[0].text, next_page[0].text
end
end
end
39 changes: 39 additions & 0 deletions test/search_test.rb
@@ -0,0 +1,39 @@
require File.join(File.dirname(__FILE__), 'test_helper')

class SearchTest < Test::Unit::TestCase # :nodoc:
context "@client.query 'Obama'" do
setup do
@tweets = read_yaml :file => 'obama'
end

should_have_default_search_behaviors

should 'find tweets containing the single word "Obama"' do
assert @tweets.all? { |tweet| tweet.text =~ /obama/i }
end
end

context "@client.query 'twitter search'" do
setup do
@tweets = read_yaml :file => 'twitter_search'
end

should_have_default_search_behaviors

should 'find tweets containing both "twitter" and "search"' do
assert @tweets.all?{ |t| t.text =~ /twitter/i && t.text =~ /search/i }
end
end

context "@client.query :q => 'twitter search'" do
setup do
@tweets = read_yaml :file => 'twitter_search_and'
end

should_have_default_search_behaviors

should 'find tweets containing both "twitter" and "search"' do
assert @tweets.all?{ |t| t.text =~ /twitter/i && t.text =~ /search/i }
end
end
end
41 changes: 37 additions & 4 deletions test/test_helper.rb
Expand Up @@ -13,17 +13,50 @@ def self.here
require 'test/unit'
require 'rubygems'
require 'shoulda'
require 'redgreen' rescue LoadError
require 'yaml'
require File.here / '..' / 'lib' / 'twitter_search'
require File.here / '..' / 'shoulda_macros' / 'twitter_search'

class Test::Unit::TestCase
gem('fakeweb', '>=1.2.0')
require 'fakeweb'

# an insurance policy against hitting http://twitter.com
FakeWeb.allow_net_connect = false

class Test::Unit::TestCase
def read_yaml(opts = {})
if opts[:file].nil?
raise ArgumentError
end
raise ArgumentError if opts[:file].nil?
YAML.load_file(File.here / 'yaml' / "#{opts[:file]}.yaml")
end

def parse_json(opts = {})
raise ArgumentError if opts[:file].nil?
json = IO.read(File.here / 'json' / "#{opts[:file]}.json").body
JSON.parse(json)
end

def convert_date(date)
date = date.split(' ')
DateTime.new(date[3].to_i, convert_month(date[2]), date[1].to_i)
end

def convert_month(str)
months = { 'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4,
'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8,
'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12 }
months[str]
end

def positive_attitude?(string)
[":)", "=)", ":-)", ":D"].any? { |emoticon| string.include?(emoticon) }
end

def negative_attitude?(string)
[":(", "=(", ":-(", ":P"].any? { |emoticon| string.include?(emoticon) }
end

def hyperlinks?(str)
str.include?('http://') || str.include?('https://')
end
end

0 comments on commit 46d742c

Please sign in to comment.