| 1 | $:.unshift(File.dirname(__FILE__)) |
|---|
| 2 | require 'spec_helper' |
|---|
| 3 | require 'tmpdir' |
|---|
| 4 | require 'fileutils' |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | describe "MyHotEntry" do |
|---|
| 8 | def cache_filename |
|---|
| 9 | "#{File.basename(__FILE__, ".rb")}-#{$$}" |
|---|
| 10 | end |
|---|
| 11 | before(:each) do |
|---|
| 12 | fake_plugin(:my_hotentry) |
|---|
| 13 | @cache_path = File.join(Dir.tmpdir, cache_filename) |
|---|
| 14 | Dir.mkdir(@cache_path) |
|---|
| 15 | @dbfile = "#{@cache_path}/my_hotentry.dat" |
|---|
| 16 | @base_url = 'http://d.hatena.ne.jp/' |
|---|
| 17 | @hotentry = MyHotEntry.new(@dbfile) |
|---|
| 18 | end |
|---|
| 19 | |
|---|
| 20 | after(:each) do |
|---|
| 21 | FileUtils.rmtree(@cache_path) |
|---|
| 22 | end |
|---|
| 23 | |
|---|
| 24 | describe "#update" do |
|---|
| 25 | before do |
|---|
| 26 | @hotentry.update(@base_url) |
|---|
| 27 | @entries = @hotentry.entries |
|---|
| 28 | end |
|---|
| 29 | |
|---|
| 30 | it "キャッシュファイルが生成されていること" do |
|---|
| 31 | File.should be_file(@dbfile) |
|---|
| 32 | end |
|---|
| 33 | |
|---|
| 34 | it "人気の日記が取得できていること" do |
|---|
| 35 | @entries.size.should > 0 |
|---|
| 36 | end |
|---|
| 37 | |
|---|
| 38 | it "取得したエントリにbase_urlとタイトルが含まれていること" do |
|---|
| 39 | @entries.each do |entry| |
|---|
| 40 | entry[:url].should be_include(@base_url) |
|---|
| 41 | entry[:title].size.should > 0 |
|---|
| 42 | end |
|---|
| 43 | end |
|---|
| 44 | end |
|---|
| 45 | |
|---|
| 46 | describe "何度もupdateした場合" do |
|---|
| 47 | before do |
|---|
| 48 | @hotentry.update(@base_url) |
|---|
| 49 | @original_entry_size = @hotentry.entries.size |
|---|
| 50 | @hotentry.update(@base_url) |
|---|
| 51 | @entry_size = @hotentry.entries.size |
|---|
| 52 | end |
|---|
| 53 | |
|---|
| 54 | it "キャッシュサイズが大きくならないこと" do |
|---|
| 55 | @entry_size.should == @original_entry_size |
|---|
| 56 | end |
|---|
| 57 | end |
|---|
| 58 | |
|---|
| 59 | describe "取得結果が空の場合" do |
|---|
| 60 | before do |
|---|
| 61 | @exist_url = 'http://d.hatena.ne.jp/' |
|---|
| 62 | @empty_url = 'http://empty-url.example.com/' |
|---|
| 63 | end |
|---|
| 64 | |
|---|
| 65 | it "キャッシュをクリアしないこと" do |
|---|
| 66 | @hotentry.update(@empty_url) |
|---|
| 67 | @hotentry.entries.size.should == 0 |
|---|
| 68 | |
|---|
| 69 | @hotentry.update(@exist_url) |
|---|
| 70 | @hotentry.entries.size.should > 0 |
|---|
| 71 | exist_size = @hotentry.entries.size |
|---|
| 72 | |
|---|
| 73 | @hotentry.update(@empty_url) |
|---|
| 74 | @hotentry.entries.size.should == exist_size |
|---|
| 75 | end |
|---|
| 76 | end |
|---|
| 77 | end |
|---|