Skip to content

Commit

Permalink
Make request factory build both request-item structures
Browse files Browse the repository at this point in the history
And also fix traits, which seem like they shouldn't have worked this
way.

[#4399]
  • Loading branch information
awwaiid committed Jul 14, 2024
1 parent 79902e3 commit a62f374
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions spec/factories/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,46 @@ def random_request_items
request_items { random_request_items }
comments { "Urgent" }
partner_user { ::User.partner_users.first || create(:partners_user) }
end

trait :started do
status { 'started' }
end
# For compatibility we can take in a list of request_items and turn it into a
# list of item_requests
after(:build) do |request|
if request.item_requests.empty?
request.request_items.each do |request_item|
item = Item.find(request_item['item_id'])
request.item_requests << Partners::ItemRequest.new(
item_id: item.id,
quantity: request_item['quantity'],
name: item.name,
partner_key: item.partner_key,
request_unit: request_item["request_unit"]
)
end
end
end

trait :fulfilled do
status { 'fulfilled' }
end
trait :started do
status { 'started' }
end

trait :pending do
status { 'pending' }
end
trait :fulfilled do
status { 'fulfilled' }
end

trait :pending do
status { 'pending' }
end

trait :with_varied_quantities do
request_items {
# get 10 unique item ids
keys = Item.active.pluck(:id).sample(10)
trait :with_varied_quantities do
request_items {
# get 10 unique item ids
keys = Item.active.pluck(:id).sample(10)

# This *could* pass in error -- if the plucking order happens to match the end order.
# This *could* pass in error -- if the plucking order happens to match the end order.

item_quantities = [50, 150, 75, 125, 200, 3, 15, 88, 46, 22]
keys.map.with_index { |k, i| { "item_id" => k, "quantity" => item_quantities[i]} }
}
item_quantities = [50, 150, 75, 125, 200, 3, 15, 88, 46, 22]
keys.map.with_index { |k, i| { "item_id" => k, "quantity" => item_quantities[i]} }
}
end
end
end

0 comments on commit a62f374

Please sign in to comment.