dry-operation_generators-test_unit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ec86e04e6f827c00e2d95c5621653c966cf63513b2a540259d985ce89ed0f9c9
4
+ data.tar.gz: 71f2fdc8f9c4d73adc1741885b347ceca6bb650d114c60d694f8002492515062
5
+ SHA512:
6
+ metadata.gz: 552c502342d28ead3a43b479c8312d617dab9a2ade00d1c8f5a607ba75d2a7f2099366fe22ccb5b32186ddac339b46e97e40bd2669efba97f02c62333c7fb86e
7
+ data.tar.gz: f92b3dfc279ae254eb88c404b6441e59c974b14acc7e7b0d740b58290616bbecfbd5f184b4495767492a3148ee2389d6b9cd2426338d7267afc3bb19b063351f
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ require:
4
+ - rubocop-performance
5
+ - rubocop-rake
6
+ - rubocop-rspec
7
+
8
+ AllCops:
9
+ TargetRubyVersion: 3.2.2
10
+ NewCops: enable
11
+
12
+ Style/StringLiterals:
13
+ Enabled: true
14
+ EnforcedStyle: double_quotes
15
+
16
+ Style/StringLiteralsInInterpolation:
17
+ Enabled: true
18
+ EnforcedStyle: double_quotes
19
+
20
+ Layout/LineLength:
21
+ Max: 120
22
+
23
+ Style/Documentation:
24
+ Enabled: false
25
+
26
+ Lint/SuppressedException:
27
+ Enabled: false
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,11 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2023-11-02 15:55:15 UTC using RuboCop version 1.57.2.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 3
10
+ RSpec/MultipleExpectations:
11
+ Max: 2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-03-18
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at joel.azemar@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Joel Azemar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # Operation Generators Test Unit
2
+
3
+ Operation Generators Test Unit is a utility library designed to add the test file to the files required for new Business Logic actions with Dry Rb ecosystem.
4
+
5
+ This library doesn't make sense without [dry-operation_generators](https://github.com/joel/dry-operation_generators)
6
+
7
+ Generates the counterpart test files:
8
+
9
+ ```shell
10
+ test/components/identity/operations/users/create/
11
+ ├── acction_test.rb
12
+ ├── contract_test.rb
13
+ ├── operation_test.rb
14
+ ├── register_test.rb
15
+ ├── repository_test.rb
16
+ ├── schema_test.rb
17
+ └── validation_test.rb
18
+ ```
19
+
20
+ Please check out [operation_generators](https://github.com/joel/dry-operation_generators)
21
+
22
+ ## Installation
23
+
24
+ Install the gem and add to the application's Gemfile by executing:
25
+
26
+ $ bundle add operation_generators-test_unit
27
+
28
+ If Bundler is not being used to manage dependencies, install the gem by executing:
29
+
30
+ $ gem install operation_generators-test_unit
31
+
32
+ NOTE: The gem needs to be required as `test_unit`
33
+
34
+ $ gem "operation_generators-test_unit", require: "test_unit"
35
+
36
+ Otherwise, you can require "test_unit" on your code.
37
+
38
+ ## Usage
39
+
40
+ Once installed, you can print out the Rails Generators Help:
41
+
42
+ ```shell
43
+ rails generate --help
44
+ ```
45
+
46
+ You should see
47
+
48
+ ```shell
49
+ TestUnit:
50
+ test_unit:channel
51
+ test_unit:generator
52
+ test_unit:install
53
+ test_unit:mailbox
54
+ test_unit:observer
55
+ test_unit:operations:setup
56
+ test_unit:plugin
57
+ ```
58
+
59
+ You will need to run the Setup to make the generators available in the Host Application.
60
+
61
+ ```shell
62
+ rails generate test_unit:operations:setup --force
63
+ # => initializer operation_generators-test_unit.rb
64
+ ```
65
+
66
+ Now the Generators should appear:
67
+
68
+ ```shell
69
+ TestUnit:
70
+ ...
71
+ test_unit:operations:action
72
+ test_unit:operations:contract
73
+ test_unit:operations:operation
74
+ test_unit:operations:register
75
+ test_unit:operations:repository
76
+ test_unit:operations:schema
77
+ test_unit:operations:validation
78
+ ...
79
+ ```
80
+
81
+ They are automatically call when the generator it's call:
82
+
83
+ ```shell
84
+ rails generate operations:contract user create firstname:string:optional --component identity/users --force
85
+ invoke test_unit
86
+ create test/components/identity/operations/users/create/contract_test.rb
87
+ create components/identity/operations/users/create/contract.rb
88
+ ```
89
+
90
+ ## Development
91
+
92
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
93
+
94
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
95
+
96
+ ## Contributing
97
+
98
+ There is a generator of generators you can use to quickly add a new generator:
99
+
100
+ ```shell
101
+ ./bin/generator <generator name>
102
+ ```
103
+
104
+ That provides the skeleton for the new generator. Note that the test suite should still run after the generated files.
105
+
106
+ Bug reports and pull requests are welcome on GitHub at https://github.com/orgs/joel/dry-operation_generators-test_unit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/joel/dry-operation_generators-test_unit/blob/main/CODE_OF_CONDUCT.md).
107
+
108
+ ## License
109
+
110
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
111
+
112
+ ## Code of Conduct
113
+
114
+ Everyone interacting in the Isms project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/joel/dry-operation_generators-test_unit/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new(:rubocop) do |task|
11
+ task.options = ["-A"] # auto_correct
12
+ end
13
+
14
+ task default: %i[spec rubocop]
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ begin
5
+ require "thor/group"
6
+ rescue LoadError
7
+ puts "Thor is not available.\nIf you ran this command from a git checkout " \
8
+ "of Rails, please make sure thor is installed,\nand run this command " \
9
+ "as `ruby #{$PROGRAM_NAME} #{(ARGV | ["--dev"]).join(" ")}`"
10
+ exit
11
+ end
12
+
13
+ require "active_support/inflector"
14
+
15
+ module TestUnit
16
+ class CreateGenerator < Thor::Group
17
+ include Thor::Actions
18
+
19
+ class << self
20
+ def exit_on_failure?
21
+ true
22
+ end
23
+ end
24
+
25
+ desc "Create a new Generator (Generator of generators)"
26
+
27
+ source_root File.expand_path(".", __dir__)
28
+
29
+ class_option :force, type: :boolean, default: false
30
+
31
+ argument :name, type: :string, required: true, desc: "Name of the generator"
32
+ argument :library, type: :string, required: true, desc: "Name of the library, i.e. Dry::Schema"
33
+ class_option :dry_run, type: :boolean, default: true
34
+
35
+ def plural_name
36
+ name.pluralize
37
+ end
38
+
39
+ def singular_name
40
+ name.singularize
41
+ end
42
+
43
+ def class_name
44
+ singular_name.capitalize
45
+ end
46
+
47
+ def copy_generator_files
48
+ return if options[:dry_run]
49
+
50
+ say "Adding files...", :green
51
+
52
+ template "templates/template_generator.rb.erb", "lib/test_unit/generators/operations/#{name}_generator.rb"
53
+ template "templates/template_generator_spec.rb.erb",
54
+ "spec/test_unit/generators/operations/#{name}_generator_spec.rb"
55
+
56
+ create_file "lib/test_unit/generators/operations/#{name}/templates/#{name}_test.rb.erb" do
57
+ "This is #{name}.rb.erb"
58
+ end
59
+ end
60
+
61
+ def update_setup
62
+ say "Updating setup...", :green
63
+
64
+ setup_file = "lib/test_unit/generators/operations/setup/templates/generators.rb"
65
+ include_text = " \"test_unit/generators/operations/#{name}_generator\" if defined?(#{library})\nrequire"
66
+
67
+ insert_into_file setup_file, include_text.to_s, after: Regexp.new(Regexp.escape("require"))
68
+ end
69
+ end
70
+ end
71
+
72
+ # ./bin/generator <generator name> --no-dry_run
73
+ TestUnit::CreateGenerator.start(ARGV)
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ # Test based on the following contract rules:
6
+ #
7
+ # module <%= class_name.pluralize %>
8
+ # module <%= verb.capitalize %>
9
+ # class Contract < Base::Contract
10
+ #
11
+ # params(Schema)
12
+ #
13
+ # rule(:user_id) do
14
+ # key.failure("must exist") if User.where(id: value).none?
15
+ # end
16
+ #
17
+ # end
18
+ # end
19
+ # end
20
+ #
21
+ # Adapt the test to the rules of your contract.
22
+ #
23
+ module <%= class_name.pluralize %>
24
+ module <%= verb.capitalize %>
25
+ class ContractTest < ActiveSupport::TestCase
26
+
27
+ <%- @reference_parsed_fields.each do |field| -%>
28
+ def <%= field.name %>
29
+ @<%= field.name %> ||= create(:<%= field.name %>)
30
+ end
31
+
32
+ <%- end -%>
33
+ def attributes
34
+ {
35
+ # TODO: Add attributes here.
36
+ <%- @reference_parsed_fields.each do |field| -%>
37
+ <%- case Operations.configuration.primary_key_type -%>
38
+ <%- when :integer -%>
39
+ <%= field.foreign_key %>: <%= field.name %>.id,
40
+ <%- when /string|uuid/ -%>
41
+ <%= field.foreign_key %>: <%= field.name %>.uuid,
42
+ <%- end -%>
43
+ <%- end -%>
44
+ }
45
+ skip("Skipping: Please replace with the correct attributes for this test.")
46
+ end
47
+
48
+ def valid_attributes
49
+ attributes
50
+ end
51
+
52
+ def invalid_attributes
53
+ attributes.merge(
54
+ {
55
+ # TODO: Add attributes here
56
+ <%- @reference_parsed_fields.each do |field| -%>
57
+ <%- case Operations.configuration.primary_key_type -%>
58
+ <%- when :integer -%>
59
+ <%= field.foreign_key %>: 42,
60
+ <%- when /string|uuid/ -%>
61
+ <%= field.foreign_key %>: SecureRandom.uuid_v7,
62
+ <%- end -%>
63
+ <%- end -%>
64
+ }
65
+ )
66
+ skip("Skipping: Please replace with the correct attributes for this test.")
67
+ end
68
+
69
+ # <%= verb.capitalize %>Schema Mock
70
+ # NOTE: All the keys used for the rules need to be present in the schema.
71
+ Mocked<%= verb.capitalize %>Schema = Dry::Schema.Params do
72
+ <%- @reference_parsed_fields.each do |field| -%>
73
+ <%= field.requirement %>(:<%= field.foreign_key %>).filled(:<%= Operations.configuration.primary_key_type %>)
74
+ <%- end -%>
75
+ end
76
+
77
+ # Safe Mocking of the <%= verb.capitalize %>Contract
78
+ class Mocked<%= verb.capitalize %>Contract < ::<%= class_name.pluralize %>::<%= verb.capitalize %>::Contract
79
+
80
+ params(Mocked<%= verb.capitalize %>Schema)
81
+
82
+ end
83
+
84
+ context "validations" do
85
+
86
+ subject { Mocked<%= verb.capitalize %>Contract.new }
87
+
88
+ context "with the right attributes" do
89
+
90
+ should "be successful" do
91
+ assert subject.call(valid_attributes).success?
92
+ end
93
+
94
+ end
95
+
96
+ context "with the wrong attributes" do
97
+
98
+ should "be unsuccessful" do
99
+ result = subject.call(invalid_attributes)
100
+ assert result.failure?
101
+ <%- @reference_parsed_fields.each do |field| -%>
102
+ # assert_equal ["must exist"], result.errors[:<%= field.foreign_key %>]
103
+ <%- end -%>
104
+ skip("Skipping: Add the correct error messages here.")
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TestUnit
4
+ module Generators
5
+ module Operations
6
+ class ContractGenerator < Rails::Generators::NamedBase
7
+ include ::Operations::Base::Generators::BaseGenerator
8
+
9
+ source_root File.expand_path("contract/templates", __dir__)
10
+
11
+ def generate_files
12
+ super
13
+
14
+ template "contract_test.rb.erb", "#{root_test_file_path}/operations/#{plural_name}/#{verb}/contract_test.rb"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,191 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ # Test based on the following operations:
6
+ #
7
+ # module <%= class_name.pluralize %>
8
+ # module <%= verb.capitalize %>
9
+ # class Operation
10
+ #
11
+ # include Dry::Transaction(container: Register)
12
+ #
13
+ # step :validate_inputs, with: "<%= plural_name %>.<%= verb %>.validation"
14
+ # step :initialize_record, with: "<%= plural_name %>.<%= verb %>.repository"
15
+ # step :<%= verb %>_record, with: "<%= plural_name %>.<%= verb %>.action"
16
+ #
17
+ # end
18
+ # end
19
+ # end
20
+ #
21
+ # Adapt the test to your operation.
22
+ #
23
+ module <%= class_name.pluralize %>
24
+ module <%= verb.capitalize %>
25
+ class OperationTest < ActiveSupport::TestCase
26
+
27
+ <%- @reference_parsed_fields.each do |field| -%>
28
+ def <%= field.name %>
29
+ @<%= field.name %> ||= create(:<%= field.name %>)
30
+ end
31
+
32
+ <%- end -%>
33
+ <%- if verb =~ /update|destroy|all/ -%>
34
+ def record
35
+ @record ||= create(:<%= singular_name %>)
36
+ end
37
+ <%- end -%>
38
+
39
+ <%- if verb != "destroy" -%>
40
+ def schema_attributes
41
+ <%= class_name.pluralize %>::Base::Schema.key_list + <%= class_name.pluralize %>::<%= verb.capitalize %>::Schema.key_list
42
+ end
43
+
44
+ # This method should return attributes with the keys as symbols.
45
+ # symbolize_keys
46
+ def model_attributes
47
+ # attributes_for(:<%= singular_name %>) # More efficient
48
+ @model_attributes ||= create(:<%= singular_name %>).attributes.symbolize_keys
49
+ end
50
+ <%- if verb == "create" -%>
51
+ alias_method :init_setup!, :model_attributes
52
+ <%- end -%>
53
+
54
+ def attributes
55
+ model_attributes.slice(*schema_attributes).merge({
56
+ <%- if verb =~ /update|destroy|all/ -%>
57
+ id: record.uuid,
58
+ <%- end -%>
59
+ # TODO: Add attributes here.
60
+ <%- @reference_parsed_fields.each do |field| -%>
61
+ <%- case Operations.configuration.primary_key_type -%>
62
+ <%- when :integer -%>
63
+ <%= field.foreign_key %>: <%= field.name %>.id,
64
+ <%- when /string|uuid/ -%>
65
+ <%= field.foreign_key %>: <%= field.name %>.uuid,
66
+ <%- end -%>
67
+ <%- end -%>
68
+ <%- @regular_parsed_fields.each do |field| -%>
69
+ <%= field.name %>: "New <%= field.name.capitalize %>",
70
+ <%- end -%>
71
+ })
72
+ skip("Skipping: Please replace with the correct attributes for this test.")
73
+ end
74
+
75
+ def valid_attributes
76
+ attributes
77
+ end
78
+ <%- else -%>
79
+ def valid_attributes
80
+ { id: record.uuid }
81
+ end
82
+ <%- end -%>
83
+
84
+ <%- if verb != "destroy" -%>
85
+ def invalid_attributes
86
+ attributes.merge(
87
+ {
88
+ # TODO: Add attributes here
89
+ <%- @reference_parsed_fields.each do |field| -%>
90
+ <%- case Operations.configuration.primary_key_type -%>
91
+ <%- when :integer -%>
92
+ <%= field.foreign_key %>: 42,
93
+ <%- when /string|uuid/ -%>
94
+ <%= field.foreign_key %>: SecureRandom.uuid_v7,
95
+ <%- end -%>
96
+ <%- end -%>
97
+ <%- @regular_parsed_fields.each do |field| -%>
98
+ <%= field.name %>: nil,
99
+ <%- end -%>
100
+ }
101
+ )
102
+ skip("Skipping: Please replace with the correct attributes for this test.")
103
+ end
104
+ <%- end -%>
105
+
106
+ <%- if verb != "destroy" -%>
107
+ def <%= verb %>d_resource
108
+ <%= class_name %>.last
109
+ end
110
+ <%- end -%>
111
+
112
+ context "#perform" do
113
+
114
+ subject { described_class.new }
115
+
116
+ context "success" do
117
+
118
+ # Arround the action
119
+ <%- case verb -%>
120
+ <%- when "create" -%>
121
+ should "create the resource" do
122
+ init_setup! # Ensure the record exists
123
+ assert_difference "<%= class_name %>.count", +1 do
124
+ subject.call(valid_attributes)
125
+ end
126
+ end
127
+ <%- when "update" -%>
128
+ should "update the resource" do
129
+ assert_changes -> {
130
+ # record.reload.name
131
+ <%= class_name %>.find_by(uuid: record.uuid).name
132
+ }, to: "New Name" do
133
+ subject.call(valid_attributes)
134
+ end
135
+ end
136
+ <%- when "destroy" -%>
137
+ should "destroy the resource" do
138
+ record # Ensure the record exists
139
+ assert_difference "<%= class_name %>.count", -1 do
140
+ subject.call(valid_attributes)
141
+ end
142
+ end
143
+ <%- end -%>
144
+
145
+ <%- if verb != "destroy" -%>
146
+ context "when the action is done" do
147
+
148
+ setup { subject.call(valid_attributes) }
149
+
150
+ should "have done whatever it was meant to do!" do
151
+ <%- @reference_parsed_fields.each do |field| -%>
152
+ # assert_equal <%= verb %>d_resource.<%= field.name %>, <%= field.name %>
153
+ <%- end -%>
154
+ skip("Skipping: Please replace with the correct assertions for this test.")
155
+ end
156
+
157
+ end
158
+ <%- end -%>
159
+
160
+ end
161
+
162
+ <%- if verb != "destroy" -%>
163
+ context "failure" do
164
+
165
+ setup { @operation = -> { subject.call(invalid_attributes) } }
166
+
167
+ should "return an unsuccessful result" do
168
+ result = @operation.call
169
+
170
+ assert result.failure?
171
+ <%- @regular_parsed_fields.select { |f| f.requirement == "required" }.each do |field| -%>
172
+ # assert_equal({:<%= field.name %> => ["must be filled"]}, result.failure.to_h) # rubocop:disable Rails/DeprecatedActiveModelErrorsMethods
173
+ <%- end -%>
174
+ skip("Skipping: Add the correct error messages here.")
175
+ end
176
+
177
+ should "not perform the operation" do
178
+ # assert_no_changes -> { # whatever } do
179
+ # @operation.call
180
+ # end
181
+ skip("Skipping: Add the correct check here.")
182
+ end
183
+
184
+ end
185
+ <%- end -%>
186
+
187
+ end
188
+
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TestUnit
4
+ module Generators
5
+ module Operations
6
+ class OperationGenerator < Rails::Generators::NamedBase
7
+ include ::Operations::Base::Generators::BaseGenerator
8
+
9
+ source_root File.expand_path("operation/templates", __dir__)
10
+
11
+ def generate_files
12
+ super
13
+
14
+ template "operation_test.rb.erb", "#{root_test_file_path}/operations/#{plural_name}/#{verb}/operation_test.rb"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ module <%= class_name.pluralize %>
6
+ module Base
7
+ class SchemaTest < ActiveSupport::TestCase
8
+
9
+ # This method should return attributes with the keys as symbols.
10
+ # symbolize_keys
11
+ def model_attributes
12
+ # attributes_for(:<%= singular_name %>) # More efficient
13
+ create(:<%= singular_name %>).attributes.symbolize_keys
14
+ end
15
+
16
+ def attributes
17
+ model_attributes.merge(
18
+ schema_attributes
19
+ ).slice(*described_class.key_list)
20
+ skip("Skipping: Please replace with the correct attributes for this test.")
21
+ end
22
+
23
+ def schema_attributes
24
+ {
25
+ # TODO: Add attributes here.
26
+ }
27
+ end
28
+
29
+ def valid_attributes
30
+ attributes
31
+ end
32
+
33
+ def invalid_attributes
34
+ attributes.merge(
35
+ {
36
+ # TODO: Add attributes here
37
+ }
38
+ )
39
+ skip("Skipping: Please replace with the correct attributes for this test.")
40
+ end
41
+
42
+ should "not provide attributes that are not defined in the schema" do
43
+ assert_empty schema_attributes.keys - described_class.key_list
44
+ end
45
+
46
+ context "validations" do
47
+
48
+ subject { described_class }
49
+
50
+ context "with valid attributes" do
51
+
52
+ should "be successful" do
53
+ assert subject.call(valid_attributes).success?
54
+ end
55
+
56
+ end
57
+
58
+ context "with invalid attributes" do
59
+
60
+ should "be unsuccessful" do
61
+ result = subject.call(invalid_attributes)
62
+ assert result.failure?
63
+ # assert_equal({:email => ["must be filled"]}, result.errors.to_h) # rubocop:disable Rails/DeprecatedActiveModelErrorsMethods
64
+ skip("Skipping: Add the correct error messages here.")
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ module <%= class_name.pluralize %>
6
+ module <%= verb.capitalize %>
7
+ class SchemaTest < ActiveSupport::TestCase
8
+
9
+ # This method should return attributes with the keys as symbols.
10
+ # symbolize_keys
11
+ # i.e create(:user).attributes.symbolize_keys
12
+ def model_attributes
13
+ # attributes_for(:<%= singular_name %>) # More efficient
14
+ create(:<%= singular_name %>).attributes.symbolize_keys
15
+ end
16
+
17
+ def attributes
18
+ model_attributes.merge(
19
+ schema_attributes
20
+ ).slice(*described_class.key_list)
21
+ skip("Skipping: Please replace with the correct attributes for this test.")
22
+ end
23
+
24
+ def schema_attributes
25
+ {
26
+ <%- if verb =~ /update|all/ -%>
27
+ id: SecureRandom.uuid_v7,
28
+ <%- end -%>
29
+ # TODO: Add attributes here.
30
+ <%- @reference_parsed_fields.each do |field| -%>
31
+ <%- case Operations.configuration.primary_key_type -%>
32
+ <%- when :integer -%>
33
+ <%= field.foreign_key %>: 42,
34
+ <%- when /string|uuid/ -%>
35
+ <%= field.foreign_key %>: SecureRandom.uuid_v7,
36
+ <%- end -%>
37
+ <%- end -%>
38
+ <%- @regular_parsed_fields.each do |field| -%>
39
+ <%= field.name %>: "<%= field.type %>",
40
+ <%- end -%>
41
+ }
42
+ end
43
+
44
+ def valid_attributes
45
+ attributes
46
+ end
47
+
48
+ def invalid_attributes
49
+ attributes.merge(
50
+ {
51
+ # TODO: Add attributes here
52
+ <%- @regular_parsed_fields.select { |f| f.requirement == "required" }.each do |field| -%>
53
+ # <%= field.name %>: nil,
54
+ <%- end -%>
55
+ }
56
+ )
57
+ skip("Skipping: Please replace with the correct attributes for this test.")
58
+ end
59
+
60
+ should "not provide attributes that are not defined in the schema" do
61
+ assert_empty schema_attributes.keys - described_class.key_list
62
+ end
63
+
64
+ context "validations" do
65
+
66
+ subject { described_class }
67
+
68
+ context "with valid attributes" do
69
+
70
+ should "be successful" do
71
+ assert subject.call(valid_attributes).success?
72
+ end
73
+
74
+ end
75
+
76
+ context "with invalid attributes" do
77
+
78
+ should "be unsuccessful" do
79
+ result = subject.call(invalid_attributes)
80
+ assert result.failure?
81
+ <%- @regular_parsed_fields.select { |f| f.requirement == "required" }.each do |field| -%>
82
+ # assert_equal({:<%= field.name %> => ["must be filled"]}, result.errors.to_h) # rubocop:disable Rails/DeprecatedActiveModelErrorsMethods
83
+ <%- end -%>
84
+ skip("Skipping: Add the correct error messages here.")
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "operations/base/generators/base_generator"
4
+
5
+ module TestUnit
6
+ module Generators
7
+ module Operations
8
+ class SchemaGenerator < Rails::Generators::NamedBase
9
+ include ::Operations::Base::Generators::BaseGenerator
10
+
11
+ source_root File.expand_path("schema/templates", __dir__)
12
+
13
+ desc "Creates a schema test file for the operation"
14
+
15
+ def generate_files
16
+ super
17
+
18
+ template "schema_test.rb.erb", "#{root_test_file_path}/operations/#{plural_name}/#{verb}/schema_test.rb"
19
+ template "base/schema_test.rb.erb", "#{root_test_file_path}/operations/#{plural_name}/base/schema_test.rb"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_unit/generators/operations/schema_generator" if defined?(Dry::Schema)
4
+ require "test_unit/generators/operations/operation_generator" if defined?(Dry::Transaction)
5
+ require "test_unit/generators/operations/contract_generator" if defined?(Dry::Validation)
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TestUnit
4
+ module Generators
5
+ module Operations
6
+ class SetupGenerator < ::Rails::Generators::Base
7
+ def copy_initializer_file
8
+ initializer "operation_generators-test_unit.rb",
9
+ File.read(File.expand_path("setup/templates/generators.rb.tt", __dir__))
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TestUnit
4
+ module Generators
5
+ module Operations
6
+ class <%= class_name %>Generator < Rails::Generators::NamedBase
7
+ include ::Operations::Base::Generators::BaseGenerator
8
+
9
+ source_root File.expand_path("<%= singular_name %>/templates", __dir__)
10
+
11
+ def generate_files
12
+ super
13
+
14
+ template "<%= singular_name %>_test.rb.erb", "#{root_test_file_path}/operations/#{plural_name}/#{verb}/<%= singular_name %>_test.rb"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TestUnit
4
+ module Generators
5
+ module Operations
6
+ RSpec.describe <%= class_name %>Generator, type: :generator do
7
+ setup_default_destination
8
+
9
+ tests described_class
10
+
11
+ let(:<%= singular_name %>_test_path) { "test/operations/users/create/<%= singular_name %>_test.rb" }
12
+
13
+ subject(:<%= singular_name %>_generator) do
14
+ run_generator(
15
+ %w[
16
+ user
17
+ create
18
+ ]
19
+ )
20
+ end
21
+
22
+ it "copy templates" do
23
+ <%= singular_name %>_generator
24
+
25
+ expect(File).to exist("#{destination_root}/#{<%= singular_name %>_test_path}")
26
+ end
27
+
28
+ it "uses the expected template files" do
29
+ <%= singular_name %>_generator
30
+
31
+ expect(content_for(<%= singular_name %>_test_path)).to match(/This is <%= singular_name %>.rb.erb/)
32
+ end
33
+
34
+ context "with component option" do
35
+ let(:<%= singular_name %>_test_path) { "test/components/identity/operations/users/create/<%= singular_name %>_test.rb" }
36
+
37
+ subject(:<%= singular_name %>_generator) do
38
+ run_generator(
39
+ %w[
40
+ user
41
+ create
42
+ --component identity/users
43
+ ]
44
+ )
45
+ end
46
+
47
+ it "copy templates" do
48
+ <%= singular_name %>_generator
49
+
50
+ expect(File).to exist("#{destination_root}/#{<%= singular_name %>_test_path}")
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
data/lib/test_unit.rb ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+ loader = Zeitwerk::Loader.for_gem
5
+ loader.setup
6
+
7
+ require "rails/generators"
8
+
9
+ require "test_unit/generators/operations/setup_generator"
10
+
11
+ require "operations/base"
12
+
13
+ module TestUnit
14
+ class Error < StandardError; end
15
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "dry-operation_generators-test_unit"
5
+ spec.version = "0.1.0"
6
+ spec.authors = ["Joel Azemar"]
7
+ spec.email = ["joel.azemar@gmail.com"]
8
+
9
+ spec.summary = "Add the test files of Business Logic actions with Dry Rb ecosystem"
10
+ spec.homepage = "https://github.com/joel/dry-operation_generators-test_unit"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = ">= 3.2.2"
13
+
14
+ # spec.metadata["allowed_push_host"] = "Set to your gem server 'https://example.com'"
15
+
16
+ # spec.metadata["homepage_uri"] = spec.homepage
17
+ # spec.metadata["source_code_uri"] = "Put your gem's public repo URL here."
18
+ # spec.metadata["changelog_uri"] = "Put your gem's CHANGELOG.md URL here."
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (File.expand_path(f) == __FILE__) ||
25
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+
34
+ spec.add_dependency "dry-operation_generators-base"
35
+ spec.add_dependency "rails"
36
+ spec.add_dependency "zeitwerk"
37
+
38
+ # For more information and examples about making a new gem, check out our
39
+ # guide at: https://bundler.io/guides/creating_gem.html
40
+ spec.metadata["rubygems_mfa_required"] = "true"
41
+ end
@@ -0,0 +1,4 @@
1
+ module OperationGeneratorsTestUnit
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-operation_generators-test_unit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joel Azemar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-operation_generators-base
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: zeitwerk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - joel.azemar@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - ".rubocop.yml"
64
+ - ".rubocop_todo.yml"
65
+ - CHANGELOG.md
66
+ - CODE_OF_CONDUCT.md
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - lib/test_unit.rb
71
+ - lib/test_unit/create_generator.rb
72
+ - lib/test_unit/generators/operations/contract/templates/contract_test.rb.erb
73
+ - lib/test_unit/generators/operations/contract_generator.rb
74
+ - lib/test_unit/generators/operations/operation/templates/operation_test.rb.erb
75
+ - lib/test_unit/generators/operations/operation_generator.rb
76
+ - lib/test_unit/generators/operations/schema/templates/base/schema_test.rb.erb
77
+ - lib/test_unit/generators/operations/schema/templates/schema_test.rb.erb
78
+ - lib/test_unit/generators/operations/schema_generator.rb
79
+ - lib/test_unit/generators/operations/setup/templates/generators.rb.tt
80
+ - lib/test_unit/generators/operations/setup_generator.rb
81
+ - lib/test_unit/templates/template_generator.rb.erb
82
+ - lib/test_unit/templates/template_generator_spec.rb.erb
83
+ - operation_generators-test_unit.gemspec
84
+ - sig/test_unit_operation_generators.rbs
85
+ homepage: https://github.com/joel/dry-operation_generators-test_unit
86
+ licenses:
87
+ - MIT
88
+ metadata:
89
+ rubygems_mfa_required: 'true'
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 3.2.2
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.5.3
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Add the test files of Business Logic actions with Dry Rb ecosystem
109
+ test_files: []