Skip to content

Commit 850a7d4

Browse files
authored
Specs: add test coverage for FeedRepository (stringer-rss#556)
* Add tests for `.fetch_by_ids`, `.delete`, `.list`, and `.in_group` * Rearrange test file to mirror ordering of class * Fix deprecation warning for `lower(name)` * Refactor `in_group` query to use AR syntax
1 parent b491326 commit 850a7d4

2 files changed

Lines changed: 78 additions & 17 deletions

File tree

app/repositories/feed_repository.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def self.set_status(status, feed)
3535
end
3636

3737
def self.list
38-
Feed.order("lower(name)")
38+
Feed.order(Feed.arel_table[:name].lower)
3939
end
4040

4141
def self.in_group
42-
Feed.where("group_id IS NOT NULL")
42+
Feed.where.not(group_id: nil)
4343
end
4444

4545
def self.valid_timestamp?(new_timestamp, current_timestamp)

spec/repositories/feed_repository_spec.rb

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,49 @@
44
app_require "repositories/feed_repository"
55

66
describe FeedRepository do
7+
describe ".fetch" do
8+
let(:feed) { Feed.new(id: 1) }
9+
10+
it "finds by id" do
11+
expect(Feed).to receive(:find).with(feed.id).and_return(feed)
12+
FeedRepository.fetch(feed.id)
13+
end
14+
15+
it "returns found feed" do
16+
allow(Feed).to receive(:find).with(feed.id).and_return(feed)
17+
18+
result = FeedRepository.fetch(feed.id)
19+
20+
expect(result).to eq feed
21+
end
22+
end
23+
24+
describe ".fetch_by_ids" do
25+
it "finds all feeds by id" do
26+
feeds = [create_feed, create_feed]
27+
28+
expect(FeedRepository.fetch_by_ids(feeds.map(&:id))).to match_array(feeds)
29+
end
30+
31+
it "does not find other feeds" do
32+
feed1 = create_feed
33+
create_feed
34+
35+
expect(FeedRepository.fetch_by_ids(feed1.id)).to eq([feed1])
36+
end
37+
end
38+
39+
describe ".update_feed" do
40+
it "saves the name and url" do
41+
feed = Feed.new
42+
43+
FeedRepository.update_feed(feed, "Test Feed", "example.com/feed")
44+
45+
expect(feed.name).to eq "Test Feed"
46+
expect(feed.url).to eq "example.com/feed"
47+
end
48+
end
49+
750
describe ".update_last_fetched" do
851
let(:timestamp) { Time.now }
952

@@ -43,31 +86,49 @@
4386
end
4487
end
4588

46-
describe ".update_feed" do
47-
it "saves the name and url" do
48-
feed = Feed.new
89+
describe ".delete" do
90+
it "deletes the feed by id" do
91+
feed = create_feed
4992

50-
FeedRepository.update_feed(feed, "Test Feed", "example.com/feed")
93+
FeedRepository.delete(feed.id)
5194

52-
expect(feed.name).to eq "Test Feed"
53-
expect(feed.url).to eq "example.com/feed"
95+
expect(Feed.unscoped.find_by(id: feed.id)).to be_nil
96+
end
97+
98+
it "does not delete other feeds" do
99+
feed1 = create_feed
100+
feed2 = create_feed
101+
102+
FeedRepository.delete(feed1.id)
103+
104+
expect(Feed.unscoped.find_by(id: feed2.id)).to eq(feed2)
54105
end
55106
end
56107

57-
describe "fetch" do
58-
let(:feed) { Feed.new(id: 1) }
108+
describe ".list" do
109+
it "returns all feeds ordered by name, case insensitive" do
110+
feed1 = create_feed(name: "foo")
111+
feed2 = create_feed(name: "Fabulous")
112+
feed3 = create_feed(name: "Zooby")
113+
feed4 = create_feed(name: "zabby")
59114

60-
it "finds by id" do
61-
expect(Feed).to receive(:find).with(feed.id).and_return(feed)
62-
FeedRepository.fetch(feed.id)
115+
expect(FeedRepository.list).to eq([feed2, feed1, feed4, feed3])
63116
end
117+
end
64118

65-
it "returns found feed" do
66-
allow(Feed).to receive(:find).with(feed.id).and_return(feed)
119+
describe ".in_group" do
120+
it "returns feeds that are in a group" do
121+
feed1 = create_feed(group_id: 5)
122+
feed2 = create_feed(group_id: 6)
67123

68-
result = FeedRepository.fetch(feed.id)
124+
expect(FeedRepository.in_group).to match_array([feed1, feed2])
125+
end
69126

70-
expect(result).to eq feed
127+
it "does not return feeds that are not in a group" do
128+
create_feed
129+
create_feed
130+
131+
expect(FeedRepository.in_group).to be_empty
71132
end
72133
end
73134
end

0 commit comments

Comments
 (0)