Update specs

This commit is contained in:
2023-02-18 16:53:09 +11:00
parent 0e6b21bd94
commit c629b49c2d
6 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
require "spec_helper"
RSpec.describe Adamantium::Commands::Posts::Delete, :db do
let(:post_repo) { spy(Adamantium::Repos::PostRepo) }
let(:subject) { described_class.new(post_repo: post_repo) }
it "deletes a post" do
url = "http://example.com/posts/slug-here"
subject.call(params: {url: url})
expect(post_repo).to have_received(:delete!).with("slug-here")
end
end

View File

@@ -0,0 +1,29 @@
require "spec_helper"
require "dry/monads"
RSpec.describe Adamantium::Commands::Posts::Syndicate do
include Dry::Monads[:result]
let(:settings) { double("settings", mastodon_server: "https://mastodon.example/@tester") }
let(:mastodon_client) { double("Adamantium::Client::Mastodon") }
let(:mastodon_syndicator) { Adamantium::Syndication::Mastodon.new(mastodon_client: mastodon_client) }
let(:pinboard_syndicator) { Adamantium::Syndication::Pinboard.new(api_key: "") }
let(:post) { {url: "example.com", syndicate_to: ["https://mastodon.example", "https://pinboard.in"]} }
subject { described_class.new(mastodon: mastodon_syndicator, pinboard: pinboard_syndicator, settings: settings) }
before do
allow(mastodon_client).to receive(:create_post).with(post: post, media_ids: nil).and_return(Success("http://mastodon.example/post/123"))
allow(pinboard_syndicator).to receive(:call).with(post: post).and_return(Success("http://pinboard.in/permalink"))
end
it "syndicates a post to Mastodon" do
result = subject.call(post)
expect(result).to be_success
expect(result.value!).to eq [
[:mastodon, "http://mastodon.example/post/123"],
[:pinboard, "http://pinboard.in/permalink"]
]
end
end

View File

@@ -0,0 +1,13 @@
require "spec_helper"
RSpec.describe Adamantium::Commands::Posts::Undelete, :db do
let(:post_repo) { spy(Adamantium::Repos::PostRepo) }
let(:subject) { described_class.new(post_repo: post_repo) }
it "deletes a post" do
url = "http://example.com/posts/slug-here"
subject.call(params: {url: url})
expect(post_repo).to have_received(:restore!).with("slug-here")
end
end

View File

@@ -0,0 +1,104 @@
require "spec_helper"
RSpec.describe Adamantium::Commands::Posts::Update, :db do
# Adding: add a property that didn't previously exist
# If there are any existing values for this property, they are not changed, the new values are added.
# If the property does not exist already, it is created.
# E.g. Adding tags or a syndication link after a post has been created
subject { described_class.new }
describe "add" do
let(:post) { Test::Factory[:post] }
let(:repo) { Adamantium::Container["repos.post_repo"] }
let(:params) {
{
url: "http://example.com/posts/#{post.slug}",
add: {
name: ["New Name"],
content: ["New Content"],
syndication: ["http://example.com"]
}
}
}
it "only adds new properties" do
subject.call(params: params)
updated_post = repo.fetch!(post.slug)
expect(updated_post.name).to eq(post.name)
expect(updated_post.content).to eq(post.content)
expect(updated_post.syndication_sources.values).to include "http://example.com"
end
end
# Replacing: Replace all values of the property. If the property does not exist already, it is created.
describe "replace" do
let(:post) { Test::Factory[:post] }
let(:repo) { Adamantium::Container["repos.post_repo"] }
let(:params) {
{
url: "http://example.com/posts/#{post.slug}",
replace: {
content: ["New Content"]
}
}
}
it "replaces existing properties" do
subject.call(params: params)
updated_post = repo.fetch!(post.slug)
expect(updated_post.name).to eq(post.name)
expect(updated_post.content).to eq("<p>New Content</p>\n")
end
end
# Remove: If the property exists, remove it. This completely removes the specified property.
describe "remove" do
let(:post1) { Test::Factory[:post] }
let(:post2) { Test::Factory[:post] }
let(:repo) { Adamantium::Container["repos.post_repo"] }
let(:complete_params) {
{
url: "http://example.com/posts/#{post1.slug}",
delete: ["category", "content"]
}
}
let(:partial_params) {
{
url: "http://example.com/posts/#{post2.slug}",
delete: {
category: ["tests"]
}
}
}
it "deletes a whole property" do
repo.tag_post(post_id: post1.id, tags: ["ruby", "tests"])
subject.call(params: complete_params)
updated_post = repo.fetch!(post1.slug)
expect(updated_post.tags.map(&:label)).to eq([])
expect(updated_post.content).to eq nil
end
it "deletes a partial property" do
repo.tag_post(post_id: post2.id, tags: ["ruby", "tests"])
subject.call(params: partial_params)
updated_post = repo.fetch!(post2.slug)
expect(updated_post.tags.map(&:label)).to eq ["ruby"]
end
end
end