Skip to content

Commit 8109e7e

Browse files
committed
Add tests for read operations
1 parent e54c746 commit 8109e7e

File tree

11 files changed

+231
-7
lines changed

11 files changed

+231
-7
lines changed

app/fever_api/authentication.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
module FeverAPI
22
class Authentication
3+
def initialize(options = {})
4+
@clock = options.fetch(:clock){ Time }
5+
end
6+
37
def call(params)
4-
{ auth: 1, last_refreshed_on_time: Time.now.to_i }
8+
{ auth: 1, last_refreshed_on_time: @clock.now.to_i }
59
end
610
end
711
end

app/fever_api/read_feeds.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
module FeverAPI
44
class ReadFeeds
5+
def initialize(options = {})
6+
@feed_repository = options.fetch(:feed_repository){ FeedRepository }
7+
end
8+
59
def call(params)
610
if params.keys.include?('feeds')
711
{ feeds: feeds }
@@ -13,7 +17,7 @@ def call(params)
1317
private
1418

1519
def feeds
16-
FeedRepository.list.map{|f| f.as_fever_json}
20+
@feed_repository.list.map{|f| f.as_fever_json }
1721
end
1822
end
1923
end

app/fever_api/read_feeds_groups.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
require_relative "../models/feed"
1+
require_relative "../repositories/feed_repository"
22

33
module FeverAPI
44
class ReadFeedsGroups
5+
def initialize(options = {})
6+
@feed_repository = options.fetch(:feed_repository){ FeedRepository }
7+
end
8+
59
def call(params)
610
if params.keys.include?('feeds') || params.keys.include?('groups')
711
{ feeds_groups: feeds_groups }
@@ -16,9 +20,13 @@ def feeds_groups
1620
[
1721
{
1822
group_id: 1,
19-
feed_ids: Feed.all.map{|f| f.id}.join(",")
23+
feed_ids: feeds.map{|f| f.id}.join(",")
2024
}
2125
]
2226
end
27+
28+
def feeds
29+
@feed_repository.list
30+
end
2331
end
2432
end

app/fever_api/read_items.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
module FeverAPI
44
class ReadItems
5+
def initialize(options = {})
6+
@story_repository = options.fetch(:story_repository){ StoryRepository }
7+
end
8+
59
def call(params)
610
if params.keys.include?('items')
711
item_ids = params[:with_ids].split(',') rescue nil
@@ -28,14 +32,14 @@ def total_items(item_ids)
2832
end
2933

3034
def stories_by_ids(ids)
31-
StoryRepository.fetch_by_ids(ids)
35+
@story_repository.fetch_by_ids(ids)
3236
end
3337

3438
def unread_stories(since_id = nil)
3539
if since_id
36-
StoryRepository.unread_since_id(since_id)
40+
@story_repository.unread_since_id(since_id)
3741
else
38-
StoryRepository.unread
42+
@story_repository.unread
3943
end
4044
end
4145
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require "spec_helper"
2+
3+
app_require "fever_api/authentication"
4+
5+
describe FeverAPI::Authentication do
6+
it "returns a hash with keys :auth and :last_refreshed_on_time" do
7+
fake_clock = double('clock')
8+
fake_clock.should_receive(:now).and_return(1234567890)
9+
result = FeverAPI::Authentication.new(clock: fake_clock).call(mock())
10+
result.should == { auth: 1, last_refreshed_on_time: 1234567890 }
11+
end
12+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "spec_helper"
2+
3+
app_require "fever_api/read_favicons"
4+
5+
describe FeverAPI::ReadFavicons do
6+
subject { FeverAPI::ReadFavicons.new }
7+
8+
it "returns a fixed icon list if requested" do
9+
subject.call('favicons' => nil).should == {
10+
favicons: [
11+
{
12+
id: 0,
13+
data: "image/gif;base64,R0lGODlhAQABAIAAAObm5gAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
14+
}
15+
]
16+
}
17+
end
18+
19+
it "returns an empty hash otherwise" do
20+
subject.call({}).should == {}
21+
end
22+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require "spec_helper"
2+
3+
app_require "fever_api/read_feeds_groups"
4+
5+
describe FeverAPI::ReadFeedsGroups do
6+
let(:feed_ids) { [5, 7, 11] }
7+
let(:feeds) { feed_ids.map{|id| double('feed', id: id) } }
8+
let(:feed_repository) { double('repo') }
9+
10+
subject do
11+
FeverAPI::ReadFeedsGroups.new(feed_repository: feed_repository)
12+
end
13+
14+
it "returns a list of groups requested through feeds" do
15+
feed_repository.should_receive(:list).and_return(feeds)
16+
subject.call('feeds' => nil).should == {
17+
feeds_groups: [
18+
{
19+
group_id: 1,
20+
feed_ids: feed_ids.join(',')
21+
}
22+
]
23+
}
24+
end
25+
26+
it "returns a list of groups requested through groups" do
27+
feed_repository.should_receive(:list).and_return(feeds)
28+
subject.call('groups' => nil).should == {
29+
feeds_groups: [
30+
{
31+
group_id: 1,
32+
feed_ids: feed_ids.join(',')
33+
}
34+
]
35+
}
36+
end
37+
38+
it "returns an empty hash otherwise" do
39+
subject.call({}).should == {}
40+
end
41+
end

spec/fever_api/read_feeds_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "spec_helper"
2+
3+
app_require "fever_api/read_feeds"
4+
5+
describe FeverAPI::ReadFeeds do
6+
let(:feed_ids) { [5, 7, 11] }
7+
let(:feeds) { feed_ids.map{|id| double('feed', id: id, as_fever_json: { id: id } ) } }
8+
let(:feed_repository) { double('repo') }
9+
10+
subject do
11+
FeverAPI::ReadFeeds.new(feed_repository: feed_repository)
12+
end
13+
14+
it "returns a list of feeds" do
15+
feed_repository.should_receive(:list).and_return(feeds)
16+
subject.call('feeds' => nil).should == {
17+
feeds: [
18+
{ id: 5 },
19+
{ id: 7 },
20+
{ id: 11 }
21+
]
22+
}
23+
end
24+
25+
it "returns an empty hash otherwise" do
26+
subject.call({}).should == {}
27+
end
28+
end

spec/fever_api/read_groups_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "spec_helper"
2+
3+
app_require "fever_api/read_groups"
4+
5+
describe FeverAPI::ReadGroups do
6+
subject { FeverAPI::ReadGroups.new }
7+
8+
it "returns a fixed group list if requested" do
9+
subject.call('groups' => nil).should == {
10+
groups: [
11+
{
12+
id: 1,
13+
title: "All items"
14+
}
15+
]
16+
}
17+
end
18+
19+
it "returns an empty hash otherwise" do
20+
subject.call({}).should == {}
21+
end
22+
end

spec/fever_api/read_items_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require "spec_helper"
2+
3+
app_require "fever_api/read_items"
4+
5+
describe FeverAPI::ReadItems do
6+
let(:story_repository) { double('repo') }
7+
8+
subject do
9+
FeverAPI::ReadItems.new(story_repository: story_repository)
10+
end
11+
12+
it "returns a list of unread items including total count" do
13+
story_repository.should_receive(:unread).twice.and_return([
14+
double('story', as_fever_json: { id: 5 } ),
15+
double('story', as_fever_json: { id: 7 } ),
16+
double('story', as_fever_json: { id: 11 } )
17+
])
18+
subject.call('items' => nil).should == {
19+
items: [
20+
{ id: 5 },
21+
{ id: 7 },
22+
{ id: 11 }
23+
],
24+
total_items: 3
25+
}
26+
end
27+
28+
it "returns a list of unread items since id including total count" do
29+
story_repository.should_receive(:unread_since_id).with(3).and_return([
30+
double('story', as_fever_json: { id: 5 } ),
31+
double('story', as_fever_json: { id: 7 } ),
32+
])
33+
story_repository.should_receive(:unread).and_return([
34+
double('story', as_fever_json: { id: 2 } ),
35+
double('story', as_fever_json: { id: 5 } ),
36+
double('story', as_fever_json: { id: 7 } ),
37+
])
38+
subject.call('items' => nil, since_id: 3).should == {
39+
items: [
40+
{ id: 5 },
41+
{ id: 7 },
42+
],
43+
total_items: 3
44+
}
45+
end
46+
47+
it "returns a list of specified items including total count" do
48+
story_repository.should_receive(:fetch_by_ids).with(['5', '11']).twice.and_return([
49+
double('story', as_fever_json: { id: 5 } ),
50+
double('story', as_fever_json: { id: 11 } )
51+
])
52+
subject.call('items' => nil, with_ids: '5,11').should == {
53+
items: [
54+
{ id: 5 },
55+
{ id: 11 }
56+
],
57+
total_items: 2
58+
}
59+
end
60+
61+
it "returns an empty hash otherwise" do
62+
subject.call({}).should == {}
63+
end
64+
end

0 commit comments

Comments
 (0)