wsdsl 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
@@ -8,6 +8,7 @@ module JSONResponseVerification
8
8
  # @return [Array<TrueClass, FalseClass, Array<String>>] True/false and an array of errors.
9
9
  def validate_hash_response(hash)
10
10
  errors = []
11
+ # nodes without the arrays
11
12
  response.nodes.each do |node|
12
13
  if node.name
13
14
  # Verify that the named node exists in the hash
@@ -27,13 +28,13 @@ module JSONResponseVerification
27
28
  # Recursively validates a hash representing a json response.
28
29
  #
29
30
  # @param [Hash>] hash the hash to verify.
30
- # @param [WDSL::Response::Element>] node the reference element defined in the response description.
31
- # @param [<TrueClass, FalseClass>] nested if the node/hash to verify is nested or not. If nested, the method expects to get the subhash
31
+ # @param [WDSL::Response::Element] node the reference element defined in the response description.
32
+ # @param [TrueClass, FalseClass] nested if the node/hash to verify is nested or not. If nested, the method expects to get the subhash
32
33
  # & won't verify that the name exists since it was done a level higher.
33
34
  # @param [Arrays<String>] errors the list of errors encountered while verifying.
34
- #
35
- # @return [<TrueClass, FalseClass>]
36
- def validate_hash_against_template_node(hash, node, nested=false, errors=[])
35
+ # @param []
36
+ # @return [TrueClass, FalseClass]
37
+ def validate_hash_against_template_node(hash, node, nested=false, errors=[], array_item=false)
37
38
  if hash.nil?
38
39
  errors << json_response_error(node, hash)
39
40
  return errors
@@ -43,20 +44,29 @@ module JSONResponseVerification
43
44
  if hash.has_key?(node.name.to_s)
44
45
  subhash = hash[node.name.to_s]
45
46
  else
46
- errors << json_response_error(node, hash) unless hash.has_key?(node.name.to_s)
47
+ errors << json_response_error(node, hash)
47
48
  end
48
49
  end
49
50
 
50
- node.properties.each do |prop|
51
- subhash ||= hash
52
- errors << json_response_error(prop, subhash) unless subhash.has_key?(prop.name.to_s)
53
- errors << json_response_error(prop, subhash, true) unless valid_hash_type?(subhash, prop)
54
- end
51
+ subhash ||= hash
52
+ if node.is_a?(WSDSL::Response::Vector) && !array_item
53
+ errors << json_response_error(node, subhash, true) unless subhash.is_a?(Array)
54
+ subhash.each do |obj|
55
+ validate_hash_against_template_node(obj, node, true, errors, true)
56
+ end
57
+ else
58
+ node.properties.each do |prop|
59
+ if !array_item && !subhash.has_key?(prop.name.to_s)
60
+ errors << json_response_error(prop, subhash)
61
+ end
62
+ errors << json_response_error(prop, subhash, true) unless valid_hash_type?(subhash, prop)
63
+ end
55
64
 
56
- node.objects.each do |obj|
57
- # recursive call
58
- validate_hash_against_template_node(subhash[obj.name.to_s], obj, true, errors)
59
- end if node.objects
65
+ node.objects.each do |obj|
66
+ # recursive call
67
+ validate_hash_against_template_node(subhash[obj.name.to_s], obj, true, errors)
68
+ end if node.objects
69
+ end
60
70
 
61
71
  errors
62
72
  end
@@ -36,6 +36,34 @@ describe "JSON response verification" do
36
36
  end
37
37
  end
38
38
 
39
+ @third_service = describe_service "with_array" do |service|
40
+ service.response do |response|
41
+ response.array :users do |node|
42
+ node.integer :id
43
+ node.string :name
44
+ node.boolean :admin, :doc => "true if the user is an admin"
45
+ node.string :state, :doc => "test"
46
+ node.datetime :last_login_at
47
+ end
48
+ end
49
+ end
50
+
51
+ @forth_service = describe_service "with_nested_array" do |service|
52
+ service.response do |response|
53
+ response.array :users do |node|
54
+ node.integer :id
55
+ node.string :name
56
+ node.boolean :admin, :doc => "true if the user is an admin"
57
+ node.string :state, :doc => "test"
58
+ node.array :pets do |pet|
59
+ pet.integer :id
60
+ pet.string :name
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+
39
67
  end
40
68
 
41
69
  def valid_response(namespaced=true)
@@ -48,6 +76,37 @@ describe "JSON response verification" do
48
76
  namespaced ? {"user" => response} : response
49
77
  end
50
78
 
79
+ def valid_array_response
80
+ {"users" => [
81
+ {"id" => 1,
82
+ "admin" => true,
83
+ "state" => "retired",
84
+ "last_login_at" => "2011-09-22T22:46:35-07:00"
85
+ },
86
+ {"id" => 2,
87
+ "admin" => false,
88
+ "state" => "playing",
89
+ "last_login_at" => "2011-09-22T22:46:35-07:00"
90
+ }]
91
+ }
92
+ end
93
+
94
+ def valid_nested_array_response
95
+ {"users" => [
96
+ {"id" => 1,
97
+ "admin" => true,
98
+ "state" => "retired",
99
+ "pets" => []
100
+ },
101
+ {"id" => 2,
102
+ "admin" => false,
103
+ "state" => "playing",
104
+ "pets" => [{"id" => 23, "name" => "medor"}, {"id" => 34, "name" => "rex"}]
105
+ }]
106
+ }
107
+ end
108
+
109
+
51
110
  it "should validate the response" do
52
111
  valid, errors = @service.validate_hash_response(valid_response)
53
112
  valid.should be_true
@@ -77,16 +136,43 @@ describe "JSON response verification" do
77
136
  response["user"].delete("creds")
78
137
  valid, errors = @service.validate_hash_response(response)
79
138
  valid.should be_false
80
- errors.should_not be_empty
81
139
  errors.first.should match(/creds/)
82
140
  errors.first.should match(/missing/)
83
141
  end
84
142
 
85
- it "should validate non namespaced responses" do
86
- valid, errors = @second_service.validate_hash_response(valid_response(false))
143
+ it "should validate nil attributes if marked as nullable" do
144
+ response = valid_response(false)
145
+ response["name"] = nil
146
+ valid, errors = @second_service.validate_hash_response(response)
147
+ valid.should be_true
148
+ end
149
+
150
+
151
+ it "should validate array items" do
152
+ valid, errors = @third_service.validate_hash_response(valid_array_response)
87
153
  valid.should be_true
88
154
  errors.should be_empty
89
155
  end
90
156
 
157
+ it "should validate an empty array" do
158
+ response = valid_array_response
159
+ response["users"] = []
160
+ valid, errors = @third_service.validate_hash_response(response)
161
+ valid.should be_true
162
+ end
163
+
164
+ it "should catch error in an array item" do
165
+ response = valid_array_response
166
+ response["users"][1]["id"] = 'test'
167
+ valid, errors = @third_service.validate_hash_response(response)
168
+ valid.should be_false
169
+ errors.should_not be_empty
170
+ end
171
+
172
+ it "should validate nested arrays" do
173
+ valid, errors = @forth_service.validate_hash_response(valid_nested_array_response)
174
+ valid.should be_true
175
+ end
176
+
91
177
 
92
178
  end
data/wsdsl.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wsdsl}
8
- s.version = "0.4.0"
8
+ s.version = "0.4.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Matt Aimonetti}]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wsdsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: