Skip to content
This repository
  • 2 commits
  • 16 files changed
  • 0 comments
  • 1 contributor
Mar 07, 2012
Small change in routing for execution creation
* config/routes.rb: POST /plans/:plan_id/cases/:test_case_id/executions,
the only difference here is the use of /cases in the url instead of
/test_cases, just to match the use of /cases elsewhere.
* spec/routing/executions_routing_spec.rb: added a routing test for
this.
c5f3be9
Mar 12, 2012
Issue #68
* jaribio/app/controllers/executions_controller.rb: add :json as a
response type
* jaribio/app/controllers/test_cases_controller.rb: add :json as a
response type
(show): adding method back, works with either id or unique_key
* jaribio/config/routes.rb: remove the :except => :show from the
test_cases resources route
* jaribio/spec/controllers/test_cases_controller_spec.rb: add a couple
of tests for show
* jaribio_formatter/lib/jaribio/execution.rb: new file, this is an
activeresource object with a custom class method for recording a set
of results
* jaribio_formatter/lib/jaribio/plan.rb (open?): new method
* jaribio_formatter/lib/jaribio/record.rb (FAIL): changed from a value
of 0 to a value of 2 to match jaribio app expectations
(error): add attribute to store an error from running an test
* jaribio_formatter/lib/jaribio/remote_object.rb (configure): can call
this with an object providing the expected methods such as
RSpec.configuration to set the activeresource site, api_key, and
timeout, which is now customizable
* jaribio_formatter/lib/jaribio/rspec_formatter.rb: by default sets
the timeout to 5 seconds, but can be overridden by jaribio_timeout
rspec configuration
(close): implemented a first cut at this, but it is not well-tested and
has issues still
(record_result): fill in the error in record from the example failure
exception
* jaribio_formatter/lib/jaribio/test_case.rb: new file
* jaribio_formatter/lib/jaribio_formatter.rb: require some new files
* jaribio_formatter/spec/lib/jaribio/plan_spec.rb: new file
* jaribio_formatter/spec/lib/jaribio/remote_object_spec.rb: new file
* jaribio_formatter/spec/lib/jaribio/rspec_formatter_spec.rb: updating
specs
* jaribio_formatter/spec/spec_helper.rb: not important right now, sets
some variables specific to me and my dev jaribio
c3b5dc7
2  jaribio/app/controllers/executions_controller.rb
... ...
@@ -1,6 +1,6 @@
1 1
 class ExecutionsController < ApplicationController
2 2
   before_filter :authenticate_user!
3  
-  respond_to :js, :html
  3
+  respond_to :json, :js, :html
4 4
 
5 5
   def show
6 6
     @execution = Execution.find(params[:id])
13  jaribio/app/controllers/test_cases_controller.rb
... ...
@@ -1,6 +1,6 @@
1 1
 class TestCasesController < ApplicationController
2 2
   before_filter :authenticate_user!
3  
-  respond_to :html
  3
+  respond_to :json, :html
4 4
 
5 5
   def index
6 6
     cases = TestCase.scoped
@@ -16,6 +16,15 @@ def index
16 16
     respond_with @test_cases
17 17
   end
18 18
 
  19
+  def show
  20
+    if (params[:id].to_s =~ /\A[+-]?\d+\Z/)
  21
+      @test_case = TestCase.find(params[:id])
  22
+    else
  23
+      @test_case = TestCase.where(:unique_key => params[:id]).first
  24
+    end
  25
+    respond_with @test_case
  26
+  end
  27
+
19 28
   def new
20 29
     @test_case = TestCase.new
21 30
     @suites = Suite.select(:name)
@@ -105,7 +114,7 @@ def copy
105 114
     if @test_case.save
106 115
       flash[:notice] = "Successfully copied test case."
107 116
     end
108  
-    
  117
+
109 118
     redirect_to edit_test_case_path(@test_case)
110 119
   end
111 120
 
4  jaribio/config/routes.rb
@@ -9,7 +9,7 @@
9 9
   devise_for :users
10 10
   resources :token_authentications, :only => [:create, :destroy]
11 11
 
12  
-  resources :test_cases, :path => '/cases', :except => :show do
  12
+  resources :test_cases, :path => '/cases' do
13 13
     member do
14 14
       get 'executions'
15 15
       post 'sort'
@@ -28,7 +28,7 @@
28 28
       get 'open'
29 29
     end
30 30
 
31  
-    resources :test_cases, :only => [] do
  31
+    resources :test_cases, :path => '/cases', :only => [] do
32 32
       resources :executions, :only => [:create]
33 33
     end
34 34
   end
15  jaribio/spec/controllers/test_cases_controller_spec.rb
@@ -26,11 +26,26 @@
26 26
     end
27 27
   end
28 28
 
  29
+  describe "GET show" do
  30
+    it "assigns the requested case as @test_case" do
  31
+      test_case = Factory.create(:test_case)
  32
+      get :show, :id => test_case.id.to_s, :format => :json
  33
+      assigns(:test_case).should eq(test_case)
  34
+    end
  35
+
  36
+    it "can retrieve by unique key" do
  37
+      test_case = Factory.create(:test_case)
  38
+      get :show, :id => test_case.unique_key, :format => :json
  39
+      assigns(:test_case).should eq(test_case)
  40
+    end
  41
+  end
  42
+
29 43
   describe "GET executions" do
30 44
     it "assigns the requested case as @test_case" do
31 45
       test_case = Factory.create(:test_case)
32 46
       get :executions, :id => test_case.id.to_s
33 47
       assigns(:test_case).should eq(test_case)
  48
+      assigns(:executions).should be_empty
34 49
     end
35 50
   end
36 51
 
3  jaribio/spec/routing/executions_routing_spec.rb
@@ -7,5 +7,8 @@
7 7
       get("/executions/1").should route_to("executions#show", :id => "1")
8 8
     end
9 9
 
  10
+    it "routes to #create" do
  11
+      post("/plans/1/cases/1/executions").should route_to("executions#create", :plan_id => "1", :test_case_id => "1")
  12
+    end
10 13
   end
11 14
 end
39  jaribio_formatter/lib/jaribio/execution.rb
... ...
@@ -0,0 +1,39 @@
  1
+require 'jaribio/plan'
  2
+require 'jaribio/test_case'
  3
+
  4
+module Jaribio
  5
+  class Execution < RemoteObject
  6
+    self.prefix = '/plans/:plan_id/cases/:test_case_id/'
  7
+
  8
+    def self.record_results(records, plans = nil)
  9
+      if plans.nil?
  10
+        plans = Jaribio::Plan.find(:all, :from => :open)
  11
+      end
  12
+
  13
+      records.each do |record|
  14
+        begin
  15
+          test_case = Jaribio::TestCase.find(record.key)
  16
+        rescue ActiveResource::ResourceNotFound
  17
+          $stderr.puts "Test case #{record.key} not found"
  18
+          next
  19
+        end
  20
+        if (test_case.nil?)
  21
+          $stderr.puts "Test case #{record.key} not found"
  22
+          next
  23
+        end
  24
+
  25
+        plans.each do |plan|
  26
+          execution = Jaribio::Execution.new(
  27
+            :status_code => record.state,
  28
+            :results => record.error
  29
+          )
  30
+          begin
  31
+            execution.save({:plan_id => plan.id, :test_case_id => test_case.id})
  32
+          rescue Exception => e
  33
+            $stderr.puts "Error saving execution of #{test_case.id} for plan #{plan.id}: #{e.message}"
  34
+          end
  35
+        end
  36
+      end
  37
+    end
  38
+  end
  39
+end
7  jaribio_formatter/lib/jaribio/plan.rb
@@ -2,7 +2,12 @@
2 2
 
3 3
 module Jaribio
4 4
   class Plan < RemoteObject
5  
-    # Usage: Jaribio::Plan.find(1, :params => {'api_key' => 'asdf'})
  5
+    def open?
  6
+      if self.respond_to?(:closed_at)
  7
+        return (closed_at == nil or closed_at > Time.now)
  8
+      end
  9
+      true
  10
+    end
6 11
   end
7 12
 end
8 13
 
5  jaribio_formatter/lib/jaribio/record.rb
... ...
@@ -1,14 +1,15 @@
1 1
 module Jaribio
2 2
   class Record
3 3
     PASS = 1
4  
-    FAIL = 0
  4
+    FAIL = 2
5 5
 
6  
-    attr_accessor :key, :description, :state
  6
+    attr_accessor :key, :description, :state, :error
7 7
 
8 8
     def initialize(args = {})
9 9
       self.key = args[:key] 
10 10
       self.description = args[:description]
11 11
       self.state = args[:state]
  12
+      self.error = args[:error]
12 13
     end
13 14
 
14 15
     def failed?
11  jaribio_formatter/lib/jaribio/remote_object.rb
@@ -8,15 +8,20 @@ class RemoteObject < ActiveResource::Base
8 8
     # use json, not xml
9 9
     self.format = :json
10 10
 
11  
-    # set timeout to 5 seconds (does not affect DNS lookups generally)
12  
-    self.timeout = 5
13  
-
14 11
     class << self
  12
+      # Override query_string to automatically add the api_key
15 13
       def query_string(options)
16 14
         options = {} if options.nil?
17 15
         options[:api_key] = api_key unless api_key.nil?
18 16
         super(options)
19 17
       end
  18
+
  19
+      def configure(config)
  20
+        self.site = config.jaribio_url
  21
+        self.api_key = config.jaribio_api_key
  22
+        # set timeout (does not affect DNS lookups generally)
  23
+        self.timeout = config.jaribio_timeout
  24
+      end
20 25
     end
21 26
 
22 27
   end
57  jaribio_formatter/lib/jaribio/rspec_formatter.rb
... ...
@@ -1,5 +1,6 @@
1 1
 require 'jaribio/record'
2 2
 require 'rspec/core'
  3
+require 'cgi'
3 4
 
4 5
 module Jaribio
5 6
   class RSpecFormatter
@@ -46,7 +47,7 @@ def message(message)
46 47
 
47 48
     def stop
48 49
     end
49  
-   
  50
+
50 51
     def start_dump()
51 52
     end
52 53
 
@@ -59,8 +60,8 @@ def dump_failures()
59 60
     def dump_summary(duration, example_count, failure_count, pending_count)
60 61
       results.each do |key, data|
61 62
         output.puts "Key: #{key}"
62  
-        output.puts "Desc: #{data[:description]}"
63  
-        output.puts "Failed: #{data[:failed].to_s}"
  63
+        output.puts "Desc: #{data.description}"
  64
+        output.puts "Failed: #{data.failed?.to_s}"
64 65
       end
65 66
     end
66 67
 
@@ -75,10 +76,50 @@ def seed(seed)
75 76
     # - configured to generate plan/suite/test case?
76 77
     #
77 78
     def close()
  79
+      Jaribio::RemoteObject.configure(RSpec.configuration)
  80
+      # create missing test cases, the new test cases are not automatically
  81
+      # added to any plan
  82
+      if RSpec.configuration.jaribio_auto_create
  83
+        results.values.each do |record|
  84
+          begin
  85
+            test_case = Jaribio::TestCase.find(CGI::escape(record.key))
  86
+          rescue ActiveResource::ResourceNotFound
  87
+          end
  88
+          if (test_case.nil?)
  89
+            test_case = Jaribio::TestCase.new(
  90
+              :name => record.description,
  91
+              :unique_key => record.key,
  92
+              :automated => true
  93
+            )
  94
+            begin
  95
+              test_case.save
  96
+            rescue Exception => e
  97
+              $stderr.puts "Error creating test case #{record.key}: #{e.message}"
  98
+            end
  99
+          end
  100
+        end
  101
+      end
  102
+
  103
+      plans = nil
  104
+      # create executions for specific plans only
  105
+      if RSpec.configuration.jaribio_plans.size > 0
  106
+        RSpec.configuration.jaribio_plans.each do |plan_id|
  107
+          begin
  108
+            plan = Jaribio::Plan.find(plan_id)
  109
+            if plan.open?
  110
+              plans << plan
  111
+            end
  112
+          rescue ActiveResource::ResourceNotFound
  113
+            $stderr.puts "RSpec configuration of jaribio_plans includes unknown plan_id #{plan_id}"
  114
+          end
  115
+        end
  116
+      end
  117
+
  118
+      Jaribio::Execution.record_results(results.values, plans)
78 119
     end
79 120
 
80 121
     protected
81  
-      
  122
+
82 123
     def configuration
83 124
       RSpec.configuration
84 125
     end
@@ -88,7 +129,12 @@ def record_result(example, failed = false)
88 129
       if (@results.has_key?(key))
89 130
         failed = failed || @results[key].failed?
90 131
       end
91  
-      record = Record.new(:key => key, :description => desc, :state => failed ? Jaribio::Record::FAIL : Jaribio::Record::PASS)
  132
+      error = nil
  133
+      if (defined? example.execution_result[:exception])
  134
+        exception = example.execution_result[:exception]
  135
+        error = "#{exception.message}\n#{exception.backtrace}"
  136
+      end
  137
+      record = Record.new(:key => key, :description => desc, :state => failed ? Jaribio::Record::FAIL : Jaribio::Record::PASS, :error => error)
92 138
       @results[key] = record
93 139
     end
94 140
 
@@ -151,6 +197,7 @@ def find_jaribio_key(example)
151 197
     c.add_setting :jaribio_api_key
152 198
     c.add_setting :jaribio_plans, :default => []
153 199
     c.add_setting :jaribio_auto_create, :default => false
  200
+    c.add_setting :jaribio_timeout, :default => 5
154 201
   end
155 202
 end
156 203
 
5  jaribio_formatter/lib/jaribio/test_case.rb
... ...
@@ -0,0 +1,5 @@
  1
+module Jaribio
  2
+  class TestCase < RemoteObject
  3
+    self.element_name = 'case'
  4
+  end
  5
+end
2  jaribio_formatter/lib/jaribio_formatter.rb
... ...
@@ -1,3 +1,3 @@
1  
-%w(remote_object plan rspec_formatter record formatter/version).each do |file|
  1
+%w(remote_object execution plan test_case rspec_formatter record formatter/version).each do |file|
2 2
   require File.expand_path(File.join(File.dirname(__FILE__), 'jaribio', file))
3 3
 end
16  jaribio_formatter/spec/lib/jaribio/plan_spec.rb
... ...
@@ -0,0 +1,16 @@
  1
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
  2
+
  3
+describe "Jaribio::Plan" do
  4
+  describe "#open?" do
  5
+    it "should be true if closed_at is nil" do
  6
+      Jaribio::Plan.new.open?.should be_true
  7
+    end
  8
+
  9
+    it "should be false if closed_at is non-nil" do
  10
+      p = Jaribio::Plan.new()
  11
+      p.closed_at = Time.now
  12
+      p.open?.should be_false
  13
+    end
  14
+  end
  15
+
  16
+end
35  jaribio_formatter/spec/lib/jaribio/remote_object_spec.rb
... ...
@@ -0,0 +1,35 @@
  1
+require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
  2
+
  3
+describe "Jaribio::RemoteObject" do
  4
+  before do
  5
+    RSpec.configuration.stub!(:jaribio_api_key).and_return('asdf1234')
  6
+    Jaribio::RemoteObject.configure(RSpec.configuration)
  7
+  end
  8
+
  9
+  after(:all) do
  10
+    Jaribio::RemoteObject.configure(RSpec.configuration)
  11
+  end
  12
+
  13
+  describe "#query_string" do
  14
+    it "automatically adds the api_key as a query parameter to requests" do
  15
+      Jaribio::RemoteObject.element_path(1).should == '/remote_objects/1.json?api_key=asdf1234'
  16
+    end
  17
+  end
  18
+
  19
+  describe "#configure" do
  20
+
  21
+    it "jaribio url" do 
  22
+      Jaribio::RemoteObject.site.to_s.should == RSpec.configuration.jaribio_url
  23
+    end
  24
+
  25
+    it "jaribio api key" do
  26
+      Jaribio::RemoteObject.api_key.should == RSpec.configuration.jaribio_api_key
  27
+    end
  28
+
  29
+    it "jaribio timeout" do
  30
+      Jaribio::RemoteObject.timeout.should == RSpec.configuration.jaribio_timeout
  31
+    end
  32
+
  33
+  end
  34
+
  35
+end
16  jaribio_formatter/spec/lib/jaribio/rspec_formatter_spec.rb
@@ -84,7 +84,7 @@ def verify_key_and_description(example, expected_key, expected_description)
84 84
       formatter.results.keys.sort.should eql ['e2', 'g1', 'g1e2', 'object', 'object subgroup2']
85 85
     end
86 86
 
87  
-    it "values are a hash with description and failed state" do
  87
+    it "results are a hash of key to Jaribio::Record values" do
88 88
       formatter.results.should eql({
89 89
         'e2' => Jaribio::Record.new(:key => 'e2', :description => 'object example 2', :state => Jaribio::Record::FAIL), 
90 90
         'g1' => Jaribio::Record.new(:key => 'g1', :description => 'object subgroup', :state => Jaribio::Record::FAIL), 
@@ -99,21 +99,13 @@ def verify_key_and_description(example, expected_key, expected_description)
99 99
   it "creates new executions for open plans and existing test cases"
100 100
 
101 101
   describe "can configure" do
102  
-    before do
103  
-      RSpec.configure do |config|
104  
-        config.jaribio_url = 'http://localhost/jaribio'
105  
-        config.jaribio_api_key = 'asdf1234'
106  
-        config.jaribio_plans = ['plan 1', 'plan 2', 'plan 3']
107  
-        config.jaribio_auto_create = true
108  
-      end
109  
-    end
110 102
 
111 103
     it "jaribio url" do 
112  
-      RSpec.configuration.jaribio_url.should == 'http://localhost/jaribio'
  104
+      RSpec.configuration.should respond_to :jaribio_url
113 105
     end
114 106
 
115 107
     it "jaribio api key" do
116  
-      RSpec.configuration.jaribio_api_key.should == 'asdf1234'
  108
+      RSpec.configuration.should respond_to :jaribio_api_key
117 109
     end
118 110
 
119 111
     it "test case creation" do
@@ -121,7 +113,7 @@ def verify_key_and_description(example, expected_key, expected_description)
121 113
     end
122 114
 
123 115
     it "specific plans" do
124  
-      RSpec.configuration.jaribio_plans.should eql(['plan 1', 'plan 2', 'plan 3'])
  116
+      RSpec.configuration.should respond_to :jaribio_plans
125 117
     end
126 118
   end
127 119
 
4  jaribio_formatter/spec/spec_helper.rb
@@ -14,4 +14,8 @@
14 14
 Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
15 15
 
16 16
 RSpec.configure do |config|
  17
+  config.jaribio_url = 'http://jaribio.dev'
  18
+  config.jaribio_api_key = 'kpwLvp4JNSMpxskZSypq'
  19
+  config.jaribio_auto_create = true
  20
+  config.jaribio_timeout = 30
17 21
 end

No commit comments for this range

Something went wrong with that request. Please try again.