service_contract 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a89e3a05c63f0cd654e67c928d715402fa1c944
4
- data.tar.gz: 6080110653f611f5cfbe03ec81612f52b60f60a9
3
+ metadata.gz: 8533a9479365f0e1fe622726619e7d269df19251
4
+ data.tar.gz: 152c90e5d6a2ce67720ba9de55998225801d2feb
5
5
  SHA512:
6
- metadata.gz: 548643b68055c8241f3c8d7746b1c08255d935c5f33584961b58e81598635449d4858aabf2fffed729da10a437e726aa34323a0098d4c957e60db1d4896bfb0c
7
- data.tar.gz: f03f58fa109ceb830b2dd3eb8f79db36058471d07c48b27cce7b8db954348518a6fa2c9e93f3763efd682ad477bc5c11af4fb2154eb6e8041b8b118e382ce02a
6
+ metadata.gz: b13fb54905469260e43b5bc26c2840ca2ddfc32e35a3a240b884070aec49ba27ecafb6ba5b8ae667a8cee83593b6b0a4ebfb4d6fe50eda0f5472afc3f76f382c
7
+ data.tar.gz: 9aa632cd295c6a161544150cb0a5d83568fdf3d6dbf0bd1e09ca77bba52d763c33e3928fed2a2f63d53652497f44f736d327247ede0aec0ec05b7ffb1f5b4335
data/Gemfile CHANGED
@@ -3,4 +3,7 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in service_contract.gemspec
4
4
  gemspec
5
5
 
6
- gem 'codeclimate-test-reporter', group: 'test', require: nil
6
+ group :test do
7
+ gem 'codeclimate-test-reporter', require: nil
8
+ gem 'random-word'
9
+ end
@@ -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)}, "foo"
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
- first_field_type = request.fields.first.type
58
- first_field_type.complex? &&
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
@@ -1,3 +1,3 @@
1
1
  module ServiceContract
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -9,6 +9,7 @@ module ServiceContract
9
9
 
10
10
  # test hooks
11
11
  autoload :Assertions, 'service_contract/assertions'
12
+ autoload :Mock, 'service_contract/mock'
12
13
 
13
14
  # providers
14
15
  autoload :Avro, 'service_contract/avro'
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "bundler"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "minitest", "~> 5"
25
+ spec.add_development_dependency "random-word"
25
26
  end
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.8
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 00:00:00.000000000 Z
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