restspec 0.0.4 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +5 -0
  3. data/CHANGELOG.md +0 -0
  4. data/README.md +11 -7
  5. data/Rakefile +7 -0
  6. data/bin/restspec +1 -1
  7. data/examples/store-api-tests/Gemfile.lock +3 -1
  8. data/examples/store-api-tests/api.md +47 -47
  9. data/examples/store-api-tests/spec/api/product_spec.rb +0 -2
  10. data/examples/store-api-tests/spec/api/restspec/endpoints.rb +6 -5
  11. data/examples/store-api-tests/spec/api/restspec/schemas.rb +2 -1
  12. data/{docs → guides}/endpoints.md +0 -0
  13. data/{docs → guides}/helpers.md +0 -0
  14. data/{docs → guides}/macros.md +0 -0
  15. data/{docs → guides}/matchers.md +0 -0
  16. data/{docs → guides}/schemas.md +0 -0
  17. data/{docs → guides}/tutorial.md +1 -1
  18. data/{docs → guides}/types.md +0 -0
  19. data/lib/restspec/configuration.rb +28 -4
  20. data/lib/restspec/endpoints/dsl.rb +281 -48
  21. data/lib/restspec/endpoints/endpoint.rb +18 -58
  22. data/lib/restspec/endpoints/has_schemas.rb +39 -0
  23. data/lib/restspec/endpoints/namespace.rb +4 -7
  24. data/lib/restspec/endpoints/network.rb +27 -0
  25. data/lib/restspec/endpoints/request.rb +3 -0
  26. data/lib/restspec/endpoints/response.rb +3 -0
  27. data/lib/restspec/endpoints/url_builder.rb +51 -0
  28. data/lib/restspec/rspec/api_macros.rb +2 -2
  29. data/lib/restspec/rspec/matchers/be_like_schema.rb +1 -1
  30. data/lib/restspec/rspec/matchers/be_like_schema_array.rb +1 -1
  31. data/lib/restspec/runners/docs/templates/docs.md.erb +2 -2
  32. data/lib/restspec/schema/attribute.rb +43 -0
  33. data/lib/restspec/schema/attribute_example.rb +13 -1
  34. data/lib/restspec/schema/checker.rb +80 -8
  35. data/lib/restspec/schema/dsl.rb +67 -11
  36. data/lib/restspec/schema/schema.rb +13 -1
  37. data/lib/restspec/schema/schema_example.rb +7 -1
  38. data/lib/restspec/schema/types/array_type.rb +42 -1
  39. data/lib/restspec/schema/types/basic_type.rb +62 -0
  40. data/lib/restspec/schema/types/boolean_type.rb +10 -0
  41. data/lib/restspec/schema/types/date_type.rb +12 -0
  42. data/lib/restspec/schema/types/datetime_type.rb +16 -0
  43. data/lib/restspec/schema/types/decimal_string_type.rb +16 -5
  44. data/lib/restspec/schema/types/decimal_type.rb +17 -1
  45. data/lib/restspec/schema/types/embedded_schema_type.rb +39 -8
  46. data/lib/restspec/schema/types/hash_type.rb +51 -12
  47. data/lib/restspec/schema/types/integer_type.rb +12 -1
  48. data/lib/restspec/schema/types/null_type.rb +7 -0
  49. data/lib/restspec/schema/types/one_of_type.rb +18 -0
  50. data/lib/restspec/schema/types/schema_id_type.rb +14 -17
  51. data/lib/restspec/schema/types/string_type.rb +9 -0
  52. data/lib/restspec/schema/types/type_methods.rb +32 -0
  53. data/lib/restspec/schema/types.rb +1 -18
  54. data/lib/restspec/shortcuts.rb +10 -0
  55. data/lib/restspec/stores/endpoint_store.rb +27 -2
  56. data/lib/restspec/stores/namespace_store.rb +23 -4
  57. data/lib/restspec/stores/schema_store.rb +15 -0
  58. data/lib/restspec/values/status_code.rb +16 -1
  59. data/lib/restspec/version.rb +1 -1
  60. data/lib/restspec.rb +2 -0
  61. data/restspec.gemspec +2 -0
  62. data/spec/restspec/endpoints/dsl_spec.rb +32 -19
  63. data/spec/restspec/endpoints/endpoint_spec.rb +20 -43
  64. data/spec/restspec/endpoints/namespace_spec.rb +0 -7
  65. data/spec/restspec/endpoints/request_spec.rb +33 -0
  66. data/spec/restspec/schema/attribute_spec.rb +44 -0
  67. data/spec/restspec/schema/checker_spec.rb +57 -0
  68. data/spec/restspec/schema/dsl_spec.rb +1 -1
  69. data/spec/restspec/schema/schema_spec.rb +15 -0
  70. data/spec/restspec/schema/types/basic_type_spec.rb +2 -2
  71. data/spec/restspec/schema/types/decimal_string_type_spec.rb +56 -0
  72. data/spec/restspec/schema/types/decimal_type_spec.rb +25 -0
  73. data/spec/restspec/schema/types/embedded_schema_type_spec.rb +32 -0
  74. data/spec/restspec/schema/types/hash_type_spec.rb +39 -0
  75. data/spec/restspec/schema/types/integer_type_spec.rb +28 -0
  76. data/spec/restspec/schema/types/one_of_type_spec.rb +21 -0
  77. data/spec/restspec/stores/endpoint_store_spec.rb +62 -0
  78. metadata +63 -10
  79. data/ROADMAP.md +0 -13
@@ -17,12 +17,12 @@ describe Restspec::Endpoints::DSL do
17
17
 
18
18
  it 'creates a namespace with the given name as a string' do
19
19
  dsl.namespace(:monkey) { }
20
- expect(Restspec::NamespaceStore.first.name).to eq('monkey')
20
+ expect(Restspec::NamespaceStore.get(:monkey).name).to eq('monkey')
21
21
  end
22
22
 
23
23
  it 'creates a namespace with the given base_path' do
24
24
  dsl.namespace(:monkey, base_path: '/monkey') { }
25
- expect(Restspec::NamespaceStore.first.full_base_path).to eq('/monkey')
25
+ expect(Restspec::NamespaceStore.get(:monkey).full_base_path).to eq('/monkey')
26
26
  end
27
27
 
28
28
  it 'yields a Restspec::Endpoints::NamespaceDSL attached to the namespace to the block' do
@@ -39,19 +39,24 @@ describe Restspec::Endpoints::DSL do
39
39
  end
40
40
 
41
41
  expect(namespace_dsl).to have_received(:call)
42
- expect(namespace_dsl.namespace).to eq(Restspec::NamespaceStore.first)
42
+ expect(namespace_dsl.namespace).to eq(Restspec::NamespaceStore.get(:monkey))
43
43
  end
44
44
  end
45
45
 
46
46
  describe '#resource' do
47
+ before do
48
+ schema = Restspec::Schema::Schema.new(:monkey)
49
+ Restspec::SchemaStore.store(schema)
50
+ end
51
+
47
52
  it 'creates a namespace with the base_path equals to /:name' do
48
53
  dsl.resource(:monkeys) { }
49
- expect(Restspec::NamespaceStore.first.full_base_path).to eq('/monkeys')
54
+ expect(Restspec::NamespaceStore.get(:monkeys).full_base_path).to eq('/monkeys')
50
55
  end
51
56
 
52
57
  it 'creates a namespace with the schema attached to some schema with the same name' do
53
58
  dsl.resource(:monkeys) { }
54
- expect(Restspec::NamespaceStore.first.schema_name).to eq(:monkey)
59
+ expect(Restspec::NamespaceStore.get(:monkeys).schema_for(:response).name).to eq(:monkey)
55
60
  end
56
61
  end
57
62
 
@@ -86,14 +91,22 @@ describe Restspec::Endpoints::DSL do
86
91
  end
87
92
 
88
93
  describe '#schema' do
94
+ before do
95
+ Restspec::SchemaStore.store(Restspec::Schema::Schema.new(:schema_name))
96
+ end
97
+
89
98
  it 'sets the schema_name of the namespace' do
90
99
  expect do
91
100
  ns_dsl.schema :schema_name
92
- end.to change { namespace.schema_name }.to(:schema_name)
101
+ end.to change { namespace.schema_for(:response).try(:name) }.to(:schema_name)
93
102
  end
94
103
  end
95
104
 
96
105
  describe '#all' do
106
+ before do
107
+ Restspec::SchemaStore.store(Restspec::Schema::Schema.new(:a_schema_name))
108
+ end
109
+
97
110
  it 'calls the same block in all the endpoints inside' do
98
111
  ns_dsl.all do
99
112
  schema :a_schema_name
@@ -105,8 +118,8 @@ describe Restspec::Endpoints::DSL do
105
118
  endpoint_a = Restspec::EndpointStore.get('monkeys/a')
106
119
  endpoint_b = Restspec::EndpointStore.get('monkeys/b')
107
120
 
108
- expect(endpoint_a.schema_name).to eq(:a_schema_name)
109
- expect(endpoint_b.schema_name).to eq(:a_schema_name)
121
+ expect(endpoint_a.schema_for(:response).name).to eq(:a_schema_name)
122
+ expect(endpoint_a.schema_for(:response).name).to eq(:a_schema_name)
110
123
  end
111
124
  end
112
125
 
@@ -196,19 +209,19 @@ describe Restspec::Endpoints::DSL do
196
209
  end
197
210
  end
198
211
 
199
- describe '#schema' do
200
- it 'sets the schema_name' do
201
- expect {
202
- endpoint_dsl.schema :monkey
203
- }.to change { endpoint.schema_name }.from(nil).to(:monkey)
212
+ describe '#no_schema' do
213
+ it 'removes the schema' do
214
+ endpoint_dsl.schema :monkey
215
+ endpoint_dsl.no_schema
216
+ expect(endpoint.all_schemas).to eq([])
204
217
  end
218
+ end
205
219
 
206
- it 'sets the schema_name with schema_extensions' do
207
- extensions = { a: 1, b: 2, c: 3 }
208
-
220
+ describe '#schema' do
221
+ it 'sets the schema for response' do
209
222
  expect {
210
- endpoint_dsl.schema :monkey, extensions
211
- }.to change { endpoint.schema_extensions }.from(nil).to(extensions)
223
+ endpoint_dsl.schema :monkey
224
+ }.to change { endpoint.schema_for(:response).try(:name) }.from(nil).to(:monkey)
212
225
  end
213
226
  end
214
227
 
@@ -250,7 +263,7 @@ describe Restspec::Endpoints::DSL do
250
263
  let(:schema) { double(attributes: { param: 1 }) }
251
264
 
252
265
  before do
253
- allow(endpoint).to receive(:schema).and_return(schema)
266
+ allow(endpoint).to receive(:schema_for).and_return(schema)
254
267
  end
255
268
 
256
269
  it 'calls the example_for with the attribute from the schema' do
@@ -6,6 +6,25 @@ describe Endpoint do
6
6
  let(:endpoint) { Endpoint.new(:endpoint) }
7
7
  subject { endpoint }
8
8
 
9
+ describe '#payload' do
10
+ let(:schema) do
11
+ dsl = Restspec::Schema::SingleSchemaDSL.new(:product, {})
12
+ dsl.instance_eval do
13
+ attribute :title, string, example: 'monkey'
14
+ end
15
+ dsl.schema
16
+ end
17
+
18
+ before do
19
+ Restspec::SchemaStore.store(schema)
20
+ endpoint.add_schema(schema.name, :for => [:payload])
21
+ end
22
+
23
+ it 'is an example for the schema' do
24
+ expect(endpoint.payload).to eq(title: 'monkey')
25
+ end
26
+ end
27
+
9
28
  describe '#full_name' do
10
29
  context 'standalone' do
11
30
  its(:full_name) { should eq('endpoint') }
@@ -66,48 +85,6 @@ describe Endpoint do
66
85
  end
67
86
  end
68
87
 
69
- describe '#schema_name' do
70
- context 'stadalone' do
71
- before { endpoint.schema_name = :schema_name }
72
- its(:schema_name) { should eq(:schema_name) }
73
- end
74
-
75
- context 'inside a namespace' do
76
- let(:namespace) do
77
- Namespace.new(:namespace).tap do |ns|
78
- ns.schema_name = :ns_schema_name
79
- end
80
- end
81
-
82
- before { endpoint.namespace = namespace }
83
-
84
- context 'without a single schema name' do
85
- its(:schema_name) { should eq(:ns_schema_name) }
86
- end
87
-
88
- context 'with a single schema name' do
89
- before { endpoint.schema_name = :single_schema_name }
90
- its(:schema_name) { should eq(:single_schema_name) }
91
- end
92
- end
93
- end
94
-
95
- describe '#schema' do
96
- let(:schema) { double }
97
-
98
- before do
99
- endpoint.schema_name = :single_schema_name
100
- allow(Restspec::SchemaStore).to receive(:get).and_return(schema)
101
- allow(schema).to receive(:extend_with) { schema }
102
- end
103
-
104
- it 'tells the Restspec::Schema::Finder to find a schema' do
105
- found_schema = endpoint.schema
106
- expect(Restspec::SchemaStore).to have_received(:get).with(:single_schema_name)
107
- expect(found_schema).to eq(schema)
108
- end
109
- end
110
-
111
88
  describe '#url_params' do
112
89
  before do
113
90
  endpoint.add_url_param_block(:mono) { 'mono' }
@@ -130,8 +107,8 @@ describe Endpoint do
130
107
  end
131
108
 
132
109
  context 'inside an anonymous namespace (a member or collection block)' do
133
- let(:member_namespace) { Namespace.create }
134
110
  let(:top_level_namespace) { Namespace.create('top_level') }
111
+ let(:member_namespace) { top_level_namespace.add_anonymous_children_namespace }
135
112
 
136
113
  before do
137
114
  top_level_namespace.base_path = '/monkeys'
@@ -10,13 +10,6 @@ describe Namespace do
10
10
  namespace = Namespace.create(:name)
11
11
  expect(Restspec::NamespaceStore.get(:name)).to eq(namespace)
12
12
  end
13
-
14
- context 'without arguments' do
15
- it 'creates an anonymous namespace' do
16
- namespace = Namespace.create
17
- expect(namespace).to be_anonymous
18
- end
19
- end
20
13
  end
21
14
 
22
15
  describe '#add_anonymous_children_namespace' do
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ include Restspec::Endpoints
4
+
5
+ describe Request do
6
+ let(:request) do
7
+ Request.new(:get, '/url', {'header' => 'header'}, {'payload' => 'payload'})
8
+ end
9
+
10
+ it 'is a bag for request data' do
11
+ expect(request.method).to eq(:get)
12
+ expect(request.url).to eq('/url')
13
+ expect(request.headers).to eq({'header' => 'header'})
14
+ expect(request.payload).to eq({'payload' => 'payload'})
15
+ end
16
+
17
+ describe '#endpoint=' do
18
+ let(:endpoint) { double }
19
+
20
+ it 'injects an endpoint' do
21
+ request.endpoint = endpoint
22
+ expect(request.endpoint).to eq(endpoint)
23
+ end
24
+ end
25
+
26
+ describe '#raw_payload' do
27
+ it 'is the raw payload to send through the wire json encoded' do
28
+ request.payload = { 'hola' => 'mundo' }
29
+ expect(JSON.parse(request.raw_payload)).to eq(request.payload)
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ include Restspec::Schema
4
+
5
+ describe Attribute do
6
+ let(:type) { double }
7
+
8
+ describe '#example' do
9
+ let(:example) { 'example' }
10
+ let(:attribute) { Attribute.new(:name, type, example: example) }
11
+
12
+ it 'returns the example option' do
13
+ expect(attribute.example).to eq(example)
14
+ end
15
+ end
16
+
17
+ describe '#can_generate_examples?' do
18
+ subject { attribute.can_generate_examples? }
19
+
20
+ context 'without the option' do
21
+ let(:attribute) { Attribute.new(:name, type, example: 'example', :for => []) }
22
+ it { should eq(false) }
23
+ end
24
+
25
+ context 'with the option' do
26
+ let(:attribute) { Attribute.new(:name, type, example: 'example', :for => [:examples]) }
27
+ it { should eq(true) }
28
+ end
29
+ end
30
+
31
+ describe '#can_be_checked?' do
32
+ subject { attribute.can_be_checked? }
33
+
34
+ context 'without the option' do
35
+ let(:attribute) { Attribute.new(:name, type, :for => []) }
36
+ it { should eq(false) }
37
+ end
38
+
39
+ context 'with the option' do
40
+ let(:attribute) { Attribute.new(:name, type, :for => [:checks]) }
41
+ it { should eq(true) }
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ include Restspec::Schema
4
+
5
+ describe Checker do
6
+ let(:schema) do
7
+ Schema.new(:product).tap do |schema|
8
+ schema.attributes[:name] = Attribute.new(:name, Types::StringType.new)
9
+ end
10
+ end
11
+
12
+ let(:checker) { Checker.new(schema) }
13
+
14
+ describe '#check!' do
15
+ context 'when no object is sent' do
16
+ it 'raises a Checker::NoObjectError' do
17
+ expect do
18
+ checker.check!(nil)
19
+ end.to raise_error(Checker::NoObjectError, /Nil/)
20
+ end
21
+ end
22
+
23
+ context 'when no attribute is present' do
24
+ it 'raises a Checker::NoAttributeError' do
25
+ expect do
26
+ checker.check!(age: 10)
27
+ end.to raise_error(Checker::NoAttributeError, /name/)
28
+ end
29
+ end
30
+
31
+ context 'when the attribute is not valid' do
32
+ it 'raises a Checker::InvalidationError' do
33
+ expect do
34
+ checker.check!(name: 10)
35
+ end.to raise_error(Checker::InvalidationError, /string/)
36
+ end
37
+ end
38
+
39
+ context 'when the attribute is present and valid' do
40
+ it 'does not raises any error' do
41
+ expect do
42
+ checker.check!(name: 'John')
43
+ end.to_not raise_error
44
+ end
45
+ end
46
+ end
47
+
48
+ describe '#check_array!' do
49
+ let(:array) { [1, 2, 3] }
50
+
51
+ it 'calls #check! with every item of the array' do
52
+ allow(checker).to receive(:check!).and_return(true)
53
+ checker.check_array!(array)
54
+ expect(checker).to have_received(:check!).exactly(3).times
55
+ end
56
+ end
57
+ end
@@ -78,7 +78,7 @@ describe SingleSchemaDSL do
78
78
 
79
79
  describe '#include_attributes' do
80
80
  let(:main_dsl) { DSL.new }
81
- let(:schema_dsl) { SingleSchemaDSL.new(:name, main_dsl.mixins) }
81
+ let(:schema_dsl) { SingleSchemaDSL.new(:name, main_dsl.send(:mixins)) }
82
82
 
83
83
  before do
84
84
  main_dsl.mixin :test_mixin do
@@ -8,4 +8,19 @@ describe Restspec::Schema::Schema do
8
8
  expect(person_schema.attributes).to eq({})
9
9
  expect(person_schema.name).to eq(:person)
10
10
  end
11
+
12
+ describe '#extend_with' do
13
+ let(:schema) { Schema.new(:person) }
14
+
15
+ before do
16
+ schema.attributes['attribute'] = 'Attribute'
17
+ end
18
+
19
+ context 'using :without' do
20
+ it 'removes an attribute' do
21
+ schema.extend_with(without: ['attribute'])
22
+ expect(schema.attributes).to_not have_key('attribute')
23
+ end
24
+ end
25
+ end
11
26
  end
@@ -13,7 +13,7 @@ describe BasicType do
13
13
  let(:attribute) { double }
14
14
  let(:value) { double }
15
15
 
16
- describe '|' do
16
+ describe '#|' do
17
17
  let(:other_type) { subclass.new }
18
18
 
19
19
  before do
@@ -38,7 +38,7 @@ describe BasicType do
38
38
  end
39
39
  end
40
40
 
41
- describe 'of' do
41
+ describe '#of' do
42
42
  let(:other_type) { subclass.new }
43
43
 
44
44
  let(:subclass) do
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ include Restspec::Schema::Types
4
+
5
+ describe DecimalStringType do
6
+ let(:options) { {} }
7
+ let(:type) { DecimalStringType.new options }
8
+
9
+ describe '#example_for' do
10
+ it 'generates a random number wrapped in a string' do
11
+ 10.times do
12
+ sample = type.example_for(double)
13
+ expect(sample).to respond_to(:to_f)
14
+ end
15
+ end
16
+ end
17
+
18
+ describe '#valid?' do
19
+ context 'without integer_part or decimal_part options' do
20
+ it 'will validate against any decimal string' do
21
+ expect(type.totally_valid?(double, '0.1001019')).to eq(true)
22
+ expect(type.totally_valid?(double, '9992329323.9')).to eq(true)
23
+ expect(type.totally_valid?(double, '9992329323.0')).to eq(true)
24
+ expect(type.totally_valid?(double, '9992329323')).to eq(true)
25
+ expect(type.totally_valid?(double, 'mono')).to eq(false)
26
+ expect(type.totally_valid?(double, '')).to eq(false)
27
+ expect(type.totally_valid?(double, 199.99)).to eq(false)
28
+ end
29
+ end
30
+
31
+ context 'with integer part and decimal part options' do
32
+ context 'with 2,2' do
33
+ let(:options) { { schema_options: { integer_part: 2, decimal_part: 2 } } }
34
+
35
+ it 'will validate only against strings with two integer parts and two decimal parts' do
36
+ expect(type.totally_valid?(double, '2.22')).to eq(true)
37
+ expect(type.totally_valid?(double, '22.22')).to eq(true)
38
+ expect(type.totally_valid?(double, '222.22')).to eq(false)
39
+ expect(type.totally_valid?(double, '22.221')).to eq(false)
40
+ end
41
+ end
42
+
43
+ context 'with 2,0' do
44
+ let(:options) { { schema_options: { integer_part: 2, decimal_part: 0 } } }
45
+
46
+ it 'will validate only against strings with two integer parts and 0 decimal parts' do
47
+ expect(type.totally_valid?(double, '2')).to eq(true)
48
+ expect(type.totally_valid?(double, '22')).to eq(true)
49
+ expect(type.totally_valid?(double, '222.22')).to eq(false)
50
+ expect(type.totally_valid?(double, '22.2')).to eq(false)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ include Restspec::Schema::Types
4
+
5
+ describe DecimalType do
6
+ let(:options) { {} }
7
+ let(:type) { DecimalType.new(options) }
8
+
9
+ describe '#example_for' do
10
+ it 'generates numbers' do
11
+ 10.times do
12
+ expect(type.example_for(double)).to be_kind_of(Numeric)
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '#valid?' do
18
+ it 'only validates numbers' do
19
+ expect(type.totally_valid?(double, 100)).to eq(true)
20
+ expect(type.totally_valid?(double, '100')).to eq(false)
21
+ expect(type.totally_valid?(double, 'mono')).to eq(false)
22
+ expect(type.totally_valid?(double, nil)).to eq(false)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ include Restspec::Schema::Types
4
+
5
+ describe EmbeddedSchemaType do
6
+ let(:type) { EmbeddedSchemaType.new(:category) }
7
+
8
+ before do
9
+ Restspec::Schema::DSL.new.instance_eval do
10
+ schema :category do
11
+ attribute :name, string
12
+ attribute :description, string
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '#example_for' do
18
+ it 'is a example of the embedded schema' do
19
+ sample = type.example_for(double)
20
+ expect(sample[:name]).to be_a_kind_of(String)
21
+ end
22
+ end
23
+
24
+ describe '#valid?' do
25
+ it 'is valid if this works with the embedded schema' do
26
+ expect(type.totally_valid?(double, name: 'Name', description: 'Desc')).to eq(true)
27
+ expect(type.totally_valid?(double, name: 'Name')).to eq(false)
28
+ expect(type.totally_valid?(double, age: 10)).to eq(false)
29
+ expect(type.totally_valid?(double, name: 'Name', description: nil)).to eq(false)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ include Restspec::Schema::Types
4
+
5
+ describe HashType do
6
+ let(:options) { {} }
7
+ let(:type) { HashType.new(options) }
8
+
9
+ describe '#example_for' do
10
+ it 'returns an empty hash' do
11
+ expect(type.example_for(double)).to eq({})
12
+ end
13
+ end
14
+
15
+ describe '#valid?' do
16
+ it 'validates if it is a hash' do
17
+ expect(type.totally_valid?(double, {a: 1})).to eq(true)
18
+ expect(type.totally_valid?(double, '{a: 1}')).to eq(false)
19
+ end
20
+
21
+ context 'with some specified keys' do
22
+ let(:options) { { schema_options: { keys: ['name', 'age'] } } }
23
+
24
+ it 'validates if the keys are present' do
25
+ expect(type.totally_valid?(double, { 'name' => 'name', 'age' => 19 })).to eq(true)
26
+ expect(type.totally_valid?(double, { 'name' => 'name', 'number' => 19 })).to eq(false)
27
+ end
28
+ end
29
+
30
+ context 'with a specified value_type' do
31
+ let(:options) { { schema_options: { value_type: StringType.new } } }
32
+
33
+ it 'validates that the values are of that type' do
34
+ expect(type.totally_valid?(double, { 'a' => '1', 'b' => '2' })).to eq(true)
35
+ expect(type.totally_valid?(double, { 'a' => '1', 'b' => 2 })).to eq(false)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ include Restspec::Schema::Types
4
+
5
+ describe IntegerType do
6
+ let(:type) { IntegerType.new }
7
+ let(:attribute) { double }
8
+
9
+ describe '#example_for' do
10
+ it 'returns a number' do
11
+ 10.times do
12
+ expect(type.example_for(attribute)).to be_a_kind_of(Fixnum)
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '#valid?' do
18
+ it 'only validates integer values' do
19
+ expect(type.valid?(attribute, 1)).to eq(true)
20
+ expect(type.valid?(attribute, -10)).to eq(true)
21
+ expect(type.valid?(attribute, -10.5)).to eq(false)
22
+ expect(type.valid?(attribute, '10.5')).to eq(false)
23
+ expect(type.valid?(attribute, '10')).to eq(false)
24
+ expect(type.valid?(attribute, 'no')).to eq(false)
25
+ expect(type.valid?(attribute, nil)).to eq(false)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ include Restspec::Schema::Types
4
+
5
+ describe OneOfType do
6
+ let(:type) { OneOfType.new(elements: [1, 2, 3]) }
7
+
8
+ describe '#example_for' do
9
+ it 'returns one example of the elements array' do
10
+ expect([1, 2, 3]).to include(type.example_for(double))
11
+ end
12
+ end
13
+
14
+ describe '#valid' do
15
+ it 'checks if the value is one of the elements array' do
16
+ expect(type.totally_valid?(double, 1)).to eq(true)
17
+ expect(type.totally_valid?(double, nil)).to eq(false)
18
+ expect(type.totally_valid?(double, '1')).to eq(false)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ include Restspec::Stores
4
+
5
+ describe "EndpointStore" do
6
+ let(:endpoint) { OpenStruct.new(:full_name => :name) }
7
+
8
+ before do
9
+ EndpointStore.clear
10
+ end
11
+
12
+ describe '#store' do
13
+ it 'stores the endpoint in a key hash called name' do
14
+ EndpointStore.store(endpoint)
15
+ expect(EndpointStore.get(:name)).to eq(endpoint)
16
+ end
17
+ end
18
+
19
+ describe '#get_by_schema_name_and_role' do
20
+ before do
21
+ Restspec::SchemaStore.store(Restspec::Schema::Schema.new(:schema_name_1))
22
+ Restspec::SchemaStore.store(Restspec::Schema::Schema.new(:schema_name_2))
23
+ end
24
+
25
+ let(:endpoint_1) do
26
+ Restspec::Endpoints::Endpoint.new(:name_1).tap do |endpoint|
27
+ endpoint.add_schema :schema_name_1, :for => [:response]
28
+ end
29
+ end
30
+
31
+ let(:endpoint_2) do
32
+ Restspec::Endpoints::Endpoint.new(:name_2).tap do |endpoint|
33
+ endpoint.add_schema :schema_name_2, :for => [:response]
34
+ end
35
+ end
36
+
37
+ before do
38
+ EndpointStore.store(endpoint_1)
39
+ EndpointStore.store(endpoint_2)
40
+ end
41
+
42
+ context 'without something with the same schema' do
43
+ let(:found_endpoint) do
44
+ EndpointStore.get_by_schema_name_and_role(:schema_name_1, :name_2, :response)
45
+ end
46
+
47
+ it 'is nil' do
48
+ expect(found_endpoint).to be_nil
49
+ end
50
+ end
51
+
52
+ context 'with something with the same schema and name' do
53
+ let(:found_endpoint) do
54
+ EndpointStore.get_by_schema_name_and_role(:schema_name_1, :name_1, :response)
55
+ end
56
+
57
+ it 'is the correct endpoint' do
58
+ expect(found_endpoint).to eq(endpoint_1)
59
+ end
60
+ end
61
+ end
62
+ end