Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fake data script #57

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ group :test do
# Test suite speedup
gem "rails-controller-testing"
end

# Dependencies for both development and testing
cycomachead marked this conversation as resolved.
Show resolved Hide resolved
group :development, :test do
gem "faker"
end
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ DEPENDENCIES
database_cleaner
debug
factory_bot_rails
faker
haml-lint
httparty (~> 0.21.0)
image_processing (>= 1.2)
Expand Down
4 changes: 4 additions & 0 deletions app/models/school.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def self.all_maps_data
validated.map(&:maps_marker_data).to_json
end

def self.get_valid_states
VALID_STATES
end

cycomachead marked this conversation as resolved.
Show resolved Hide resolved
private
def prefix_url(url)
return unless url
Expand Down
4 changes: 4 additions & 0 deletions app/models/teacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ def personal_emails
non_primary_emails
end

def self.get_languages
WORLD_LANGUAGES
end

cycomachead marked this conversation as resolved.
Show resolved Hide resolved
private
def non_primary_emails
# email_addresses.where(primary: false)&.pluck(:email)
Expand Down
25 changes: 25 additions & 0 deletions lib/school_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module SchoolCreator
def self.create_schools(number_of_schools, is_us)
cycomachead marked this conversation as resolved.
Show resolved Hide resolved
valid_states = School.get_valid_states
cycomachead marked this conversation as resolved.
Show resolved Hide resolved
grade_levels = School.grade_levels.keys
school_types = School.school_types.keys
available_countries = ISO3166::Country.all.map(&:alpha2)
cycomachead marked this conversation as resolved.
Show resolved Hide resolved

school_ids = []
number_of_schools.times do
school = School.create!(
name: Faker::Educator.university,
state: is_us ? valid_states.sample : nil,
city: Faker::Address.city,
country: is_us ? "US" : available_countries.sample,
cycomachead marked this conversation as resolved.
Show resolved Hide resolved
website: Faker::Internet.url,
grade_level: grade_levels.sample,
school_type: school_types.sample
)
cycomachead marked this conversation as resolved.
Show resolved Hide resolved
school_ids.push(school.id)
end
school_ids
end
end
29 changes: 29 additions & 0 deletions lib/tasks/populate_teachers.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "school_creator"
require "teacher_creator"

namespace :db do
desc "Populate the database with fake teacher data"
task populate_teachers: :environment do
number_of_US_schools = 5
number_of_international_schools = 5
school_ids = []

# Create US schools
school_ids += SchoolCreator.create_schools(number_of_US_schools, true)
cycomachead marked this conversation as resolved.
Show resolved Hide resolved

# Create International schools
school_ids += SchoolCreator.create_schools(number_of_international_schools, false)
cycomachead marked this conversation as resolved.
Show resolved Hide resolved

if school_ids.empty?
puts "No schools available to assign. Please create schools first."
exit
end

number_of_teachers = 30
TeacherCreator.create_teachers(number_of_teachers, school_ids)

puts "#{number_of_US_schools} US schools created. \n#{number_of_US_schools} international schools created. \n#{number_of_teachers} teachers created."
end
end
35 changes: 35 additions & 0 deletions lib/teacher_creator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module TeacherCreator
def self.create_teachers(number_of_teachers, school_ids)
statuses = Teacher.statuses.keys
education_levels = Teacher.education_levels.keys
all_languages = Teacher.get_languages
teachers = []

number_of_teachers.times do
teacher = Teacher.create!(
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
snap: Faker::Internet.username,
school_id: school_ids.sample,
personal_website: Faker::Internet.domain_name,
status: statuses.sample,
more_info: Faker::Lorem.sentence(word_count: 20),
education_level: education_levels.sample,
languages: all_languages.sample(rand(1..3))
)
if teacher.persisted?
# Generate an email using the first_name and last_name from the teacher instance
email = Faker::Internet.email(name: "#{teacher.first_name} #{teacher.last_name}", separators: ["."])
EmailAddress.create!(
teacher_id: teacher.id,
email:,
primary: true
)
end
teachers.push(teacher)
end
teachers
end
end
40 changes: 40 additions & 0 deletions spec/lib/school_creator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

require "rails_helper"
require "./lib/school_creator"

RSpec.describe SchoolCreator do
describe ".create_schools" do
cycomachead marked this conversation as resolved.
Show resolved Hide resolved
let(:valid_states) { School.get_valid_states }
cycomachead marked this conversation as resolved.
Show resolved Hide resolved
let(:grade_levels) { ["elementary", "middle_school", "high_school"] }
let(:school_types) { ["public", "private"] }

before do
allow(School).to receive(:get_valid_states).and_return(valid_states)
allow(School).to receive_message_chain(:grade_levels, :keys).and_return(grade_levels)
allow(School).to receive_message_chain(:school_types, :keys).and_return(school_types)
end

it "creates the specified number of US schools" do
schools = SchoolCreator.create_schools(5, true)
expect(schools.length).to eq(5)
end

it "creates schools with appropriate attributes for US schools" do
SchoolCreator.create_schools(1, true)
school = School.last

expect(school.country).to eq("US")
expect(valid_states).to include(school.state)
expect(grade_levels).to include(school.grade_level)
expect(school_types).to include(school.school_type)
end

it "creates schools with no state for international schools" do
SchoolCreator.create_schools(1, false)
school = School.last

expect(school.state).to be_nil
end
end
end
45 changes: 45 additions & 0 deletions spec/lib/teacher_creator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require "rails_helper"
require "./lib/teacher_creator"

RSpec.describe TeacherCreator do
describe ".create_teachers" do
cycomachead marked this conversation as resolved.
Show resolved Hide resolved
let(:school_ids) { [] }
let(:statuses) { ["csp_teacher", "non_csp_teacher", "mixed_class"] }
let(:education_levels) { ["middle_school", "high_school", "college"] }
let(:languages) { ["English", "Romanian", "German"] }
let(:valid_states) { ["CA", "NY", "TX"] }

before do
# Create schools directly and push their IDs to the school_ids array
3.times do |i|
school = School.create!(
name: "School #{i}",
city: Faker::Address.city,
country: "US",
website: Faker::Internet.url,
state: valid_states.sample
)
school_ids << school.id
end

allow(Teacher).to receive_message_chain(:statuses, :keys).and_return(statuses)
allow(Teacher).to receive_message_chain(:education_levels, :keys).and_return(education_levels)
allow(Teacher).to receive(:get_languages).and_return(languages)
end

it "creates the specified number of teachers" do
expect {
TeacherCreator.create_teachers(10, school_ids)
}.to change(Teacher, :count).by(10)
end

it "assigns valid school_ids to each teacher" do
TeacherCreator.create_teachers(5, school_ids)
Teacher.last(5).each do |teacher|
expect(school_ids).to include(teacher.school_id)
end
end
end
end
Loading