|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +RSpec.describe ProcessSingleSiteJob do |
| 4 | + describe "#perform" do |
| 5 | + subject(:run_process_single_site_job) { described_class.new.perform(site_data, team.id, extra_tag_ids) } |
| 6 | + |
| 7 | + let(:team) { create(:team) } |
| 8 | + let(:site_data) do |
| 9 | + { |
| 10 | + "url" => "https://example.com/", |
| 11 | + "name" => "New Name", |
| 12 | + "tag_names" => ["tag_1", "tag_2"] |
| 13 | + } |
| 14 | + end |
| 15 | + let(:extra_tag_ids) { [] } |
| 16 | + |
| 17 | + context "when the site does not exist" do |
| 18 | + it "creates a new site" do |
| 19 | + expect { run_process_single_site_job }.to change(Site, :count).by(1) |
| 20 | + end |
| 21 | + |
| 22 | + it "sets the correct attributes" do |
| 23 | + run_process_single_site_job |
| 24 | + |
| 25 | + site = Site.last |
| 26 | + expect(site.url).to eq("https://example.com/") |
| 27 | + expect(site.name).to eq("New Name") |
| 28 | + expect(site.team).to eq(team) |
| 29 | + end |
| 30 | + |
| 31 | + it "creates and associates the tags" do |
| 32 | + run_process_single_site_job |
| 33 | + |
| 34 | + site = Site.last |
| 35 | + expect(site.tags.map(&:name)).to contain_exactly("tag_1", "tag_2") |
| 36 | + end |
| 37 | + |
| 38 | + context "with extra tags provided" do |
| 39 | + let(:extra_tag) { create(:tag, team: team, name: "extra_tag") } |
| 40 | + let(:extra_tag_ids) { [extra_tag.id] } |
| 41 | + |
| 42 | + it "associates both CSV tags and extra tags" do |
| 43 | + run_process_single_site_job |
| 44 | + |
| 45 | + expect(Site.last.tags.map(&:name)).to contain_exactly("tag_1", "tag_2", "extra_tag") |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context "when the name is missing in the CSV" do |
| 50 | + let(:site_data) { { "url" => "https://example.com/", "tag_names" => [] } } |
| 51 | + |
| 52 | + it "creates the site without a name" do |
| 53 | + run_process_single_site_job |
| 54 | + |
| 55 | + expect(Site.last.name).to be_nil |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + context "when the site already exists" do |
| 61 | + let!(:site) { create(:site, team: team, url: "https://example.com/", name: "Original Name") } |
| 62 | + let!(:existing_tag) { create(:tag, team: team, name: "existing_tag") } |
| 63 | + |
| 64 | + before do |
| 65 | + site.tags << existing_tag |
| 66 | + end |
| 67 | + |
| 68 | + it "does not create a new site" do |
| 69 | + expect { run_process_single_site_job }.not_to change(Site, :count) |
| 70 | + end |
| 71 | + |
| 72 | + it "creates only new tags" do |
| 73 | + expect { run_process_single_site_job }.to change(Tag, :count).by(2) |
| 74 | + end |
| 75 | + |
| 76 | + it "merges the new tags with the existing ones" do |
| 77 | + run_process_single_site_job |
| 78 | + |
| 79 | + expect(site.reload.tags.map(&:name)).to contain_exactly("existing_tag", "tag_1", "tag_2") |
| 80 | + end |
| 81 | + |
| 82 | + context "when the site name is blank" do |
| 83 | + before { site.update!(name: nil) } |
| 84 | + |
| 85 | + it "updates the name from the CSV" do |
| 86 | + expect { run_process_single_site_job }.to change { site.reload.name }.from(nil).to("New Name") |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + context "when the site name is already present" do |
| 91 | + it "does not overwrite the existing name" do |
| 92 | + expect { run_process_single_site_job }.not_to change { site.reload.name } |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + context "with duplicate tags" do |
| 98 | + let(:site_data) do |
| 99 | + { |
| 100 | + "url" => "https://example.com/", |
| 101 | + "tag_names" => ["tag", "tag"] |
| 102 | + } |
| 103 | + end |
| 104 | + let(:duplicate_tag) { create(:tag, team: team, name: "tag") } |
| 105 | + let(:extra_tag_ids) { [duplicate_tag.id] } |
| 106 | + |
| 107 | + it "deduplicates the tags" do |
| 108 | + run_process_single_site_job |
| 109 | + |
| 110 | + expect(Site.last.tags.map(&:name)).to contain_exactly("tag") |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments