Permalink
Browse files

Added tests. (For now, we need to run `PADRINO_ENV=test padrino rake …

…import` first then `padrino rake test`.)
  • Loading branch information...
1 parent d0a8146 commit 4a883dcbabc561893a5d0c598cb4a8b78bc1d66f @fredwu committed Jan 5, 2011
Showing with 216 additions and 0 deletions.
  1. +11 −0 test/functional/heroes_test.rb
  2. +6 −0 test/test.rake
  3. +24 −0 test/test_config.rb
  4. +122 −0 test/unit/heroes_test.rb
  5. +53 −0 test/unit/ruby-committers.yml
@@ -0,0 +1,11 @@
+# coding: utf-8
+
+require File.expand_path(File.dirname(__FILE__) + '/../test_config.rb')
+
+class HeroTest < Test::Unit::TestCase
+ def test_get_index
+ get '/'
+ assert last_response.ok?
+ assert last_response.body.include?('Matz')
+ end
+end
View
@@ -0,0 +1,6 @@
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |test|
+ test.pattern = 'test/**/*_test.rb'
+ test.verbose = true
+end
View
@@ -0,0 +1,24 @@
+require 'test/unit'
+
+PADRINO_ENV = 'test' unless defined?(PADRINO_ENV)
+require File.expand_path(File.dirname(__FILE__) + "/../config/boot")
+
+class Test::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ RubyCommitters
+ end
+
+ protected
+
+ def assert_difference(expressions, difference = 1, message = nil, &block)
+ expression_evaluations = Array(expressions).collect{ |expression| lambda { eval(expression, block.send(:binding)) } }
+
+ original_values = expression_evaluations.inject([]) { |memo, expression| memo << expression.call }
+ yield
+ expression_evaluations.each_with_index do |expression, i|
+ assert_equal original_values[i] + difference, expression.call, message
+ end
+ end
+end
@@ -0,0 +1,122 @@
+# coding: utf-8
+
+require File.expand_path(File.dirname(__FILE__) + '/../test_config.rb')
+
+class HeroTest < Test::Unit::TestCase
+ def setup
+ @matz ||= Hero.first
+ @yml ||= File.join(File.expand_path(File.dirname(__FILE__)), 'ruby-committers.yml')
+ end
+
+ def test_has_many_names
+ assert_operator 3, :<=, @matz.names.length
+ end
+
+ def test_has_many_nicks
+ assert_operator 1, :<=, @matz.nicks.length
+ end
+
+ def test_has_many_sites
+ assert_operator 1, :<=, @matz.sites.length
+ end
+
+ def test_has_many_services
+ assert_operator 5, :<=, @matz.services.length
+ end
+
+ def test_has_many_portraits
+ assert_operator 1, :<=, @matz.portraits.length
+ end
+
+ def test_has_many_books
+ assert_operator 5, :<=, @matz.books.length
+ end
+
+ def test_import_heroes
+ assert_difference('Hero.count', 3) do
+ File.open(@yml, 'rb') { |f| Hero.import f }
+ end
+ end
+
+ def test_imports_names
+ assert_difference('Name.count', 6) do
+ File.open(@yml, 'rb') { |f| Hero.import f }
+ end
+
+ hero = Hero.find_by_username('H_Konishi')
+ assert_equal ['小西弘将', 'KONISHI Hiromasa'].sort, hero.names.map { |n|
+ n.value
+ }.sort
+ end
+
+ def test_imports_nicks
+ assert_difference('Nick.count', 3) do
+ File.open(@yml, 'rb') { |f| Hero.import f }
+ end
+
+ hero = Hero.find_by_username('H_Konishi')
+ assert_equal [], hero.nicks
+
+ hero = Hero.find_by_username('aamine')
+ assert_equal ['青木さん'], hero.nicks.map { |x| x.value }
+ end
+
+ def test_imports_sites
+ assert_difference('Site.count', 5) do
+ File.open(@yml, 'rb') { |f| Hero.import f }
+ end
+
+ hero = Hero.find_by_username('aamine')
+ assert_equal 2, hero.sites.length
+
+ site = hero.sites.find_by_title('LoveRubyNet')
+ assert_nil site.lang
+ assert_nil site.feed
+ assert_equal 'http://i.loveruby.net/', site.url
+
+ site = hero.sites.find_by_title('青木日記')
+ assert_equal 'http://i.loveruby.net/d/', site.url
+ assert_equal 'ja', site.lang
+ assert_equal 'http://i.loveruby.net/d/index.rdf', site.feed
+ end
+
+ def test_imports_portraits
+ assert_difference('Portrait.count', 2) do
+ File.open(@yml, 'rb') { |f| Hero.import f }
+ end
+
+ hero = Hero.find_by_username 'aamine'
+ assert_equal 'http://jp.rubyist.net/magazine/?c=plugin;plugin=attach_download;p=0017-Hotlinks;file_name=aoki1.jpg', hero.portraits.first.url
+ end
+
+ def test_imports_services
+ assert_difference('Service.count', 4) do
+ File.open(@yml, 'rb') { |f| Hero.import f }
+ end
+
+ hero = Hero.find_by_username 'akira'
+ assert_equal 4, hero.services.count
+
+ assert_equal [
+ ['twitter', 'arika'],
+ ['friendfeed', 'arika'],
+ ['iddy', 'arika'],
+ ['mixi', '1549'],
+ ].sort, hero.services.map { |x| [x.name, x.key] }.sort
+ end
+
+ def test_imports_books
+ assert_difference('Book.count', 7) do
+ File.open(@yml, 'rb') { |f| Hero.import f }
+ end
+
+ hero = Hero.find_by_username 'aamine'
+ assert_equal %w{
+ 9784844317210
+ 9784797340044
+ 9784756137098
+ 9784797324297
+ 9784839923204
+ }.sort, hero.books.map { |x| x.isbn }.sort
+ end
+end
@@ -0,0 +1,53 @@
+- hero: H_Konishi
+ name:
+ - 小西弘将
+ - KONISHI Hiromasa
+ sites:
+ - title: '小西の物置'
+ url: 'http://www001.upp.so-net.ne.jp/konishi/'
+- hero: aamine
+ name:
+ - 青木峰郎
+ - Minero Aoki
+ nick:
+ - 青木さん
+ sites:
+ - title: LoveRubyNet
+ url: 'http://i.loveruby.net/'
+ - title: 青木日記
+ url: 'http://i.loveruby.net/d/'
+ lang: ja
+ feed: 'http://i.loveruby.net/d/index.rdf'
+ portraits:
+ - 'http://jp.rubyist.net/magazine/?c=plugin;plugin=attach_download;p=0017-Hotlinks;file_name=aoki1.jpg'
+ ruby-books:
+ - '9784844317210'
+ - '9784797340044'
+ - '9784756137098'
+ - '9784797324297'
+ - '9784839923204'
+- hero: akira
+ name:
+ - やまだあきら
+ - akira yamada
+ nick:
+ - ay
+ - やまださん
+ sites:
+ - title: やまだあきらのサイト
+ url: 'http://arika.org/'
+ lang: ja
+ - title: '\ay diary'
+ url: 'http://arika.org/diary/'
+ lang: ja
+ feed: 'http://arika.org/diary/articles.rss'
+ portraits:
+ - 'http://iddy.jp/userphoto/5baa27c3da176d038ce96ca4baacc7b1c2a66a0f/me96x96s.jpg'
+ services:
+ twitter: arika
+ friendfeed: arika
+ iddy: arika
+ mixi: 1549
+ ruby-books:
+ - '9784274064616'
+ - '9784900813250'

0 comments on commit 4a883dc

Please sign in to comment.