Module: ElasticGraph::SchemaDefinition::TestSupport

Extended by:
TestSupport
Included in:
TestSupport
Defined in:
lib/elastic_graph/schema_definition/test_support.rb

Overview

Mixin designed to facilitate writing tests that define schemas.

Constant Summary collapse

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.
)

Instance Method Summary collapse

Instance Method Details

#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: {}, output: nil, &block) ⇒ Object



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
# File 'lib/elastic_graph/schema_definition/test_support.rb', line 21

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: {},
  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,
    output: output,
    &block
  )
end

#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: {}, output: nil) {|api| ... } ⇒ Object

Yields:

  • (api)


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
# File 'lib/elastic_graph/schema_definition/test_support.rb', line 51

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: {},
  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,
    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

  api.results
end

#strip_docs(string) ⇒ Object



106
107
108
109
110
# File 'lib/elastic_graph/schema_definition/test_support.rb', line 106

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

#type_def_from(sdl, type, include_docs: false) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/elastic_graph/schema_definition/test_support.rb', line 87

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(&:first)
  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