# -*- coding: utf-8 -*- # # class CommandTest # # Copyright (C) 2013-2016 Masafumi Yokoyama # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA require "fileutils" require "stringio" require "feedcellar/version" require "feedcellar/command" require "feedcellar/groonga_database" class CommandTest < Test::Unit::TestCase def setup @tmpdir = File.join(File.dirname(__FILE__), "tmp") FileUtils.rm_rf(@tmpdir) FileUtils.mkdir_p(@tmpdir) ENV["FEEDCELLAR_HOME"] = @tmpdir @command = Feedcellar::Command.new @database_dir = @command.database_dir rss = File.new(File.join(fixtures_dir, "feed.xml")) stub_request(:get, "http://localhost:20075/feed.xml"). to_return(:status => 200, :body => rss, :headers => {}) end def teardown FileUtils.rm_rf(@tmpdir) end def test_version s = "" io = StringIO.new(s) $stdout = io @command.version assert_equal("#{Feedcellar::VERSION}\n", s) $stdout = STDOUT end def test_command # confirm import command file = File.join(fixtures_dir, "subscriptions.xml") @command.import(file) @command.collect Feedcellar::GroongaDatabase.new.open(@database_dir) do |database| # NOTE: a tag of outline is not register. assert_equal(1, database.resources.size) assert_equal(3, database.feeds.count) end # confirm export command s = "" io = StringIO.new(s) $stdout = io @command.export assert_equal(1, s.scan(/ "http://my.today/atom", :title => "What is Today?", :link => "http://my.today/201501", :description => "January 1, 2015.", :date => Time.new(2015, 1, 1), }) @command.reset assert_equal(2015, @database.__send__(:feeds).first.year) end private def fixtures_dir File.join(File.dirname(__FILE__), "fixtures") end class RegisterTest < self def test_single @command.register("http://localhost:20075/feed.xml") Feedcellar::GroongaDatabase.new.open(@database_dir) do |database| assert_equal(1, database.resources.size) end end def test_multiple rss = File.new(File.join(fixtures_dir, "feed.xml")) stub_request(:get, "http://localhost:20076/feed.xml"). to_return(:status => 200, :body => rss, :headers => {}) @command.register("http://localhost:20075/feed.xml", "http://localhost:20076/feed.xml") Feedcellar::GroongaDatabase.new.open(@database_dir) do |database| assert_equal(2, database.resources.size) end end end class DeleteTest < self def setup super @command.register("http://localhost:20075/feed.xml") @command.collect end def teardown super $stdout = STDOUT end def test_by_link @str = "" io = StringIO.new(@str) $stdout = io @command.search("Alice") $stdout = STDOUT assert_equal(3, @str.lines.size) @command.delete("http://localhost:20075/article2.html") @str = "" io = StringIO.new(@str) $stdout = io @command.search("Alice") assert_equal(2, @str.lines.size) end def test_by_resource_key @str = "" io = StringIO.new(@str) $stdout = io @command.search("Alice") $stdout = STDOUT assert_equal(3, @str.lines.size) @command.delete(:resource_key => "localhost:20075/feed.xml") @str = "" io = StringIO.new(@str) $stdout = io @command.search("ruby") assert_equal(0, @str.lines.size) end end end