-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
test_initialize_data.rb
118 lines (75 loc) · 3.41 KB
/
test_initialize_data.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
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
require File.expand_path('../test_helper', __FILE__)
class InitializeDataTest < Minitest::Test
def test_parse_character_string
schema = {'type' => 'string'}
data = 'hello world'
assert(JSON::Validator.validate(schema, data))
assert(JSON::Validator.validate(schema, data, :parse_data => false))
assert_raises(parser_error) do
JSON::Validator.validate(schema, data, :json => true)
end
assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) }
end
def test_parse_integer_string
schema = {'type' => 'integer'}
data = '42'
assert(JSON::Validator.validate(schema, data))
refute(JSON::Validator.validate(schema, data, :parse_data => false))
assert(JSON::Validator.validate(schema, data, :json => true))
assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) }
end
def test_parse_hash_string
schema = { 'type' => 'object', 'properties' => { 'a' => { 'type' => 'string' } } }
data = '{"a": "b"}'
assert(JSON::Validator.validate(schema, data))
refute(JSON::Validator.validate(schema, data, :parse_data => false))
assert(JSON::Validator.validate(schema, data, :json => true))
assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) }
end
def test_parse_json_string
schema = {'type' => 'string'}
data = '"hello world"'
assert(JSON::Validator.validate(schema, data))
assert(JSON::Validator.validate(schema, data, :parse_data => false))
assert(JSON::Validator.validate(schema, data, :json => true))
assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) }
end
def test_parse_valid_uri_string
schema = {'type' => 'string'}
data = 'http://foo.bar/'
stub_request(:get, "foo.bar").to_return(:body => '"hello world"', :status => 200)
assert(JSON::Validator.validate(schema, data))
assert(JSON::Validator.validate(schema, data, :parse_data => false))
assert_raises(parser_error) do
JSON::Validator.validate(schema, data, :json => true)
end
assert(JSON::Validator.validate(schema, data, :uri => true))
end
def test_parse_invalid_uri_string
schema = {'type' => 'string'}
data = 'http://foo.bar/'
stub_request(:get, "foo.bar").to_timeout
assert(JSON::Validator.validate(schema, data))
assert(JSON::Validator.validate(schema, data, :parse_data => false))
assert_raises(parser_error) do
JSON::Validator.validate(schema, data, :json => true)
end
assert_raises(Timeout::Error) { JSON::Validator.validate(schema, data, :uri => true) }
end
def test_parse_integer
schema = {'type' => 'integer'}
data = 42
assert(JSON::Validator.validate(schema, data))
assert(JSON::Validator.validate(schema, data, :parse_data => false))
assert_raises(TypeError) { JSON::Validator.validate(schema, data, :json => true) }
assert_raises(TypeError) { JSON::Validator.validate(schema, data, :uri => true) }
end
def test_parse_hash
schema = { 'type' => 'object', 'properties' => { 'a' => { 'type' => 'string' } } }
data = { 'a' => 'b' }
assert(JSON::Validator.validate(schema, data))
assert(JSON::Validator.validate(schema, data, :parse_data => false))
assert_raises(TypeError) { JSON::Validator.validate(schema, data, :json => true) }
assert_raises(TypeError) { JSON::Validator.validate(schema, data, :uri => true) }
end
end