|
| 1 | +RSpec.describe Exports::ExportAdjustmentsCSVService do |
| 2 | + # Create organization after items to ensure proper associations |
| 3 | + let!(:item1) { create(:item, name: "item1") } |
| 4 | + let!(:item2) { create(:item, name: "item2") } |
| 5 | + let!(:item3) { create(:item, name: "item3") } |
| 6 | + let!(:item4) { create(:item, name: "item4") } |
| 7 | + let!(:item5) { create(:item, :inactive, name: "item5") } |
| 8 | + |
| 9 | + # Now create organization and associate items with it |
| 10 | + let(:organization) do |
| 11 | + org = create(:organization) |
| 12 | + [item1, item2, item3, item4, item5].each do |item| |
| 13 | + item.update!(organization_id: org.id) |
| 14 | + end |
| 15 | + org |
| 16 | + end |
| 17 | + |
| 18 | + let(:sorted_item_names) do |
| 19 | + [item1, item2, item3, item4, item5].map(&:name).sort |
| 20 | + end |
| 21 | + |
| 22 | + let(:storage_location) { create(:storage_location, organization: organization) } |
| 23 | + let(:user) { create(:user, organization: organization) } |
| 24 | + |
| 25 | + around do |example| |
| 26 | + travel_to Time.zone.local(2024, 12, 25) |
| 27 | + example.run |
| 28 | + travel_back |
| 29 | + end |
| 30 | + |
| 31 | + describe "#generate_csv_data" do |
| 32 | + subject { described_class.generate_csv(adjustments, organization) } |
| 33 | + |
| 34 | + context "with multiple adjustments and items" do |
| 35 | + let(:adjustments) do |
| 36 | + [ |
| 37 | + # 1st adjustment with 2 items |
| 38 | + create(:adjustment, |
| 39 | + user_id: user.id, |
| 40 | + storage_location: storage_location, |
| 41 | + organization: organization, |
| 42 | + comment: "adjustment 1", |
| 43 | + line_items_attributes: [ |
| 44 | + {item_id: item1.id, quantity: 10}, |
| 45 | + {item_id: item2.id, quantity: -5} |
| 46 | + ]), |
| 47 | + |
| 48 | + # 2nd adjustment with 1 item |
| 49 | + create(:adjustment, |
| 50 | + user_id: user.id, |
| 51 | + storage_location: storage_location, |
| 52 | + organization: organization, |
| 53 | + comment: "adjustment 2", |
| 54 | + line_items_attributes: [ |
| 55 | + {item_id: item3.id, quantity: 3} |
| 56 | + ]), |
| 57 | + |
| 58 | + # 3rd adjustment with the :with_items trait |
| 59 | + create(:adjustment, :with_items, |
| 60 | + user_id: user.id, |
| 61 | + storage_location: storage_location, |
| 62 | + organization: organization, |
| 63 | + comment: "adjustment 3", |
| 64 | + item: item1, |
| 65 | + item_quantity: 7) |
| 66 | + ] |
| 67 | + end |
| 68 | + |
| 69 | + it "should include the correct adjustment data" do |
| 70 | + csv = <<~CSV |
| 71 | + Created date,Storage Area,Comment,# of changes,item1,item2,item3,item4,item5 |
| 72 | + 2024-12-25,Smithsonian Conservation Center,adjustment 1,2,10,-5,0,0,0 |
| 73 | + 2024-12-25,Smithsonian Conservation Center,adjustment 2,1,0,0,3,0,0 |
| 74 | + 2024-12-25,Smithsonian Conservation Center,adjustment 3,1,7,0,0,0,0 |
| 75 | + CSV |
| 76 | + |
| 77 | + expect(subject).to eq(csv) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + context "when there are no adjustments" do |
| 82 | + let(:adjustments) { [] } |
| 83 | + |
| 84 | + it "returns only headers row" do |
| 85 | + csv = <<~CSV |
| 86 | + Created date,Storage Area,Comment,# of changes,item1,item2,item3,item4,item5 |
| 87 | + CSV |
| 88 | + |
| 89 | + expect(subject).to eq(csv) |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments