restspec 0.2.6 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/examples/store-api-tests/Gemfile.lock +1 -1
- data/lib/restspec/schema/checker.rb +18 -0
- data/lib/restspec/schema/dsl.rb +6 -5
- data/lib/restspec/schema/schema.rb +19 -2
- data/lib/restspec/schema/schema_example.rb +11 -1
- data/lib/restspec/version.rb +1 -1
- data/spec/restspec/schema/checker_spec.rb +22 -1
- data/spec/restspec/schema/dsl_spec.rb +15 -5
- data/spec/restspec/schema/schema_example_spec.rb +22 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d41fb030fd5379b0d4b7ebfa5b11f4ca75bdfbe
|
4
|
+
data.tar.gz: c911b0e8737d4101434572e183124c1cc556d694
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12f39219a3aab6c2d7bbddafc5c13ef0d61ce689d34305363a46fbb9d75134c790f4f1ddd0cf4bdd62b988c43d485b881e9e2ca2cd8641ca862e438927584b4a
|
7
|
+
data.tar.gz: 4517a6136fe97b44156ece0da456fccc122be6bfcaa9fa04217ba255ced88709ef69df7dbaa634b2d63bdea5fd175743ebd0e173c4dee0e4751adf79dd0b2109
|
@@ -24,6 +24,11 @@ module Restspec
|
|
24
24
|
# @raise NoObjectError if parameter passed is not a hash.
|
25
25
|
def check!(object)
|
26
26
|
raise NoObjectError.new(object) unless object.is_a?(Hash)
|
27
|
+
raise NoRootFoundError.new(object, schema) if schema.root? && !object.has_key?(schema.root_name)
|
28
|
+
|
29
|
+
if schema.root?
|
30
|
+
object = object.fetch(schema.root_name)
|
31
|
+
end
|
27
32
|
|
28
33
|
schema.attributes.each do |_, attribute|
|
29
34
|
if attribute.can_be_checked?
|
@@ -129,6 +134,19 @@ module Restspec
|
|
129
134
|
end
|
130
135
|
end
|
131
136
|
|
137
|
+
class NoRootFoundError < StandardError
|
138
|
+
attr_accessor :object, :schema
|
139
|
+
|
140
|
+
def initialize(object, schema)
|
141
|
+
self.object = object
|
142
|
+
self.schema = schema
|
143
|
+
end
|
144
|
+
|
145
|
+
def to_s
|
146
|
+
"The object #{object}:#{object.class} does not contain a root called #{schema.root_name}"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
132
150
|
class NoObjectError < StandardError
|
133
151
|
attr_accessor :object
|
134
152
|
|
data/lib/restspec/schema/dsl.rb
CHANGED
@@ -11,16 +11,17 @@ module Restspec
|
|
11
11
|
# instance for further definitions.
|
12
12
|
#
|
13
13
|
# @example
|
14
|
-
# schema :book do
|
14
|
+
# schema :book, root: true do
|
15
15
|
# puts self.class # SingleSchemaDSL
|
16
16
|
# puts self.schema.class # Schema
|
17
17
|
# end
|
18
18
|
#
|
19
19
|
# @param name {Symbol} the schema's name
|
20
|
+
# @param options {Hash} a set of options for {Schema#initialize}
|
20
21
|
# @param definition A block that will be executed inside the context
|
21
22
|
# of a {SingleSchemaDSL} object.
|
22
|
-
def schema(name, &definition)
|
23
|
-
dsl = SingleSchemaDSL.new(name, mixins)
|
23
|
+
def schema(name, options = {}, &definition)
|
24
|
+
dsl = SingleSchemaDSL.new(name, options, mixins)
|
24
25
|
dsl.instance_eval(&definition)
|
25
26
|
Restspec::SchemaStore.store(dsl.schema)
|
26
27
|
end
|
@@ -66,8 +67,8 @@ module Restspec
|
|
66
67
|
# @return {Schema} the current schema
|
67
68
|
attr_reader :schema
|
68
69
|
|
69
|
-
def initialize(name, mixins = {})
|
70
|
-
self.schema = Schema.new(name)
|
70
|
+
def initialize(name, options = {}, mixins = {})
|
71
|
+
self.schema = Schema.new(name, options)
|
71
72
|
self.mixins = mixins
|
72
73
|
end
|
73
74
|
|
@@ -10,15 +10,22 @@ module Restspec
|
|
10
10
|
# The set of attributes that conforms the schema.
|
11
11
|
attr_reader :attributes
|
12
12
|
|
13
|
+
# The root raw value
|
14
|
+
attr_reader :root
|
15
|
+
|
13
16
|
# TODO: Document
|
14
17
|
attr_accessor :intention
|
15
18
|
attr_accessor :original_schema
|
16
19
|
|
17
20
|
# @param name [Symbol] The name of the schema
|
21
|
+
# @param options [Hash] Some options:
|
22
|
+
# - root: If the schema should have a root `{ schema: }` around the object. If this
|
23
|
+
# attribute is a symbol or string, that will be the schema root to use.
|
18
24
|
# @return a new {Restspec::Schema::Schema Schema} object
|
19
|
-
def initialize(name)
|
25
|
+
def initialize(name, options = {})
|
20
26
|
self.name = name
|
21
27
|
self.attributes = {}
|
28
|
+
self.root = options[:root]
|
22
29
|
end
|
23
30
|
|
24
31
|
# @param without [Array] An array of attributes that should be removed from the schema.
|
@@ -37,9 +44,19 @@ module Restspec
|
|
37
44
|
end
|
38
45
|
end
|
39
46
|
|
47
|
+
#
|
48
|
+
# @return [true, false] if the schema must include a root.
|
49
|
+
def root?
|
50
|
+
!!root
|
51
|
+
end
|
52
|
+
|
53
|
+
def root_name
|
54
|
+
root == true ? name.to_sym : root.to_sym
|
55
|
+
end
|
56
|
+
|
40
57
|
private
|
41
58
|
|
42
|
-
attr_writer :name, :attributes
|
59
|
+
attr_writer :name, :attributes, :root
|
43
60
|
end
|
44
61
|
end
|
45
62
|
end
|
@@ -15,9 +15,15 @@ module Restspec
|
|
15
15
|
# It returns the generated example.
|
16
16
|
# @return [Restspec::Values::SuperHash] generated example.
|
17
17
|
def value
|
18
|
-
attributes.inject({}) do |sample, (_, attribute)|
|
18
|
+
example_attributes = attributes.inject({}) do |sample, (_, attribute)|
|
19
19
|
sample.merge(attribute.name => AttributeExample.new(attribute).value)
|
20
20
|
end.merge(extensions)
|
21
|
+
|
22
|
+
if schema.root?
|
23
|
+
wrap_in_root(example_attributes)
|
24
|
+
else
|
25
|
+
example_attributes
|
26
|
+
end
|
21
27
|
end
|
22
28
|
|
23
29
|
private
|
@@ -25,6 +31,10 @@ module Restspec
|
|
25
31
|
def attributes
|
26
32
|
schema.attributes_for_intention
|
27
33
|
end
|
34
|
+
|
35
|
+
def wrap_in_root(hash)
|
36
|
+
{ schema.root_name => hash }
|
37
|
+
end
|
28
38
|
end
|
29
39
|
end
|
30
40
|
end
|
data/lib/restspec/version.rb
CHANGED
@@ -3,8 +3,9 @@ require 'spec_helper'
|
|
3
3
|
include Restspec::Schema
|
4
4
|
|
5
5
|
describe Checker do
|
6
|
+
let(:options) { {} }
|
6
7
|
let(:schema) do
|
7
|
-
Schema.new(:product).tap do |schema|
|
8
|
+
Schema.new(:product, options).tap do |schema|
|
8
9
|
schema.attributes[:name] = Attribute.new(:name, Types::StringType.new)
|
9
10
|
end
|
10
11
|
end
|
@@ -20,6 +21,26 @@ describe Checker do
|
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
24
|
+
context 'when the schema is root?' do
|
25
|
+
let(:options) { { root: 'monkey' } }
|
26
|
+
|
27
|
+
context 'when no root is present' do
|
28
|
+
it 'raises a Checker::NoRootFoundError' do
|
29
|
+
expect do
|
30
|
+
checker.check!(age: 10)
|
31
|
+
end.to raise_error(Checker::NoRootFoundError, /monkey/)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when the root is present' do
|
36
|
+
it 'does not raises a Checker::NoRootFoundError' do
|
37
|
+
expect do
|
38
|
+
checker.check!(monkey: { name: 'Apu' })
|
39
|
+
end.to_not raise_error
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
23
44
|
context 'when no attribute is present' do
|
24
45
|
it 'raises a Checker::NoAttributeError' do
|
25
46
|
expect do
|
@@ -7,12 +7,10 @@ describe DSL do
|
|
7
7
|
|
8
8
|
describe '#schema' do
|
9
9
|
let(:single_dsl) { double }
|
10
|
-
let(:schema) {
|
10
|
+
let(:schema) { Schema.new(name: 'name') }
|
11
11
|
|
12
12
|
before do
|
13
|
-
allow(SingleSchemaDSL).to receive(:new).
|
14
|
-
allow(single_dsl).to receive(:instance_eval).and_return(single_dsl)
|
15
|
-
allow(single_dsl).to receive(:schema).and_return(schema)
|
13
|
+
allow(SingleSchemaDSL).to receive(:new).and_call_original
|
16
14
|
end
|
17
15
|
|
18
16
|
it 'creates a SingleSchemaDSL with the given name' do
|
@@ -24,6 +22,18 @@ describe DSL do
|
|
24
22
|
dsl.schema('name') { }
|
25
23
|
expect(Restspec::SchemaStore.get('name')).to be_present
|
26
24
|
end
|
25
|
+
|
26
|
+
it 'stores a schema with the root attribute set to false' do
|
27
|
+
dsl.schema('dog') { }
|
28
|
+
expect(Restspec::SchemaStore.get('name').root?).to eq(false)
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'with the root option set to true' do
|
32
|
+
it 'creates a schema with the root option set to true' do
|
33
|
+
dsl.schema('dog', root: true) { }
|
34
|
+
expect(Restspec::SchemaStore.get('dog').root?).to eq(true)
|
35
|
+
end
|
36
|
+
end
|
27
37
|
end
|
28
38
|
end
|
29
39
|
|
@@ -54,7 +64,7 @@ describe SingleSchemaDSL do
|
|
54
64
|
|
55
65
|
describe '#include_attributes' do
|
56
66
|
let(:main_dsl) { DSL.new }
|
57
|
-
let(:schema_dsl) { SingleSchemaDSL.new(:name, main_dsl.send(:mixins)) }
|
67
|
+
let(:schema_dsl) { SingleSchemaDSL.new(:name, {}, main_dsl.send(:mixins)) }
|
58
68
|
|
59
69
|
before do
|
60
70
|
main_dsl.mixin :test_mixin do
|
@@ -15,6 +15,28 @@ describe SchemaExample do
|
|
15
15
|
schema.attributes['age'] = Attribute.new(:age, integer_type)
|
16
16
|
end
|
17
17
|
|
18
|
+
describe 'root cases' do
|
19
|
+
let(:schema_example) { SchemaExample.new(schema) }
|
20
|
+
|
21
|
+
context 'with a schema that root? with a value' do
|
22
|
+
let(:schema) { Schema.new(:dog, root: 'monkey') }
|
23
|
+
|
24
|
+
it 'generates an example wrapped in the root value' do
|
25
|
+
expect(schema_example.value).to have_key(:monkey)
|
26
|
+
expect(schema_example.value[:monkey]).to have_key(:name)
|
27
|
+
expect(schema_example.value[:monkey]).to have_key(:age)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'with a schema that root? with true' do
|
32
|
+
let(:schema) { Schema.new(:dog, root: true) }
|
33
|
+
|
34
|
+
it 'generates an example wrapped in the schema name' do
|
35
|
+
expect(schema_example.value).to have_key(:dog)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
18
40
|
context 'without extensions' do
|
19
41
|
let(:schema_example) { SchemaExample.new(schema) }
|
20
42
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- juliogarciag
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|