Module: DagMe::TestHelper

Defined in:
lib/dag_me/test_helper.rb

Overview

Minitest assertions for host applications:

class GraphSetupTest < ActiveSupport::TestCase
include DagMe::TestHelper

test 'tasks form a healthy DAG' do
  assert_dag_model Task, maintain: :postgresql_closure
  assert_dag_valid Task
end
end

Instance Method Summary collapse

Instance Method Details

#assert_dag_model(klass, maintain: nil, scope: nil, dag: nil) ⇒ Object

Asserts the class is wired up as a dag_me model, optionally pinning the maintain mode and scope columns. Pass dag: for a named graph.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dag_me/test_helper.rb', line 17

def assert_dag_model(klass, maintain: nil, scope: nil, dag: nil)
  assert klass.include?(DagMe::Model), "#{klass} should call dag_me"

  config = klass.dag_config_for(dag)
  if maintain
    assert_equal maintain, config.maintain,
                 "#{klass} should maintain its DAG via #{maintain}"
  end

  return unless scope

  assert_equal Array(scope).map(&:to_sym), config.scope_columns,
               "#{klass} should be scoped by #{Array(scope).join(', ')}"
end

#assert_dag_reachable(ancestor, descendant, dag: nil) ⇒ Object



40
41
42
43
# File 'lib/dag_me/test_helper.rb', line 40

def assert_dag_reachable(ancestor, descendant, dag: nil)
  assert ancestor.ancestor_of?(descendant, dag:),
         "expected #{node_label(ancestor)} to reach #{node_label(descendant)}"
end

#assert_dag_valid(klass, dag: nil) ⇒ Object

Asserts the stored closure agrees with the recursive-CTE truth.



33
34
35
36
37
38
# File 'lib/dag_me/test_helper.rb', line 33

def assert_dag_valid(klass, dag: nil)
  discrepancies = klass.dag(dag).validate

  assert_empty discrepancies,
               "#{klass} closure diverged from edge truth: #{discrepancies.inspect}"
end

#assert_topological_order(model, ordered_nodes, dag: nil) ⇒ Object

Asserts the ordered node list is a valid topological order: for every edge among the listed nodes, the parent appears before the child.



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dag_me/test_helper.rb', line 52

def assert_topological_order(model, ordered_nodes, dag: nil)
  config = model.dag_config_for(dag)
  position = ordered_nodes.each_with_index.to_h { |node, i| [Array(node.id), i] }

  model.dag(dag).edges.find_each do |edge|
    parent_key = config.edge_parent_columns.map { |c| edge[c] }
    child_key = config.edge_child_columns.map { |c| edge[c] }
    next unless position.key?(parent_key) && position.key?(child_key)

    assert_operator position[parent_key], :<, position[child_key],
                    "edge #{parent_key.join('/')} -> #{child_key.join('/')} violates topological order"
  end
end

#refute_dag_reachable(ancestor, descendant, dag: nil) ⇒ Object



45
46
47
48
# File 'lib/dag_me/test_helper.rb', line 45

def refute_dag_reachable(ancestor, descendant, dag: nil)
  refute ancestor.ancestor_of?(descendant, dag:),
         "expected #{node_label(ancestor)} not to reach #{node_label(descendant)}"
end