/
schema_spec.rb
91 lines (80 loc) · 2.67 KB
/
schema_spec.rb
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
require "render"
module Render
describe Schema do
before(:all) do
Render::Definition.load_from_directory!(Helpers::SCHEMA_DIRECTORY)
Render.live = false
module ::TransformerExample
class << self
def process_name
uri = URI("http://films.local")
response = JSON.parse(Net::HTTP.get(uri))
{ transformed_to: response.fetch("name") }
end
end
end
end
after(:all) do
Render::Definition.instances.clear
Render.live = true
Object.send(:remove_const, :TransformerExample)
end
it "stubs data for testing" do
name = "am I transforming this right?"
rendered_stub = Render::Schema.new("films_show").serialize!({ name: name })
stub_request(:get, "http://films.local").to_return({ body: rendered_stub.to_json })
TransformerExample.process_name[:transformed_to].should == name
end
it "enforces schema's definition" do
name = "am I transforming this right?"
rendered_stub = Render::Schema.new("films_show").serialize!({ wrong_key: name })
stub_request(:get, "http://films.local").to_return({ body: rendered_stub.to_json })
TransformerExample.process_name[:transformed_to].should_not == name
end
it "prevents errors related to code anticipating actual data" do
rendered_stub = Render::Schema.new("films_show").serialize!
stub_request(:get, "http://films.local").to_return({ body: rendered_stub.to_json })
expect {
response = TransformerExample.process_name
}.not_to raise_error
end
it "creates fake data for varying types" do
schema = {
title: :film,
type: Object,
properties: {
id: { type: UUID },
title: { type: String },
director: {
type: Object,
properties: {
rating: { type: Float }
}
},
genre: {
type: String,
enum: ["horror", "action", "sci-fi"]
},
tags: {
type: Array,
minItems: 1,
items: {
type: Object,
properties: {
name: { type: String },
id: { type: Integer }
}
}
}
}
}
response = Render::Schema.new(schema).serialize!
UUID.validate(response[:id]).should eq(true)
response[:title].should be_a(String)
response[:director][:rating].should be_a(Float)
%w(horror action sci-fi).should include(response[:genre])
response[:tags].first[:name].should be_a(String)
response[:tags].first[:id].should be_a(Integer)
end
end
end