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
- #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: {}, reload_schema_artifacts: false, output: nil, &block) ⇒ Object
- #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: {}, reload_schema_artifacts: false, output: nil) {|api| ... } ⇒ Object
- #strip_docs(string) ⇒ Object
- #type_def_from(sdl, type, include_docs: false) ⇒ Object
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: {}, reload_schema_artifacts: false, output: nil, &block) ⇒ Object
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 |
# File 'lib/elastic_graph/schema_definition/test_support.rb', line 22 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: {}, 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, reload_schema_artifacts: reload_schema_artifacts, 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: {}, reload_schema_artifacts: false, output: nil) {|api| ... } ⇒ Object
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 |
# File 'lib/elastic_graph/schema_definition/test_support.rb', line 54 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: {}, 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, 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 = SchemaDefinition::SchemaArtifactManager.new( 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 |
#strip_docs(string) ⇒ Object
126 127 128 129 130 |
# File 'lib/elastic_graph/schema_definition/test_support.rb', line 126 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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/elastic_graph/schema_definition/test_support.rb', line 107 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 |