summaryrefslogtreecommitdiff
path: root/test/rb/integration/simple_client.rb
blob: fe2ed821998d025383d6490301d120f6668429c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
require File.join(File.dirname(__FILE__), '../test_helper')

require 'thrift'
require 'thrift/protocol/binaryprotocol'
require 'ThriftTest'

class SimpleClientTest < Test::Unit::TestCase
  def setup    
    unless @socket
      @socket   = Thrift::Socket.new('localhost', 9090)
      @protocol = Thrift::BinaryProtocol.new(@socket)
      @client   = Thrift::Test::ThriftTest::Client.new(@protocol)
      @socket.open
    end
  end
  
  def test_string
    assert_equal(@client.testString('string'), 'string')
  end

  def test_byte
    val = 8
    assert_equal(@client.testByte(val), val)
    assert_equal(@client.testByte(-val), -val)
  end

  def test_i32
    val = 32
    assert_equal(@client.testI32(val), val)
    assert_equal(@client.testI32(-val), -val)
  end

  def test_i64
    val = 64
    assert_equal(@client.testI64(val), val)
    assert_equal(@client.testI64(-val), -val)
  end

  def test_double
    val = 3.14
    assert_equal(@client.testDouble(val), val)
    assert_equal(@client.testDouble(-val), -val)
    assert_kind_of(Float, @client.testDouble(val))
  end

  def test_map
    val = {1 => 1, 2 => 2, 3 => 3}
    assert_equal(@client.testMap(val), val)
    assert_kind_of(Hash, @client.testMap(val))
  end

  def test_list
    val = [1,2,3,4,5]
    assert_equal(@client.testList(val), val)
    assert_kind_of(Array, @client.testList(val))
  end

  def test_enum
    val = Thrift::Test::Numberz::SIX
    ret = @client.testEnum(val)

    assert_equal(ret, 6)
    assert_kind_of(Fixnum, ret)
  end

  def test_typedef
    #UserId  testTypedef(1: UserId thing),
    true
  end

  def test_set
    val = Set.new([1,2,3])
    assert_equal(@client.testSet(val), val)
    assert_kind_of(Set, @client.testSet(val))
  end

  def get_struct
    Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 })
  end

  def test_struct
    ret = @client.testStruct(get_struct)

    assert_nil(ret.byte_thing, nil)
    assert_nil(ret.i64_thing, nil)
    assert_equal(ret.string_thing, 'hi!')
    assert_equal(ret.i32_thing, 4)
    assert_kind_of(Thrift::Test::Xtruct, ret)
  end

  def test_nest
    struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10})

    ret = @client.testNest(struct2)

    assert_nil(ret.struct_thing.byte_thing, nil)
    assert_nil(ret.struct_thing.i64_thing, nil)
    assert_equal(ret.struct_thing.string_thing, 'hi!')
    assert_equal(ret.struct_thing.i32_thing, 4)
    assert_equal(ret.i32_thing, 10)

    assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing)
    assert_kind_of(Thrift::Test::Xtruct2, ret)
  end

  def test_insane
    insane = Thrift::Test::Insanity.new({
      'userMap' => { Thrift::Test::Numberz::ONE => 44 },
      'xtructs' => [get_struct,
        Thrift::Test::Xtruct.new({
          'string_thing' => 'hi again',
          'i32_thing' => 12
        })
      ]
    })

    ret = @client.testInsanity(insane)

    assert_not_nil(ret[44])
    assert_not_nil(ret[44][1])

    struct = ret[44][1]

    assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44)
    assert_equal(struct.xtructs[1].string_thing, 'hi again')
    assert_equal(struct.xtructs[1].i32_thing, 12)

    assert_kind_of(Hash, struct.userMap)
    assert_kind_of(Array, struct.xtructs)
    assert_kind_of(Thrift::Test::Insanity, struct)
  end

  def test_map_map
    ret = @client.testMapMap(4)
    assert_kind_of(Hash, ret)
    assert_equal(ret, { 4 => { 4 => 4}})
  end

  def test_exception
    assert_raise Thrift::Test::Xception do
      @client.testException('foo')
    end
  end
end