rspec-graphql_assistant 0.1.1 → 0.1.3

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
  SHA256:
3
- metadata.gz: 7264719c695c490b72a959f8eec53ca141e5acb0e708b0358d7ad8d6dae3f4e5
4
- data.tar.gz: 97d95948b00d13de9e286cf9bfd2ca222dd20d7c784e3360190c7f48d56d4222
3
+ metadata.gz: adc1ae0ff51cd09b61d84afd36a6c28af5d62341a23c06804e95460717af5414
4
+ data.tar.gz: 165f94dadbd1854d9eb16478221817a7bdb722b103861eadd671dd5049927961
5
5
  SHA512:
6
- metadata.gz: da718b13e8e30ba0743b1c9245e175bc4185b9001c75b1d0569ec420f6b2b82edfaa00383191f1e1222f143ad43eea91fea2834ded531d430e464abd86cbce17
7
- data.tar.gz: fba275eb1c29e966f90ad92cb2da1da4ff4fe5868c18f0058509dad539ef8591ec8fe0daec6e518bdc823fe6578c60d33169a07c49443e7f6c89cabae47bbb13
6
+ metadata.gz: a7c012bab3cf51c944df522d759248ba7842db262f6694346d65785e48c02a23d23dd858c263f910d82c305087974824398a2aa1111cea27420f7ce7bf140baf
7
+ data.tar.gz: a0c20a07b6b6353d95325a118623f5a56ad3025bbec6a29e4e4db8db8a87ae881e57f986153bd06aa7a6c11265899b2b99f62cab51af79f54dd0c090b4260a3a
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-graphql_assistant (0.1.0)
4
+ rspec-graphql_assistant (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -26,7 +26,7 @@ end
26
26
  ```
27
27
 
28
28
  ```ruby
29
- graphql_mutation('updatePost', { post_id: 1, content: "some test" }, [:id, :content])
29
+ graphql_mutation('updatePost', { post_id: 1, content: "some test" }, [:id, author: [:name]])
30
30
  ```
31
31
 
32
32
  produce this one query
@@ -34,7 +34,8 @@ produce this one query
34
34
  mutation {
35
35
  updatePost(input: {postId: 1, content: "some text"}) {
36
36
  id
37
- content
37
+ author {
38
+ name
38
39
  }
39
40
  }
40
41
 
@@ -49,7 +50,21 @@ graphql_query('queryName', args, response)
49
50
  graphql_subscription('subscriptionName', args, response)
50
51
  ```
51
52
 
52
- Work in progress and now only supports inline syntax. Which means without passing variables. Later try to cover both cases
53
+ ## Matchers
54
+ ```ruby
55
+ expect(result).to graphql_have_attributes([:book, :author_reviews], { score: 10 })
56
+ ```
57
+ it's equivalent to `expect(result.dig('data', 'book', 'authorReviews')).to have_attributes({'score' => 10})`
58
+
59
+ ```ruby
60
+ expect(result).to graphql_include(:book, %w[id title])
61
+ ```
62
+
63
+ it's equivalent to `expect(result.dig('data', 'book')).to include('id', 'title')`
64
+
65
+ Work in progress and now only supports inline syntax. Which means without passing variables. Later try to cover both cases.
66
+ I'm still working on documentation if you want to help me be appreciated
67
+
53
68
  Cheers
54
69
 
55
70
  ## Contributing
@@ -3,12 +3,14 @@ require "rspec/core"
3
3
  require "rspec/expectations"
4
4
  require 'active_support'
5
5
  require 'active_support/core_ext'
6
+ require 'rspec/graphql_assistant/matchers/matcher_helper'
7
+ require 'rspec/graphql_assistant/matchers/include'
8
+ require 'rspec/graphql_assistant/matchers/have_attributes'
6
9
  require 'rspec/graphql_assistant/matchers'
7
- require 'rspec/graphql_assistant/has_attrs'
8
- require 'rspec/graphql_assistant/contain_attrs'
9
10
  require 'rspec/graphql_assistant/query_builder'
10
11
  require 'rspec/graphql_assistant/argument_builder'
11
12
  require 'rspec/graphql_assistant/argument_builder_with_vars'
13
+ require 'rspec/graphql_assistant/response_builder'
12
14
  require 'rspec/graphql_assistant/helpers'
13
15
 
14
16
  module Rspec
@@ -1,11 +1,11 @@
1
1
  module RSpec
2
2
  module Matchers
3
- def has_graphql_attrs(key, expected)
4
- RSpec::GraphqlAssistant::HasAttrs.new(key, expected)
3
+ def graphql_include(key, expected)
4
+ RSpec::GraphqlAssistant::Matchers::Include.new(key, expected)
5
5
  end
6
6
 
7
- def contain_graphql_attrs(key, expected)
8
- RSpec::GraphqlAssistant::ContainAttrs.new(key, expected)
7
+ def graphql_have_attributes(key, expected)
8
+ RSpec::GraphqlAssistant::Matchers::HaveAttributes.new(key, expected)
9
9
  end
10
10
  end
11
11
  end
@@ -0,0 +1,35 @@
1
+ module RSpec
2
+ module GraphqlAssistant
3
+ module Matchers
4
+ class HaveAttributes
5
+ include MatcherHelper
6
+
7
+ attr_reader :sample, :expected, :key, :root
8
+
9
+ def initialize(key, expected, **args)
10
+ @key = key
11
+ @expected = expected
12
+ @root = args.fetch(:root, :data)
13
+ end
14
+
15
+ def matches?(actual)
16
+ value = lookup_value(actual)
17
+ res = true
18
+ expected.each do |k, v|
19
+ res = value[k] == v
20
+ break unless res
21
+ end
22
+ res
23
+ end
24
+
25
+ def failure_message
26
+ "'#{key}' not equal attributes '#{expected}'"
27
+ end
28
+
29
+ def description
30
+ "'#{key}' has attributes '#{expected}'"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ module RSpec
2
+ module GraphqlAssistant
3
+ module Matchers
4
+ class Include
5
+ include MatcherHelper
6
+ attr_reader :sample, :expected, :key, :root
7
+
8
+ def initialize(key, expected, **args)
9
+ @expected = expected
10
+ @key = key
11
+ @root = args.fetch(:root, :data)
12
+ end
13
+
14
+ def matches?(actual)
15
+ value = lookup_value(actual)
16
+
17
+ return value.any? { |k, _v| expected.include? k } if value.is_a?(Hash)
18
+
19
+ value.all? { |el| el.any? { |k, _v| expected.include? k } } if value.is_a?(Array)
20
+ end
21
+
22
+ def failure_message
23
+ "'#{key}' not include all attributes '#{expected}'"
24
+ end
25
+
26
+ def description
27
+ "'#{key}' has attributes '#{expected}'"
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ module RSpec
2
+ module GraphqlAssistant
3
+ module Matchers
4
+ module MatcherHelper
5
+ def key_to_array(key, root)
6
+ if key.is_a?(Array)
7
+ return ([root] + key).map { |el| el.to_s.camelize(:lower) }
8
+ end
9
+ ([root] + [key]).map { |el| el.to_s.camelize(:lower) }
10
+ end
11
+
12
+ def lookup_value(actual)
13
+ actual_copy = actual.with_indifferent_access
14
+ keys_to_dig = key_to_array(key, root)
15
+ actual_copy.dig(*keys_to_dig)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -1,38 +1,35 @@
1
1
  module RSpec
2
2
  module GraphqlAssistant
3
3
  class QueryBuilder
4
- attr_reader :name, :args, :response, :type
5
- def initialize(type, name, args, response)
4
+ attr_reader :name, :input_args, :resp_args, :type
5
+ def initialize(type, name, input_args, resp_args)
6
6
  @type = type
7
7
  @name = name
8
- @args = args
9
- @response = response
8
+ @input_args = input_args
9
+ @resp_args = resp_args
10
10
  end
11
11
 
12
12
 
13
13
  def call
14
- template % {type: type, name: name, args: transform_args(args), response: transform_response(response)}
14
+ template % {type: type, name: name, input_args: transform_input_args(input_args), resp_args: transform_resp_args(resp_args)}
15
15
  end
16
16
 
17
17
  private
18
18
 
19
- def transform_args(args)
19
+ def transform_input_args(args)
20
20
  return if args.blank?
21
21
  "(#{ArgumentBuilder.new(args).call})"
22
22
  end
23
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")
24
+ def transform_resp_args(args)
25
+ ResponseBuilder.new(args).call
29
26
  end
30
27
 
31
28
  def template
32
29
  <<-GQL
33
30
  %{type} {
34
- %{name}%{args} {
35
- %{response}
31
+ %{name}%{input_args} {
32
+ %{resp_args}
36
33
  }
37
34
  }
38
35
  GQL
@@ -0,0 +1,51 @@
1
+ module RSpec
2
+ module GraphqlAssistant
3
+ class ResponseBuilder
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
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
@@ -1,5 +1,5 @@
1
1
  module Rspec
2
2
  module GraphqlAssistant
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-graphql_assistant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Lopatin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-17 00:00:00.000000000 Z
11
+ date: 2020-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -44,11 +44,13 @@ files:
44
44
  - lib/rspec/graphql_assistant.rb
45
45
  - lib/rspec/graphql_assistant/argument_builder.rb
46
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
47
  - lib/rspec/graphql_assistant/helpers.rb
50
48
  - lib/rspec/graphql_assistant/matchers.rb
49
+ - lib/rspec/graphql_assistant/matchers/have_attributes.rb
50
+ - lib/rspec/graphql_assistant/matchers/include.rb
51
+ - lib/rspec/graphql_assistant/matchers/matcher_helper.rb
51
52
  - lib/rspec/graphql_assistant/query_builder.rb
53
+ - lib/rspec/graphql_assistant/response_builder.rb
52
54
  - lib/rspec/graphql_assistant/version.rb
53
55
  - rspec-graphql_assistant.gemspec
54
56
  homepage: https://github.com/gingray/rspec-graphql_assistant
@@ -1,31 +0,0 @@
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
@@ -1,27 +0,0 @@
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