| | 270 | def test_find_one_complex |
|---|
| | 271 | side_1 = { :id => 1, :length => 10 } |
|---|
| | 272 | side_2 = { :id => 2, :length => 20 } |
|---|
| | 273 | rectangle = { :id => 2, :name => 'square', :sides => [ side_1, side_2 ]}.to_xml(:root => 'shape') |
|---|
| | 274 | |
|---|
| | 275 | ActiveResource::HttpMock.respond_to { |m| m.get "/shapes/2.xml", {}, rectangle } |
|---|
| | 276 | |
|---|
| | 277 | shape = Shape.find(2) |
|---|
| | 278 | assert_equal 2, shape.id |
|---|
| | 279 | assert_equal 'square', shape.name |
|---|
| | 280 | assert_equal 2, shape.sides.length |
|---|
| | 281 | assert_equal side_1, shape.sides[0].attributes.symbolize_keys |
|---|
| | 282 | assert_equal side_2, shape.sides[1].attributes.symbolize_keys |
|---|
| | 283 | end |
|---|
| | 284 | |
|---|
| | 285 | def test_find_one_complex_from_xml |
|---|
| | 286 | shapes_xml = "<shapes><polygons><square>blue</square><square>gold</square></polygons></shapes>" |
|---|
| | 287 | |
|---|
| | 288 | ActiveResource::HttpMock.respond_to { |m| m.get "/shapes.xml", {}, shapes_xml } |
|---|
| | 289 | |
|---|
| | 290 | shapes = Shape.find(:all) |
|---|
| | 291 | assert_equal 1, shapes.length |
|---|
| | 292 | assert_kind_of Array, shapes.first.square |
|---|
| | 293 | assert_equal 2, shapes.first.square.length |
|---|
| | 294 | assert shapes.first.square.include?('blue') |
|---|
| | 295 | assert shapes.first.square.include?('gold') |
|---|
| | 296 | end |
|---|
| | 297 | |
|---|
| | 298 | def test_find_all_complex |
|---|
| | 299 | circle = { :id => 1, :name => 'circle' } |
|---|
| | 300 | square = { :id => 2, :name => 'square' } |
|---|
| | 301 | rectangle = { :id => 3, :name => 'rectangle' } |
|---|
| | 302 | shapes = { :circle => circle, :polygons => [square, rectangle] }.to_xml(:root => 'shapes') |
|---|
| | 303 | |
|---|
| | 304 | ActiveResource::HttpMock.respond_to { |m| m.get "/shapes.xml", {}, shapes } |
|---|
| | 305 | |
|---|
| | 306 | shapes = Shape.find(:all) |
|---|
| | 307 | assert_equal 2, shapes.length |
|---|
| | 308 | assert_equal circle, shapes[0].attributes.symbolize_keys |
|---|
| | 309 | assert_equal 2, shapes[1].length |
|---|
| | 310 | assert_equal square, shapes[1][0].attributes.symbolize_keys |
|---|
| | 311 | assert_equal rectangle, shapes[1][1].attributes.symbolize_keys |
|---|
| | 312 | end |
|---|
| | 313 | |
|---|
| | 314 | def test_find_all_complex_from_xml |
|---|
| | 315 | shapes_xml = "<shapes><polygons><square>blue</square><square>gold</square></polygons><circles><round>big</round></circles></shapes>" |
|---|
| | 316 | |
|---|
| | 317 | ActiveResource::HttpMock.respond_to { |m| m.get "/shapes.xml", {}, shapes_xml } |
|---|
| | 318 | |
|---|
| | 319 | shapes = Shape.find(:all) |
|---|
| | 320 | assert_equal 2, shapes.length |
|---|
| | 321 | assert_kind_of String, shapes[0].round |
|---|
| | 322 | assert_equal 'big', shapes[0].round |
|---|
| | 323 | assert_kind_of Array, shapes[1].square |
|---|
| | 324 | assert shapes[1].square.include?('blue') |
|---|
| | 325 | assert shapes[1].square.include?('gold') |
|---|
| | 326 | end |
|---|
| | 327 | |
|---|