Skip to content

Commit

Permalink
Specs: add specs for StoryRepository
Browse files Browse the repository at this point in the history
Also adjust the way that factories are required and introduce a
`next_id` helper. There are still some untested methods in
StoryRepository which I will continue to work on in followups.
  • Loading branch information
mockdeep committed Feb 24, 2021
1 parent 58ea9fe commit f685e41
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
15 changes: 15 additions & 0 deletions spec/factories.rb
@@ -0,0 +1,15 @@
require_relative "factories/feed_factory"
require_relative "factories/story_factory"
require_relative "factories/user_factory"
require_relative "factories/group_factory"
require_relative "factories/feeds"
require_relative "factories/groups"
require_relative "factories/stories"
require_relative "factories/users"

module Factories
def next_id
@next_id ||= 0
@next_id += 1
end
end
2 changes: 1 addition & 1 deletion spec/factories/stories.rb
Expand Up @@ -4,6 +4,6 @@ def create_story(params = {})
end

def build_story(params = {})
Story.new(feed: build_feed, **params)
Story.new(entry_id: next_id, feed: build_feed, **params)
end
end
47 changes: 47 additions & 0 deletions spec/repositories/story_repository_spec.rb
@@ -1,4 +1,5 @@
require "spec_helper"
require "support/active_record"

app_require "repositories/story_repository"

Expand Down Expand Up @@ -35,6 +36,52 @@
end
end

describe ".fetch" do
it "finds the story by id" do
story = create_story

expect(StoryRepository.fetch(story.id)).to eq(story)
end
end

describe ".fetch_by_ids" do
it "finds all stories by id" do
story1 = create_story
story2 = create_story
expected_stories = [story1, story2]

actual_stories = StoryRepository.fetch_by_ids(expected_stories.map(&:id))

expect(actual_stories).to match_array(expected_stories)
end
end

describe ".fetch_unread_by_timestamp" do
it "returns unread stories from before the timestamp" do
story = create_story(created_at: 1.week.ago, is_read: false)

actual_stories = StoryRepository.fetch_unread_by_timestamp(4.days.ago)

expect(actual_stories).to eq([story])
end

it "does not return unread stories from after the timestamp" do
create_story(created_at: 3.days.ago, is_read: false)

actual_stories = StoryRepository.fetch_unread_by_timestamp(4.days.ago)

expect(actual_stories).to be_empty
end

it "does not return read stories from before the timestamp" do
create_story(created_at: 1.week.ago, is_read: true)

actual_stories = StoryRepository.fetch_unread_by_timestamp(4.days.ago)

expect(actual_stories).to be_empty
end
end

describe ".extract_url" do
it "returns the url" do
feed = double(url: "http://github.com")
Expand Down
9 changes: 1 addition & 8 deletions spec/spec_helper.rb
Expand Up @@ -9,14 +9,7 @@
require "date"

require_relative "support/coverage"
require "factories/feed_factory"
require "factories/story_factory"
require "factories/user_factory"
require "factories/group_factory"
require "factories/feeds"
require "factories/groups"
require "factories/stories"
require "factories/users"
require_relative "factories"

require "./app"

Expand Down

0 comments on commit f685e41

Please sign in to comment.