File: xml_object_test.rb

package info (click to toggle)
ruby-roxml 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 800 kB
  • sloc: ruby: 4,133; xml: 1,013; makefile: 7
file content (207 lines) | stat: -rw-r--r-- 6,861 bytes parent folder | download | duplicates (3)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# encoding: utf-8
require_relative './../test_helper'
require 'minitest/autorun'

class EmptyCart
  include ROXML

  xml_reader :id

  def empty?
    true
  end
end

class CartHolder
  include ROXML

  xml_reader :cart, :as => EmptyCart, :required => true
end

class TestXMLObject < Minitest::Test
  # Test book with text and attribute
  def test_book_author_text_attribute
    book = BookWithAuthorTextAttribute.from_xml(fixture(:book_text_with_attribute))
    assert_equal("primary",book.author.role)
    assert_equal("David Thomas",book.author.text)
  end

  # Test XML object containing list of other XML objects (one-to-many)
  # In this case, book with contibutions
  def test_one_to_many_with_container
    expected_authors = ["David Thomas","Andrew Hunt","Chad Fowler"]
    book = BookWithContributions.from_xml(fixture(:book_with_contributions))
    assert_equal("Programming Ruby - 2nd Edition", book.title)
    book.contributions.each do |contributor|
      assert expected_authors.include?(contributor.name)
    end
  end

  # Test XML object containing 1-n other XML objects without container (one-to-many)
  # In this case, book with contibutions
  def test_one_to_many_without_container
    expected_contributors = ["David Thomas","Andrew Hunt","Chad Fowler"]
    book = BookWithContributors.from_xml(fixture(:book_with_contributors))
    assert_equal("Programming Ruby - 2nd Edition", book.title)
    book.contributors.each do |contributor|
      assert(expected_contributors.include?(contributor.name))
    end
  end

  # when building objects that contain arrays, the second object seems to
  # inherit data from the first
  #
  def test_one_to_many_without_container_sequence
    contrib = WriteableContributor.new
    contrib.name = "David Thomas"

    book_one = WriteableBookWithContributors.new
    book_one.isbn = "9781843549161"
    book_one.title = "Anathem"
    book_one.description = "A new title from Neal Stephenson"
    book_one.contributors = [contrib]

    # this book should be completely empty
    book_two = WriteableBookWithContributors.new

    assert_nil book_two.isbn
    assert_nil book_two.title
    assert_nil book_two.description
    assert_equal [],  book_two.contributors
  end

  # Test XML object containing one other XML object (one-to-one)
  # In this case, book with publisher
  def test_one_to_one
    book = BookWithPublisher.from_xml(fixture(:book_with_publisher))
    assert_equal("Programming Ruby - 2nd Edition", book.title)
    assert_equal("Pragmatic Bookshelf", book.publisher.name)
  end

  # Test XML object containing type of self (self-reference)
  def test_self_reference
    book = BookPair.from_xml(fixture(:book_pair))
    assert_equal("Programming Ruby - 2nd Edition", book.title)
    assert_equal("Agile Web Development with Rails", book.book.title)
  end

  # Test three-level composition (one-to-many-to-many)
  def test_one_to_many_to_many
    expected_contributors = ["David Thomas","Andrew Hunt","Chad Fowler", "David Heinemeier Hansson"]
    expected_books = ["Programming Ruby - 2nd Edition", "Agile Web Development with Rails"]
    library = Library.from_xml(fixture(:library))
    assert_equal("Ruby library", library.name)
    assert !library.books.empty?
    library.books.each do |book|
      assert expected_books.include?(book.title)
      book.contributions.each do |contributor|
        assert(expected_contributors.include?(contributor.name))
      end
    end
  end

  def test_without_needed_from
    assert_equal [], UppercaseLibrary.from_xml(fixture(:library)).books
    assert_equal [], Library.from_xml(fixture(:library_uppercase)).books
  end

  def test_with_needed_from
    assert Library.from_xml(fixture(:library)).books
    assert UppercaseLibrary.from_xml(fixture(:library_uppercase)).books
  end

  def test_with_recursion
    p = PersonWithMother.from_xml(fixture(:person_with_mothers))
    assert_equal 'Ben Franklin', p.name
    assert_equal 'Abiah Folger', p.mother.name
    assert_equal 'Madeup Mother', p.mother.mother.name
    assert_nil p.mother.mother.mother
  end

  class Node
    include ROXML

    xml_reader :name, :from => 'node_name'
    xml_reader :nodes, :as => [Node]
  end

  class Taxonomy
    include ROXML

    xml_reader :name, :from => 'taxonomy_name'
    xml_reader :nodes, :as => [Node]
  end

  class Taxonomies
    include ROXML
    xml_reader :taxonomies, :as => [Taxonomy]
  end

  def test_more_recursion
    taxonomies = Taxonomies.from_xml(<<HERE)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taxonomies SYSTEM "taxonomy.dtd">
<taxonomies>
	<taxonomy>
		<taxonomy_name>World</taxonomy_name>
		<node node_id="2" content_object_id="82534" object_type_id="2">
			<node_name lang_iso="eng">Africa</node_name>
			<node node_id="331" content_object_id="11" object_type_id="4">
				<node_name lang_iso="eng">Algeria</node_name>
				<node node_id="7271" content_object_id="117629" object_type_id="8">
					<node_name lang_iso="eng">Algiers</node_name>
				</node>
				<node node_id="7272" content_object_id="117630" object_type_id="8">
					<node_name lang_iso="eng">Gharda&#239;a</node_name>
				</node>
				<node node_id="7871" content_object_id="1000713999" object_type_id="8">
					<node_name lang_iso="eng">El Oued</node_name>
				</node>
				<node node_id="7872" content_object_id="1000714008" object_type_id="8">
					<node_name lang_iso="eng">Timimoun</node_name>
				</node>
				<node node_id="8903" content_object_id="1000565565" object_type_id="8">
					<node_name lang_iso="eng">Annaba</node_name>
				</node>
			</node>
		</node>
	</taxonomy>
</taxonomies>
HERE
    assert_equal 1, taxonomies.taxonomies.size
    assert_equal 'World', taxonomies.taxonomies.first.name
    node = taxonomies.taxonomies.first.nodes.first
    assert_equal 'Africa', node.name
    assert_equal 'Algeria', node.nodes.first.name
    assert_equal ['Algiers', "Ghardaïa", 'El Oued', 'Timimoun', 'Annaba'],
      node.nodes.first.nodes.map(&:name)
  end

  def test_with_guarded_recursion
    p = PersonWithGuardedMother.from_xml(fixture(:person_with_guarded_mothers))
    assert_equal 'Ben "Benji" Franklin', p.name
    assert_equal 'Abiah \'Abby\' Folger', p.mother.name
    assert_equal 'Madeup Mother < the third >', p.mother.mother.name
    assert_nil p.mother.mother.mother
  end

  def test_recursive_with_default_initialization
    p = PersonWithMotherOrMissing.from_xml(fixture(:person_with_mothers))
    assert_equal 'Unknown', p.mother.mother.mother.name
    assert_equal Person, p.mother.mother.mother.class
  end

  def test_defining_empty_on_object_doesnt_cause_it_to_be_seen_as_absent
    # absent means defaulting, failing required

    holder = CartHolder.from_xml(%{
      <cartholder>
        <cart>
          <id>111111</id>
        </cart>
      </cartholder>
    })

    assert_equal "111111", holder.cart.id
  end
end