Project

General

Profile

Download (2.33 KB) Statistics
| Branch: | Tag: | Revision:

runcible / test / unit / base_test.rb @ master

1
require 'rubygems'
2
require 'minitest/autorun'
3
require 'minitest/mock'
4
require './lib/runcible/base'
5
require './test/support/logger_support'
6

    
7
module Base
8
  class TestBase < MiniTest::Unit::TestCase
9
    def setup
10
      @logger = ::Runcible::Logger.new
11
      @my_runcible = Runcible::Base.new(
12
        :base_url => 'http://localhost/',
13
        :user     => 'test_user',
14
        :password => 'test_password',
15
        :headers  => { :content_type => 'application/json',
16
                       :accept       => 'application/json' },
17
        :logging  => { :logger => @logger }
18
      )
19

    
20
      @log_message = 'Fake log message.'
21
    end
22

    
23
    def test_config
24
      refute_nil @my_runcible.config
25
    end
26

    
27
    def test_process_response_returns_hash
28
      body = { 'a' => 'test', 'b' => 'data' }
29
      json = body.to_json
30
      response = OpenStruct.new(:body => json)
31
      data = @my_runcible.process_response(response)
32

    
33
      assert_equal 'test', data.body['a']
34
      assert_equal body, data.to_hash
35
    end
36

    
37
    def test_process_response_returns_nil
38
      body = nil
39
      json = nil.to_json
40

    
41
      response = OpenStruct.new(:body => json)
42
      data = @my_runcible.process_response(response)
43

    
44
      assert_equal body, data.to_hash
45
    end
46

    
47
    def test_process_response_returns_string
48
      response = OpenStruct.new(:body => 'Test body')
49
      data = @my_runcible.process_response(response)
50

    
51
      assert_equal 'Test body', data.body
52
    end
53

    
54
    def test_verbose_logger
55
      @my_runcible.config[:logging][:debug] = true
56
      @my_runcible.logs << @log_message
57
      @my_runcible.log_debug
58

    
59
      assert_equal @log_message, @logger.message
60
    end
61

    
62
    def test_exception_logger
63
      @my_runcible.config[:logging][:exception] = true
64
      @my_runcible.logs << @log_message
65
      @my_runcible.log_exception
66

    
67
      assert_equal @log_message, @logger.message
68
    end
69

    
70
    def test_generate_payload
71
      options = {:payload => 'abc123'}
72
      assert_equal 'abc123', @my_runcible.generate_payload(options)
73

    
74
      payload = {:required => {:a => '1',
75
                               :b => '2'
76
                              },
77
                 :optional => {:c => '3'
78
                              }
79
      }
80
      result = {:a => '1',
81
                :b => '2',
82
                :c => '3'
83
      }
84

    
85
      assert_equal result.to_json, @my_runcible.generate_payload(:payload => payload)
86
    end
87
  end
88
end