easy_json_matcher 0.0.2.pre.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ class RequireValidationTest < ActiveSupport::TestCase
6
6
  astronaut_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
7
7
  s.has_attribute key: :has_oxygen, opts: {type: :boolean, required: true}
8
8
  s.has_attribute key: :name, opts: {type: :string}
9
- }.generate_node
9
+ }.generate_schema
10
10
 
11
11
  valid_astronaut = {
12
12
  has_oxygen: true,
@@ -26,7 +26,7 @@ class RequireValidationTest < ActiveSupport::TestCase
26
26
  astronaut_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
27
27
  s.has_attribute key: :has_oxygen, opts: {type: :boolean}
28
28
  s.has_attribute key: :name, opts: {type: :string}
29
- }.generate_node
29
+ }.generate_schema
30
30
 
31
31
  valid_astronaut = {
32
32
  name: 'Buzz Aldrin'
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+ class ResetTest < ActiveSupport::TestCase
3
+
4
+ test 'As a user, I want to be able to reuse a validator' do
5
+ # In other words, after a valid? has been called, the error messages need to be cleared
6
+ test_schema = EasyJSONMatcher::SchemaGenerator.new(global_opts: { strict: true }) { |sc|
7
+ sc.has_boolean key: :bool, opts: { required: true }
8
+ sc.contains_node key: :node do |n|
9
+ n.has_value key: :val
10
+ end
11
+ }.generate_schema
12
+
13
+ invalid_json = {
14
+
15
+ }.to_json
16
+
17
+ assert_not(test_schema.valid?(invalid_json))
18
+ assert_not(test_schema.get_errors[:bool][0].nil?)
19
+
20
+ test_schema.reset!
21
+
22
+ errors = test_schema.get_errors
23
+ assert errors[:node][:val].empty?
24
+ assert errors[:bool].empty?
25
+ end
26
+ end
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+
3
+ class StrictModeTest < ActiveSupport::TestCase
4
+
5
+ test "As a user I want my schema to only validate objects iff the set of keys
6
+ in the candidate minus the set of keys specified in the schema equals the
7
+ empty set" do
8
+
9
+ test_schema = EasyJSONMatcher::SchemaGenerator.new(global_opts: {strict: true}) {|s|
10
+ s.has_string key: :first
11
+ s.has_string key: :second
12
+ s.has_string key: :third
13
+ }.generate_schema
14
+
15
+ too_many_planets = {
16
+ first: 'Zeus',
17
+ second: 'Poseidon',
18
+ third: 'Hera',
19
+ fourth: 'Demeter'
20
+ }.to_json
21
+
22
+ assert_not(test_schema.valid?(too_many_planets), "#{too_many_planets} should not be valid as it has the unspecified :fourth key")
23
+ end
24
+
25
+ setup do
26
+ @test_schema = EasyJSONMatcher::SchemaGenerator.new(global_opts: {strict: true}) {|s|
27
+ s.has_string key: :name
28
+ s.contains_node key: :about do |n|
29
+ n.has_string key: :religion
30
+ n.has_string key: :domain
31
+ end
32
+ }.generate_schema
33
+ end
34
+
35
+ test "As a user, if I have specified strict at the top level, I expect it to apply to nested nodes" do
36
+
37
+ poseidon = {
38
+ name: 'Poseidon',
39
+ about: {
40
+ religion: 'Olympian Gods',
41
+ domain: 'The Sea',
42
+ hobbies: ['Battleships Champion', 'Horse Tamer', 'Seismology']
43
+ }
44
+ }.to_json
45
+
46
+ assert_not(@test_schema.valid?(poseidon), "#{poseidon} should not be valid as hobbies wasn't expected in the schema, and he didn't have any")
47
+ end
48
+
49
+ test "As a user, if the strict validation fails, I want to be told why" do
50
+ zeus = {
51
+ name: 'Zeus',
52
+ about: {
53
+ religion: 'Olympian Gods',
54
+ domain: 'Firmament',
55
+ spouse: 'Hera',
56
+ address: 'Mount Olympus'
57
+ }
58
+ }.to_json
59
+
60
+ assert_not(@test_schema.valid? zeus)
61
+ assert_match(/\[:spouse, :address\] found in addition to expected keys/, @test_schema.get_errors[:about][:node_errors_][0])
62
+ end
63
+ end
@@ -2,53 +2,22 @@ require 'test_helper'
2
2
 
3
3
  class ValidatingArraysTest < ActiveSupport::TestCase
4
4
 
5
- setup do
6
- @test_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
7
- s.has_attribute(key: :name, opts: {type: :string})
8
- s.contains_array(key: :items, opts: {required: true}) do |array|
9
- array.should_only_contain(type: :string, opts: {required: true})
10
- end
11
- }.generate_node
12
- end
13
-
14
- test "As a user I want to validate that my arrays contain values of the correct type" do
15
- valid_json = {
16
- name: 'Greek Heroes',
17
- items: ['Heracles', 'Perseus', 'Theseus', 'Achilles']
18
- }.to_json
19
-
20
- assert(@test_schema.valid?(valid_json), "#{valid_json} should have been valid")
21
-
22
- invalid_no_items = {
23
- name: 'Strictly Come Dancing Heroes'
24
- }.to_json
25
-
26
- assert_not(@test_schema.valid?(invalid_no_items), "#{invalid_no_items} should not have been valid as it has no array called items")
27
-
28
- invalid_wrong_items = {
29
- name: 'Prime Heroes',
30
- items: [1,3,5,7,9,8191]
31
- }.to_json
32
-
33
- assert_not(@test_schema.valid?(invalid_wrong_items), "#{invalid_wrong_items} should not have been validated since the array contains the wrong type")
34
- end
35
-
36
5
  test "As a user I want to validate that my arrays can contain objects of a specific schema" do
37
6
  EasyJSONMatcher::SchemaGenerator.new {|s|
38
7
 
39
8
  s.has_attribute key: :name, opts: {type: :string, required: true}
40
9
  s.has_attribute key: :spouse, opts: {type: :string}
41
- }.register(schema_name: :greek_hero)
10
+ }.register as: :greek_hero
42
11
 
43
12
  test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
44
13
  s.contains_array(key: :data, opts: {required: true}) do |a|
45
14
  a.should_only_contain(type: :schema, opts: {name: :greek_hero})
46
15
  end
47
- }.generate_node
16
+ }.generate_schema
48
17
 
49
18
  valid_json = {
50
19
  data: [{
51
- name: 'Greek Heroes',
20
+ name: 'More Greek Heroes',
52
21
  items: ['Hector', 'Ajax', 'Hippolyta', 'Penthesila']
53
22
  },
54
23
  {
@@ -61,8 +30,110 @@ class ValidatingArraysTest < ActiveSupport::TestCase
61
30
 
62
31
  invalid_json = {
63
32
  data: [1,2,3,4,5]
64
- }
33
+ }.to_json
65
34
 
66
35
  assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should not have validated as it does not contain a :heroes schema")
67
36
  end
37
+
38
+ test "As a user I want to validate that my arrays only contain strings" do
39
+ test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
40
+ s.has_string key: :name, opts: {required: true}
41
+ s.contains_array key: :data, opts: {required: true} do |a|
42
+ a.should_only_contain_strings
43
+ end
44
+ }.generate_schema
45
+
46
+ valid_json = {
47
+ name: 'German Heroes',
48
+ data: ['Brunhilda', 'Siegfried', 'Faust']
49
+ }.to_json
50
+
51
+ assert(test_schema.valid?(valid_json), "#{valid_json} should have been valid as it only contained strings")
52
+
53
+ invalid_json = {
54
+ name: 'Fibonacci sequence',
55
+ data: [0,1,1,2,3,5,8,13,24]
56
+ }.to_json
57
+
58
+ assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should not have been valid as it did not contain strings")
59
+
60
+ end
61
+
62
+ test 'As a user I want to validate that my arrays only contain numbers' do
63
+ test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
64
+ s.has_string key: :name, opts: {required: true}
65
+ s.contains_array key: :data, opts: {required: true} do |a|
66
+ a.should_only_contain_numbers
67
+ end
68
+ }.generate_schema
69
+
70
+ valid_json = {
71
+ name: 'Square Numbers',
72
+ data: [1,4,9,16,25,36,49,64]
73
+ }.to_json
74
+
75
+ assert(test_schema.valid?(valid_json), "#{valid_json} should have been valid as it contained only numbers")
76
+
77
+ invalid_json = {
78
+ name: 'Notable Scots',
79
+ data: ['Alexander Fleming', 'Adam Smith', 'John Logie Baird', 'St. Andrew', 'St. Columba', 1, 2, 3, 4, 5]
80
+ }.to_json
81
+ assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should not have been valid as it did not only contain numbers")
82
+ end
83
+
84
+ test 'As a user I want to validate that my arrays only contain booleans' do
85
+
86
+ test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
87
+ s.has_string key: :name, opts: {required: true}
88
+ s.contains_array key: :data, opts: {required: true} do |a|
89
+ a.should_only_contain_booleans
90
+ end
91
+ }.generate_schema
92
+
93
+ valid_json = {
94
+ name: 'Predicates',
95
+ data: [true, true, false, true, false, false]
96
+ }.to_json
97
+
98
+ assert(test_schema.valid?(valid_json), "#{valid_json} should have been valid as it contains only booleans")
99
+
100
+ invalid_json = {
101
+ name: 'Notable English',
102
+ data: ['St. George', 'Winston Churchill', 'Admiral Nelson', true, true, false, false]
103
+ }.to_json
104
+
105
+ assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should have been invalid as it contains strings")
106
+ end
107
+
108
+ test 'As a user I want to validate that my arrays contain only objects' do
109
+
110
+ test_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
111
+ s.has_string key: :name, opts: {required: true}
112
+ s.contains_array key: :data, opts: {required: true} do |a|
113
+ a.should_only_contain_objects
114
+ end
115
+ }.generate_schema
116
+
117
+ valid_json = {
118
+ name: 'Hand of Cards',
119
+ data: [
120
+ {suite: 'Spades', value: 'Ace'},
121
+ {suite: 'Hearts', value: 'Queen'},
122
+ {suite: 'Clubs', value: 'Jack'},
123
+ {suite: 'Diamonds', value: '3'}]
124
+ }.to_json
125
+
126
+ assert(test_schema.valid?(valid_json), "#{valid_json} should have been valid as it only contains objects")
127
+
128
+ invalid_json = {
129
+ name: 'Norse Gods',
130
+ data: [{'Thor': 'Thunder'}, 'Odin', 'Loki', 'Freya']
131
+ }.to_json
132
+
133
+ assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should not have been valid as it does not only contain objects")
134
+ end
135
+
136
+ test 'as a user I want to validate that my arrays contain only dates' do
137
+
138
+ end
68
139
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_json_matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.pre.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - WJD Hamilton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-17 00:00:00.000000000 Z
11
+ date: 2016-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,7 +38,8 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: 'TBC: Description of JsonapiMatcher.'
41
+ description: Test your JSON output in Ruby, with a DSL that makes reasoning about
42
+ your JSON very straightforward. See the Homepage for docs.
42
43
  email:
43
44
  - wjdhamilton@hotmail.co.uk
44
45
  executables: []
@@ -102,9 +103,13 @@ files:
102
103
  - test/dummy/public/500.html
103
104
  - test/dummy/public/favicon.ico
104
105
  - test/easy_json_matcher_test.rb
106
+ - test/error_messages_test.rb
107
+ - test/global_validation_options_test.rb
105
108
  - test/json_api_home.txt
106
109
  - test/managing_schemas_test.rb
107
110
  - test/required_validation_test.rb
111
+ - test/reset_test.rb
112
+ - test/strict_mode_test.rb
108
113
  - test/test_helper.rb
109
114
  - test/validating_arrays_test.rb
110
115
  homepage: https://github.com/wjdhamilton/easy-json-matcher
@@ -122,55 +127,59 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
127
  version: '0'
123
128
  required_rubygems_version: !ruby/object:Gem::Requirement
124
129
  requirements:
125
- - - ">"
130
+ - - ">="
126
131
  - !ruby/object:Gem::Version
127
- version: 1.3.1
132
+ version: '0'
128
133
  requirements: []
129
134
  rubyforge_project:
130
- rubygems_version: 2.2.2
135
+ rubygems_version: 2.5.1
131
136
  signing_key:
132
137
  specification_version: 4
133
138
  summary: Easily test your JSON output with templates and Schemas
134
139
  test_files:
135
- - test/required_validation_test.rb
136
- - test/validating_arrays_test.rb
137
- - test/easy_json_matcher_test.rb
138
- - test/json_api_home.txt
139
- - test/managing_schemas_test.rb
140
- - test/test_helper.rb
141
- - test/dummy/config.ru
142
- - test/dummy/bin/rails
140
+ - test/dummy/app/assets/javascripts/application.js
141
+ - test/dummy/app/assets/stylesheets/application.css
142
+ - test/dummy/app/controllers/application_controller.rb
143
+ - test/dummy/app/helpers/application_helper.rb
144
+ - test/dummy/app/views/layouts/application.html.erb
143
145
  - test/dummy/bin/bundle
144
- - test/dummy/bin/setup
146
+ - test/dummy/bin/rails
145
147
  - test/dummy/bin/rake
146
- - test/dummy/config/initializers/inflections.rb
147
- - test/dummy/config/initializers/wrap_parameters.rb
148
+ - test/dummy/bin/setup
149
+ - test/dummy/config/application.rb
150
+ - test/dummy/config/boot.rb
151
+ - test/dummy/config/database.yml
152
+ - test/dummy/config/environment.rb
153
+ - test/dummy/config/environments/development.rb
154
+ - test/dummy/config/environments/production.rb
155
+ - test/dummy/config/environments/test.rb
148
156
  - test/dummy/config/initializers/assets.rb
149
- - test/dummy/config/initializers/cookies_serializer.rb
150
157
  - test/dummy/config/initializers/backtrace_silencers.rb
158
+ - test/dummy/config/initializers/cookies_serializer.rb
159
+ - test/dummy/config/initializers/filter_parameter_logging.rb
160
+ - test/dummy/config/initializers/inflections.rb
151
161
  - test/dummy/config/initializers/mime_types.rb
152
162
  - test/dummy/config/initializers/session_store.rb
153
- - test/dummy/config/initializers/filter_parameter_logging.rb
154
- - test/dummy/config/secrets.yml
155
- - test/dummy/config/database.yml
156
- - test/dummy/config/application.rb
157
- - test/dummy/config/environments/test.rb
158
- - test/dummy/config/environments/development.rb
159
- - test/dummy/config/environments/production.rb
163
+ - test/dummy/config/initializers/wrap_parameters.rb
160
164
  - test/dummy/config/locales/en.yml
161
- - test/dummy/config/environment.rb
162
- - test/dummy/config/boot.rb
163
165
  - test/dummy/config/routes.rb
166
+ - test/dummy/config/secrets.yml
167
+ - test/dummy/config.ru
164
168
  - test/dummy/db/test.sqlite3
165
- - test/dummy/app/assets/javascripts/application.js
166
- - test/dummy/app/assets/stylesheets/application.css
167
- - test/dummy/app/views/layouts/application.html.erb
168
- - test/dummy/app/helpers/application_helper.rb
169
- - test/dummy/app/controllers/application_controller.rb
170
- - test/dummy/Rakefile
169
+ - test/dummy/log/test.log
171
170
  - test/dummy/public/404.html
172
171
  - test/dummy/public/422.html
173
172
  - test/dummy/public/500.html
174
173
  - test/dummy/public/favicon.ico
174
+ - test/dummy/Rakefile
175
175
  - test/dummy/README.rdoc
176
- - test/dummy/log/test.log
176
+ - test/easy_json_matcher_test.rb
177
+ - test/error_messages_test.rb
178
+ - test/global_validation_options_test.rb
179
+ - test/json_api_home.txt
180
+ - test/managing_schemas_test.rb
181
+ - test/required_validation_test.rb
182
+ - test/reset_test.rb
183
+ - test/strict_mode_test.rb
184
+ - test/test_helper.rb
185
+ - test/validating_arrays_test.rb