Skip to content
Browse files

added tests for seed code

  • Loading branch information...
1 parent a3ec432 commit fb7592fc9d1bfb6d8a9e17ac40a7d5cbcfaa718c @zenhob committed May 17, 2012
Showing with 77 additions and 6 deletions.
  1. +6 −1 README.markdown
  2. +5 −5 lib/catptcha/seed.rb
  3. +6 −0 lib/tasks/catptcha.rake
  4. +60 −0 test/catptcha_seed_test.rb
View
7 README.markdown
@@ -9,7 +9,7 @@ This is still experimental and incomplete. Don't use it.
## Installation
-Add the gem to your Gemfile and optionally require the rake task.
+Add the gem to your Gemfile and require the rake task.
load 'tasks/catptcha.rake'
@@ -24,6 +24,11 @@ Seed the images:
rake catptcha:seed
+Run the tests:
+
+ rake db:test:prepare
+ rake catptcha:test
+
## Author
Copyright (c) 2012 ENTP, released under the MIT license
View
10 lib/catptcha/seed.rb
@@ -14,17 +14,17 @@ def flickr
@flickr ||= Flickr.new(Rails.root.join("config/flickr.yml").to_s)
end
- def update
+ def update silent=false
SEED_GROUP_KITTENS.each do |name, id|
Catptcha::PhotoGroup.create(:name => name.to_s, :flickr_group_id => id, :kittens => true)
end
SEED_GROUP_NOTKITTENS.each do |name, id|
Catptcha::PhotoGroup.create(:name => name.to_s, :flickr_group_id => id, :kittens => false)
end
Catptcha::PhotoGroup.all.each do |group|
- puts "updating photos for `#{group.name}`..."
+ puts "updating photos for `#{group.name}`..." unless silent
count = update_photos group
- puts "...done, #{count} photos imported"
+ puts "...done, #{count} photos imported" unless silent
end
end
@@ -45,11 +45,11 @@ def update_photos group
photo_count += 1
end
if total_pages.nil?
- total_pages = [result.pages, 4].min
+ total_pages = [result.pages, 5].min
else
params[:page] += 1
- break if params[:page] > total_pages
end
+ break if params[:page] >= total_pages
end
group.update_attributes(:photos_updated_at => DateTime.now.utc)
photo_count
View
6 lib/tasks/catptcha.rake
@@ -3,5 +3,11 @@ namespace :catptcha do
task :seed => :environment do
Catptcha::Seed.update
end
+
+ desc 'run catptcha plugin tests'
+ Rake::TestTask.new do |t|
+ t.test_files = FileList[File.dirname(__FILE__)+'/../../test/**/*_test.rb']
+ t.verbose = true
+ end
end
View
60 test/catptcha_seed_test.rb
@@ -0,0 +1,60 @@
+ENV['RAILS_ENV'] = 'test'
+require "./config/environment"
+require 'test/unit'
+require 'rr'
+
+class Test::Unit::TestCase
+ include RR::Adapters::RRMethods
+end
+
+class CatptchaSeedTest < Test::Unit::TestCase
+ def setup
+ Catptcha::PhotoGroup.delete_all
+ Catptcha::Photo.delete_all
+ end
+
+ def test_stub
+ setup_flickr_stub []
+ assert Catptcha::Seed.flickr.photos.search.empty?
+ end
+
+ def test_seed_with_no_photos
+ setup_flickr_stub []
+ Catptcha::Seed.update :silent
+ assert_equal 3, Catptcha::PhotoGroup.count
+ assert_equal 0, Catptcha::Photo.count
+ # doesn't recreate groups
+ Catptcha::Seed.update :silent
+ assert_equal 3, Catptcha::PhotoGroup.count
+ end
+
+ def test_seed_with_no_photos
+ setup_flickr_stub []
+ Catptcha::Seed.update :silent
+ assert_equal 3, Catptcha::PhotoGroup.count
+ assert_equal 0, Catptcha::Photo.count
+ # doesn't recreate groups
+ Catptcha::Seed.update :silent
+ assert_equal 3, Catptcha::PhotoGroup.count
+ end
+
+ def test_seed_with_photos
+ setup_flickr_stub [ get_flickr_photo_stub('foo') ]
+ Catptcha::Seed.update :silent
+ assert_equal 3, Catptcha::Photo.count
+ end
+
+ def get_flickr_photo_stub url
+ Object.new.tap do |photo|
+ stub(photo).url { url }
+ end
+ end
+
+ def setup_flickr_stub value
+ stub(value).pages { 1 }
+ flickr = Object.new
+ photos = Object.new
+ stub(flickr).photos { stub(photos).search { value } }
+ stub(Catptcha::Seed).flickr { flickr }
+ end
+end

0 comments on commit fb7592f

Please sign in to comment.
Something went wrong with that request. Please try again.