render 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,171 +2,151 @@ require "render"
2
2
 
3
3
  module Render
4
4
  describe Schema do
5
- before(:each) do
6
- @film_schema = {
7
- title: "film",
8
- type: Object,
9
- attributes: {
10
- name: { type: String },
11
- genre: { type: String }
12
- }
13
- }
14
-
15
- @films_schema = {
16
- title: "films",
17
- type: Array,
18
- elements: {
19
- title: :film,
20
- attributes: {
21
- name: { type: String },
22
- genre: { type: String }
23
- }
24
- }
25
- }
26
-
27
- @director_schema = {
28
- title: "director",
29
- type: Object,
30
- attributes: {
31
- films: {
32
- type: Array,
33
- elements: {
34
- type: Object,
35
- attributes: {
36
- name: { type: String },
37
- year: { type: Integer }
38
- }
39
- }
40
- }
41
- }
42
- }
43
- end
44
-
45
5
  describe "#initialize" do
46
- describe "#schema" do
47
- before(:each) do
48
- @schema = { attributes: {} }
6
+ describe "#definition" do
7
+ before(:all) do
8
+ @original_defs = Render.definitions
49
9
  end
50
10
 
51
- it "is set to hash argument" do
52
- Schema.new(@schema).schema.should == @schema
11
+ after(:all) do
12
+ Render.definitions = @original_defs
53
13
  end
54
14
 
55
- it "is set to preloaded schema" do
56
- Render.stub({ schemas: { film: @schema } })
57
- Schema.new(:film).schema.should == @schema
15
+ it "is set from argument" do
16
+ schema_definition = { properties: {} }
17
+ Schema.new(schema_definition).definition.should == schema_definition
58
18
  end
59
19
 
60
- it "raises an error if preloaded schema cannot be found" do
20
+ it "is set to preloaded definition" do
21
+ definition_title = :preloaded_schema
22
+ definition = { title: definition_title, properties: { title: { type: String } } }
23
+ Render.load_definition!(definition)
24
+ Schema.new(definition_title).definition.should == definition
25
+ end
26
+
27
+ it "raises an error if definition is not found and argument is not a schema" do
61
28
  expect {
62
- Schema.new(:unloaded_schema)
63
- }.to raise_error(Render::Errors::Schema::NotFound)
29
+ Schema.new(:does_not_exists)
30
+ }.to raise_error(Errors::DefinitionNotFound)
64
31
  end
65
32
  end
66
33
 
67
- it "sets title from schema" do
68
- title = "films"
69
- schema = { title: title, attributes: {} }
70
- Schema.new(schema).title.should == title
34
+ it "sets its type from schema" do
35
+ type = [Array, Object].sample
36
+ definition = { type: type, properties: {}, items: {} }
37
+ Schema.new(definition).type.should == type
71
38
  end
72
39
 
73
- describe "#type" do
74
- it "is set from schema" do
75
- type = [Array, Object].sample
76
- schema = { type: type, attributes: {} }
77
- Schema.new(schema).type.should == type
78
- end
40
+ describe "#array_attribute" do
41
+ it "is set for array schemas" do
42
+ archetype_schema = {
43
+ type: Array,
44
+ items: {
45
+ type: String
46
+ }
47
+ }
79
48
 
80
- it "is parsed from string" do
81
- schema = { type: "string", attributes: {} }
82
- Schema.new(schema).type.should == String
49
+ attribute = Schema.new(archetype_schema).array_attribute
50
+ attribute.type.should == String
51
+ attribute.archetype.should == true
83
52
  end
84
53
  end
85
54
 
86
- describe "#attributes" do
87
- it "is set with simple Attributes" do
55
+ describe "#hash_attributes" do
56
+ it "is set for object schemas" do
88
57
  simple_schema = {
89
- attributes: {
58
+ properties: {
90
59
  name: { type: String },
91
60
  genre: { type: String }
92
61
  }
93
62
  }
94
63
 
95
64
  schema = Schema.new(simple_schema)
96
- schema.attributes.size.should == 2
97
- schema.attributes.any? { |a| a.name == :name && a.type == String }.should == true
98
- schema.attributes.any? { |a| a.name == :genre && a.type == String }.should == true
65
+ schema.hash_attributes.size.should == 2
66
+ schema.hash_attributes.any? { |a| a.name == :name && a.type == String }.should == true
67
+ schema.hash_attributes.any? { |a| a.name == :genre && a.type == String }.should == true
99
68
  end
69
+ end
70
+ end
100
71
 
101
- it "is set with array archetypes" do
102
- archetype_schema = {
103
- elements: {
104
- type: String
105
- }
72
+ describe "#serialize!" do
73
+ it "returns serialized array" do
74
+ definition = {
75
+ type: Array,
76
+ items: {
77
+ type: UUID
106
78
  }
79
+ }
80
+ schema = Schema.new(definition)
81
+ schema.array_attribute.should_receive(:serialize).with(nil).and_return([:data])
82
+ schema.serialize!.should == [:data]
83
+ end
107
84
 
108
- attributes = Schema.new(archetype_schema).attributes
109
- attributes.size.should == 1
110
- attributes.first.type.should == String
111
- end
85
+ it "returns serialized hash" do
86
+ definition = {
87
+ type: Object,
88
+ properties: {
89
+ title: { type: String }
90
+ }
91
+ }
92
+ schema = Schema.new(definition)
93
+ schema.hash_attributes.first.should_receive(:serialize).with(nil).and_return({ title: "foo" })
94
+ schema.serialize!.should == { title: "foo" }
95
+ end
96
+ end
112
97
 
113
- it "is set with schema-Attributes" do
114
- nested_schema = {
115
- attributes: {
116
- film: {
117
- type: Object,
118
- attributes: {
119
- name: { type: String }
120
- }
121
- }
122
- }
98
+ describe "#render!" do
99
+ before(:all) do
100
+ @original_defs = Render.definitions
101
+ Render.load_definition!({
102
+ title: :film,
103
+ properties: {
104
+ genre: { type: String }
123
105
  }
106
+ })
107
+ end
124
108
 
125
- schema = Schema.new(nested_schema)
126
- schema.attributes.size.should == 1
127
- schema.attributes.first.schema.should be
109
+ after(:all) do
110
+ Render.definitions = @original_defs
111
+ end
112
+
113
+ describe "#raw_data" do
114
+ it "is set from endpoint response" do
115
+ schema = Schema.new(:film)
116
+ schema.stub({ request: { response: :body } })
117
+ schema.render!
118
+ schema.raw_data.should == { response: :body }
128
119
  end
129
120
 
130
- it "is set with array-Attributes" do
131
- array_schema = {
132
- elements: {
133
- film: {
134
- type: Object,
135
- attributes: {
136
- name: { type: String }
137
- }
138
- }
139
- }
140
- }
141
- schema = Schema.new(array_schema)
142
- schema.attributes.size.should == 1
143
- schema.attributes.first.schema.should be
121
+ it "is set to explicit_data when offline" do
122
+ Render.stub({ live: false })
123
+ schema = Schema.new(:film)
124
+ schema.render!({ explicit: :value })
125
+ schema.raw_data.should == { explicit: :value }
144
126
  end
145
- end
146
- end
147
127
 
148
- describe "#pull" do
149
- context "live" do
150
- it "returns schema with values from endpoint" do
151
- endpoint = "http://endpoint.local"
152
- name = "The Shining"
153
- genre = "Horror"
154
- response_body = { film: { name: name, genre: genre } }
155
- response = { status: 200, body: response_body.to_json }
156
- stub_request(:get, endpoint).to_return(response)
128
+ it "is yielded" do
129
+ Render.stub({ live: false })
130
+ schema = Schema.new(:film)
131
+
132
+ data = { explicit: :value }
133
+ schema.should_receive(:serialize!).with(data)
134
+ schema.serialized_data = :serialized_data
157
135
 
158
- film = Schema.new(@film_schema)
159
- film.render({ endpoint: endpoint }).should == response_body
136
+ expect { |a_block|
137
+ schema.render!(data, &a_block)
138
+ }.to yield_with_args(:serialized_data)
160
139
  end
140
+ end
161
141
 
162
- it "raises error if response is not 2xx" do
142
+ context "request" do
143
+ it "raises error if endpoint does not return a 2xx" do
163
144
  endpoint = "http://endpoint.local"
164
- response = { status: 403, body: "OMGWTFBBQ" }
165
- stub_request(:get, endpoint).to_return(response)
145
+ stub_request(:get, endpoint).to_return({ status: 403 })
166
146
 
167
147
  expect {
168
- film = Schema.new(@film_schema)
169
- film.render({ endpoint: endpoint })
148
+ schema = Schema.new(:film)
149
+ schema.render!({ endpoint: endpoint })
170
150
  }.to raise_error(Errors::Schema::RequestError)
171
151
  end
172
152
 
@@ -175,74 +155,38 @@ module Render
175
155
  stub_request(:get, endpoint).to_return({ body: "Server Error: 500" })
176
156
 
177
157
  expect {
178
- Schema.new(@film_schema).render({ endpoint: endpoint })
158
+ Schema.new(:film).render!({ endpoint: endpoint })
179
159
  }.to raise_error(Errors::Schema::InvalidResponse)
180
160
  end
181
- end
182
-
183
- context "faked" do
184
- before(:each) do
185
- Render.stub({ live: false })
186
- end
187
161
 
188
- it "returns schema with fake values" do
189
- film = Schema.new(@film_schema)
190
- film = film.render[:film]
191
- film[:name].should be_a(String)
192
- film[:genre].should be_a(String)
193
- end
162
+ it "uses configured request logic"
194
163
  end
195
164
 
196
- it "handles responses that do not use schema title as root key" do
197
- endpoint = "http://endpoint.local"
198
- response_body = { name: "the name", genre: "the genre" }
199
- response = { status: 200, body: response_body.to_json }
200
- stub_request(:get, endpoint).to_return(response)
201
-
202
- film = Schema.new(@film_schema)
203
- film.render({ endpoint: endpoint }).should == { film: response_body }
204
- end
165
+ context "return value" do
166
+ it "is serialized data" do
167
+ endpoint = "http://endpoint.local"
168
+ genre = "The Shining"
169
+ data = { genre: genre }
170
+ response = { status: 200, body: data.to_json }
171
+ stub_request(:get, endpoint).to_return(response)
205
172
 
206
- it "handles Array responses" do
207
- endpoint = "http://endpoint.local"
208
- first_name = "The Shining"
209
- second_name = "Eyes Wide Shut"
210
- genre = "Horror"
211
-
212
- response_body = [
213
- { name: first_name, genre: genre },
214
- { name: second_name, genre: genre }
215
- ]
216
- response = { status: 200, body: response_body.to_json }
217
- stub_request(:get, endpoint).to_return(response)
218
-
219
- film = Schema.new(@films_schema)
220
- film.render({ endpoint: endpoint }).should == { films: response_body }
221
- end
222
- end
173
+ schema = Schema.new(:film)
174
+ schema.hash_attributes.first.should_receive(:serialize).with(genre).and_return({ genre: genre })
223
175
 
224
- describe "#serialize" do
225
- it "returns parsed array elements" do
226
- Render.stub({ live: false })
227
- director = Schema.new(@director_schema)
228
-
229
- kubrick = "Stanley Kubrick"
230
- first_film = "Flying Padre: An RKO-Pathe Screenliner"
231
- year = 1951
232
- data = {
233
- films: [{
234
- name: first_film,
235
- notInSchema: "experimental",
236
- year: year
237
- }]
238
- }
176
+ schema.render!({ endpoint: endpoint }).should == { film: data }
177
+ end
239
178
 
240
- director.serialize(data).should == {
241
- films: [{
242
- name: first_film,
243
- year: year
244
- }]
245
- }
179
+ it "is serialized value nested under #universal_title" do
180
+ Render.stub({ live: false })
181
+ definition = {
182
+ title: :film,
183
+ universal_title: :imdb_films_show,
184
+ properties: {
185
+ genre: { type: String }
186
+ }
187
+ }
188
+ Schema.new(definition).render!.should have_key(:imdb_films_show)
189
+ end
246
190
  end
247
191
 
248
192
  end
@@ -26,6 +26,12 @@ describe Render do
26
26
  end
27
27
  end
28
28
 
29
+ describe "logger" do
30
+ it "exits" do
31
+ Render.logger.should be
32
+ end
33
+ end
34
+
29
35
  describe ".generators" do
30
36
  before(:each) do
31
37
  @original_value = Render.generators
@@ -41,40 +47,128 @@ describe Render do
41
47
  end
42
48
  end
43
49
 
44
- describe ".load_schemas!" do
50
+ context "schema definitions" do
45
51
  before(:each) do
46
- @schema_title = "film"
47
- @json_schema = <<-JSON
52
+ @original_defs = Render.definitions
53
+ end
54
+
55
+ after(:each) do
56
+ Render.definitions = @original_defs
57
+ end
58
+
59
+ describe ".load_defintion!" do
60
+ it "preferences #universal_title over title" do
61
+ universal_title = "a_service_films_show"
62
+ definition = {
63
+ universal_title: universal_title,
64
+ title: "film",
65
+ type: Object,
66
+ properties: {
67
+ name: { type: String },
68
+ year: { type: Integer }
69
+ }
70
+ }
71
+
72
+ Render.load_definition!(definition)
73
+ Render.definitions.keys.should include(universal_title.to_sym)
74
+ end
75
+ end
76
+
77
+ describe ".load_schemas!" do
78
+ before(:each) do
79
+ Render.definitions.clear
80
+ @schema_title = "film"
81
+ @json_schema = <<-JSON
48
82
  {
49
83
  "title": "#{@schema_title}",
50
84
  "type": "object",
51
- "attributes": {
85
+ "properties": {
52
86
  "name": { "type": "string" },
53
87
  "year": { "type": "integer" }
54
88
  }
55
89
  }
56
- JSON
90
+ JSON
91
+
92
+ @directory = "/a"
93
+ @schema_file = "/a/schema.json"
94
+ Dir.stub(:glob).with(%r{#{@directory}}).and_return([@schema_file])
95
+ IO.stub(:read).with(@schema_file).and_return(@json_schema)
96
+ end
97
+
98
+ after(:each) do
99
+ Render.definitions = {}
100
+ end
101
+
102
+ it "stores JSON files" do
103
+ expect {
104
+ Render.load_definitions!(@directory)
105
+ }.to change { Render.definitions.keys.size }.by(1)
106
+ end
57
107
 
58
- @directory = "/a"
59
- @schema_file = "/a/schema.json"
60
- Dir.stub(:glob).with(%r{#{@directory}}).and_return([@schema_file])
61
- IO.stub(:read).with(@schema_file).and_return(@json_schema)
108
+ it "accesses parsed schemas with symbols" do
109
+ Render.load_definitions!(@directory)
110
+ parsed_json = JSON.parse(@json_schema).recursive_symbolize_keys!
111
+ Render.definitions[@schema_title.to_sym].should == parsed_json
112
+ end
62
113
  end
114
+ end
63
115
 
64
- after(:each) do
65
- Render.schemas = {}
116
+ describe ".definition" do
117
+ it "returns definition by its title" do
118
+ def_title = :the_name
119
+ definition = { title: def_title, properties: {} }
120
+ Render.load_definition!(definition)
121
+
122
+ Render.definition(def_title).should == definition
66
123
  end
67
124
 
68
- it "stores JSON files" do
125
+ it "raises meaningful error if definition is not found" do
69
126
  expect {
70
- Render.load_schemas!(@directory)
71
- }.to change { Render.schemas.keys.size }.by(1)
127
+ Render.definition(:definition_with_this_title_has_not_been_loaded)
128
+ }.to raise_error(Render::Errors::DefinitionNotFound)
129
+ end
130
+ end
131
+
132
+ describe ".parse_type" do
133
+ it "returns constant for string" do
134
+ Render.parse_type("integer").should == Integer
135
+ end
136
+
137
+ it "returns argument when not a string" do
138
+ class Foo; end
139
+ Render.parse_type(Foo).should == Foo
140
+ Object.__send__(:remove_const, :Foo)
72
141
  end
73
142
 
74
- it "accesses parsed schemas with symbols" do
75
- Render.load_schemas!(@directory)
76
- parsed_json = JSON.parse(@json_schema).recursive_symbolize_keys!
77
- Render.schemas[@schema_title.to_sym].should == parsed_json
143
+ it "raises meaningful error for unmatched types" do
144
+ expect {
145
+ Render.parse_type("NotAClass")
146
+ }.to raise_error(Render::Errors::InvalidType)
147
+ end
148
+
149
+ describe "non-standard formats" do
150
+ it "maps regardless of capitalization" do
151
+ string_representations = %w(uuid UUID)
152
+ string_representations.each do |name|
153
+ Render.parse_type(name).should == UUID
154
+ end
155
+ end
156
+
157
+ it "returns UUID for uuid" do
158
+ Render.parse_type("uUId").should == UUID
159
+ end
160
+
161
+ it "returns Boolean for boolean" do
162
+ Render.parse_type("boolean").should == Boolean
163
+ end
164
+
165
+ it "returns Float for number" do
166
+ Render.parse_type("FloAt").should == Float
167
+ end
168
+
169
+ it "returns Time for date-time" do
170
+ Render.parse_type("date-time").should == Time
171
+ end
78
172
  end
79
173
  end
80
174
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: render
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-25 00:00:00.000000000 Z
12
+ date: 2013-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -142,17 +142,20 @@ files:
142
142
  - lib/extensions/enumerable.rb
143
143
  - lib/extensions/hash.rb
144
144
  - lib/render.rb
145
+ - lib/render/array_attribute.rb
145
146
  - lib/render/attribute.rb
146
147
  - lib/render/dottable_hash.rb
147
148
  - lib/render/errors.rb
148
149
  - lib/render/generator.rb
149
150
  - lib/render/graph.rb
151
+ - lib/render/hash_attribute.rb
150
152
  - lib/render/schema.rb
151
153
  - lib/render/version.rb
152
154
  - rakefile.rb
153
155
  - readme.md
154
156
  - render.gemspec
155
157
  - spec/functional/representation/attribute_spec.rb
158
+ - spec/functional/representation/graph_spec.rb
156
159
  - spec/functional/representation/nested_schemas_spec.rb
157
160
  - spec/functional/representation/schema_spec.rb
158
161
  - spec/integration/nested_graph_spec.rb
@@ -161,11 +164,12 @@ files:
161
164
  - spec/schemas/films.json
162
165
  - spec/support.rb
163
166
  - spec/support/helpers.rb
167
+ - spec/unit/array_attribute_spec.rb
164
168
  - spec/unit/extensions/boolean_spec.rb
165
- - spec/unit/render/attribute_spec.rb
166
169
  - spec/unit/render/dottable_hash_spec.rb
167
170
  - spec/unit/render/generator_spec.rb
168
171
  - spec/unit/render/graph_spec.rb
172
+ - spec/unit/render/hash_attribute_spec.rb
169
173
  - spec/unit/render/schema_spec.rb
170
174
  - spec/unit/render_spec.rb
171
175
  homepage: https://github.com/stevenweber/render
@@ -183,7 +187,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
183
187
  version: '0'
184
188
  segments:
185
189
  - 0
186
- hash: 1123791212905751399
190
+ hash: 2584837128307883424
187
191
  required_rubygems_version: !ruby/object:Gem::Requirement
188
192
  none: false
189
193
  requirements:
@@ -192,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
196
  version: '0'
193
197
  segments:
194
198
  - 0
195
- hash: 1123791212905751399
199
+ hash: 2584837128307883424
196
200
  requirements: []
197
201
  rubyforge_project:
198
202
  rubygems_version: 1.8.25
@@ -202,6 +206,7 @@ summary: With a JSON schema, Render will manage requests, dependent request and
202
206
  meaningful, extensible sample data for testing.
203
207
  test_files:
204
208
  - spec/functional/representation/attribute_spec.rb
209
+ - spec/functional/representation/graph_spec.rb
205
210
  - spec/functional/representation/nested_schemas_spec.rb
206
211
  - spec/functional/representation/schema_spec.rb
207
212
  - spec/integration/nested_graph_spec.rb
@@ -210,10 +215,11 @@ test_files:
210
215
  - spec/schemas/films.json
211
216
  - spec/support.rb
212
217
  - spec/support/helpers.rb
218
+ - spec/unit/array_attribute_spec.rb
213
219
  - spec/unit/extensions/boolean_spec.rb
214
- - spec/unit/render/attribute_spec.rb
215
220
  - spec/unit/render/dottable_hash_spec.rb
216
221
  - spec/unit/render/generator_spec.rb
217
222
  - spec/unit/render/graph_spec.rb
223
+ - spec/unit/render/hash_attribute_spec.rb
218
224
  - spec/unit/render/schema_spec.rb
219
225
  - spec/unit/render_spec.rb