Skip to content

Commit 4efe669

Browse files
committed
Use SchemaHelper for testing
1 parent 66825be commit 4efe669

3 files changed

Lines changed: 94 additions & 93 deletions

File tree

gems/smithy-schema/lib/smithy-schema/document_utils.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def format(data)
1717
when Time
1818
data.to_i # timestamp format is "epoch-seconds" by default
1919
when Hash
20-
data.transform_values { |v| format(v) }
20+
data.each_with_object({}) do |(k, v), h|
21+
h[k.to_s] = format(v)
22+
end
2123
when Array
2224
data.map { |d| format(d) }
2325
else
@@ -130,7 +132,7 @@ def extract_list(data, shape)
130132

131133
def extract_map(data, shape)
132134
shape = shape_reference(shape)
133-
data.each.with_object({}) { |(k, v), h| h[k] = extract(v, shape.value) }
135+
data.each.with_object({}) { |(k, v), h| h[k.to_s] = extract(v, shape.value) }
134136
end
135137

136138
def extract_structure(data, shape, opts)
@@ -140,6 +142,7 @@ def extract_structure(data, shape, opts)
140142

141143
member_shape = shape.member(k)
142144
member_name = resolve_member_name(member_shape, opts)
145+
pp member_name
143146
o[member_name] = extract(v, member_shape, opts)
144147
end
145148
end

gems/smithy-schema/spec/smithy-schema/document_spec.rb

Lines changed: 75 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,33 @@
11
# frozen_string_literal: true
22

33
require_relative '../spec_helper'
4+
require_relative '../support/schema_helper'
45

56
module Smithy
67
module Schema
78
describe Document do
8-
let(:simple_runtime) do
9-
Struct.new(:string, keyword_init: true) do
10-
include Smithy::Schema::Structure
11-
end
12-
end
9+
let(:structure_shape) { SchemaHelper.sample_schema.const_get(:Structure) }
1310

1411
let(:simple_schema) do
1512
shape = Shapes::StructureShape.new(id: 'smithy.ruby.tests#SimpleStructure')
1613
string = Shapes::StringShape.new(id: 'smithy.api#String')
17-
shape.add_member(:string, 'stringMember', string)
14+
shape.add_member(:string, 'string', string)
1815
shape.type = simple_runtime
1916
shape
2017
end
2118

22-
let(:runtime) do
23-
Struct.new(:string, :list, :foo_map, :structure, :union, :blob, :timestamp, keyword_init: true) do
19+
let(:simple_runtime) do
20+
Struct.new(:string, keyword_init: true) do
2421
include Smithy::Schema::Structure
2522
end
2623
end
2724

28-
let(:union_runtime) { Class.new(Union) }
29-
let(:union_value_runtime) do
30-
Class.new(union_runtime) do
31-
def to_h
32-
{ union_string: super(__getobj__) }
33-
end
34-
35-
# anonymous class, need a class name to test to_s
36-
def self.name
37-
'TestUnion::UnionString'
38-
end
39-
end
40-
end
41-
42-
let(:schema) do
43-
shape = Shapes::StructureShape.new(id: 'smithy.ruby.tests#Structure')
44-
string = Shapes::StringShape.new(id: 'smithy.api#String')
45-
list = Shapes::ListShape.new(id: 'smithy.ruby.tests#List')
46-
list.set_member(Shapes::Prelude::String)
47-
map = Shapes::MapShape.new(id: 'smithy.ruby.tests#Map')
48-
map.set_key(Shapes::Prelude::String)
49-
map.set_value(list)
50-
union = Shapes::UnionShape.new(id: 'smithy.ruby.tests#Union')
51-
union.add_member(
52-
:union_string,
53-
'unionString',
54-
string,
55-
union_value_runtime,
56-
traits: { 'smithy.api#jsonName' => 'json' }
57-
)
58-
union.type = union_runtime
59-
shape.add_member(:string, 'stringMember', string, traits: { 'smithy.api#jsonName' => 'json' })
60-
shape.add_member(:list, 'listMember', list)
61-
shape.add_member(:foo_map, 'mapMember', map)
62-
shape.add_member(:union, 'unionMember', union)
63-
shape.add_member(
64-
:timestamp,
65-
'timeMember',
66-
Shapes::TimestampShape.new(id: 'smithy.ruby.tests#Timestamp'),
67-
traits: { 'smithy.api#timestampFormat' => 'http-date' }
68-
)
69-
shape.add_member(:blob, 'blobMember', Shapes::BlobShape.new(id: 'smithy.ruby.tests#Blob'))
70-
shape.add_member(:structure, 'structureMember', shape)
71-
shape.type = runtime
72-
shape
73-
end
74-
7525
context 'untyped document' do
76-
subject { Document.new('foo') }
26+
subject { Document.new(foo: 'bar') }
7727

7828
describe '#initialize' do
7929
it 'sets data' do
80-
expect(subject.data).to eq('foo')
30+
expect(subject.data).to eq('foo' => 'bar')
8131
end
8232

8333
it 'sets time data using default format' do
@@ -94,11 +44,11 @@ def self.name
9444
subject { Document.new({ foo: 'bar' }) }
9545

9646
it 'returns member value' do
97-
expect(subject[:foo]).to eq('bar')
47+
expect(subject['foo']).to eq('bar')
9848
end
9949

10050
it 'returns nil when member key is not applicable' do
101-
expect(subject[:bar]).to be_nil
51+
expect(subject['bar']).to be_nil
10252
end
10353
end
10454

@@ -110,8 +60,8 @@ def self.name
11060

11161
describe '#as_typed' do
11262
it 'converts document as runtime shape' do
113-
typed_shape = Document.new({ string: 'foo' }).as_typed(simple_schema)
114-
expect(typed_shape).to be_a(simple_runtime)
63+
typed_shape = Document.new({ string: 'foo' }).as_typed(structure_shape)
64+
expect(typed_shape).to be_a(structure_shape.type)
11565
expect(typed_shape[:string]).to eq('foo')
11666
end
11767

@@ -127,47 +77,73 @@ def self.name
12777
context 'typed document' do
12878
context 'when runtime shape is the input' do
12979
let(:typed_shape) do
130-
runtime.new(
131-
string: 'foo',
80+
structure_shape.type.new(
81+
big_decimal: 0,
82+
big_integer: 0,
83+
blob: StringIO.new('foo'),
84+
boolean: true,
85+
byte: 1,
86+
double: 1.1,
87+
float: 1.1,
88+
enum: 'enum',
89+
int_enum: 0,
90+
integer: 1,
91+
long: 1,
92+
short: 1,
13293
list: %w[Item1 Item2],
133-
foo_map: { foo: ['Thing1'], bar: ['Thing2'] },
134-
structure: { list: ['AnotherThing'] },
135-
union: { union_string: 'hello world' },
94+
map: { color: 'red' },
95+
streaming_blob: 'streaming blob',
96+
string: 'foo',
97+
structure_list: [{ integer: 1 }, { integer: 2 }, { integer: 3 }],
98+
structure_map: { 'key' => { map: { 'color' => 'blue' } } },
13699
timestamp: Time.utc(2024, 12, 25),
137-
blob: StringIO.new('foo')
100+
union: { string: 'string' }
138101
)
139102
end
140103

141-
subject { Document.new(typed_shape, shape: schema) }
104+
subject { Document.new(typed_shape, shape: structure_shape) }
142105

143106
describe '#initialize' do
144107
it 'set data' do
145108
expect(subject.data).to include(
146109
{
147-
'stringMember' => 'foo',
148-
'listMember' => %w[Item1 Item2],
149-
'mapMember' => { foo: ['Thing1'], bar: ['Thing2'] },
150-
'structureMember' => { 'listMember' => ['AnotherThing'] },
151-
'unionMember' => { 'unionString' => 'hello world' },
152-
'timeMember' => 1_735_084_800,
153-
'blobMember' => 'Zm9v'
110+
'bigDecimal' => 0,
111+
'bigInteger' => 0,
112+
'blob' => 'Zm9v',
113+
'boolean' => true,
114+
'byte' => 1,
115+
'double' => 1.1,
116+
'float' => 1.1,
117+
'enum' => 'enum',
118+
'intEnum' => 0,
119+
'integer' => 1,
120+
'long' => 1,
121+
'short' => 1,
122+
'list' => %w[Item1 Item2],
123+
'map' => { 'color' => 'red' },
124+
'streamingBlob' => 'c3RyZWFtaW5nIGJsb2I=',
125+
'string' => 'foo',
126+
'structureList' => [{ 'integer' => 1 }, { 'integer' => 2 }, { 'integer' => 3 }],
127+
'structureMap' => { 'key' => { 'map' => { 'color' => 'blue' } } },
128+
'timestamp' => 1_735_084_800,
129+
'union' => { 'string' => 'string' }
154130
}
155131
)
156132
end
157133

158134
it 'set data using jsonName when applicable' do
159-
typed_shape = runtime.new(string: 'foo', union: { union_string: 'bar' })
160-
doc = Document.new(typed_shape, shape: schema, use_json_name: true)
161-
expect(doc.data).to include({ 'json' => 'foo', 'unionMember' => { 'json' => 'bar' } })
135+
typed_shape = structure_shape.type.new(string: 'foo', union: { string: 'bar' })
136+
doc = Document.new(typed_shape, shape: structure_shape, use_json_name: true)
137+
expect(doc.data).to include({ 'jsonName' => 'foo', 'union' => { 'jsonName' => 'bar' } })
162138
end
163139

164140
it 'set data using timestampTrait when applicable' do
165-
doc = Document.new(typed_shape, shape: schema, use_timestamp_format: true)
166-
expect(doc.data['timeMember']).to eq('2024-12-25T00:00:00Z')
141+
doc = Document.new(typed_shape, shape: structure_shape, use_timestamp_format: true)
142+
expect(doc.data['timestamp']).to eq('2024-12-25T00:00:00Z')
167143
end
168144

169145
it 'set discriminator' do
170-
expect(subject.discriminator).to be(schema.id)
146+
expect(subject.discriminator).to be(structure_shape.id)
171147
end
172148

173149
it 'raises when no schema is given' do
@@ -186,16 +162,27 @@ def self.name
186162

187163
describe '#as_typed' do
188164
it 'converts document as a runtime shape' do
189-
typed_shape = subject.as_typed(schema)
165+
typed_shape = subject.as_typed(structure_shape)
190166
expect(typed_shape.to_h).to include(
191167
{
168+
big_decimal: 0,
169+
big_integer: 0,
170+
blob: 'foo',
171+
boolean: true,
172+
byte: 1,
173+
double: 1.1,
174+
float: 1.1,
175+
enum: 'enum',
176+
int_enum: 0,
177+
integer: 1,
178+
long: 1,
179+
short: 1,
192180
string: 'foo',
193-
list: %w[Item1 Item2],
194-
foo_map: { foo: ['Thing1'], bar: ['Thing2'] },
195-
structure: { list: ['AnotherThing'] },
196-
union: { union_string: 'hello world' },
197-
timestamp: '2024-12-25T00:00:00Z',
198-
blob: 'foo'
181+
streaming_blob: 'streaming blob',
182+
structure_list: [{ integer: 1 }, { integer: 2 }, { integer: 3 }],
183+
structure_map: { 'key' => { map: { 'color' => 'blue' } } },
184+
union: { string: 'string' },
185+
timestamp: '2024-12-25T00:00:00Z'
199186
}
200187
)
201188
end

gems/smithy-schema/spec/support/schema_helper.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require_relative '../../../smithy/lib/smithy'
4+
35
module SchemaHelper
46
class << self
57
def sample_shapes
@@ -87,18 +89,27 @@ def sample_shapes
8789
'target' => 'smithy.ruby.tests#StreamingBlob',
8890
'traits' => { 'smithy.api#default' => 'streamingBlob' }
8991
},
90-
'string' => { 'target' => 'smithy.api#String' },
92+
'string' => {
93+
'target' => 'smithy.api#String',
94+
'traits' => { 'smithy.api#jsonName' => 'jsonName' }
95+
},
9196
'structure' => { 'target' => 'smithy.ruby.tests#Structure' },
9297
'structureList' => { 'target' => 'smithy.ruby.tests#StructureList' },
9398
'structureMap' => { 'target' => 'smithy.ruby.tests#StructureMap' },
94-
'timestamp' => { 'target' => 'smithy.api#Timestamp' },
99+
'timestamp' => {
100+
'target' => 'smithy.api#Timestamp',
101+
'traits' => { 'smithy.api#timestampFormat' => 'http-date' }
102+
},
95103
'union' => { 'target' => 'smithy.ruby.tests#Union' }
96104
}
97105
},
98106
'smithy.ruby.tests#Union' => {
99107
'type' => 'union',
100108
'members' => {
101-
'string' => { 'target' => 'smithy.api#String' },
109+
'string' => {
110+
'target' => 'smithy.api#String',
111+
'traits' => { 'smithy.api#jsonName' => 'jsonName' }
112+
},
102113
'structure' => { 'target' => 'smithy.ruby.tests#Structure' },
103114
'unit' => { 'target' => 'smithy.api#Unit' }
104115
}

0 commit comments

Comments
 (0)