Skip to content

Commit

Permalink
Specs: add test coverage for FeedRepository
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mockdeep committed Mar 13, 2021
1 parent b491326 commit 9229228
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/repositories/feed_repository.rb
Expand Up @@ -35,11 +35,11 @@ def self.set_status(status, feed)
end

def self.list
Feed.order("lower(name)")
Feed.order(Feed.arel_table[:name].lower)
end

def self.in_group
Feed.where("group_id IS NOT NULL")
Feed.where.not(group_id: nil)
end

def self.valid_timestamp?(new_timestamp, current_timestamp)
Expand Down
91 changes: 76 additions & 15 deletions spec/repositories/feed_repository_spec.rb
Expand Up @@ -4,6 +4,49 @@
app_require "repositories/feed_repository"

describe FeedRepository do
describe ".fetch" do
let(:feed) { Feed.new(id: 1) }

it "finds by id" do
expect(Feed).to receive(:find).with(feed.id).and_return(feed)
FeedRepository.fetch(feed.id)
end

it "returns found feed" do
allow(Feed).to receive(:find).with(feed.id).and_return(feed)

result = FeedRepository.fetch(feed.id)

expect(result).to eq feed
end
end

describe ".fetch_by_ids" do
it "finds all feeds by id" do
feeds = [create_feed, create_feed]

expect(FeedRepository.fetch_by_ids(feeds.map(&:id))).to match_array(feeds)
end

it "does not find other feeds" do
feed1 = create_feed
create_feed

expect(FeedRepository.fetch_by_ids(feed1.id)).to eq([feed1])
end
end

describe ".update_feed" do
it "saves the name and url" do
feed = Feed.new

FeedRepository.update_feed(feed, "Test Feed", "example.com/feed")

expect(feed.name).to eq "Test Feed"
expect(feed.url).to eq "example.com/feed"
end
end

describe ".update_last_fetched" do
let(:timestamp) { Time.now }

Expand Down Expand Up @@ -43,31 +86,49 @@
end
end

describe ".update_feed" do
it "saves the name and url" do
feed = Feed.new
describe ".delete" do
it "deletes the feed by id" do
feed = create_feed

FeedRepository.update_feed(feed, "Test Feed", "example.com/feed")
FeedRepository.delete(feed.id)

expect(feed.name).to eq "Test Feed"
expect(feed.url).to eq "example.com/feed"
expect(Feed.unscoped.find_by(id: feed.id)).to be_nil
end

it "does not delete other feeds" do
feed1 = create_feed
feed2 = create_feed

FeedRepository.delete(feed1.id)

expect(Feed.unscoped.find_by(id: feed2.id)).to eq(feed2)
end
end

describe "fetch" do
let(:feed) { Feed.new(id: 1) }
describe ".list" do
it "returns all feeds ordered by name, case insensitive" do
feed1 = create_feed(name: "foo")
feed2 = create_feed(name: "Fabulous")
feed3 = create_feed(name: "Zooby")
feed4 = create_feed(name: "zabby")

it "finds by id" do
expect(Feed).to receive(:find).with(feed.id).and_return(feed)
FeedRepository.fetch(feed.id)
expect(FeedRepository.list).to eq([feed2, feed1, feed4, feed3])
end
end

it "returns found feed" do
allow(Feed).to receive(:find).with(feed.id).and_return(feed)
describe ".in_group" do
it "returns feeds that are in a group" do
feed1 = create_feed(group_id: 5)
feed2 = create_feed(group_id: 6)

result = FeedRepository.fetch(feed.id)
expect(FeedRepository.in_group).to match_array([feed1, feed2])
end

expect(result).to eq feed
it "does not return feeds that are not in a group" do
create_feed
create_feed

expect(FeedRepository.in_group).to be_empty
end
end
end

0 comments on commit 9229228

Please sign in to comment.