service_contract 0.0.8 → 0.0.10
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/Gemfile +4 -1
- data/lib/service_contract/assertions.rb +2 -2
- data/lib/service_contract/avro/endpoint.rb +2 -3
- data/lib/service_contract/mock.rb +58 -0
- data/lib/service_contract/version.rb +1 -1
- data/lib/service_contract.rb +1 -0
- data/service_contract.gemspec +1 -0
- data/test/mock_test.rb +35 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8533a9479365f0e1fe622726619e7d269df19251
|
4
|
+
data.tar.gz: 152c90e5d6a2ce67720ba9de55998225801d2feb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b13fb54905469260e43b5bc26c2840ca2ddfc32e35a3a240b884070aec49ba27ecafb6ba5b8ae667a8cee83593b6b0a4ebfb4d6fe50eda0f5472afc3f76f382c
|
7
|
+
data.tar.gz: 9aa632cd295c6a161544150cb0a5d83568fdf3d6dbf0bd1e09ca77bba52d763c33e3928fed2a2f63d53652497f44f736d327247ede0aec0ec05b7ffb1f5b4335
|
data/Gemfile
CHANGED
@@ -7,7 +7,7 @@ module ServiceContract
|
|
7
7
|
|
8
8
|
def assert_data_matches_type(data, type, allow_nil = true)
|
9
9
|
if type.array?
|
10
|
-
assert data.is_a?(Array), "expected #{type.name} to be an Array"
|
10
|
+
assert data.is_a?(Array), "expected #{type.name} to be an Array; instead got: #{data.inspect} (#{data.class.name})"
|
11
11
|
data.each do |datum|
|
12
12
|
assert_data_matches_type(datum, type.subtype, allow_nil)
|
13
13
|
end
|
@@ -28,7 +28,7 @@ module ServiceContract
|
|
28
28
|
end
|
29
29
|
else
|
30
30
|
# type is a scalar
|
31
|
-
assert (allow_nil && data.nil?) || type.valid_ruby_types.any?{|klass| data.is_a?(klass)}, "
|
31
|
+
assert (allow_nil && data.nil?) || type.valid_ruby_types.any?{|klass| data.is_a?(klass)}, "expected scalar type #{type.inspect} or nil; instead got: #{data.inspect} (#{data.class.name})"
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
@@ -54,9 +54,8 @@ module ServiceContract
|
|
54
54
|
|
55
55
|
# seems kinda hacky
|
56
56
|
def member?
|
57
|
-
|
58
|
-
|
59
|
-
first_field_type.name == protocol.main_type
|
57
|
+
first_param_type = parameters.first.type
|
58
|
+
first_param_type.complex? && first_param_type.name == protocol.main_type
|
60
59
|
end
|
61
60
|
end
|
62
61
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'random-word'
|
2
|
+
|
3
|
+
module ServiceContract
|
4
|
+
class Mock
|
5
|
+
class << self
|
6
|
+
# generates fake data that adheres to the types defined in the service contract
|
7
|
+
def generate!(version)
|
8
|
+
mock_fields(type(version))
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def type(version)
|
14
|
+
raise "not_implemented"
|
15
|
+
end
|
16
|
+
|
17
|
+
# can set up custom generators by field name
|
18
|
+
# should be a hash of <field_name> => <lambda>
|
19
|
+
def customizations
|
20
|
+
{}
|
21
|
+
end
|
22
|
+
|
23
|
+
def mock_fields(type)
|
24
|
+
{}.tap do |res|
|
25
|
+
type.fields.each do |parameter|
|
26
|
+
if customizations.key?(parameter.name)
|
27
|
+
res[parameter.name] = customizations[parameter.name].call
|
28
|
+
else
|
29
|
+
res[parameter.name] = mock_value(parameter.type)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def mock_value(field)
|
36
|
+
if field.array?
|
37
|
+
Array.new(3) do
|
38
|
+
mock_value(field.subtype)
|
39
|
+
end
|
40
|
+
elsif field.complex?
|
41
|
+
# recursively mock values
|
42
|
+
mock_fields(field)
|
43
|
+
else
|
44
|
+
case field.name
|
45
|
+
when "int", :int
|
46
|
+
Random.new.rand(10000000)
|
47
|
+
when "string", :string
|
48
|
+
RandomWord.nouns.next
|
49
|
+
when "float", :float
|
50
|
+
Random.new.rand(10000.0)
|
51
|
+
when "boolean", :boolean
|
52
|
+
[true, false].sample
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/service_contract.rb
CHANGED
data/service_contract.gemspec
CHANGED
data/test/mock_test.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class LocationMock < ServiceContract::Mock
|
4
|
+
VALID_TYPES = %w(country state city county neighborhood postal_code)
|
5
|
+
|
6
|
+
def self.type(version)
|
7
|
+
SampleService.find(version).protocol("location").type("Location")
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.customizations
|
11
|
+
{
|
12
|
+
"type" => -> { VALID_TYPES.sample }
|
13
|
+
}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class MockTest < Minitest::Test
|
18
|
+
include ServiceContract::Assertions
|
19
|
+
|
20
|
+
def test_mocking
|
21
|
+
data = Array.new(2) { LocationMock.generate!(1) }
|
22
|
+
|
23
|
+
service = SampleService.find(1)
|
24
|
+
protocol = service.protocol("location")
|
25
|
+
endpoint = protocol.endpoint("index")
|
26
|
+
|
27
|
+
assert_endpoint_response(data, endpoint)
|
28
|
+
|
29
|
+
# test customizations
|
30
|
+
data.each do |datum|
|
31
|
+
assert LocationMock::VALID_TYPES.include?(datum["type"]), "should be able to override a type generator with class level customizations"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: service_contract
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Ching
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: avro
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: random-word
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: Abstract the definition of a service's interface contract. Supports Avro
|
70
84
|
email:
|
71
85
|
- jching@avvo.com
|
@@ -99,11 +113,13 @@ files:
|
|
99
113
|
- lib/service_contract/avro/views/layout.slim
|
100
114
|
- lib/service_contract/avro/views/protocol.slim
|
101
115
|
- lib/service_contract/avro/views/version.slim
|
116
|
+
- lib/service_contract/mock.rb
|
102
117
|
- lib/service_contract/tasks.rb
|
103
118
|
- lib/service_contract/version.rb
|
104
119
|
- service_contract.gemspec
|
105
120
|
- src/avro-tools-1.7.7.jar
|
106
121
|
- test/assertions_test.rb
|
122
|
+
- test/mock_test.rb
|
107
123
|
- test/sample/1/compiled/location.avpr
|
108
124
|
- test/sample/1/compiled/sales_region.avpr
|
109
125
|
- test/sample/1/source/location.avdl
|
@@ -136,6 +152,7 @@ specification_version: 4
|
|
136
152
|
summary: Abstract the definition of a service's interface contract
|
137
153
|
test_files:
|
138
154
|
- test/assertions_test.rb
|
155
|
+
- test/mock_test.rb
|
139
156
|
- test/sample/1/compiled/location.avpr
|
140
157
|
- test/sample/1/compiled/sales_region.avpr
|
141
158
|
- test/sample/1/source/location.avdl
|