Skip to content

Commit ed76a81

Browse files
committed
Fix testkit request (WIP)
1 parent 31639e3 commit ed76a81

File tree

4 files changed

+88
-52
lines changed

4 files changed

+88
-52
lines changed

testkit-backend/lib/testkit/backend/messages/requests/execute_query.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
# frozen_string_literal: true
2-
31
module Testkit::Backend::Messages
42
module Requests
53
class ExecuteQuery < Request
64
def response
7-
Responses::Result.new(fetch(driver_id).execute_query(cypher, auth_token, config, decode(params)))
8-
end
9-
10-
private
11-
12-
def auth_token
13-
config.authorizationToken
5+
Responses::EagerResult.new(fetch(driver_id).execute_query(cypher, auth_token, config, **decode(params)))
146
end
157
end
168
end

testkit-backend/lib/testkit/backend/messages/requests/result_consume.rb

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,10 @@
11
module Testkit::Backend::Messages
22
module Requests
33
class ResultConsume < Request
4-
def process
5-
named_entity('Summary',
6-
**{
7-
serverInfo: to_map(summary.server, :protocol_version, :address, :agent),
8-
counters: to_map(summary.counters, *%w[constraints_added constraints_removed contains_system_updates? contains_updates? indexes_added
9-
indexes_removed labels_added labels_removed nodes_created nodes_deleted properties_set relationships_created
10-
relationships_deleted system_updates]),
11-
query: { text: summary.query.text, parameters: summary.query.parameters.transform_values(&method(:to_testkit)) },
12-
database: summary.database.name,
13-
queryType: summary.query_type,
14-
notifications: summary.notifications&.then(&method(:notifications)),
15-
plan: (plan_to_h(summary.plan) if summary.has_plan?),
16-
profile: summary.has_profile? ? summary.profile.then { |p| { db_hits: p.db_hits } } : nil,
17-
}.merge!(to_map(summary, *%w[result_available_after result_consumed_after])))
18-
end
19-
20-
private
21-
22-
def summary
23-
@object ||= fetch(result_id).consume
24-
end
25-
26-
def to_map(o, *methods)
27-
methods.map { |name| [key(name), o.send(name).then { |obj| block_given? ? yield(obj) : obj }] }.to_h
28-
end
4+
include SummaryHelper
295

30-
def key(name)
31-
name.to_s.gsub('?', '').camelize(:lower).to_sym
32-
end
33-
34-
def map_entry(n, method, *methods)
35-
n.send(method)&.then { |o| { key(method) => to_map(o, *methods) } } || {}
36-
end
37-
38-
def notifications(ns)
39-
ns.map do |n|
40-
to_map(n, *%w[code title description raw_category severity raw_severity_level])
41-
.merge(to_map(n, *%w[category severity_level]) { |o| o&.name || 'UNKNOWN' })
42-
.merge(map_entry(n, :position, :column, :line, :offset))
43-
end
44-
end
45-
46-
def plan_to_h(plan)
47-
plan.to_h.transform_keys(&method(:key)).tap {|hash| hash[:children]&.map!(&method(:plan_to_h))}
6+
def process
7+
summary_to_testkit(fetch(result_id).consume)
488
end
499
end
5010
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Testkit
2+
module Backend
3+
module Messages
4+
module Responses
5+
class EagerResult < Response
6+
alias_method :response_to_testkit, :to_testkit
7+
8+
include Conversion
9+
alias_method :value_to_testkit, :to_testkit
10+
11+
include SummaryHelper
12+
13+
def to_testkit(*args)
14+
if args.empty?
15+
response_to_testkit
16+
else
17+
value_to_testkit(*args)
18+
end
19+
end
20+
21+
def data
22+
{
23+
keys: @object.records.first&.keys || [],
24+
records: @object.records.map do |record|
25+
{ values: record.values.map(&method(:to_testkit)) }
26+
end,
27+
summary: summary_to_testkit(@object.summary)[:data]
28+
}
29+
end
30+
end
31+
end
32+
end
33+
end
34+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module Testkit
2+
module Backend
3+
module Messages
4+
module SummaryHelper
5+
def summary_to_testkit(summary)
6+
named_entity('Summary',
7+
**{
8+
serverInfo: to_map(summary.server, :protocol_version, :address, :agent),
9+
counters: to_map(summary.counters, *%w[constraints_added constraints_removed contains_system_updates? contains_updates? indexes_added indexes_removed labels_added labels_removed nodes_created nodes_deleted properties_set relationships_created relationships_deleted system_updates]),
10+
query: {
11+
text: summary.query.text,
12+
parameters: summary.query.parameters.transform_values(&method(:to_testkit))
13+
},
14+
database: summary.database.name,
15+
queryType: summary.query_type,
16+
notifications: summary.notifications&.then(&method(:notifications)),
17+
plan: (plan_to_h(summary.plan) if summary.has_plan?),
18+
profile: summary.has_profile? ? summary.profile.then { |p| { db_hits: p.db_hits } } : nil
19+
}.merge!(to_map(summary, *%w[result_available_after result_consumed_after])))
20+
end
21+
22+
private
23+
24+
def to_map(o, *methods)
25+
methods.map { |name| [key(name), o.send(name).then { |obj| block_given? ? yield(obj) : obj }] }.to_h
26+
end
27+
28+
def key(name)
29+
name.to_s.gsub('?', '').camelize(:lower).to_sym
30+
end
31+
32+
def map_entry(n, method, *methods)
33+
n.send(method)&.then { |o| { key(method) => to_map(o, *methods) } } || {}
34+
end
35+
36+
def notifications(ns)
37+
ns.map do |n|
38+
to_map(n, *%w[code title description raw_category severity raw_severity_level])
39+
.merge(to_map(n, *%w[category severity_level]) { |o| o&.name || 'UNKNOWN' })
40+
.merge(map_entry(n, :position, :column, :line, :offset))
41+
end
42+
end
43+
44+
def plan_to_h(plan)
45+
plan.to_h.transform_keys(&method(:key)).tap { |hash| hash[:children]&.map!(&method(:plan_to_h)) }
46+
end
47+
end
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)