From 33e62cf94d7b5b50358a43a7d0dca4351dcb127b Mon Sep 17 00:00:00 2001 From: raymondberg Date: Tue, 22 Sep 2015 21:44:20 -0500 Subject: [PATCH] Bugfix - Repaired tests and replaced swath of missing code --- Rakefile | 8 ++++ lib/github_to_trello.rb | 36 +++++++++++------- spec/github_to_trello_spec.rb | 72 +++++++++++++++++++++++++++++++++++ spec/trello_gateway_spec.rb | 2 +- 4 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 spec/github_to_trello_spec.rb diff --git a/Rakefile b/Rakefile index 2995527..a184008 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,9 @@ require "bundler/gem_tasks" +require "rspec/core/rake_task" + +task :default => "spec:integration" + +desc "Run tests" +RSpec::Core::RakeTask.new("spec:integration") do |t| + t.pattern = "spec/*.rb" +end diff --git a/lib/github_to_trello.rb b/lib/github_to_trello.rb index b65b422..4139d32 100755 --- a/lib/github_to_trello.rb +++ b/lib/github_to_trello.rb @@ -5,12 +5,17 @@ class GithubToTrello - def initialize(public_key, token, board_id, repo_name) - @github_gateway = GithubGateway.new(repo_name) - @trello_gateway = TrelloGateway.new(public_key, - token, - board_id, - repo_name) + def initialize(options) + [:public_key, :token, :board_id, :repo_name].each do |required_field| + _raise_argument_error(required_field) unless options[required_field] + end + + @github_gateway = GithubGateway.new(options[:repo_name]) + @trello_gateway = TrelloGateway.new( + { + :inbox_name => options[:repo_name], + }.merge(options) + ) end def update @@ -18,19 +23,24 @@ def update @trello_gateway.create_or_update_card(issue) end end + + def _raise_argument_error(field) + raise ArgumentError, "Argument '#{field}' is required yet missing" + end end # Example usage with dotenv gem and .env file # # if __FILE__ == $PROGRAM_NAME -# public_key = ENV["PUBLIC_KEY"] -# token = ENV["TOKEN"] -# board_id = ENV["BOARD_ID"] -# repos = ENV["REPOS"].split(",") -# -# repos.each do |repo| +# ENV["REPOS"].split(",").each do |repo| # puts "Updating repo: #{repo}" -# GithubToTrello.new(public_key, token, board_id, repo).update +# +# GithubToTrello.new( +# :public_key => ENV["PUBLIC_KEY"], +# :token => ENV["TOKEN"], +# :board_id => ENV["BOARD_ID"], +# :repo_name => repo, +# ).update # end # end diff --git a/spec/github_to_trello_spec.rb b/spec/github_to_trello_spec.rb new file mode 100644 index 0000000..2f2a362 --- /dev/null +++ b/spec/github_to_trello_spec.rb @@ -0,0 +1,72 @@ +require 'rspec' +require_relative '../lib/github_to_trello' + +describe GithubToTrello do + before(:all) do + @options = { + :public_key => "abcdef", + :token => "ghilmnopqrstuvwxyz", + :board_id => "123456", + :repo_name => "chrissiedeist/django_blog", + :inbox_name => "django_blog", + } + end + + describe "new" do + it "sets up github and trello gateways" do + expect(GithubGateway).to receive(:new).with(@options[:repo_name]) + expect(TrelloGateway).to receive(:new).with(@options) + + gateway = GithubToTrello.new(@options) + end + + it "defaults to repository name when inbox isn't provided" do + allow(GithubGateway).to receive(:new).with(@options[:repo_name]) + + @options.delete(:inbox_name) + expected_options = { + :inbox_name => @options[:repo_name], + }.merge(@options) + + expect(TrelloGateway).to receive(:new).with(expected_options) + + gateway = GithubToTrello.new(@options) + end + + it "raises an error if any mandatory fields aren't passed" do + required_fields = [:public_key, :token, :board_id, :repo_name] + + (1..3).each do |field_count| + required_fields.combination(field_count).to_a.each do |field_combo| + bad_options = {} + field_combo.each do |field| + bad_options[field] = @options[field] + end + + expect{ GithubToTrello.new(bad_options)}.to raise_error(ArgumentError) + end + end + end + end + + describe "update" do + it "iterates over issues" do + issues = ["first", "second", "third"] + fake_github_gateway = fake_trello_gateway = double() + + + allow(GithubGateway).to receive(:new).and_return(fake_github_gateway) + allow(TrelloGateway).to receive(:new).and_return(fake_trello_gateway) + + gateway = GithubToTrello.new(@options) + + expect(fake_github_gateway).to receive(:issues).and_return(issues) + + issues.each do |issue| + expect(fake_trello_gateway).to receive(:create_or_update_card).with(issue) + end + + gateway.update + end + end +end diff --git a/spec/trello_gateway_spec.rb b/spec/trello_gateway_spec.rb index b0510be..a399f82 100644 --- a/spec/trello_gateway_spec.rb +++ b/spec/trello_gateway_spec.rb @@ -7,7 +7,7 @@ :public_key => "56acdaa7404ebcc8bbaffab18428d4d2", :token => "08f4481d00aba0091592ad9e0ce7e025ac9e238ead31852fe4a75270fbd562e9", :board_id => "5jGWvKui", - :repo_name => "django_blog", + :inbox_name => "django_blog", ) @issue = double(:issue,