res0nat0r / ruby-aws

Ruby/AWS is a Ruby language library that makes it relatively easy for the programmer to retrieve information from the popular Amazon Web site via Amazon's Associates Web Services (AWS).

This URL has Read+Write access

ruby-aws / test / tc_serialisation.rb
100644 104 lines (79 sloc) 2.517 kb
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
# $Id: tc_serialisation.rb,v 1.2 2008/06/22 21:18:50 ianmacd Exp $
#
 
require 'test/unit'
require './setup'
require 'yaml'
require 'tempfile'
 
class AWSSerialisationTest < AWSTest
 
  def test_yaml_load
 
    results_file = Tempfile.new( 'ruby_aws' )
 
    # Serialise some results.
    #
    is = ItemSearch.new( 'Music', { 'Artist' => 'Voice Of The Beehive' } )
    response = @req.search( is, @rg )
    results = response.kernel
 
    YAML.dump( results, results_file )
    results = nil
 
    # Remove knowledge of Amazon::AWS::AWSObject, so that YAML.load knows
    # nothing of its subclasses.
    #
    Amazon::AWS.module_eval( %Q( remove_const :AWSObject ) )
 
    # Silence warnings about redefined constants.
    #
    v = $VERBOSE
    $VERBOSE = nil
 
    # Reload Amazon::AWS and friends.
    #
    load 'amazon/aws.rb'
 
    # Reset warning status.
    #
    $VERBOSE = v
 
    # Demonstrate that normal YAML.load can't cope with instantiating objects
    # from classes it knows nothing about.
    #
    results_file.open
    results = YAML.load( results_file )
    assert_instance_of( YAML::Object, results[0] )
 
    # Ensure that AWSObject.yaml_load does the right thing.
    #
    results_file.open
    results = Amazon::AWS::AWSObject.yaml_load( results_file )
    assert_instance_of( Amazon::AWS::AWSObject::Item, results[0] )
  end
 
 
  def test_marshal_load
 
    results_file = Tempfile.new( 'ruby_aws' )
 
    # Serialise some results.
    #
    is = ItemSearch.new( 'Music', { 'Artist' => 'Voice Of The Beehive' } )
    response = @req.search( is, @rg )
    results = response.kernel
 
    results_file.puts Marshal.dump( results )
    results = nil
 
    # Remove knowledge of Amazon::AWS::AWSObject, so that Marshal.load knows
    # nothing of its subclasses.
    #
    Amazon::AWS.module_eval( %Q( remove_const :AWSObject ) )
 
    # Silence warnings about redefined constants.
    #
    v = $VERBOSE
    $VERBOSE = nil
 
    # Reload Amazon::AWS and friends.
    #
    load 'amazon/aws.rb'
 
    # Reset warning status.
    #
    $VERBOSE = v
 
    # Demonstrate that normal Marshal.load can't cope with instantiating
    # objects from classes it knows nothing about.
    #
    results_file.open
 
    assert_raise ArgumentError do
      Marshal.load( results_file )
    end
 
    # Ensure that Amazon::AWS::AWSObject.load does the right thing.
    #
    results_file.open
    results = Amazon::AWS::AWSObject.load( results_file )
    assert_instance_of( Amazon::AWS::AWSObject::Item, results[0] )
  end
end