easy_json_matcher 0.0.1 → 0.0.2.pre.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,10 +14,10 @@ class JsonapiMatcherTest < ActiveSupport::TestCase
14
14
  }.register(schema_name: :test)
15
15
 
16
16
  valid_json = {
17
- data: {
18
- 'title'=> "Here's a title"
19
- }
20
- }.to_json
17
+ data: {
18
+ 'title'=> "here's a title"
19
+ }
20
+ }.to_json
21
21
  assert(test_schema.valid? valid_json)
22
22
  end
23
23
 
@@ -25,7 +25,7 @@ class JsonapiMatcherTest < ActiveSupport::TestCase
25
25
  test "As a user I want to be able to validate numbers" do
26
26
 
27
27
  test_schema = EasyJSONMatcher::SchemaGenerator.new {|schema|
28
- schema.has_attribute(key: :number, opts: {type: :number})
28
+ schema.has_attribute(key: :number, opts: {type: :number, required: :true})
29
29
  }.generate_node
30
30
 
31
31
  valid_json = {
@@ -38,6 +38,11 @@ class JsonapiMatcherTest < ActiveSupport::TestCase
38
38
  number: "hi"
39
39
  }.to_json
40
40
  assert_not(test_schema.valid?(invalid_json), "\"hi\" should not have been valid")
41
+
42
+ invalid_nil = {
43
+ number: nil
44
+ }.to_json
45
+ assert_not(test_schema.valid?(invalid_nil), "#{invalid_nil} should not have validated, or thrown an error")
41
46
  end
42
47
 
43
48
  test "As a user I want to be able to validate booleans" do
@@ -57,8 +62,9 @@ class JsonapiMatcherTest < ActiveSupport::TestCase
57
62
  invalid_json = {
58
63
  true: 1,
59
64
  false: "wibble"
60
- }
65
+ }.to_json
61
66
 
67
+ # byebug
62
68
  assert_not(test_schema.valid?(invalid_json), "\"1\" and \"wibble\" are not valid boolean values")
63
69
  end
64
70
 
@@ -80,10 +86,6 @@ class JsonapiMatcherTest < ActiveSupport::TestCase
80
86
  assert(!test_schema.valid?(invalid_json), "\"1\" is not a valid array value")
81
87
  end
82
88
 
83
- test "As a user I want to be able to specify what should be found in an array" do
84
- flunk "Implement me"
85
- end
86
-
87
89
  test "As a user I want to be able to validate date values" do
88
90
  test_schema = EasyJSONMatcher::SchemaGenerator.new { |schema|
89
91
  schema.has_attribute(key: :date, opts: {type: :date})
@@ -124,7 +126,7 @@ class JsonapiMatcherTest < ActiveSupport::TestCase
124
126
 
125
127
  invalid_json = {
126
128
  object: not_an_object
127
- }
129
+ }.to_json
128
130
 
129
131
  assert_not(test_schema.valid?(invalid_json), "#{not_an_object} should not have validated as an object")
130
132
  end
@@ -192,16 +194,6 @@ class JsonapiMatcherTest < ActiveSupport::TestCase
192
194
  assert(test_schema.valid?(valid_json), "Nested JSON was not correctly validated")
193
195
  end
194
196
 
195
- test "As a user I want to be able to register a Schema so I can reuse it later" do
196
-
197
- EasyJSONMatcher::SchemaGenerator.new { |schema|
198
- schema.has_attribute(key: :ignore_me, opts: {type: :value})
199
- }.register(schema_name: :test)
200
-
201
- assert(EasyJSONMatcher.available_schemas.include?(:test), ":test not found in available_nodes")
202
- end
203
-
204
-
205
197
  test "As a user I want to know why my json was not valid" do
206
198
 
207
199
  class Validator
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ class ManagingSchemasTest < ActiveSupport::TestCase
4
+
5
+ setup do
6
+ @name = :test
7
+ EasyJSONMatcher::SchemaGenerator.new { |schema|
8
+ schema.has_attribute(key: :name, opts: {type: :string, required: true})
9
+ }.register(schema_name: @name)
10
+ end
11
+
12
+ test "As a user I want to be able to register a Schema so I can reuse it later" do
13
+ assert(EasyJSONMatcher::SchemaLibrary.available_schemas.include?(:test), ":test not found in available_nodes")
14
+ end
15
+
16
+ test "As as user I want to reuse a saved schema" do
17
+ test_schema = {
18
+ name: 'Green Mandarin'
19
+ }.to_json
20
+
21
+ schema = EasyJSONMatcher::SchemaLibrary.get_schema(@name)
22
+ assert(schema.valid?(test_schema), "test_schema did not validate correctly")
23
+ end
24
+
25
+ test "SchemaLibrary should thrown a MissingSchemaException if an unregistered schema is requested" do
26
+ assert_raises(EasyJSONMatcher::MissingSchemaException) do
27
+ EasyJSONMatcher::SchemaLibrary.get_schema("#{@name.to_s}-wibble")
28
+ end
29
+ end
30
+
31
+ test "As a user I want to reuse a schema within another schema" do
32
+ test_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
33
+ s.has_attribute(key: :is_present, opts: {type: :boolean, required: true})
34
+ s.contains_schema(schema_name: @name)
35
+ }.generate_node
36
+
37
+ invalid_json = {
38
+ is_present: true,
39
+ }
40
+
41
+ assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should not have been valid as it does not include the saved schema")
42
+
43
+ valid_json = invalid_json.dup
44
+ valid_json.store(@name, {name: 'Achilles Tang'})
45
+ valid_json = valid_json.to_json
46
+ assert(test_schema.valid?(valid_json), "Stored schema did not validate correctly")
47
+ end
48
+
49
+ test "It can validate JSON Schema payloads" do
50
+ EasyJSONMatcher::SchemaGenerator.new { |country|
51
+ country.has_attribute key: :id, opts: {type: :number, required: true}
52
+ country.contains_node(key: :attributes) do |atts|
53
+ atts.has_attribute key: :alpha_2, opts: {type: :string, required: true}
54
+ atts.has_attribute key: :alpha_3, opts: {type: :string, required: true}
55
+ atts.has_attribute key: :name, opts: {type: :string, required: true}
56
+ end
57
+ }.register(schema_name: :country)
58
+
59
+ EasyJSONMatcher::SchemaGenerator.new {|country_payload|
60
+ country_payload.contains_schema(schema_name: :country, opts: {key: :data})
61
+ }.register(schema_name: :country_payload)
62
+
63
+ valid_json = "{\"data\":{\"id\":\"4376\",\"type\":\"countries\",\"attributes\":{\"alpha_2\":\"GB\",\"alpha_3\":\"GBR\",\"name\":\"United Kingdom of Great Britain and Northern Ireland\"}}}"
64
+
65
+ validator = EasyJSONMatcher::SchemaLibrary.get_schema(:country_payload)
66
+ assert(validator.valid? valid_json)
67
+ end
68
+ end
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ class RequireValidationTest < ActiveSupport::TestCase
4
+
5
+ test "As a user I want to insist that a value is present" do
6
+ astronaut_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
7
+ s.has_attribute key: :has_oxygen, opts: {type: :boolean, required: true}
8
+ s.has_attribute key: :name, opts: {type: :string}
9
+ }.generate_node
10
+
11
+ valid_astronaut = {
12
+ has_oxygen: true,
13
+ name: 'Buzz Aldrin'
14
+ }.to_json
15
+
16
+ assert(astronaut_schema.valid?(valid_astronaut), "#{valid_astronaut} should be a valid object")
17
+
18
+ invalid_astronaut = {
19
+ name: 'Neil Armstrong'
20
+ }.to_json
21
+
22
+ assert_not(astronaut_schema.valid?(invalid_astronaut), "#{invalid_astronaut} should not be a valid object")
23
+ end
24
+
25
+ test "As a user I want validations to pass if the value is not present and I have not required it to be there" do
26
+ astronaut_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
27
+ s.has_attribute key: :has_oxygen, opts: {type: :boolean}
28
+ s.has_attribute key: :name, opts: {type: :string}
29
+ }.generate_node
30
+
31
+ valid_astronaut = {
32
+ name: 'Buzz Aldrin'
33
+ }.to_json
34
+
35
+ assert(astronaut_schema.valid?(valid_astronaut), "#{valid_astronaut} should be valid since :has_oxygen is not required")
36
+ end
37
+ end
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ class ValidatingArraysTest < ActiveSupport::TestCase
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
+ test "As a user I want to validate that my arrays can contain objects of a specific schema" do
37
+ EasyJSONMatcher::SchemaGenerator.new {|s|
38
+
39
+ s.has_attribute key: :name, opts: {type: :string, required: true}
40
+ s.has_attribute key: :spouse, opts: {type: :string}
41
+ }.register(schema_name: :greek_hero)
42
+
43
+ test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
44
+ s.contains_array(key: :data, opts: {required: true}) do |a|
45
+ a.should_only_contain(type: :schema, opts: {name: :greek_hero})
46
+ end
47
+ }.generate_node
48
+
49
+ valid_json = {
50
+ data: [{
51
+ name: 'Greek Heroes',
52
+ items: ['Hector', 'Ajax', 'Hippolyta', 'Penthesila']
53
+ },
54
+ {
55
+ name: 'Roman Heroes',
56
+ items: ['Romulus', 'Remus', 'Aeneus']
57
+ }]
58
+ }.to_json
59
+
60
+ assert(test_schema.valid?(valid_json), "#{valid_json} should have been validated as it follows the correct schema")
61
+
62
+ invalid_json = {
63
+ data: [1,2,3,4,5]
64
+ }
65
+
66
+ assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should not have validated as it does not contain a :heroes schema")
67
+ end
68
+ 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.1
4
+ version: 0.0.2.pre.1
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-10 00:00:00.000000000 Z
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,10 +52,12 @@ files:
52
52
  - lib/easy_json_matcher/boolean_validator.rb
53
53
  - lib/easy_json_matcher/content_wrapper.rb
54
54
  - lib/easy_json_matcher/date_validator.rb
55
+ - lib/easy_json_matcher/exceptions.rb
55
56
  - lib/easy_json_matcher/node.rb
56
57
  - lib/easy_json_matcher/number_validator.rb
57
58
  - lib/easy_json_matcher/object_validator.rb
58
59
  - lib/easy_json_matcher/schema_generator.rb
60
+ - lib/easy_json_matcher/schema_library.rb
59
61
  - lib/easy_json_matcher/string_validator.rb
60
62
  - lib/easy_json_matcher/validation_error.rb
61
63
  - lib/easy_json_matcher/validator.rb
@@ -63,7 +65,6 @@ files:
63
65
  - lib/easy_json_matcher/value_validator.rb
64
66
  - lib/easy_json_matcher/version.rb
65
67
  - lib/tasks/jsonapi_matcher_tasks.rake
66
- - test/date_format_test.rb
67
68
  - test/dummy/README.rdoc
68
69
  - test/dummy/Rakefile
69
70
  - test/dummy/app/assets/javascripts/application.js
@@ -100,9 +101,12 @@ files:
100
101
  - test/dummy/public/422.html
101
102
  - test/dummy/public/500.html
102
103
  - test/dummy/public/favicon.ico
104
+ - test/easy_json_matcher_test.rb
103
105
  - test/json_api_home.txt
104
- - test/jsonapi_matcher_test.rb
106
+ - test/managing_schemas_test.rb
107
+ - test/required_validation_test.rb
105
108
  - test/test_helper.rb
109
+ - test/validating_arrays_test.rb
106
110
  homepage: https://github.com/wjdhamilton/easy-json-matcher
107
111
  licenses:
108
112
  - MIT
@@ -118,9 +122,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
122
  version: '0'
119
123
  required_rubygems_version: !ruby/object:Gem::Requirement
120
124
  requirements:
121
- - - ">="
125
+ - - ">"
122
126
  - !ruby/object:Gem::Version
123
- version: '0'
127
+ version: 1.3.1
124
128
  requirements: []
125
129
  rubyforge_project:
126
130
  rubygems_version: 2.2.2
@@ -128,10 +132,12 @@ signing_key:
128
132
  specification_version: 4
129
133
  summary: Easily test your JSON output with templates and Schemas
130
134
  test_files:
131
- - test/date_format_test.rb
135
+ - test/required_validation_test.rb
136
+ - test/validating_arrays_test.rb
137
+ - test/easy_json_matcher_test.rb
132
138
  - test/json_api_home.txt
139
+ - test/managing_schemas_test.rb
133
140
  - test/test_helper.rb
134
- - test/jsonapi_matcher_test.rb
135
141
  - test/dummy/config.ru
136
142
  - test/dummy/bin/rails
137
143
  - test/dummy/bin/bundle
File without changes