Module: LinkedRails::TestMethods

Defined in:
lib/linked_rails/test_methods.rb

Instance Method Summary collapse

Instance Method Details

#expect_disabled_form(iri: requested_iri, error: 'This action is currently not available') ⇒ Object



5
6
7
8
9
# File 'lib/linked_rails/test_methods.rb', line 5

def expect_disabled_form(iri: requested_iri, error: 'This action is currently not available')
  assert_response 200
  expect_triple(iri, NS.schema.actionStatus, NS.ontola[:DisabledActionStatus])
  expect_triple(iri, NS.schema.error, error) if error
end

#expect_enabled_form(iri: requested_iri) ⇒ Object



11
12
13
14
# File 'lib/linked_rails/test_methods.rb', line 11

def expect_enabled_form(iri: requested_iri)
  assert_response 200
  expect_triple(iri, NS.schema.actionStatus, NS.schema.PotentialActionStatus)
end

#expect_errors(iri, errors) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/linked_rails/test_methods.rb', line 16

def expect_errors(iri, errors)
  error_response = expect_triple(iri, NS.ll[:errorResponse], nil).first.object
  assert_equal expect_triple(error_response, nil, nil).count, errors.count + 2
  errors.each do |key, value|
    expect_triple(error_response, key, value)
  end
end

#expect_header(key, value) ⇒ Object



24
25
26
# File 'lib/linked_rails/test_methods.rb', line 24

def expect_header(key, value)
  expect(response.headers[key]).to(include(value))
end

#expect_ontola_action(redirect: nil, snackbar: nil, reload: nil) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/linked_rails/test_methods.rb', line 28

def expect_ontola_action(redirect: nil, snackbar: nil, reload: nil)
  if redirect
    expect_header('Exec-Action', "actions/redirect?#{{location: redirect, reload: reload}.compact.to_param}")
  end
  expect_header('Exec-Action', "actions/snackbar?#{{text: snackbar}.to_param}") if snackbar

  expect_ontola_action_count([redirect, snackbar].compact.size)
end

#expect_ontola_action_count(count) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/linked_rails/test_methods.rb', line 37

def expect_ontola_action_count(count)
  if count.zero?
    assert_nil response.headers['Exec-Action']
  else
    assert_equal count, response.headers['Exec-Action'].count("\n"), response.headers['Exec-Action']
  end
end

#expect_path(start, path, value) ⇒ Object

Tests if the property path starting at start resolves to value. If multiple solutions are present only one has to match to pass.

Parameters:

  • start (IRI)

    The node to start at

  • path (Array)

    The property path to traverse

  • value (IRI|IRI[])

    The value to match at the end, pass an array to match multiple values.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/linked_rails/test_methods.rb', line 52

def expect_path(start, path, value) # rubocop:disable Metrics/AbcSize
  match = path.each_with_index.reduce(start) do |node, (path_seg, i)|
    obj = [*node].map { |cur_node| rdf_body.query([cur_node, path_seg, nil, nil]).map(&:object) }.flatten
    break (obj & [*value]).present? if path.length - 1 == i

    nodes = obj.filter { |o| o.is_a?(RDF::Resource) }
    break false if nodes.blank?

    nodes
  end

  segs = path.map(&:to_s).join(', ')
  assert match,
         "Expected to find '#{value}' from '#{start}' through '[#{segs}]' in\n#{response.body}"
end

#expect_resource_type(type, iri: requested_iri) ⇒ Object



68
69
70
# File 'lib/linked_rails/test_methods.rb', line 68

def expect_resource_type(type, iri: requested_iri)
  expect_triple(iri, RDF[:type], type)
end

#expect_sequence(subject, predicate) ⇒ Object



72
73
74
# File 'lib/linked_rails/test_methods.rb', line 72

def expect_sequence(subject, predicate)
  expect_triple(subject, predicate, nil).first.object
end

#expect_sequence_member(subject, index, object) ⇒ Object



76
77
78
79
# File 'lib/linked_rails/test_methods.rb', line 76

def expect_sequence_member(subject, index, object)
  expect_triple(subject, RDF[:"_#{index}"], object)
  object
end

#expect_sequence_size(subject, expected_count) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/linked_rails/test_methods.rb', line 81

def expect_sequence_size(subject, expected_count)
  count =
    expect_triple(subject, nil, nil)
      .select { |s| s.predicate.to_s.starts_with?('http://www.w3.org/1999/02/22-rdf-syntax-ns#_') }
      .count
  assert_equal expected_count, count
end

#expect_triple(subject, predicate, object, graph = ) ⇒ Object



89
90
91
92
93
94
# File 'lib/linked_rails/test_methods.rb', line 89

def expect_triple(subject, predicate, object, graph = NS.ll[:supplant])
  statement = RDF::Statement(subject, predicate, object, graph_name: graph)
  match = rdf_body.query(statement)
  assert match.present?, "Expected to find #{statement} in\n#{response.body}"
  match
end

#rdf_bodyObject



106
107
108
109
110
# File 'lib/linked_rails/test_methods.rb', line 106

def rdf_body
  @rdf_body ||= RDF::Graph.new << RDF::Reader
                                    .for(content_type: response.headers['Content-Type'])
                                    .new(response.body)
end

#refute_triple(subject, predicate, object, graph = nil) ⇒ Object



96
97
98
99
100
# File 'lib/linked_rails/test_methods.rb', line 96

def refute_triple(subject, predicate, object, graph = nil)
  statement = RDF::Statement(subject, predicate, object, graph_name: graph)
  assert_not rdf_body.query([subject, predicate, object, graph]).present?,
             "Expected not to find #{statement} in\n#{response.body}"
end

#requested_iriObject



102
103
104
# File 'lib/linked_rails/test_methods.rb', line 102

def requested_iri
  RDF::URI(request.original_url.sub(".#{request.format.symbol}", ''))
end