Skip to content

Commit

Permalink
EPBR-8023 Refactor to create a wrapper test helper
Browse files Browse the repository at this point in the history
Create a wrapper test helper to get the reusable method out of the new sap wrapper test.
  • Loading branch information
Christine-horrocks committed Jan 6, 2025
1 parent aab0fd4 commit 80f52dd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 69 deletions.
70 changes: 1 addition & 69 deletions spec/view_model/sap_wrapper_new_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# require_relative "../xml_view_test_helper"
require "active_support/core_ext/hash/deep_merge"
require "json"
require_relative "../wrapper_test_helper"

RSpec.describe ViewModel::SapWrapper do

context "when calling the sap wrapper for a valid schema" do
it "returns the expected assertion" do
schema_tests = [
Expand Down Expand Up @@ -47,68 +44,3 @@
end
end
end

def test_wrapper(schema_case)

schema_case[:type] = "epc" unless schema_case[:type]

sample = Samples.xml(schema_case[:schema], schema_case[:type])

validate_xml(sample, schema_case)

view_model = ViewModel::Factory.new.create(sample, schema_case[:schema], nil)

source_hash = view_model.method(schema_case[:method_called]).call
print source_hash.to_json
assertion = fetch_assertion(schema_case[:schema], schema_case[:method_called], schema_case[:type])

assertion.each do |key, value|
result = source_hash[key]

expect(result).to eq(value), <<~ERROR
Failed on #{schema_case[:schema]}:#{schema_case[:type]}:#{key}
EXPECTED: "#{value}" (#{value.class})
GOT: "#{result}" (#{result.class})
ERROR
end

expect(assertion.keys).to eq(source_hash.keys)
end

def validate_xml(sample, schema_case)
schema_path = Helper::SchemaListHelper.new(schema_case[:schema]).schema_path

schema =
Nokogiri::XML::Schema.from_document Nokogiri.XML(
File.read(schema_path),
schema_path,
)

validation = schema.validate(Nokogiri.XML(sample))

expect(validation).to be_empty, <<~ERROR
Failed on #{schema_case[:schema]}:#{schema_case[:type]}
This document does not validate against the chosen schema,
Errors:
#{validation.join("\n ")}
ERROR
end

def fetch_assertion(assertion, method_called, type)

unless type == "epc"
assertion_path = File.join Dir.pwd, "spec/fixtures/assertions/#{assertion}/#{method_called.to_s}_#{type}.json"
else
assertion_path = File.join Dir.pwd, "spec/fixtures/assertions/#{assertion}/#{method_called.to_s}.json"
end

unless File.exist? assertion_path
raise ArgumentError,
"No assertion found for schema #{schema}, create one at #{
assertion_path
}"
end

json_assertion = File.read assertion_path
JSON.parse(json_assertion, symbolize_names: true)
end
62 changes: 62 additions & 0 deletions spec/wrapper_test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
def test_wrapper(schema_case)
schema_case[:type] = "epc" unless schema_case[:type]

sample = Samples.xml(schema_case[:schema], schema_case[:type])

validate_xml(sample, schema_case)

view_model = ViewModel::Factory.new.create(sample, schema_case[:schema], nil)

source_hash = view_model.method(schema_case[:method_called]).call
# print source_hash.to_json
assertion = fetch_assertion(schema_case[:schema], schema_case[:method_called], schema_case[:type])

assertion.each do |key, value|
result = source_hash[key]

expect(result).to eq(value), <<~ERROR
Failed on #{schema_case[:schema]}:#{schema_case[:type]}:#{key}
EXPECTED: "#{value}" (#{value.class})
GOT: "#{result}" (#{result.class})
ERROR
end

expect(assertion.keys).to eq(source_hash.keys)
end

def validate_xml(sample, schema_case)
schema_path = Helper::SchemaListHelper.new(schema_case[:schema]).schema_path

schema =
Nokogiri::XML::Schema.from_document Nokogiri.XML(
File.read(schema_path),
schema_path,
)

validation = schema.validate(Nokogiri.XML(sample))

expect(validation).to be_empty, <<~ERROR
Failed on #{schema_case[:schema]}:#{schema_case[:type]}
This document does not validate against the chosen schema,
Errors:
#{validation.join("\n ")}
ERROR
end

def fetch_assertion(assertion, method_called, type)
assertion_path = if type == "epc"
File.join Dir.pwd, "spec/fixtures/assertions/#{assertion}/#{method_called}.json"
else
File.join Dir.pwd, "spec/fixtures/assertions/#{assertion}/#{method_called}_#{type}.json"
end

unless File.exist? assertion_path
raise ArgumentError,
"No assertion found for schema #{schema}, create one at #{
assertion_path
}"
end

json_assertion = File.read assertion_path
JSON.parse(json_assertion, symbolize_names: true)
end

0 comments on commit 80f52dd

Please sign in to comment.