|
| 1 | +RSpec.describe Floe::Workflow::ItemBatcher do |
| 2 | + let(:subject) { described_class.new(payload, ["Map", "ItemBatcher"]) } |
| 3 | + |
| 4 | + describe "#initialize" do |
| 5 | + context "with no MaxItems or MaxInputBytes" do |
| 6 | + let(:payload) { {} } |
| 7 | + |
| 8 | + it "raises an exception" do |
| 9 | + expect { subject } |
| 10 | + .to raise_error( |
| 11 | + Floe::InvalidWorkflowError, |
| 12 | + "Map.ItemBatcher must have one of \"MaxItemsPerBatch\", \"MaxItemsPerBatchPath\"" |
| 13 | + ) |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + context "with a BatchInput field" do |
| 18 | + let(:payload) { {"BatchInput" => "foo", "MaxItemsPerBatch" => 10} } |
| 19 | + |
| 20 | + it "returns an ItemBatcher" do |
| 21 | + expect(subject).to be_kind_of(described_class) |
| 22 | + end |
| 23 | + |
| 24 | + it "sets the BatchInput to a PayloadTemplate" do |
| 25 | + expect(subject.batch_input).to be_kind_of(Floe::Workflow::PayloadTemplate) |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + context "with MaxItemsPerBatch" do |
| 30 | + let(:payload) { {"MaxItemsPerBatch" => 10} } |
| 31 | + |
| 32 | + it "returns an ItemBatcher" do |
| 33 | + expect(subject).to be_kind_of(described_class) |
| 34 | + end |
| 35 | + |
| 36 | + it "sets max_items_per_batch" do |
| 37 | + expect(subject.max_items_per_batch).to eq(payload["MaxItemsPerBatch"]) |
| 38 | + end |
| 39 | + |
| 40 | + context "that is an invalid value" do |
| 41 | + it "raises an exception" do |
| 42 | + expect { described_class.new({"MaxItemsPerBatch" => 0}, ["Map", "ItemBatcher"]) } |
| 43 | + .to raise_error(Floe::InvalidWorkflowError, "Map.ItemBatcher field \"MaxItemsPerBatch\" value \"0\" must be a positive integer") |
| 44 | + expect { described_class.new({"MaxItemsPerBatch" => -1}, ["Map", "ItemBatcher"]) } |
| 45 | + .to raise_error(Floe::InvalidWorkflowError, "Map.ItemBatcher field \"MaxItemsPerBatch\" value \"-1\" must be a positive integer") |
| 46 | + expect { described_class.new({"MaxItemsPerBatch" => 2.5}, ["Map", "ItemBatcher"]) } |
| 47 | + .to raise_error(Floe::InvalidWorkflowError, "Map.ItemBatcher field \"MaxItemsPerBatch\" value \"2.5\" must be a positive integer") |
| 48 | + expect { described_class.new({"MaxItemsPerBatch" => "1"}, ["Map", "ItemBatcher"]) } |
| 49 | + .to raise_error(Floe::InvalidWorkflowError, "Map.ItemBatcher field \"MaxItemsPerBatch\" value \"1\" must be a positive integer") |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + context "with MaxInputBytesPerBatch" do |
| 55 | + let(:payload) { {"MaxInputBytesPerBatch" => 1_024} } |
| 56 | + |
| 57 | + it "returns an ItemBatcher" do |
| 58 | + pending "implement MaxInputBytesPerBatch" |
| 59 | + expect(subject).to be_kind_of(described_class) |
| 60 | + end |
| 61 | + |
| 62 | + it "sets max_input_bytes_per_batch" do |
| 63 | + pending "implement MaxInputBytesPerBatch" |
| 64 | + |
| 65 | + expect(subject.max_input_bytes_per_batch).to eq(payload["MaxInputBytesPerBatch"]) |
| 66 | + end |
| 67 | + |
| 68 | + context "that is an invalid value" do |
| 69 | + it "raises an exception" do |
| 70 | + pending "implement MaxInputBytesPerBatch" |
| 71 | + |
| 72 | + expect { described_class.new({"MaxInputBytesPerBatch" => 0}, ["Map", "ItemBatcher"]) } |
| 73 | + .to raise_error(Floe::InvalidWorkflowError, "Map.ItemBatcher field \"MaxInputBytesPerBatch\" value \"0\" must be a positive integer") |
| 74 | + expect { described_class.new({"MaxInputBytesPerBatch" => -1}, ["Map", "ItemBatcher"]) } |
| 75 | + .to raise_error(Floe::InvalidWorkflowError, "Map.ItemBatcher field \"MaxInputBytesPerBatch\" value \"-1\" must be a positive integer") |
| 76 | + expect { described_class.new({"MaxInputBytesPerBatch" => 2.5}, ["Map", "ItemBatcher"]) } |
| 77 | + .to raise_error(Floe::InvalidWorkflowError, "Map.ItemBatcher field \"MaxInputBytesPerBatch\" value \"2.5\" must be a positive integer") |
| 78 | + expect { described_class.new({"MaxInputBytesPerBatch" => "1"}, ["Map", "ItemBatcher"]) } |
| 79 | + .to raise_error(Floe::InvalidWorkflowError, "Map.ItemBatcher field \"MaxInputBytesPerBatch\" value \"1\" must be a positive integer") |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + context "with MaxItemsPerBatchPath" do |
| 85 | + let(:payload) { {"MaxItemsPerBatchPath" => "$.maxBatchItems"} } |
| 86 | + |
| 87 | + it "returns an ItemBatcher" do |
| 88 | + expect(subject).to be_kind_of(described_class) |
| 89 | + end |
| 90 | + |
| 91 | + it "sets max_items_per_batch_path" do |
| 92 | + expect(subject.max_items_per_batch_path).to be_kind_of(Floe::Workflow::ReferencePath) |
| 93 | + expect(subject.max_items_per_batch_path).to have_attributes(:path => ["maxBatchItems"]) |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + context "with MaxInputBytesPerBatchPath" do |
| 98 | + let(:payload) { {"MaxInputBytesPerBatchPath" => "$.batchSize"} } |
| 99 | + |
| 100 | + it "returns an ItemBatcher" do |
| 101 | + pending "implement MaxInputBytesPerBatchPath" |
| 102 | + |
| 103 | + expect(subject).to be_kind_of(described_class) |
| 104 | + end |
| 105 | + |
| 106 | + it "sets max_input_bytes_per_batch_path" do |
| 107 | + pending "implement MaxInputBytesPerBatchPath" |
| 108 | + |
| 109 | + expect(subject.max_input_bytes_per_batch_path).to be_kind_of(Floe::Workflow::ReferencePath) |
| 110 | + expect(subject.max_input_bytes_per_batch_path).to have_attributes(:path => ["batchSize"]) |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + context "with MaxItemsPerBatch and MaxItemsPerBatchPath" do |
| 115 | + let(:payload) { {"MaxItemsPerBatch" => 10, "MaxItemsPerBatchPath" => "$.maxBatchItems"} } |
| 116 | + |
| 117 | + it "raises an exception" do |
| 118 | + expect { subject }.to raise_error(Floe::InvalidWorkflowError, "Map.ItemBatcher must not specify both \"MaxItemsPerBatch\" and \"MaxItemsPerBatchPath\"") |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + context "with MaxInputBytesPerBatch and MaxInputBytesPerBatchPath" do |
| 123 | + let(:payload) { {"MaxInputBytesPerBatch" => 1_024, "MaxInputBytesPerBatchPath" => "$.batchSize"} } |
| 124 | + |
| 125 | + it "raises an exception" do |
| 126 | + pending "implement MaxInputBytesPerBatchPath" |
| 127 | + |
| 128 | + expect { subject }.to raise_error(Floe::InvalidWorkflowError, "Map.ItemBatcher must not specify both \"MaxInputBytesPerBatch\" and \"MaxInputBytesPerBatchPath\"") |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + describe "#value" do |
| 134 | + let(:context) { {} } |
| 135 | + let(:input) { %w[a b c d e] } |
| 136 | + |
| 137 | + context "with MaxItemsPerBatch" do |
| 138 | + let(:payload) { {"MaxItemsPerBatch" => 2} } |
| 139 | + |
| 140 | + it "returns in batches of 2" do |
| 141 | + expect(subject.value(context, input)).to eq([{"Items" => %w[a b]}, {"Items" => %w[c d]}, {"Items" => %w[e]}]) |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + context "with MaxItemsPerBatchPath" do |
| 146 | + let(:payload) { {"MaxItemsPerBatchPath" => "$.batchSize"} } |
| 147 | + let(:state_input) { {"batchSize" => 2, "items" => input} } |
| 148 | + |
| 149 | + it "returns in batches of 2" do |
| 150 | + expect(subject.value(context, input, state_input)).to eq([{"Items" => %w[a b]}, {"Items" => %w[c d]}, {"Items" => %w[e]}]) |
| 151 | + end |
| 152 | + |
| 153 | + context "with an invalid value in input" do |
| 154 | + it "raises an exception" do |
| 155 | + expect { subject.value(context, input, {"batchSize" => 0, "items" => input}) } |
| 156 | + .to raise_error(Floe::ExecutionError, "Map.ItemBatcher field \"MaxItemsPerBatchPath\" value \"0\" must be a positive integer") |
| 157 | + expect { subject.value(context, input, {"batchSize" => -1, "items" => input}) } |
| 158 | + .to raise_error(Floe::ExecutionError, "Map.ItemBatcher field \"MaxItemsPerBatchPath\" value \"-1\" must be a positive integer") |
| 159 | + expect { subject.value(context, input, {"batchSize" => 2.5, "items" => input}) } |
| 160 | + .to raise_error(Floe::ExecutionError, "Map.ItemBatcher field \"MaxItemsPerBatchPath\" value \"2.5\" must be a positive integer") |
| 161 | + expect { subject.value(context, input, {"batchSize" => "1", "items" => input}) } |
| 162 | + .to raise_error(Floe::ExecutionError, "Map.ItemBatcher field \"MaxItemsPerBatchPath\" value \"1\" must be a positive integer") |
| 163 | + end |
| 164 | + end |
| 165 | + end |
| 166 | + |
| 167 | + context "with MaxInputBytesPerBatch" do |
| 168 | + let(:payload) { {"MaxInputBytesPerBatch" => 1_024} } |
| 169 | + |
| 170 | + it "returns in batches of 2" do |
| 171 | + pending "support max bytes per batch" |
| 172 | + |
| 173 | + expect(subject.value(context, input)).to eq([{"Items" => %w[a b]}, {"Items" => %w[c d]}, {"Items" => %w[e]}]) |
| 174 | + end |
| 175 | + end |
| 176 | + |
| 177 | + context "with MaxInputBytesPerBatchPath" do |
| 178 | + let(:payload) { {"MaxInputBytesPerBatchPath" => "$.bytesPerBatch"} } |
| 179 | + let(:state_input) { {"bytesPerBatch" => 1_024, "items" => input} } |
| 180 | + |
| 181 | + it "returns in batches of 2" do |
| 182 | + pending "support max bytes per batch" |
| 183 | + |
| 184 | + expect(subject.value(context, input, state_input)).to eq([{"Items" => %w[a b]}, {"Items" => %w[c d]}, {"Items" => %w[e]}]) |
| 185 | + end |
| 186 | + |
| 187 | + context "with an invalid value in input" do |
| 188 | + let(:state_input) { {"bytesPerBatch" => 0, "items" => input} } |
| 189 | + |
| 190 | + it "raises an exception" do |
| 191 | + pending "support max bytes per batch" |
| 192 | + |
| 193 | + expect { subject.value(context, input, state_input) } |
| 194 | + .to raise_error(Floe::ExecutionError, "Map.ItemBatcher field \"MaxInputBytesPerBatchPath\" value \"0\" must be a positive integer") |
| 195 | + end |
| 196 | + end |
| 197 | + end |
| 198 | + |
| 199 | + context "with BatchInput" do |
| 200 | + let(:payload) { {"BatchInput" => {"foo.$" => "$.bar"}, "MaxItemsPerBatch" => 2} } |
| 201 | + let(:state_input) { {"bar" => "bar", "items" => input} } |
| 202 | + |
| 203 | + it "merges BatchInput with payloads" do |
| 204 | + expect(subject.value(context, input, state_input)).to eq([{"foo" => "bar", "Items" => %w[a b]}, {"foo" => "bar", "Items" => %w[c d]}, {"foo" => "bar", "Items" => %w[e]}]) |
| 205 | + end |
| 206 | + end |
| 207 | + end |
| 208 | +end |
0 commit comments