Skip to content

Commit

Permalink
Merge pull request #35 from cs169/187156425/bugfix/refract-test-email…
Browse files Browse the repository at this point in the history
…-reseed

Refract implementation & test cases for email template seed
  • Loading branch information
ArushC authored Mar 2, 2024
2 parents 771324f + 0fe0be5 commit df52d67
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 163 deletions.
5 changes: 3 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
161 changes: 0 additions & 161 deletions spec/controllers/email_templates_reseed_spec.rb

This file was deleted.

182 changes: 182 additions & 0 deletions spec/models/email_templates_reseed_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# frozen_string_literal: true

require "rails_helper"

# This test suite verifies the reseeding behavior of EmailTemplate models to ensure
# that email templates are correctly handled (i.e., updated or not-removal) without duplication
# 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.
let(:form_submission_old) do
<<-FORM_SUBMISSION
<p>Submitted Information:</p>
<p>I am the original content of the form submission email.</p>
FORM_SUBMISSION
end

let(:request_info_email_old) do
<<-REQUEST_INFO_EMAIL
<p>Dear {{teacher_first_name}},</p>
<p>I am the original content of the request info email.</p>
REQUEST_INFO_EMAIL
end

let(:request_info_email_new) do
<<-REQUEST_INFO_EMAIL
<p>Dear {{teacher_first_name}},</p>
<p>I am NEW Template Content of the request info email.</p>
REQUEST_INFO_EMAIL
end

def email_template_hash(body:, title:, subject:, path:)
{
to: title == "Form Submission" ?
"[email protected], [email protected]"
: "{{teacher_email}}, {{teacher_personal_email}}",
body:,
path:,
locale: nil,
handler: "liquid",
partial: false,
format: "html",
title:,
required: true,
subject:
}
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 "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
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

0 comments on commit df52d67

Please sign in to comment.