-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathtest_support.rb
More file actions
136 lines (120 loc) · 5.02 KB
/
test_support.rb
File metadata and controls
136 lines (120 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright 2024 - 2026 Block, Inc.
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
#
# frozen_string_literal: true
require "elastic_graph/errors"
require "elastic_graph/schema_artifacts/runtime_metadata/schema_element_names"
require "elastic_graph/schema_definition/api"
require "elastic_graph/schema_definition/schema_artifact_manager"
module ElasticGraph
module SchemaDefinition
# Mixin designed to facilitate writing tests that define schemas.
#
# @private
module TestSupport
extend self
def define_schema(
schema_element_name_form:,
schema_element_name_overrides: {},
index_document_sizes: true,
json_schema_version: 1,
extension_modules: [],
derived_type_name_formats: {},
type_name_overrides: {},
enum_value_overrides_by_type: {},
enums_in_transition: [],
reload_schema_artifacts: false,
output: nil,
&block
)
schema_elements = SchemaArtifacts::RuntimeMetadata::SchemaElementNames.new(
form: schema_element_name_form,
overrides: schema_element_name_overrides
)
define_schema_with_schema_elements(
schema_elements,
index_document_sizes: index_document_sizes,
json_schema_version: json_schema_version,
extension_modules: extension_modules,
derived_type_name_formats: derived_type_name_formats,
type_name_overrides: type_name_overrides,
enum_value_overrides_by_type: enum_value_overrides_by_type,
enums_in_transition: enums_in_transition,
reload_schema_artifacts: reload_schema_artifacts,
output: output,
&block
)
end
def define_schema_with_schema_elements(
schema_elements,
index_document_sizes: true,
json_schema_version: 1,
extension_modules: [],
derived_type_name_formats: {},
type_name_overrides: {},
enum_value_overrides_by_type: {},
enums_in_transition: [],
reload_schema_artifacts: false,
output: nil
)
api = API.new(
schema_elements,
index_document_sizes,
extension_modules: extension_modules,
derived_type_name_formats: derived_type_name_formats,
type_name_overrides: type_name_overrides,
enum_value_overrides_by_type: enum_value_overrides_by_type,
enums_in_transition: enums_in_transition,
output: output || $stdout
)
yield api if block_given?
# Set the json_schema_version to the provided value, if needed.
if !json_schema_version.nil? && api.state.json_schema_version.nil?
api.json_schema_version json_schema_version
end
# :nocov: -- the else branch and code past this aren't used by tests in elasticgraph-schema_definition.
return api.results unless reload_schema_artifacts
# Reloading the schema artifacts takes extra time that we don't usually want to spend (so it's opt-in)
# but it can be useful in some cases because there is a bit of extra pruning/validation that it applies.
tmp_dir = ::Dir.mktmpdir
artifacts_manager = api.factory.new_schema_artifact_manager(
schema_definition_results: api.results,
schema_artifacts_directory: tmp_dir,
enforce_json_schema_version: false,
output: ::StringIO.new
)
artifacts_manager.dump_artifacts
SchemaArtifacts::FromDisk.new(tmp_dir)
# :nocov:
end
DOC_COMMENTS = (
'(?:^ *"""\\n' + # opening sequence of `"""` on its own line.
'(?:[^"]|(?:"(?!")))*' + # any sequence characters with no `""` sequence. (either no `"` or `"` is not followed by another)
'\\n *"""\\n)' # closing sequence of `"""` on its own line.
)
def type_def_from(sdl, type, include_docs: false)
type_def_keyword = '^(type|input|enum|union|interface|scalar|directive)\b'
# capture from the start of the type definition for `type` until the next type definition (or `\z` for end of string)
type_extraction_regex = /(#{DOC_COMMENTS}?#{type_def_keyword} #{type}\b.*?)(?:(?:#{DOC_COMMENTS}?#{type_def_keyword})|\z)/m
type_defs = sdl.scan(type_extraction_regex).map { |match| [match].flatten.first }
if type_defs.size >= 2
# :nocov: -- only executed when a mistake has been made; causes a failing test.
raise Errors::SchemaError,
"Expected to find 0 or 1 type definition for #{type}, but found #{type_defs.size}. Type defs:\n\n#{type_defs.join("\n\n")}"
# :nocov:
end
result = type_defs.first&.strip
result &&= strip_docs(result) unless include_docs
result
end
def strip_docs(string)
string
.gsub(/#{DOC_COMMENTS}/o, "") # strip doc comments
.gsub("\n\n", "\n") # cleanup formatting so we don't have extra blank lines.
end
end
end
end