Skip to content

Instantly share code, notes, and snippets.

@zuchmanski
Created January 25, 2014 16:27
Show Gist options
  • Save zuchmanski/8618935 to your computer and use it in GitHub Desktop.
Save zuchmanski/8618935 to your computer and use it in GitHub Desktop.
require 'digest/sha1'
require 'faker'
require './generator/generator'
require 'date'
END_DATE = DateTime.now + 14 # days
START_DATE = END_DATE - 365 * 3
CONFERENCE_NUMBER = 8
PLAIN_USER_PER_CONFERENCE = 20
COMPANY_USER_PER_CONFERENCE = 20
COMPANY_NUMBER = 40
COMPANY_RESERVATION_START = CONFERENCE_NUMBER * PLAIN_USER_PER_CONFERENCE
$conferences = {}
class Conference < Generator
set_function_name :create_conference
set_number CONFERENCE_NUMBER
attribute :name, :string do
Faker::Company.catch_phrase
end
attribute :daterange, :daterange do |attrs|
@start_date = (START_DATE..END_DATE).to_a.sample
@end_date = @start_date + 3
$conferences[attrs[:parent_id]] = { :date_range => @start_date..@end_date }
@start_date..@end_date
end
attribute :description, :string do
""
end
attribute :logo, :string do
""
end
attribute :base_price, :integer do |attrs|
rand(100..5000).to_f
end
attribute :student_discount, :integer do
rand(0..0.5).round(2).to_f
end
attribute :early_birds_discount, :integer do
rand(0..0.5).round(2).to_f
end
attribute :early_birds_ends_at, :datetime do
@start_date - rand(30...90)
end
set_children do |attrs|
(@start_date...@end_date).inject("") do |memo, date|
memo + Day.new(conference_id: attrs[:parent_id], date: date).output
end
end
end
class Day < Generator
set_function_name :create_day
set_number 1
attribute :conference_id, :integer do |attrs|
attrs[:conference_id]
end
attribute :date, :datetime do |attrs|
attrs[:date]
end
attribute :price, :integer do
rand(0..500).to_f
end
attribute :reservations_limit, :integer do
@reservations_limit = rand(500..2000)
end
set_children do |attrs|
$conferences[attrs[:conference_id]][:days] ||= { }
(0..rand(0..3)).to_a.inject("") do |memo, _|
memo + Workshop.new(conference_id: attrs[:conference_id], day_id: attrs[:parent_id], day_limit: @reservations_limit).output
end
end
end
class Workshop < Generator
set_function_name :create_workshop
set_number 1
attribute :day_id, :integer do |attrs|
$conferences[attrs[:conference_id]][:days][attrs[:day_id]] ||= []
$conferences[attrs[:conference_id]][:days][attrs[:day_id]] << attrs[:parent_id]
attrs[:day_id]
end
attribute :name, :string do
Faker::Company.catch_phrase
end
attribute :description, :string do
Faker::Lorem.paragraph
end
attribute :price, :integer do
rand(0..200).to_f
end
attribute :reservations_limit, :integer do |attrs|
rand(500..attrs[:day_limit])
end
end
class PlainUser < Generator
set_function_name :user_sign_up
set_number CONFERENCE_NUMBER * PLAIN_USER_PER_CONFERENCE
attribute :company_id, :null do
end
attribute :name, :string do
Faker::Name.name
end
attribute :email, :string do
rand(1...1000).to_s + Faker::Internet.email
end
attribute :sex, :string do
rand > 0.5 ? 'm' : 'f'
end
attribute :phone, :string do
Faker::PhoneNumber.phone_number[0..12]
end
attribute :student_id, :string do
rand > 0.8 ? Digest::SHA1.hexdigest(rand(0..10000000000).to_s)[0..12] : 'NULL'
end
attribute :conference_id, :integer do |attrs|
@conference_assigned = rand(1..CONFERENCE_NUMBER)
@reservation_id = attrs[:parent_id]
@conference_assigned
end
attribute :timestamp, :datetime do |attrs|
@created_at = $conferences[@conference_assigned][:date_range].first - rand(1..90)
end
set_children do
@days = $conferences[@conference_assigned][:days].keys
@days.sample(rand([email protected])).inject("") do |memo, day_id|
memo + PlainUserDayReservation.new(
:conference_id => @conference_assigned,
:reservation_id => @reservation_id,
:day_id => day_id
).output
end +
PlainUserPayment.new(
:conference_id => @conference_assigned,
:reservation_id => @reservation_id,
:created_at => @created_at
).output
end
end
class PlainUserDayReservation < Generator
set_function_name :sign_for_day
set_number 1
attribute :day_id, :integer do |attrs|
@days = $conferences[attrs[:conference_id]][:days]
@day_id = attrs[:day_id]
@workshops = @days[@day_id]
@day_id
end
attribute :reservation_id, :integer do |attrs|
@reservation_id = attrs[:reservation_id]
end
set_children do
@workshops.sample(rand([email protected])).inject("") do |memo, workshop_id|
memo + PlainUserWorkshopReservation.new(
:day_id => @day_id,
:workshop_id => workshop_id,
:reservation_id => @reservation_id
).output
end
end
end
class PlainUserWorkshopReservation < Generator
set_function_name :sign_for_workshop
set_number 1
attribute :workshop_id, :integer do |attrs|
attrs[:workshop_id]
end
attribute :reservation_id, :integer do |attrs|
attrs[:reservation_id]
end
end
class PlainUserPayment < Generator
set_function_name :pay_for_reservation
set_number 1
attribute :reservation, :integer do |attrs|
attrs[:reservation_id]
end
attribute :timestamp, :datetime do |attrs|
attrs[:created_at]
end
end
class Company < Generator
set_function_name :create_company
set_number COMPANY_NUMBER
attribute :name, :string do
Faker::Company.name + " #{rand(1...1000)}"
end
attribute :description, :string do
Faker::Lorem.sentence
end
attribute :email, :string do
rand(1..1000).to_s + Faker::Internet.email
end
attribute :phone, :string do
Faker::PhoneNumber.phone_number[0..12]
end
attribute :street_address, :string do
Faker::Address.street_address
end
attribute :zip_code, :string do
Faker::Address.zip_code
end
attribute :state, :string do
Faker::Address.state
end
attribute :country, :string do
Faker::Address.country
end
attribute :vat_id, :string do
Digest::SHA1.hexdigest(rand(0..1000000000).to_s)[0..12]
end
set_children do |attrs|
@conference_assigned = rand(1..CONFERENCE_NUMBER)
@company_reservations = rand(1..COMPANY_USER_PER_CONFERENCE)
@created_at = $conferences[@conference_assigned][:date_range].first - rand(1..90)
(1..@company_reservations).inject("") do |memo, _|
memo + CompanyReservation.new(
:company_id => attrs[:parent_id],
:conference_id => @conference_assigned,
:created_at => @created_at
).output
end +
CompanyPayReservation.new(
:company_id => attrs[:parent_id],
:created_at => @created_at
).output
end
end
class CompanyPayReservation < Generator
set_function_name :pay_for_reservations_for_company
set_number 1
attribute :company, :integer do |attrs|
attrs[:company_id]
end
attribute :timestamp, :datetime do |attrs|
attrs[:created_at]
end
end
class CompanyReservation < Generator
set_function_name :make_company_reservation
set_number 1
attribute :conference_id, :integer do |attrs|
attrs[:conference_id]
end
attribute :company_id, :integer do |attrs|
attrs[:company_id]
end
attribute :number, :integer do
1
end
attribute :timestamp, :datetime do |attrs|
attrs[:created_at]
end
set_children do |attrs|
@days = $conferences[attrs[:conference_id]][:days].keys
CompanyUser.new(:company_id => attrs[:company_id]).output +
CompanyAssignReservation.new(
:reservation_id => COMPANY_RESERVATION_START + attrs[:parent_id],
:user_id => COMPANY_RESERVATION_START + attrs[:parent_id]
).output +
CompanyConfirmReservation.new(
:reservation_id => COMPANY_RESERVATION_START + attrs[:parent_id]
).output +
@days.sample(rand([email protected])).inject("") do |memo, day_id|
memo + PlainUserDayReservation.new(
:conference_id => attrs[:conference_id],
:reservation_id => COMPANY_RESERVATION_START + attrs[:parent_id],
:day_id => day_id
).output
end
end
end
class CompanyAssignReservation < Generator
set_function_name :assign_reservation_to_user
set_number 1
attribute :reservation, :integer do |attrs|
attrs[:reservation_id]
end
attribute :user, :integer do |attrs|
attrs[:user_id]
end
end
class CompanyConfirmReservation < Generator
set_function_name :confirm_reservation
set_number 1
attribute :reservation, :integer do |attrs|
attrs[:reservation_id]
end
end
class CompanyUser < Generator
set_function_name :create_user
set_number 1
attribute :company_id, :integer do |attrs|
attrs[:company_id]
end
attribute :name, :string do
Faker::Name.name
end
attribute :email, :string do
rand(1...1000).to_s + Faker::Internet.email
end
attribute :sex, :string do
rand > 0.5 ? 'm' : 'f'
end
attribute :phone, :string do
Faker::PhoneNumber.phone_number[0..12]
end
attribute :student_id, :null do
nil
end
end
puts 'ALTER TABLE days_reservations DISABLE TRIGGER ALL;'
puts 'ALTER TABLE workshops_reservations DISABLE TRIGGER ALL;';
puts Conference.new.output
puts PlainUser.new.output
puts Company.new.output
puts 'ALTER TABLE days_reservations ENABLE TRIGGER ALL;';
puts 'ALTER TABLE workshops_reservations ENABLE TRIGGER ALL;';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment