Skip to content

Commit

Permalink
Merge pull request #4114 from isidzukuri/3729_wider_spread_of_dates_i…
Browse files Browse the repository at this point in the history
…n_the_seed

3729 wider spread of dates in the seed
  • Loading branch information
cielf authored Feb 22, 2024
2 parents b0e6295 + 2bbf0f9 commit 3841e1f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 28 deletions.
48 changes: 22 additions & 26 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# ----------------------------------------------------------------------------
# Random Record Generators
# ----------------------------------------------------------------------------
load "lib/dispersed_past_dates_generator.rb"

def random_record(klass)
klass.limit(1).order(Arel.sql('random()')).first
Expand Down Expand Up @@ -327,12 +328,18 @@ def random_record_for_org(org, klass)
end
end

dates_generator = DispersedPastDatesGenerator.new

Faker::Number.within(range: 32..56).times do
date = dates_generator.next

partner_request = ::Request.new(
partner_id: p.id,
organization_id: p.organization_id,
comments: Faker::Lorem.paragraph,
partner_user_id: p.primary_user.id
partner_user_id: p.primary_user.id,
created_at: date,
updated_at: date
)

item_requests = []
Expand All @@ -343,7 +350,9 @@ def random_record_for_org(org, klass)
quantity: Faker::Number.within(range: 10..30),
children: [],
name: item.name,
partner_key: item.partner_key
partner_key: item.partner_key,
created_at: date,
updated_at: date
)
partner_request.item_requests << new_item_request
end
Expand Down Expand Up @@ -500,13 +509,15 @@ def seed_quantity(item_name, organization, storage_location, quantity)
# Donations
# ----------------------------------------------------------------------------

dates_generator = DispersedPastDatesGenerator.new
# Make some donations of all sorts
20.times.each do
source = Donation::SOURCES.values.sample
# Depending on which source it uses, additional data may need to be provided.
donation = Donation.new(source: source,
storage_location: random_record_for_org(pdx_org, StorageLocation),
organization: pdx_org, issued_at: Time.zone.now)
organization: pdx_org,
issued_at: dates_generator.next)
case source
when Donation::SOURCES[:product_drive]
donation.product_drive = ProductDrive.first
Expand All @@ -526,6 +537,7 @@ def seed_quantity(item_name, organization, storage_location, quantity)
# ----------------------------------------------------------------------------
# Distributions
# ----------------------------------------------------------------------------
dates_generator = DispersedPastDatesGenerator.new

inventory = InventoryAggregate.inventory_for(pdx_org.id)
# Make some distributions, but don't use up all the inventory
Expand All @@ -538,7 +550,7 @@ def seed_quantity(item_name, organization, storage_location, quantity)
storage_location: storage_location,
partner: random_record_for_org(pdx_org, Partner),
organization: pdx_org,
issued_at: Faker::Date.between(from: 4.days.ago, to: Time.zone.today),
issued_at: dates_generator.next,
delivery_method: delivery_method,
shipping_cost: shipping_cost,
comment: 'Urgent'
Expand Down Expand Up @@ -619,25 +631,10 @@ def seed_quantity(item_name, organization, storage_location, quantity)
"Nullam dictum ac lectus at scelerisque. Phasellus volutpat, sem at eleifend tristique, massa mi cursus dui, eget pharetra ligula arcu sit amet nunc."
]

20.times do
storage_location = random_record_for_org(pdx_org, StorageLocation)
vendor = random_record_for_org(pdx_org, Vendor)
purchase = Purchase.new(
purchased_from: suppliers.sample,
comment: comments.sample,
organization_id: pdx_org.id,
storage_location_id: storage_location.id,
amount_spent_in_cents: rand(200..10_000),
issued_at: (Time.zone.today - rand(15).days),
created_at: (Time.zone.today - rand(15).days),
updated_at: (Time.zone.today - rand(15).days),
vendor_id: vendor.id
)
PurchaseCreateService.call(purchase)
end
dates_generator = DispersedPastDatesGenerator.new

#re 2813_update_annual_report add some data for last year (enables system testing of reports)
5.times do
25.times do
purchase_date = dates_generator.next
storage_location = random_record_for_org(pdx_org, StorageLocation)
vendor = random_record_for_org(pdx_org, Vendor)
purchase = Purchase.new(
Expand All @@ -646,15 +643,14 @@ def seed_quantity(item_name, organization, storage_location, quantity)
organization_id: pdx_org.id,
storage_location_id: storage_location.id,
amount_spent_in_cents: rand(200..10_000),
issued_at: (Time.zone.today - 1.year),
created_at: (Time.zone.today - 1.year),
updated_at: (Time.zone.today - 1.year),
issued_at: purchase_date,
created_at: purchase_date,
updated_at: purchase_date,
vendor_id: vendor.id
)
PurchaseCreateService.call(purchase)
end


# ----------------------------------------------------------------------------
# Flipper
# ----------------------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions lib/dispersed_past_dates_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class DispersedPastDatesGenerator
DAYS_RANGES = [0..6, 7..30, 31..300, 350..700].freeze

def initialize
@current_index = 0
end

def next
day = Time.zone.today - rand(DAYS_RANGES[@current_index]).days
@current_index = if DAYS_RANGES.size - 1 > @current_index
@current_index.next
else
0
end

day
end
end
24 changes: 24 additions & 0 deletions spec/lib/dispersed_past_dates_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load "lib/dispersed_past_dates_generator.rb"

RSpec.describe DispersedPastDatesGenerator do
describe "constants" do
it "has 4 day ranges for generation of past dates" do
expect(described_class::DAYS_RANGES).to eq([0..6, 7..30, 31..300, 350..700])
end
end

describe "#next" do
let(:dates_generator) { described_class.new }

it "returns equally dispersed dates between all time ranges" do
expect(dates_generator.next).to be_between(Time.zone.today - 6.days, Time.zone.today)
expect(dates_generator.next).to be_between(Time.zone.today - 30.days, Time.zone.today - 7.days)
expect(dates_generator.next).to be_between(Time.zone.today - 300.days, Time.zone.today - 31.days)
expect(dates_generator.next).to be_between(Time.zone.today - 700.days, Time.zone.today - 350.days)
expect(dates_generator.next).to be_between(Time.zone.today - 6.days, Time.zone.today)
expect(dates_generator.next).to be_between(Time.zone.today - 30.days, Time.zone.today - 7.days)
expect(dates_generator.next).to be_between(Time.zone.today - 300.days, Time.zone.today - 31.days)
expect(dates_generator.next).to be_between(Time.zone.today - 700.days, Time.zone.today - 350.days)
end
end
end
4 changes: 2 additions & 2 deletions spec/services/distribution_itemized_breakdown_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
let(:organization) { create(:organization) }
let(:distribution_ids) { [distribution_1, distribution_2, distribution_3].map(&:id) }
let(:item_a) do
create(:item, organization: organization, on_hand_minimum_quantity: 9999)
create(:item, organization: organization, on_hand_minimum_quantity: 9999, name: "A Diapers")
end
let(:item_b) do
create(:item, organization: organization, on_hand_minimum_quantity: 5)
create(:item, organization: organization, on_hand_minimum_quantity: 5, name: "B Diapers")
end
let(:distribution_1) { create(:distribution, :with_items, item: item_a, item_quantity: 500, organization: organization) }
let(:distribution_2) { create(:distribution, :with_items, item: item_b, item_quantity: 100, organization: organization) }
Expand Down

0 comments on commit 3841e1f

Please sign in to comment.