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

Add tests #5

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
59 changes: 59 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
PATH
remote: .
specs:
amazon_ses_mailer (0.1.0)
aws-sdk-sesv2 (~> 1.0)

GEM
remote: https://rubygems.org/
specs:
aws-eventstream (1.2.0)
aws-partitions (1.631.0)
aws-sdk-core (3.149.0)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.525.0)
aws-sigv4 (~> 1.1)
jmespath (~> 1, >= 1.6.1)
aws-sdk-sesv2 (1.27.0)
aws-sdk-core (~> 3, >= 3.127.0)
aws-sigv4 (~> 1.1)
aws-sigv4 (1.5.1)
aws-eventstream (~> 1, >= 1.0.2)
diff-lcs (1.5.0)
docile (1.3.5)
jmespath (1.6.1)
json (2.6.2)
rake (10.5.0)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.1)
simplecov (0.17.1)
docile (~> 1.1)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
simplecov-lcov (0.7.0)

PLATFORMS
ruby

DEPENDENCIES
amazon_ses_mailer!
bundler (~> 1.17)
rake (~> 10.0)
rspec (~> 3.0)
simplecov (~> 0.17.1)
simplecov-lcov (~> 0.7.0)

BUNDLED WITH
1.17.3
2 changes: 2 additions & 0 deletions amazon_ses_mailer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.17"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "simplecov", "~> 0.17.1"
spec.add_development_dependency "simplecov-lcov", "~> 0.7.0"

spec.add_runtime_dependency "aws-sdk-sesv2", "~> 1.0"
end
16 changes: 11 additions & 5 deletions lib/amazon_ses_mailer/base.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require "./lib/core_extensions/hash.rb"

module AmazonSesMailer
class Base
attr_reader :template_name

class << self
attr_accessor :default_options, :delivery_method

def deliveries
@@_deliveries ||= []
end
Expand Down Expand Up @@ -57,27 +59,31 @@ def process_merge_vars(merge_vars)
end

def convert_instance_variables_to_merge_vars
self.instance_variable_names.reduce({}) do |result, variable_name|
instance_variable_names.reduce({}) do |result, variable_name|
key = variable_name.delete "@"
value = self.instance_variable_get(variable_name)
value = instance_variable_get(variable_name)
result.merge!(key => value)
end
end

def transform_hash(hash)
hash.transform_values{ |value| transform_value(value) }
end

def transform_array(arr)
arr.map{|value| transform_value(value)}
end

def transform_value(value)
# recurse on hashes/arrays, converts nil/false values to empty strings, and converts all others to strings
return '' unless !!value
return transform_hash(value) if value.is_a?(Hash)
return transform_array(value) if value.is_a?(Array)
return value.to_s
end

def instance_variable_names
hammady marked this conversation as resolved.
Show resolved Hide resolved
instance_variables.map(&:to_s)
end
end
end
15 changes: 15 additions & 0 deletions lib/core_extensions/hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module CoreExtensions
hammady marked this conversation as resolved.
Show resolved Hide resolved
module Hash
def transform_values
return enum_for(:transform_values) { size } unless block_given?
return {} if empty?
result = self.class.new
each do |key, value|
result[key] = yield(value)
end
result
end
end
end

Hash.include CoreExtensions::Hash
18 changes: 18 additions & 0 deletions spec/amazon_ses_mailer/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
RSpec.describe AmazonSesMailer::Base do
let(:options) { {} }
let(:template_name) { '' }
let(:delivery_method) { nil }
subject { described_class.new(template_name) }

describe '#mail' do
before do
allow(AmazonSesMailer::Base).to receieve(:delivery_method)
.and_return(delivery_method)
end

it 'creates a new message' do
expect(AmazonSesMailer::Message).to receive(:new)
subject.mail(options)
end
end
end
10 changes: 10 additions & 0 deletions spec/core_extensions/hash_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RSpec.describe CoreExtensions::Hash do
describe '#transform_values' do
it 'transforms hash values' do
original = { a: 'a', b: 'b' }
mapped = original.transform_values { |val| "#{val}!" }

expect(mapped[:a]).to eq 'a!'
end
end
end
27 changes: 26 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
require "bundler/setup"
require "amazon_ses_mailer"
require 'simplecov'
require 'simplecov-lcov'

SimpleCov.start :rails do
if ENV['CI']
SimpleCov::Formatter::LcovFormatter.config do |config|
config.report_with_single_file = true
config.single_report_path = 'coverage/lcov.info'
end

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new [
SimpleCov::Formatter::LcovFormatter
]
else
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new [
SimpleCov::Formatter::HTMLFormatter
]
end
end

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand All @@ -10,5 +28,12 @@

config.expect_with :rspec do |c|
c.syntax = :expect
c.include_chain_clauses_in_custom_matcher_descriptions = true
end

config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.shared_context_metadata_behavior = :apply_to_host_groups
end