Skip to content

Commit

Permalink
Specs clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
dreikanter committed Aug 19, 2023
1 parent 7ceb505 commit 288e137
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
4 changes: 2 additions & 2 deletions spec/fixtures/files/feeds/monkeyuser/normalized.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"uid": "https://www.monkeyuser.com/2019/platypuscorn/",
"link": "https://www.monkeyuser.com/2019/platypuscorn/",
"published_at": "2019-08-20 00:00:00 +0000",
"text": "Platypuscorn - https://www.monkeyuser.com/2019/platypuscorn/ - https://www.monkeyuser.com/2019/platypuscorn/",
"text": "Platypuscorn - https://www.monkeyuser.com/2019/platypuscorn/",
"attachments": [
"https://www.monkeyuser.com/assets/images/2019/145-platypuscorn.png"
],
Expand All @@ -20,7 +20,7 @@
"uid": "https://www.monkeyuser.com/2019/brittle/",
"link": "https://www.monkeyuser.com/2019/brittle/",
"published_at": "2019-08-13 00:00:00 +0000",
"text": "Brittle - https://www.monkeyuser.com/2019/brittle/ - https://www.monkeyuser.com/2019/brittle/",
"text": "Brittle - https://www.monkeyuser.com/2019/brittle/",
"attachments": [
"https://www.monkeyuser.com/assets/images/2019/144-brittle.png"
],
Expand Down
32 changes: 16 additions & 16 deletions spec/normalizers/base_normalizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,86 @@
subject(:normalizer) { described_class }

let(:feed) { build(:feed) }
let(:entity) { FeedEntity.new(uid: "UID", content: "CONTENT", feed: feed) }
let(:feed_entity) { FeedEntity.new(uid: "UID", content: "CONTENT", feed: feed) }
let(:sample_errors) { ["sample error"] }

context "with basic concrete normalizer" do
let(:concrete_normalizer) { Class.new(subject) }
let(:concrete_normalizer) { Class.new(normalizer) }

it "returns normalized entity" do
expect(concrete_normalizer.call(entity)).to be_a(NormalizedEntity)
expect(concrete_normalizer.call(feed_entity)).to be_a(NormalizedEntity)
end
end

context "with non-valid entity" do
context "with non-valid feed_entity" do
let(:concrete_normalizer) do
Class.new(subject) do
Class.new(normalizer) do
define_method(:validation_errors) { ["ERROR"] }
end
end

it "returns normalized entity with validation errors" do
expect(concrete_normalizer.call(entity).validation_errors).to eq(["ERROR"])
expect(concrete_normalizer.call(feed_entity).validation_errors).to eq(["ERROR"])
end
end

context "with messy or blank attachment URLs" do
let(:concrete_normalizer) do
Class.new(subject) do
Class.new(normalizer) do
define_method(:attachments) { ["//example.com", "", nil] }
end
end

it "sanitizes attachment URLs" do
expect(concrete_normalizer.call(entity).attachments).to eq(["https://example.com"])
expect(concrete_normalizer.call(feed_entity).attachments).to eq(["https://example.com"])
end
end

context "when #attachments return non-valid URLs" do
let(:concrete_normalizer) do
Class.new(subject) do
Class.new(normalizer) do
define_method(:attachments) { [":"] }
end
end

it "raises an error" do
expect { concrete_normalizer.call(entity) }.to raise_error(StandardError)
expect { concrete_normalizer.call(feed_entity) }.to raise_error(StandardError)
end
end

context "when #attachments return non-array object" do
let(:concrete_normalizer) do
Class.new(subject) do
Class.new(normalizer) do
define_method(:attachments) { nil }
end
end

it "raises an error" do
expect { concrete_normalizer.call(entity) }.to raise_error(StandardError)
expect { concrete_normalizer.call(feed_entity) }.to raise_error(StandardError)
end
end

context "when #comments return non-array object" do
let(:concrete_normalizer) do
Class.new(subject) do
Class.new(normalizer) do
define_method(:comments) { nil }
end
end

it "raises an error" do
expect { concrete_normalizer.call(entity) }.to raise_error(StandardError)
expect { concrete_normalizer.call(feed_entity) }.to raise_error(StandardError)
end
end

context "with blank comments" do
let(:concrete_normalizer) do
Class.new(subject) do
Class.new(normalizer) do
define_method(:comments) { [nil, ""] }
end
end

it "skips blank comments" do
expect(concrete_normalizer.call(entity).comments).to be_blank
expect(concrete_normalizer.call(feed_entity).comments).to be_blank
end
end
end
10 changes: 6 additions & 4 deletions spec/services/pull_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def validation_errors
feed_id: feed.id,
uid: "https://example.com/0",
link: "https://example.com/0",
published_at: nil,
published_at: DateTime.now,
text: "Sample entity",
attachments: [],
comments: [],
Expand All @@ -61,7 +61,7 @@ def validation_errors
feed_id: feed.id,
uid: "https://example.com/0",
link: "https://example.com/0",
published_at: nil,
published_at: DateTime.now,
text: "Sample entity",
attachments: [],
comments: [],
Expand All @@ -71,7 +71,7 @@ def validation_errors
feed_id: feed.id,
uid: "https://example.com/1",
link: "https://example.com/1",
published_at: nil,
published_at: DateTime.now,
text: "Sample entity",
attachments: [],
comments: [],
Expand All @@ -81,7 +81,7 @@ def validation_errors
feed_id: feed.id,
uid: "https://example.com/2",
link: "https://example.com/2",
published_at: nil,
published_at: DateTime.now,
text: "",
attachments: [],
comments: [],
Expand Down Expand Up @@ -137,6 +137,8 @@ def validation_errors
JSON
end

before { freeze_time }

it "do the call" do
stub_feed_loader_request(feed_content)
expect(feed_entities).to eq(service.call(feed))
Expand Down

0 comments on commit 288e137

Please sign in to comment.