From a596eb5b45738bfad5ec8228c8c2c53220aec12d Mon Sep 17 00:00:00 2001 From: David Czarnecki Date: Tue, 15 Feb 2011 10:42:14 -0500 Subject: [PATCH] Added Redis dependency in Gemfile. Start and stop Redis instance in Rakefile for test. Simple integration test to demonstrate loading and query. --- Gemfile | 2 ++ Gemfile.lock | 2 ++ Rakefile | 40 ++++++++++++++++++++++++++++++++++++++-- test/db/.gitkeep | 0 test/samples/venues.json | 5 +++++ test/test.conf | 8 ++++++++ test/test_soulmate.rb | 16 ++++++++++++++-- 7 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 test/db/.gitkeep create mode 100644 test/samples/venues.json create mode 100644 test/test.conf diff --git a/Gemfile b/Gemfile index bdb0a5f..b352063 100644 --- a/Gemfile +++ b/Gemfile @@ -11,3 +11,5 @@ group :development do gem "jeweler", "~> 1.5.2" gem "rcov", ">= 0" end + +gem 'redis', "~> 2.1.1" \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 07df601..fef942b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,6 +8,7 @@ GEM rake rake (0.8.7) rcov (0.9.9) + redis (2.1.1) shoulda (2.11.3) PLATFORMS @@ -17,4 +18,5 @@ DEPENDENCIES bundler (~> 1.0.0) jeweler (~> 1.5.2) rcov + redis (~> 2.1.1) shoulda diff --git a/Rakefile b/Rakefile index 2281194..25b4698 100644 --- a/Rakefile +++ b/Rakefile @@ -45,8 +45,6 @@ Rcov::RcovTask.new do |test| test.verbose = true end -task :default => :test - require 'rake/rdoctask' Rake::RDocTask.new do |rdoc| version = File.exist?('VERSION') ? File.read('VERSION') : "" @@ -56,3 +54,41 @@ Rake::RDocTask.new do |rdoc| rdoc.rdoc_files.include('README*') rdoc.rdoc_files.include('lib/**/*.rb') end + +REDIS_DIR = File.expand_path(File.join("..", "test"), __FILE__) +REDIS_CNF = File.join(REDIS_DIR, "test.conf") +REDIS_PID = File.join(REDIS_DIR, "db", "redis.pid") +REDIS_LOCATION = ENV['REDIS_LOCATION'] + +task :default => :run + +desc "Run tests and manage server start/stop" +task :run => [:start, :test, :stop] + +desc "Run rcov and manage server start/stop" +task :rcoverage => [:start, :rcov, :stop] + +desc "Start the Redis server" +task :start do + redis_running = \ + begin + File.exists?(REDIS_PID) && Process.kill(0, File.read(REDIS_PID).to_i) + rescue Errno::ESRCH + FileUtils.rm REDIS_PID + false + end + + if REDIS_LOCATION + system "#{REDIS_LOCATION}/redis-server #{REDIS_CNF}" unless redis_running + else + system "redis-server #{REDIS_CNF}" unless redis_running + end +end + +desc "Stop the Redis server" +task :stop do + if File.exists?(REDIS_PID) + Process.kill "INT", File.read(REDIS_PID).to_i + FileUtils.rm REDIS_PID + end +end diff --git a/test/db/.gitkeep b/test/db/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/samples/venues.json b/test/samples/venues.json new file mode 100644 index 0000000..c8901c6 --- /dev/null +++ b/test/samples/venues.json @@ -0,0 +1,5 @@ +{"id":1,"term":"Dodger Stadium","score":85,"data":{"url":"\/dodger-stadium-tickets\/","subtitle":"Los Angeles, CA"}} +{"id":28,"term":"Angel Stadium","score":85,"data":{"url":"\/angel-stadium-tickets\/","subtitle":"Anaheim, CA"}} +{"id":30,"term":"Chase Field ","score":85,"data":{"url":"\/chase-field-tickets\/","subtitle":"Phoenix, AZ"}} +{"id":29,"term":"Sun Life Stadium","score":84,"data":{"url":"\/sun-life-stadium-tickets\/","subtitle":"Miami, FL"}} +{"id":2,"term":"Turner Field","score":83,"data":{"url":"\/turner-field-tickets\/","subtitle":"Atlanta, GA"}} diff --git a/test/test.conf b/test/test.conf new file mode 100644 index 0000000..e83408f --- /dev/null +++ b/test/test.conf @@ -0,0 +1,8 @@ +dir ./test/db +pidfile ./redis.pid +port 6379 +timeout 300 +loglevel debug +logfile stdout +databases 16 +daemonize yes diff --git a/test/test_soulmate.rb b/test/test_soulmate.rb index fe84ca4..316215e 100644 --- a/test/test_soulmate.rb +++ b/test/test_soulmate.rb @@ -1,7 +1,19 @@ require 'helper' class TestSoulmate < Test::Unit::TestCase - should "probably rename this file and start testing for real" do - flunk "hey buddy, you should probably rename this file and start testing for real" + def test_integration_can_load_values_and_query + items = [] + venues = File.open(File.expand_path(File.dirname(__FILE__)) + '/samples/venues.json', "r") + venues.each_line do |venue| + items << JSON.parse(venue) + end + + Soulmate::Loader.new('venues').load(items) + + matcher = Soulmate::Matcher.new('venues') + results = matcher.matches_for_term('stad', :limit => 5) + + assert_equal 3, results.size + assert_equal 'Angel Stadium', results[0]['term'] end end