dryer_factories 0.0.2
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 +7 -0
- data/Gemfile +2 -0
- data/LICENSE +18 -0
- data/README.md +76 -0
- data/dryer_factories.gemspec +35 -0
- data/lib/dryer/factories/build_from_contract.rb +39 -0
- data/lib/dryer/factories/fields/build.rb +48 -0
- data/lib/dryer/factories/generated_payload.rb +33 -0
- data/lib/dryer/factories/simple_service.rb +13 -0
- data/lib/dryer/factories/version.rb +5 -0
- data/lib/dryer_factories.rb +1 -0
- metadata +112 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 18286ce2561d13272e7fbd832db21eeaecbff6ff36c71e66f492ad7e10fca800
|
|
4
|
+
data.tar.gz: e01e177bbfacd57ab481e87401582b84d4e29ae6468bdd8a787932f05d53c0f1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0c2587adad52a5703f84dd7a16fced350f95d9577a0cd6579a910e78455d56e0a31471e0625590bf1a5763bff65313930ba3e941fffed370c6724626a643b9e2
|
|
7
|
+
data.tar.gz: 2e3d26a0a4e5d1f2a533d368eb929fab80d05619c317e8157cc297f5684c1480cf13083a96b26fdd35f7be3fabc9a5a83501e1b908d260b684cf8dd1d75dc0ea
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
Copyright (c) 2023 John Bernier
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
The above copyright notice and this permission notice shall be
|
|
11
|
+
included in all copies or substantial portions of the Software.
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
13
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
14
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
15
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
16
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
17
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
18
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
|
|
2
|
+
# Dryer Factories
|
|
3
|
+
Dryer Factories generates test data from an instance of [Dry::Validation::Contract](https://dry-rb.org/gems/dry-validation/1.10/)
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
add the following to you gemfile
|
|
7
|
+
```
|
|
8
|
+
gem "dryer_factories"
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
```
|
|
13
|
+
test "POST 200 - successfully creating a user" do
|
|
14
|
+
request = Dryer::Factories::BuildFromContract.call(
|
|
15
|
+
ApiResources::Users::Post.request_contract
|
|
16
|
+
)
|
|
17
|
+
post ApiResources::Users::Post.url, params: request.as_json
|
|
18
|
+
|
|
19
|
+
assert_response :success
|
|
20
|
+
end
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Features
|
|
24
|
+
### Built in Generators
|
|
25
|
+
This gem currently only provides default generators for `String` `Hash` and `Array` types. Support for other types will be added as needed.
|
|
26
|
+
[Documentation for the built in dry types](https://dry-rb.org/gems/dry-types/1.7/built-in-types/)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
### Custom Types and Generators
|
|
30
|
+
You can create new types based on the existing `Dry::Types` and then provide
|
|
31
|
+
a lambda function for your generator as part of the type's metadata
|
|
32
|
+
|
|
33
|
+
Example:
|
|
34
|
+
```
|
|
35
|
+
module Contracts
|
|
36
|
+
module Types
|
|
37
|
+
dry_types = Dry::Types(default: :params)
|
|
38
|
+
Email = dry_types::String.meta(
|
|
39
|
+
generator: lambda { Faker::Internet.email }
|
|
40
|
+
)
|
|
41
|
+
Password = dry_types::String.meta(
|
|
42
|
+
generator: lambda { Faker::Internet.password }
|
|
43
|
+
)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Limitations
|
|
49
|
+
Type contstraints and that are not encoded in the custom generator are ignored.
|
|
50
|
+
For example:
|
|
51
|
+
```
|
|
52
|
+
required(:contacts).value(:array, min_size?: 3)
|
|
53
|
+
```
|
|
54
|
+
will not cause the generator to create an array of length 3, it will generate an
|
|
55
|
+
array of length 1.
|
|
56
|
+
For arrays specifically, there is currently no way to generate an array larger
|
|
57
|
+
than length 1.
|
|
58
|
+
|
|
59
|
+
## Development
|
|
60
|
+
This gem is set up to be developed using [Nix](https://nixos.org/)
|
|
61
|
+
Once you have nix installed you can run
|
|
62
|
+
`make bundle`
|
|
63
|
+
to install all dependencies and
|
|
64
|
+
`make dev-shell`
|
|
65
|
+
to enter the development environment.
|
|
66
|
+
|
|
67
|
+
## Contributing
|
|
68
|
+
Please create a github issue to report any problems using the Gem.
|
|
69
|
+
Thanks for your help in making testing easier for everyone!
|
|
70
|
+
|
|
71
|
+
## Versioning
|
|
72
|
+
Dryer Factories follows Semantic Versioning 2.0 as defined at https://semver.org.
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
This code is free to use under the terms of the MIT license.
|
|
76
|
+
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#require File.expand_path('lib/dryer/factories/version.rb', __dir__)
|
|
2
|
+
Gem::Specification.new do |spec|
|
|
3
|
+
spec.name = 'dryer_factories'
|
|
4
|
+
|
|
5
|
+
# this should be imported from lib/dryer/factories/version.rb
|
|
6
|
+
# but I am having nix issues with importing the version file
|
|
7
|
+
# when this file is evaluated
|
|
8
|
+
#spec.version = Dryer::Factories::Version
|
|
9
|
+
spec.version = "0.0.2"
|
|
10
|
+
spec.authors = ['John Bernier']
|
|
11
|
+
spec.email = ['john.b.bernier@gmail.com']
|
|
12
|
+
spec.summary = 'Generate payloads from dry-validation definitions'
|
|
13
|
+
spec.description = <<~DOC
|
|
14
|
+
An extension of the Dry family of gems (dry-rb.org).
|
|
15
|
+
This gem can generate valid payloads for contracts defined using the
|
|
16
|
+
dry-validation gem.
|
|
17
|
+
DOC
|
|
18
|
+
spec.homepage = 'https://github.com/jbernie2/dryer-factories'
|
|
19
|
+
spec.license = 'MIT'
|
|
20
|
+
spec.platform = Gem::Platform::RUBY
|
|
21
|
+
spec.required_ruby_version = '>= 3.0.0'
|
|
22
|
+
spec.files = Dir[
|
|
23
|
+
'README.md',
|
|
24
|
+
'LICENSE',
|
|
25
|
+
'CHANGELOG.md',
|
|
26
|
+
'lib/**/*.rb',
|
|
27
|
+
'dryer_factories.gemspec',
|
|
28
|
+
'.github/*.md',
|
|
29
|
+
'Gemfile'
|
|
30
|
+
]
|
|
31
|
+
spec.add_dependency "dry-validation", "~> 1.10"
|
|
32
|
+
spec.add_dependency "dry-types", "~> 1.7"
|
|
33
|
+
spec.add_development_dependency "rspec", "~> 3.10"
|
|
34
|
+
spec.add_development_dependency "debug", "~> 1.8"
|
|
35
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require_relative "./simple_service.rb"
|
|
2
|
+
require_relative "./generated_payload.rb"
|
|
3
|
+
require_relative "./fields/build.rb"
|
|
4
|
+
|
|
5
|
+
module Dryer
|
|
6
|
+
module Factories
|
|
7
|
+
class BuildFromContract < SimpleService
|
|
8
|
+
def initialize(contract)
|
|
9
|
+
@contract = contract
|
|
10
|
+
check_contract_type!
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def check_contract_type!
|
|
14
|
+
raise(
|
|
15
|
+
"#{self.class}.call: Argument must be an instance "+
|
|
16
|
+
"or subclass of Dry::Validation::Contract"
|
|
17
|
+
) unless contract.respond_to?(:ancestors) &&
|
|
18
|
+
contract.ancestors.include?(Dry::Validation::Contract)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def call
|
|
22
|
+
gen_object = Object.new
|
|
23
|
+
contract.schema.types.inject({}) do |fields, (field_name, field_type)|
|
|
24
|
+
fields.merge(Fields::Build.call(
|
|
25
|
+
name: field_name.to_sym,
|
|
26
|
+
type: field_type
|
|
27
|
+
))
|
|
28
|
+
end.then do |fields|
|
|
29
|
+
GeneratedPayload.new(
|
|
30
|
+
contract: contract,
|
|
31
|
+
fields: fields
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
attr_reader :contract
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
require_relative "../simple_service.rb"
|
|
2
|
+
|
|
3
|
+
module Dryer
|
|
4
|
+
module Factories
|
|
5
|
+
module Fields
|
|
6
|
+
class Build < SimpleService
|
|
7
|
+
def initialize(name:, type:)
|
|
8
|
+
@name = name
|
|
9
|
+
@type = type
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def call
|
|
13
|
+
build_field(name, type)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def build_field(field_name, field_type)
|
|
19
|
+
{ field_name => build_field_value(field_type) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def build_field_value(field_type)
|
|
23
|
+
generator = field_type.meta[:generator]
|
|
24
|
+
if generator
|
|
25
|
+
generator.call
|
|
26
|
+
else
|
|
27
|
+
case field_type.type.name
|
|
28
|
+
when "String"
|
|
29
|
+
SecureRandom.uuid.to_s
|
|
30
|
+
when "Hash"
|
|
31
|
+
generate_hash(field_type)
|
|
32
|
+
when "Array"
|
|
33
|
+
[ build_field_value(field_type.type.member) ]
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def generate_hash(field_type)
|
|
39
|
+
field_type.keys.inject({}) do |hash, k|
|
|
40
|
+
hash.merge(build_field(k.name, k.type))
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
attr_reader :name, :type
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module Dryer
|
|
2
|
+
module Factories
|
|
3
|
+
class GeneratedPayload
|
|
4
|
+
def initialize(contract:, fields:)
|
|
5
|
+
@contract = contract
|
|
6
|
+
@fields = fields
|
|
7
|
+
create_instance_variables
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def create_instance_variables
|
|
11
|
+
fields.inject(self) do |obj, (name, value)|
|
|
12
|
+
self.define_singleton_method(name.to_sym) { value }
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def to_h
|
|
17
|
+
fields
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def as_json
|
|
21
|
+
to_h
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def to_json
|
|
25
|
+
to_h.to_json
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
attr_reader :contract
|
|
29
|
+
private
|
|
30
|
+
attr_reader :fields
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require_relative "./dryer/factories/build_from_contract.rb"
|
metadata
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: dryer_factories
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- John Bernier
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2023-11-29 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: dry-validation
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.10'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.10'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: dry-types
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.7'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.7'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.10'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.10'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: debug
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '1.8'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '1.8'
|
|
69
|
+
description: |
|
|
70
|
+
An extension of the Dry family of gems (dry-rb.org).
|
|
71
|
+
This gem can generate valid payloads for contracts defined using the
|
|
72
|
+
dry-validation gem.
|
|
73
|
+
email:
|
|
74
|
+
- john.b.bernier@gmail.com
|
|
75
|
+
executables: []
|
|
76
|
+
extensions: []
|
|
77
|
+
extra_rdoc_files: []
|
|
78
|
+
files:
|
|
79
|
+
- Gemfile
|
|
80
|
+
- LICENSE
|
|
81
|
+
- README.md
|
|
82
|
+
- dryer_factories.gemspec
|
|
83
|
+
- lib/dryer/factories/build_from_contract.rb
|
|
84
|
+
- lib/dryer/factories/fields/build.rb
|
|
85
|
+
- lib/dryer/factories/generated_payload.rb
|
|
86
|
+
- lib/dryer/factories/simple_service.rb
|
|
87
|
+
- lib/dryer/factories/version.rb
|
|
88
|
+
- lib/dryer_factories.rb
|
|
89
|
+
homepage: https://github.com/jbernie2/dryer-factories
|
|
90
|
+
licenses:
|
|
91
|
+
- MIT
|
|
92
|
+
metadata: {}
|
|
93
|
+
post_install_message:
|
|
94
|
+
rdoc_options: []
|
|
95
|
+
require_paths:
|
|
96
|
+
- lib
|
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
|
+
requirements:
|
|
99
|
+
- - ">="
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: 3.0.0
|
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
|
+
requirements:
|
|
104
|
+
- - ">="
|
|
105
|
+
- !ruby/object:Gem::Version
|
|
106
|
+
version: '0'
|
|
107
|
+
requirements: []
|
|
108
|
+
rubygems_version: 3.4.13
|
|
109
|
+
signing_key:
|
|
110
|
+
specification_version: 4
|
|
111
|
+
summary: Generate payloads from dry-validation definitions
|
|
112
|
+
test_files: []
|