forked from beautyjoy/BJC-Teacher-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added rspec test for each creation script
- Loading branch information
Showing
5 changed files
with
157 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module SchoolCreator | ||
def self.create_schools(number_of_schools, is_us) | ||
valid_states = School.get_valid_states | ||
grade_levels = School.grade_levels.keys | ||
school_types = School.school_types.keys | ||
available_countries = ISO3166::Country.all.map(&:alpha2) | ||
def self.create_schools(number_of_schools, is_us) | ||
valid_states = School.get_valid_states | ||
grade_levels = School.grade_levels.keys | ||
school_types = School.school_types.keys | ||
available_countries = ISO3166::Country.all.map(&:alpha2) | ||
|
||
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, | ||
website: Faker::Internet.url, | ||
grade_level: grade_levels.sample, | ||
school_type: school_types.sample | ||
) | ||
school_ids.push(school.id) | ||
end | ||
return school_ids | ||
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, | ||
website: Faker::Internet.url, | ||
grade_level: grade_levels.sample, | ||
school_type: school_types.sample | ||
) | ||
school_ids.push(school.id) | ||
end | ||
school_ids | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
require 'school_creator' | ||
require 'teacher_creator' | ||
# frozen_string_literal: true | ||
|
||
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 = [] | ||
require "school_creator" | ||
require "teacher_creator" | ||
|
||
# Create US schools | ||
school_ids += SchoolCreator.create_schools(number_of_US_schools, true) | ||
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 International schools | ||
school_ids += SchoolCreator.create_schools(number_of_international_schools, false) | ||
# Create US schools | ||
school_ids += SchoolCreator.create_schools(number_of_US_schools, true) | ||
|
||
if school_ids.empty? | ||
puts "No schools available to assign. Please create schools first." | ||
exit | ||
end | ||
# Create International schools | ||
school_ids += SchoolCreator.create_schools(number_of_international_schools, false) | ||
|
||
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." | ||
if school_ids.empty? | ||
puts "No schools available to assign. Please create schools first." | ||
exit | ||
end | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,32 @@ | ||
# 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 | ||
def self.create_teachers(number_of_teachers, school_ids) | ||
statuses = Teacher.statuses.keys | ||
education_levels = Teacher.education_levels.keys | ||
all_languages = Teacher.get_languages | ||
|
||
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: email, | ||
primary: true | ||
) | ||
end | ||
end | ||
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 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
require "./lib/school_creator" | ||
|
||
RSpec.describe SchoolCreator do | ||
describe ".create_schools" do | ||
let(:valid_states) { ["CA", "NY", "TX"] } | ||
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 | ||
expect { SchoolCreator.create_schools(5, true) }.to change { School.count }.by(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 |