From dad60c56ada6f26d6a3c638d12efd96ed13f5e96 Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Sat, 2 Mar 2024 10:33:38 -0800 Subject: [PATCH 1/5] Refract Rspec test for email reseed --- .../email_templates_reseed_spec.rb | 87 ++++++------------- 1 file changed, 27 insertions(+), 60 deletions(-) diff --git a/spec/controllers/email_templates_reseed_spec.rb b/spec/controllers/email_templates_reseed_spec.rb index 642e0684..9ec989e4 100644 --- a/spec/controllers/email_templates_reseed_spec.rb +++ b/spec/controllers/email_templates_reseed_spec.rb @@ -31,36 +31,33 @@ REQUEST_INFO_EMAIL end + def email_template_hash(body:, title:, subject:, path:) + { + to: title == "Form Submission" ? + "lmock@berkeley.edu, contact@bjc.berkeley.edu" + : "{{teacher_email}}, {{teacher_personal_email}}", + body:, + path:, + locale: nil, + handler: "liquid", + partial: false, + format: "html", + title:, + required: true, + subject: + } + end + before do # Clear the EmailTemplate table to ensure a clean slate for each test. # This is safe to do because it's under test environment, not production EmailTemplate.delete_all allow(SeedData).to receive(:emails).and_return( [ - { - to: "lmock@berkeley.edu, contact@bjc.berkeley.edu", - body: form_submission_old, - path: "teacher_mailer/form_submission", - locale: nil, - handler: "liquid", - partial: false, - format: "html", - title: "Form Submission", - required: true, - subject: "Form Submission" - }, - { - body: request_info_email_old, - to: "{{teacher_email}}, {{teacher_personal_email}}", - path: "teacher_mailer/request_info_email", - locale: nil, - handler: "liquid", - partial: false, - format: "html", - title: "Request Info Email", - required: true, - subject: "Request Info Email" - } + email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", + path: "teacher_mailer/form_submission"), + email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") ]) # Simulates the seeding action. load Rails.root.join("db/seeds.rb") @@ -83,30 +80,10 @@ # This simulates the reseeding process with updated email content. allow(SeedData).to receive(:emails).and_return( [ - { - to: "lmock@berkeley.edu, contact@bjc.berkeley.edu", - body: form_submission_old, - path: "teacher_mailer/form_submission", - locale: nil, - handler: "liquid", - partial: false, - format: "html", - title: "Form Submission", - required: true, - subject: "Form Submission" - }, - { - body: request_info_email_new, - to: "{{teacher_email}}, {{teacher_personal_email}}", - path: "teacher_mailer/request_info_email", - locale: nil, - handler: "liquid", - partial: false, - format: "html", - title: "Request Info Email", - required: true, - subject: "Request Info Email" - } + email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", + path: "teacher_mailer/form_submission"), + email_template_hash(body: request_info_email_new, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") ]) load Rails.root.join("db/seeds.rb") @@ -131,18 +108,8 @@ # even though it's no longer present in the seed data. allow(SeedData).to receive(:emails).and_return( [ - { - body: request_info_email_old, - to: "{{teacher_email}}, {{teacher_personal_email}}", - path: "teacher_mailer/request_info_email", - locale: nil, - handler: "liquid", - partial: false, - format: "html", - title: "Request Info Email", - required: true, - subject: "Request Info Email" - } + email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") ]) load Rails.root.join("db/seeds.rb") From 54bd3fde046ecd24dcb43aafa74a8c071977be74 Mon Sep 17 00:00:00 2001 From: MT Date: Sat, 2 Mar 2024 11:44:13 -0800 Subject: [PATCH 2/5] finish refract email reseed test cases --- .../email_templates_reseed_spec.rb | 199 +++++++++++------- 1 file changed, 127 insertions(+), 72 deletions(-) diff --git a/spec/controllers/email_templates_reseed_spec.rb b/spec/controllers/email_templates_reseed_spec.rb index 9ec989e4..8d6b2509 100644 --- a/spec/controllers/email_templates_reseed_spec.rb +++ b/spec/controllers/email_templates_reseed_spec.rb @@ -7,6 +7,44 @@ # upon reseeding the database with new seed data. It tests the initial seeding, updates to # templates, and the non-removal of templates that are no longer present in the seed data. RSpec.describe "EmailTemplate Reseed Behavior", type: :model do + + RSpec.shared_context "setup initial email templates" do + before do + # Clear the EmailTemplate table to ensure a clean slate for each test. + # This is safe to do because it's under test environment, not production + EmailTemplate.delete_all + allow(SeedData).to receive(:emails).and_return( + [ + email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", + path: "teacher_mailer/form_submission"), + email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") + ]) + # Simulates the seeding action. + load Rails.root.join("db/seeds.rb") + end + end + + RSpec.shared_examples "verify initial email templates" do + it "has the correct number of email templates" do + expect(EmailTemplate.count).to eq(2) + end + + it "verifies the Request Info Email template content" do + request_info_email = EmailTemplate.find_by(title: "Request Info Email") + expect(request_info_email).to be_present + expect(request_info_email.title).to eq("Request Info Email") + expect(request_info_email.body).to include("I am the original content of the request info email") + end + + it "verifies the Form Submission Email template content" do + form_submission_email = EmailTemplate.find_by(title: "Form Submission") + expect(form_submission_email).to be_present + expect(form_submission_email.title).to eq("Form Submission") + expect(form_submission_email.body).to include("I am the original content of the form submission email") + end + end + # Define email content for initial seeding and subsequent updates. These blocks # provide a flexible way to manage test data, allowing us to simulate changes in seed data # across different test scenarios. @@ -48,81 +86,98 @@ def email_template_hash(body:, title:, subject:, path:) } end - before do - # Clear the EmailTemplate table to ensure a clean slate for each test. - # This is safe to do because it's under test environment, not production - EmailTemplate.delete_all - allow(SeedData).to receive(:emails).and_return( - [ - email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", - path: "teacher_mailer/form_submission"), - email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", - path: "teacher_mailer/request_info_email") - ]) - # Simulates the seeding action. - load Rails.root.join("db/seeds.rb") - - # Verify original content - expect(EmailTemplate.count).to eq(2) - request_info_email = EmailTemplate.find_by(title: "Request Info Email") - expect(request_info_email).to be_present - expect(request_info_email.title).to eq("Request Info Email") - expect(request_info_email.body).to include("I am the original content of the request info email") - - form_submission_email = EmailTemplate.find_by(title: "Form Submission") - expect(form_submission_email).to be_present - expect(form_submission_email.title).to eq("Form Submission") - expect(form_submission_email.body).to include("I am the original content of the form submission email") - end + context "when reseeded with updated content" do + include_context "setup initial email templates" + include_examples "verify initial email templates" + + context "after initial setup" do + before do + allow(SeedData).to receive(:emails).and_return( + [ + email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", + path: "teacher_mailer/form_submission"), + email_template_hash(body: request_info_email_new, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") + ]) + load Rails.root.join("db/seeds.rb") + end + + it "maintains the expected number of email templates" do + expect(EmailTemplate.count).to eq(2) + end + + context "with the updated Request Info Email template" do + subject(:request_info_email) { EmailTemplate.find_by(title: "Request Info Email") } + + it "is present in the database" do + expect(request_info_email).to be_present + end - it "does not duplicate email templates on reseed with updated content" do - # Mock the SeedData.emails call to return updated email content for the "Request Info Email" template. - # This simulates the reseeding process with updated email content. - allow(SeedData).to receive(:emails).and_return( - [ - email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", - path: "teacher_mailer/form_submission"), - email_template_hash(body: request_info_email_new, title: "Request Info Email", subject: "Request Info Email", - path: "teacher_mailer/request_info_email") - ]) - load Rails.root.join("db/seeds.rb") - - # Verify updated content: Email count is still 2, with only updated content for the request info email. - expect(EmailTemplate.count).to eq(2) - request_info_email = EmailTemplate.find_by(title: "Request Info Email") - expect(request_info_email).to be_present - expect(request_info_email.title).to eq("Request Info Email") - expect(request_info_email.body).to include("I am NEW Template Content of the request info email") - - form_submission_email = EmailTemplate.find_by(title: "Form Submission") - expect(form_submission_email).to be_present - expect(form_submission_email.title).to eq("Form Submission") - expect(form_submission_email.body).to include("I am the original content of the form submission email") + it "reflects the updated content" do + expect(request_info_email.title).to eq("Request Info Email") + expect(request_info_email.body).to include("I am NEW Template Content of the request info email") + end + end + + context "with the unchanged Form Submission Email template" do + subject(:form_submission_email) { EmailTemplate.find_by(title: "Form Submission") } + + it "is present in the database" do + expect(form_submission_email).to be_present + end + + it "retains its original content" do + expect(form_submission_email.title).to eq("Form Submission") + expect(form_submission_email.body).to include("I am the original content of the form submission email") + end + end + end end # Test the special needs from Professor Ball: I would not have the seeds file delete templates - it "does not remove email templates not present in updated seed data" do - # Mock the SeedData.emails call to only return the "Request Info Email" template, - # simulating the removal of the "Form Submission" template from the seed data. - # Expected behavior: The "Form Submission" template should not be removed from the **database**, - # even though it's no longer present in the seed data. - allow(SeedData).to receive(:emails).and_return( - [ - email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", - path: "teacher_mailer/request_info_email") - ]) - load Rails.root.join("db/seeds.rb") - - # Verify that both email templates still present and remain same - expect(EmailTemplate.count).to eq(2) - request_info_email = EmailTemplate.find_by(title: "Request Info Email") - expect(request_info_email).to be_present - expect(request_info_email.title).to eq("Request Info Email") - expect(request_info_email.body).to include("I am the original content of the request info email") - - form_submission_email = EmailTemplate.find_by(title: "Form Submission") - expect(form_submission_email).to be_present - expect(form_submission_email.title).to eq("Form Submission") - expect(form_submission_email.body).to include("I am the original content of the form submission email") + context "when reseed does not remove email templates not present in updated seed data" do + include_context "setup initial email templates" + include_examples "verify initial email templates" + + context "after initial setup" do + before do + allow(SeedData).to receive(:emails).and_return( + [ + email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") + ]) + load Rails.root.join("db/seeds.rb") + end + + it "maintains the expected number of email templates" do + expect(EmailTemplate.count).to eq(2) + end + + context "with the Request Info Email template" do + subject(:request_info_email) { EmailTemplate.find_by(title: "Request Info Email") } + + it "is present in the database" do + expect(request_info_email).to be_present + end + + it "has unchanged content" do + expect(request_info_email.title).to eq("Request Info Email") + expect(request_info_email.body).to include("I am the original content of the request info email") + end + end + + context "with the Form Submission Email template" do + subject(:form_submission_email) { EmailTemplate.find_by(title: "Form Submission") } + + it "is present in the database" do + expect(form_submission_email).to be_present + end + + it "has unchanged content" do + expect(form_submission_email.title).to eq("Form Submission") + expect(form_submission_email.body).to include("I am the original content of the form submission email") + end + end + end end end From 5bffa763a71793625470a8950d19d1c5e9c1585c Mon Sep 17 00:00:00 2001 From: MT Date: Sat, 2 Mar 2024 11:45:20 -0800 Subject: [PATCH 3/5] move email reseed test cases to models folder --- spec/{controllers => models}/email_templates_reseed_spec.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename spec/{controllers => models}/email_templates_reseed_spec.rb (100%) diff --git a/spec/controllers/email_templates_reseed_spec.rb b/spec/models/email_templates_reseed_spec.rb similarity index 100% rename from spec/controllers/email_templates_reseed_spec.rb rename to spec/models/email_templates_reseed_spec.rb From ba4a96bb1ca8429a29a932e7c8f9dc9e10210711 Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Sat, 2 Mar 2024 11:49:27 -0800 Subject: [PATCH 4/5] Fix rubocop check --- spec/models/email_templates_reseed_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/models/email_templates_reseed_spec.rb b/spec/models/email_templates_reseed_spec.rb index 8d6b2509..f8ded296 100644 --- a/spec/models/email_templates_reseed_spec.rb +++ b/spec/models/email_templates_reseed_spec.rb @@ -7,7 +7,6 @@ # upon reseeding the database with new seed data. It tests the initial seeding, updates to # templates, and the non-removal of templates that are no longer present in the seed data. RSpec.describe "EmailTemplate Reseed Behavior", type: :model do - RSpec.shared_context "setup initial email templates" do before do # Clear the EmailTemplate table to ensure a clean slate for each test. From 0fe0be57948f62a826d80ff6a3060a5a7574fe8c Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Sat, 2 Mar 2024 11:57:23 -0800 Subject: [PATCH 5/5] Change the way of hashing for updating email template seed --- db/seeds.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 8c6d7945..15bd64ef 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -3,8 +3,9 @@ require_relative "seed_data" SeedData.emails.each do |email_attrs| - # to, body fields are updated through development, should not be used as part of the uniqueness check - email_template = EmailTemplate.find_or_initialize_by(email_attrs.except(:to, :body)) + # Based on what Professor Ball mentioned, 'title' is the unique identifier for each EmailTemplate + unique_identifier = { title: email_attrs[:title] } + email_template = EmailTemplate.find_or_initialize_by(unique_identifier) email_template.update(email_attrs) end