xsd-reader 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,169 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe XsdReader::Element do
4
+
5
+ before :all do
6
+ @reader ||= XsdReader::XML.new(:xsd_file => File.expand_path(File.join(File.dirname(__FILE__), 'examples', 'ddex-ern-v36.xsd')))
7
+ @element = @reader.elements[0]
8
+ end
9
+
10
+ it "gives the element's name" do
11
+ expect(@element.name).to eq 'NewReleaseMessage'
12
+ end
13
+
14
+ it "gives a complex type reader" do
15
+ expect(@element.complex_type.class).to eq XsdReader::ComplexType
16
+ end
17
+
18
+ it "gives child elements defined within a complex type" do
19
+ # byebug
20
+ expect(@element.elements.map(&:name)).to eq [
21
+ "MessageHeader",
22
+ "UpdateIndicator",
23
+ "IsBackfill",
24
+ "CatalogTransfer",
25
+ "WorkList",
26
+ "CueSheetList",
27
+ "ResourceList",
28
+ "CollectionList",
29
+ "ReleaseList",
30
+ "DealList"]
31
+ end
32
+
33
+ describe "External type definition" do
34
+ before :each do
35
+ @element = @element.elements.first
36
+ end
37
+
38
+ it "gives the type name" do
39
+ expect(@element.type_name).to eq 'MessageHeader'
40
+ end
41
+
42
+ it "gives the type namespace" do
43
+ expect(@element.type_namespace).to eq 'ern'
44
+ end
45
+
46
+ it "gives the type string" do
47
+ expect(@element.type).to eq 'ern:MessageHeader'
48
+ end
49
+
50
+ it "gives the remote complex_type, linked by type" do
51
+ expect(@element.complex_type.name).to eq 'MessageHeader'
52
+ end
53
+
54
+ it "includes child elements defined in external the complex type type definitions" do
55
+ expected = [
56
+ "MessageThreadId",
57
+ "MessageId",
58
+ "MessageFileName",
59
+ "MessageSender",
60
+ "SentOnBehalfOf",
61
+ "MessageRecipient",
62
+ "MessageCreatedDateTime",
63
+ "MessageAuditTrail",
64
+ "Comment",
65
+ "MessageControlType"]
66
+
67
+ expect(@element.elements.map(&:name)).to eq expected
68
+ expect(@element.complex_type.all_elements.map(&:name)).to eq expected
69
+ end
70
+ end
71
+
72
+ describe "#elements" do
73
+
74
+ it "includes elements within a choice node" do
75
+ el = @element.elements[3]
76
+ expect(el.name).to eq 'CatalogTransfer'
77
+ elements_without = ["CatalogTransferCompleted", "EffectiveTransferDate", "CatalogReleaseReferenceList", "TransferringFrom", "TransferringTo"]
78
+ elements_with = ["CatalogTransferCompleted", "EffectiveTransferDate", "CatalogReleaseReferenceList", "TerritoryCode", "ExcludedTerritoryCode", "TransferringFrom", "TransferringTo"]
79
+ expect(el.complex_type.sequences.map{|seq| seq.elements}.flatten.map(&:name)).to eq elements_without
80
+ expect(el.complex_type.all_elements.map(&:name)).to eq elements_with
81
+ expect(el.complex_type.sequences[0].choices.map{|ch| ch.elements}.flatten.map(&:name)).to eq elements_with - elements_without
82
+ end
83
+
84
+ it "gives child elements in the right order" do
85
+ expected = [
86
+ "CommercialModelType",
87
+ "Usage",
88
+ "AllDealsCancelled",
89
+ "TakeDown",
90
+ "TerritoryCode",
91
+ "ExcludedTerritoryCode",
92
+ "DistributionChannel",
93
+ "ExcludedDistributionChannel",
94
+ "PriceInformation",
95
+ "IsPromotional",
96
+ "PromotionalCode",
97
+ "ValidityPeriod",
98
+ "ConsumerRentalPeriod",
99
+ "PreOrderReleaseDate",
100
+ "ReleaseDisplayStartDate",
101
+ "TrackListingPreviewStartDate",
102
+ "CoverArtPreviewStartDate",
103
+ "ClipPreviewStartDate",
104
+ "PreOrderPreviewDate",
105
+ "IsExclusive",
106
+ "RelatedReleaseOfferSet",
107
+ "PhysicalReturns",
108
+ "NumberOfProductsPerCarton",
109
+ "RightsClaimPolicy",
110
+ "WebPolicy"]
111
+
112
+ # byebug
113
+
114
+ expect(@reader['NewReleaseMessage']['DealList']['ReleaseDeal']['Deal']['DealTerms'].elements.map(&:name)).to eq expected
115
+ end
116
+ end
117
+
118
+ # # this is pretty slow...
119
+ # describe "#family_tree" do
120
+ # before :each do
121
+ # @element = @reader.elements[0]
122
+ # end
123
+
124
+ # it "gives a family tree as a nested list of children, keys being nodes name, values being either another hash of children of strings with the names of the children" do
125
+ # expect(@element.family_tree.keys).to eq [
126
+ # "MessageHeader",
127
+ # "UpdateIndicator",
128
+ # "IsBackfill",
129
+ # "CatalogTransfer",
130
+ # "WorkList",
131
+ # "CueSheetList",
132
+ # "ResourceList",
133
+ # "CollectionList",
134
+ # "ReleaseList",
135
+ # "DealList"
136
+ # ]
137
+
138
+ # expect(@element.family_tree['ResourceList'].keys).to eq [
139
+ # "SoundRecording",
140
+ # "MIDI",
141
+ # "Video",
142
+ # "Image",
143
+ # "Text",
144
+ # "SheetMusic",
145
+ # "Software",
146
+ # "UserDefinedResource"
147
+ # ]
148
+
149
+ # expect(@element.family_tree['DealList']['ReleaseDeal']['Deal']['DealTerms']['RightsClaimPolicy']['Condition']['Unit']).to eq 'type:UnitOfConditionValue'
150
+ # end
151
+ # end
152
+
153
+ describe '#attributes' do
154
+ it "gives attributes defined in a complexType" do
155
+ expected = [
156
+ "MessageSchemaVersionId",
157
+ "BusinessProfileVersionId",
158
+ "ReleaseProfileVersionId",
159
+ "LanguageAndScriptCode"]
160
+ expect(@element.attributes.map(&:name)).to eq expected
161
+ expect(@element.complex_type.attributes.map(&:name)).to eq expected
162
+ end
163
+
164
+ it "gives attributes defined in a simpleContent extension" do
165
+ expect(@element['ResourceList']['SoundRecording']['SoundRecordingDetailsByTerritory']['DisplayArtist']['ArtistRole'].attributes.map(&:name)).to eq ["Namespace", "UserDefinedValue"]
166
+ end
167
+ end
168
+
169
+ end