rspec-graphql_assistant 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '0658fbabb671fea77f65b9ac7550f6e4fa3b08a069b3160165db182769b817f7'
4
+ data.tar.gz: 9f205605a4f6c88fd1e6f4cb1449e973dfff1fe9f717fd6b23892d4434f3dd5b
5
+ SHA512:
6
+ metadata.gz: 13ce16383005507827f2e9bf9bdb4b9f7f7ed0caf06da557a9abf1b40bb9b9d1bd6ef880bd74fb26f57f6d618734ec0e2a3ca991ebee9b8dbad9a4e43b5ad81f
7
+ data.tar.gz: a79f5a0fad5a8220c930d3fc633c510fd1973df313e965c72e4ada88dc3da8601ef461b91350c99b6bea2f29241fe205fcb707bba512b3b65b90d3650e6ba961
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .rakeTasks
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rspec-graphql_assistant.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "activesupport"
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rspec-graphql_assistant (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (6.0.3.2)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 0.7, < 2)
12
+ minitest (~> 5.1)
13
+ tzinfo (~> 1.1)
14
+ zeitwerk (~> 2.2, >= 2.2.2)
15
+ coderay (1.1.3)
16
+ concurrent-ruby (1.1.6)
17
+ diff-lcs (1.4.4)
18
+ i18n (1.8.3)
19
+ concurrent-ruby (~> 1.0)
20
+ method_source (1.0.0)
21
+ minitest (5.14.1)
22
+ pry (0.13.1)
23
+ coderay (~> 1.1)
24
+ method_source (~> 1.0)
25
+ rake (12.3.3)
26
+ rspec (3.9.0)
27
+ rspec-core (~> 3.9.0)
28
+ rspec-expectations (~> 3.9.0)
29
+ rspec-mocks (~> 3.9.0)
30
+ rspec-core (3.9.2)
31
+ rspec-support (~> 3.9.3)
32
+ rspec-expectations (3.9.2)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.9.0)
35
+ rspec-mocks (3.9.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-support (3.9.3)
39
+ thread_safe (0.3.6)
40
+ tzinfo (1.2.7)
41
+ thread_safe (~> 0.1)
42
+ zeitwerk (2.3.1)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ activesupport
49
+ pry (~> 0)
50
+ rake (~> 12.0)
51
+ rspec (~> 3.0)
52
+ rspec-graphql_assistant!
53
+
54
+ BUNDLED WITH
55
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Ivan Lopatin
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.
@@ -0,0 +1,62 @@
1
+ # Rspec::GraphqlAssistant
2
+ Helps you to generate query for testing GraphQL
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'rspec-graphql_assistant'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install rspec-graphql_assistant
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ RSpec.configure do |config|
24
+ config.include RSpec::GraphqlAssistant::Helpers
25
+ end
26
+ ```
27
+
28
+ ```ruby
29
+ graphql_mutation('updatePost', { post_id: 1, content: "some test" }, [:id, :content])
30
+ ```
31
+
32
+ produce this one query
33
+ ```gql
34
+ mutation {
35
+ updatePost(input: {postId: 1, content: "some text"}) {
36
+ id
37
+ content
38
+ }
39
+ }
40
+
41
+ ```
42
+ Examples of query and subscription
43
+ ```ruby
44
+ args = {post_id: 1}
45
+ response = [:id, :name]
46
+
47
+ graphql_query('queryName', args, response)
48
+
49
+ graphql_subscription('subscriptionName', args, response)
50
+ ```
51
+
52
+ Work in progress and now only supports inline syntax. Which means without passing variables. Later try to cover both cases
53
+ Cheers
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ginray/rspec-graphql_assistant.
58
+
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rspec/graphql_assistant"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ require "rspec/graphql_assistant/version"
2
+ require 'active_support'
3
+ require 'active_support/core_ext'
4
+ require 'rspec/graphql_assistant/matchers'
5
+ require 'rspec/graphql_assistant/has_attrs'
6
+ require 'rspec/graphql_assistant/contain_attrs'
7
+ require 'rspec/graphql_assistant/query_builder'
8
+ require 'rspec/graphql_assistant/argument_builder'
9
+ require 'rspec/graphql_assistant/argument_builder_with_vars'
10
+ require 'rspec/graphql_assistant/helpers'
11
+
12
+ module Rspec
13
+ module GraphqlAssistant
14
+ class Error < StandardError; end
15
+ # Your code goes here...
16
+ end
17
+ end
@@ -0,0 +1,56 @@
1
+ module RSpec
2
+ module GraphqlAssistant
3
+ class ArgumentBuilder
4
+ attr_reader :arguments_hash
5
+
6
+ def initialize(arguments_hash)
7
+ @arguments_hash = arguments_hash
8
+ end
9
+
10
+ def call
11
+ process_hash(arguments_hash)
12
+ end
13
+
14
+ private
15
+
16
+ def process_root(arg)
17
+ return process_hash(arg) if arg.is_a?(Hash)
18
+ return process_array(arg) if arg.is_a?(Array)
19
+ process_scalar(arg) if arg.is_a?(Symbol) || arg.is_a?(String) || arg.is_a?(Numeric)
20
+ end
21
+
22
+ def process_scalar(arg)
23
+ arg = "\"#{arg}\"" if arg.is_a?(Symbol) || arg.is_a?(String)
24
+ arg
25
+ end
26
+
27
+ def process_array(arg)
28
+ result = []
29
+ arg.each do |item|
30
+ result << process_root(item)
31
+ end
32
+ line = result.map { |item| "{#{item}}"}.join(', ')
33
+ end
34
+
35
+ def process_hash(arg)
36
+ result = []
37
+ arg.each do |k, v|
38
+ arg_name = k
39
+ arg_name = k.to_s.camelize(:lower) if k.is_a?(Symbol)
40
+ if is_scalar?(v)
41
+ result << "#{arg_name}: #{process_scalar(v)}"
42
+ elsif v.is_a?(Array)
43
+ result << "#{arg_name}: [ #{process_root(v)} ]"
44
+ else
45
+ result << "#{arg_name}: { #{process_root(v)} }"
46
+ end
47
+ end
48
+ result.join(', ')
49
+ end
50
+
51
+ def is_scalar?(arg)
52
+ arg.is_a?(Symbol) || arg.is_a?(String) || arg.is_a?(Numeric)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,51 @@
1
+ module RSpec
2
+ module GraphqlAssistant
3
+ class ArgumentBuilderWithVars
4
+ attr_reader :arguments_arr
5
+
6
+ def initialize(arguments_arr)
7
+ @arguments_arr = arguments_arr
8
+ end
9
+
10
+ def call
11
+ result = []
12
+ arguments_arr.each do |arg|
13
+ result << process_root(arg)
14
+ end
15
+ result.join ', '
16
+ end
17
+
18
+ private
19
+
20
+ def process_root(arg)
21
+ return process_hash(arg) if arg.is_a?(Hash)
22
+ return process_array(arg) if arg.is_a?(Array)
23
+ process_string_or_symbol(arg) if arg.is_a?(Symbol) || arg.is_a?(String)
24
+ end
25
+
26
+ def process_string_or_symbol(arg)
27
+ arg = arg.to_s.camelize(:lower) if arg.is_a?(Symbol)
28
+ "#{arg}: $#{arg}"
29
+ end
30
+
31
+ def process_array(arg)
32
+ result = []
33
+ arg.each do |item|
34
+ result << process_root(item)
35
+ end
36
+ result.join(', ')
37
+ end
38
+
39
+ def process_hash(arg)
40
+ result = ''
41
+ arg.each do |k, v|
42
+ arg_name = k
43
+ arg_name = k.to_s.camelize(:lower) if k.is_a?(Symbol)
44
+ arg_value = process_root(v)
45
+ result << "#{arg_name}: { #{arg_value} }"
46
+ end
47
+ result
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,31 @@
1
+ module RSpec
2
+ module GraphqlAssistant
3
+ class ContainAttrs
4
+ attr_reader :sample, :expected, :key
5
+
6
+ def initialize(key, expected)
7
+ @expected = expected
8
+ @key = key
9
+ end
10
+
11
+ def matches?(actual)
12
+ actual_copy = actual.with_indifferent_access
13
+ value = actual_copy.dig(:data, key)
14
+ res = true
15
+ expected.each do |k,v|
16
+ res = value[k] == v
17
+ break unless res
18
+ end
19
+ res
20
+ end
21
+
22
+ def failure_message
23
+ "'#{key}' not equal attributes '#{expected}'"
24
+ end
25
+
26
+ def description
27
+ "'#{key}' has attributes '#{expected}'"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ module RSpec
2
+ module GraphqlAssistant
3
+ class HasAttrs
4
+ attr_reader :sample, :expected, :key
5
+
6
+ def initialize(key, expected)
7
+ @expected = expected
8
+ @key = key
9
+ end
10
+
11
+ def matches?(actual)
12
+ value = actual.dig('data', key)
13
+ return value.any? { |k,_v| expected.include? k } if value.is_a?(Hash)
14
+
15
+ value.all? { |el| el.any? { |k,_v| expected.include? k } } if value.is_a?(Array)
16
+ end
17
+
18
+ def failure_message
19
+ "'#{key}' not include all attributes '#{expected}'"
20
+ end
21
+
22
+ def description
23
+ "'#{key}' has attributes '#{expected}'"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ module RSpec
2
+ module GraphqlAssistant
3
+ module Helpers
4
+
5
+ def graphql_mutation(name, args, response)
6
+ QueryBuilder.new(:mutation, name, args, response).call
7
+ end
8
+
9
+ def graphql_query(name, args, response)
10
+ QueryBuilder.new(:query, name, args, response).call
11
+ end
12
+
13
+ def graphql_subscription(name, args, response)
14
+ QueryBuilder.new(:subscription, name, args, response).call
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ module RSpec
2
+ module Matchers
3
+ def has_graphql_attrs(key, expected)
4
+ RSpec::GraphqlAssistant::HasAttrs.new(key, expected)
5
+ end
6
+
7
+ def contain_graphql_attrs(key, expected)
8
+ RSpec::GraphqlAssistant::ContainAttrs.new(key, expected)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,42 @@
1
+ module RSpec
2
+ module GraphqlAssistant
3
+ class QueryBuilder
4
+ attr_reader :name, :args, :response, :type
5
+ def initialize(type, name, args, response)
6
+ @type = type
7
+ @name = name
8
+ @args = args
9
+ @response = response
10
+ end
11
+
12
+
13
+ def call
14
+ template % {type: type, name: name, args: transform_args(args), response: transform_response(response)}
15
+ end
16
+
17
+ private
18
+
19
+ def transform_args(args)
20
+ return if args.blank?
21
+ "(#{ArgumentBuilder.new(args).call})"
22
+ end
23
+
24
+ def transform_response(response)
25
+ response.map do |item|
26
+ next item.to_s.camelize(:lower) if item.is_a?(Symbol)
27
+ item
28
+ end.join("\n")
29
+ end
30
+
31
+ def template
32
+ <<-GQL
33
+ %{type} {
34
+ %{name}%{args} {
35
+ %{response}
36
+ }
37
+ }
38
+ GQL
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ module Rspec
2
+ module GraphqlAssistant
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/rspec/graphql_assistant/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rspec-graphql_assistant"
5
+ spec.version = Rspec::GraphqlAssistant::VERSION
6
+ spec.authors = ["Ivan Lopatin"]
7
+ spec.email = ["gingray.dev@gmail.com"]
8
+
9
+ spec.summary = %q{Collection of RSpec helpers to easy test GraphQL query, mutation, subscription}
10
+ spec.description = %q{Collection of RSpec helpers to easy test GraphQL query, mutation, subscription}
11
+ spec.homepage = "https://github.com/gingray/rspec-graphql_assistant"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/gingray/rspec-graphql_assistant"
18
+ spec.metadata["changelog_uri"] = "https://github.com/gingray/rspec-graphql_assistant"
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(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+ spec.add_development_dependency 'pry', '~> 0'
29
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-graphql_assistant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Lopatin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Collection of RSpec helpers to easy test GraphQL query, mutation, subscription
28
+ email:
29
+ - gingray.dev@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - bin/console
43
+ - bin/setup
44
+ - lib/rspec/graphql_assistant.rb
45
+ - lib/rspec/graphql_assistant/argument_builder.rb
46
+ - lib/rspec/graphql_assistant/argument_builder_with_vars.rb
47
+ - lib/rspec/graphql_assistant/contain_attrs.rb
48
+ - lib/rspec/graphql_assistant/has_attrs.rb
49
+ - lib/rspec/graphql_assistant/helpers.rb
50
+ - lib/rspec/graphql_assistant/matchers.rb
51
+ - lib/rspec/graphql_assistant/query_builder.rb
52
+ - lib/rspec/graphql_assistant/version.rb
53
+ - rspec-graphql_assistant.gemspec
54
+ homepage: https://github.com/gingray/rspec-graphql_assistant
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ homepage_uri: https://github.com/gingray/rspec-graphql_assistant
59
+ source_code_uri: https://github.com/gingray/rspec-graphql_assistant
60
+ changelog_uri: https://github.com/gingray/rspec-graphql_assistant
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 2.3.0
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.0.8
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Collection of RSpec helpers to easy test GraphQL query, mutation, subscription
80
+ test_files: []