diff --git a/db/seeds.rb b/db/seeds.rb index 7da24334b8..8be1d64bd4 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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 @@ -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 = [] @@ -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 @@ -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 @@ -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 @@ -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' @@ -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( @@ -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 # ---------------------------------------------------------------------------- diff --git a/lib/dispersed_past_dates_generator.rb b/lib/dispersed_past_dates_generator.rb new file mode 100644 index 0000000000..5b5cfcce63 --- /dev/null +++ b/lib/dispersed_past_dates_generator.rb @@ -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 diff --git a/spec/lib/dispersed_past_dates_generator_spec.rb b/spec/lib/dispersed_past_dates_generator_spec.rb new file mode 100644 index 0000000000..49d07d5d5b --- /dev/null +++ b/spec/lib/dispersed_past_dates_generator_spec.rb @@ -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 diff --git a/spec/services/distribution_itemized_breakdown_service_spec.rb b/spec/services/distribution_itemized_breakdown_service_spec.rb index d0b688c9f5..f342eae28a 100644 --- a/spec/services/distribution_itemized_breakdown_service_spec.rb +++ b/spec/services/distribution_itemized_breakdown_service_spec.rb @@ -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) }