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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +18 -3
- data/lib/rspec/graphql_assistant.rb +4 -2
- data/lib/rspec/graphql_assistant/matchers.rb +4 -4
- data/lib/rspec/graphql_assistant/matchers/have_attributes.rb +35 -0
- data/lib/rspec/graphql_assistant/matchers/include.rb +32 -0
- data/lib/rspec/graphql_assistant/matchers/matcher_helper.rb +21 -0
- data/lib/rspec/graphql_assistant/query_builder.rb +10 -13
- data/lib/rspec/graphql_assistant/response_builder.rb +51 -0
- data/lib/rspec/graphql_assistant/version.rb +1 -1
- metadata +6 -4
- data/lib/rspec/graphql_assistant/contain_attrs.rb +0 -31
- data/lib/rspec/graphql_assistant/has_attrs.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adc1ae0ff51cd09b61d84afd36a6c28af5d62341a23c06804e95460717af5414
|
4
|
+
data.tar.gz: 165f94dadbd1854d9eb16478221817a7bdb722b103861eadd671dd5049927961
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7c012bab3cf51c944df522d759248ba7842db262f6694346d65785e48c02a23d23dd858c263f910d82c305087974824398a2aa1111cea27420f7ce7bf140baf
|
7
|
+
data.tar.gz: a0c20a07b6b6353d95325a118623f5a56ad3025bbec6a29e4e4db8db8a87ae881e57f986153bd06aa7a6c11265899b2b99f62cab51af79f54dd0c090b4260a3a
|
data/Gemfile.lock
CHANGED
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, :
|
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
|
-
|
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
|
-
|
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
|
4
|
-
RSpec::GraphqlAssistant::
|
3
|
+
def graphql_include(key, expected)
|
4
|
+
RSpec::GraphqlAssistant::Matchers::Include.new(key, expected)
|
5
5
|
end
|
6
6
|
|
7
|
-
def
|
8
|
-
RSpec::GraphqlAssistant::
|
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, :
|
5
|
-
def initialize(type, name,
|
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
|
-
@
|
9
|
-
@
|
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,
|
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
|
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
|
25
|
-
|
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}%{
|
35
|
-
%{
|
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
|
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.
|
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-
|
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
|