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
Extract email sending logic from deliver_poste_messages cron script into lib/cdo/poste.rb #31456
Conversation
…nto lib/cdo/poste.rb To make it easier to write unit tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great call. I have a couple of implementation questions before I approve.
end | ||
end | ||
|
||
class Deliverer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason not to give each class its own file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mostly a desire not to clutter up the already-large lib/cdo
directory
shared/test/test_deliverer.rb
Outdated
@@ -0,0 +1,48 @@ | |||
require_relative 'test_helper' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why'd you put a test for a class in lib/cdo
in shared/test
instead of lib/test/cdo
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, that's a much better location. I just looked for where the existing Poste
tests were
shared/test/test_deliverer.rb
Outdated
def setup | ||
@fake_smtp = FakeSmtp.new | ||
Deliverer.any_instance.stubs(:reset_connection).returns(@fake_smtp) | ||
@deliverer = Deliverer.new({}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great clean setup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for pointing me in this direction!
shared/test/test_deliverer.rb
Outdated
params: {}.to_json | ||
} | ||
@deliverer.send(delivery) | ||
message = @fake_smtp.instance_variable_get(:@message) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might read better to add
attr_reader :message, :from_address, :to_address
to FakeSmtp
. My only concern would be whether you think the test becomes misleading as documentation of how you'd typically use this class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a comment explaining why I'm doing that
shared/test/test_deliverer.rb
Outdated
# Sequel doesn't have a "find or create by", so we implement it manually | ||
message = POSTE_DB[:poste_messages].where(name: "dashboard").first | ||
message_id = message.nil? ? POSTE_DB[:poste_messages].insert({name: "dashboard"}) : message[:id] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL! Looks like it only exists if you're using Sequel models, which we don't actually do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Looks great!
Thanks for your help! |
Description
To make it easier to write unit tests; also, add some unit tests!
Note that this change also requires that we lazily populate the MESSAGE_TEMPLATES constant: 3966745
Otherwise, circle will fail to build because this code expects the database to already be set up as soon as the file gets required, rather than not until use.
This is a follow-up to #31421, as it adds the unit tests that would have caught the error had they existed.
Reviewer Checklist: