From c629b49c2d191c4535a18066d2744c1c399f73c4 Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Sat, 18 Feb 2023 16:53:09 +1100 Subject: [PATCH] Update specs --- .../posts}/add_syndication_source_spec.rb | 0 .../unit/commands/posts/delete_spec.rb | 13 +++ .../unit/commands/posts/syndicate_spec.rb | 29 +++++ .../unit/commands/posts/undelete_spec.rb | 13 +++ .../unit/commands/posts/update_spec.rb | 104 ++++++++++++++++++ spec/syndication/{ => unit}/mastodon_spec.rb | 0 6 files changed, 159 insertions(+) rename spec/adamantium/unit/{ => commands/posts}/add_syndication_source_spec.rb (100%) create mode 100644 spec/adamantium/unit/commands/posts/delete_spec.rb create mode 100644 spec/adamantium/unit/commands/posts/syndicate_spec.rb create mode 100644 spec/adamantium/unit/commands/posts/undelete_spec.rb create mode 100644 spec/adamantium/unit/commands/posts/update_spec.rb rename spec/syndication/{ => unit}/mastodon_spec.rb (100%) diff --git a/spec/adamantium/unit/add_syndication_source_spec.rb b/spec/adamantium/unit/commands/posts/add_syndication_source_spec.rb similarity index 100% rename from spec/adamantium/unit/add_syndication_source_spec.rb rename to spec/adamantium/unit/commands/posts/add_syndication_source_spec.rb diff --git a/spec/adamantium/unit/commands/posts/delete_spec.rb b/spec/adamantium/unit/commands/posts/delete_spec.rb new file mode 100644 index 0000000..97c2f9a --- /dev/null +++ b/spec/adamantium/unit/commands/posts/delete_spec.rb @@ -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 diff --git a/spec/adamantium/unit/commands/posts/syndicate_spec.rb b/spec/adamantium/unit/commands/posts/syndicate_spec.rb new file mode 100644 index 0000000..00a0e83 --- /dev/null +++ b/spec/adamantium/unit/commands/posts/syndicate_spec.rb @@ -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 diff --git a/spec/adamantium/unit/commands/posts/undelete_spec.rb b/spec/adamantium/unit/commands/posts/undelete_spec.rb new file mode 100644 index 0000000..01f9520 --- /dev/null +++ b/spec/adamantium/unit/commands/posts/undelete_spec.rb @@ -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 diff --git a/spec/adamantium/unit/commands/posts/update_spec.rb b/spec/adamantium/unit/commands/posts/update_spec.rb new file mode 100644 index 0000000..5dc7305 --- /dev/null +++ b/spec/adamantium/unit/commands/posts/update_spec.rb @@ -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("

New Content

\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 diff --git a/spec/syndication/mastodon_spec.rb b/spec/syndication/unit/mastodon_spec.rb similarity index 100% rename from spec/syndication/mastodon_spec.rb rename to spec/syndication/unit/mastodon_spec.rb