|
| 1 | +require_relative '../lib/redisgraph.rb' |
| 2 | +require "test/unit" |
| 3 | +include Test::Unit::Assertions |
| 4 | + |
| 5 | +# Helper functions |
| 6 | +# TODO it would be nice to have something like DisposableRedis |
| 7 | + |
| 8 | +# Connect to a Redis server on localhost:6379 |
| 9 | +def connect_test |
| 10 | + begin |
| 11 | + @r = RedisGraph.new("rubytest") |
| 12 | + rescue Redis::BaseError => e |
| 13 | + puts e |
| 14 | + puts "RedisGraph tests require that a Redis server with the graph module loaded be running on localhost:6379" |
| 15 | + exit 1 |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +# Ensure that the graph "rubytest" does not exist |
| 20 | +def delete_graph |
| 21 | + @r.delete |
| 22 | +end |
| 23 | + |
| 24 | +# Test functions - each validates one or more EXPLAIN and QUERY calls |
| 25 | + |
| 26 | +def validate_node_creation |
| 27 | + query_str = """CREATE (t:node {name: 'src'})""" |
| 28 | + x = @r.query(query_str) |
| 29 | + plan = @r.explain(query_str) |
| 30 | + assert(plan =~ /Create/) |
| 31 | + assert(x.resultset.nil?) |
| 32 | + assert(x.stats[:labels_added] == 1) |
| 33 | + assert(x.stats[:nodes_created] == 1) |
| 34 | + assert(x.stats[:properties_set] == 1) |
| 35 | + puts "Create node - PASSED" |
| 36 | +end |
| 37 | + |
| 38 | +def validate_node_deletion |
| 39 | + query_str = """MATCH (t:node) WHERE t.name = 'src' DELETE t""" |
| 40 | + plan = @r.explain(query_str) |
| 41 | + assert(plan =~ /Delete/) |
| 42 | + x = @r.query(query_str) |
| 43 | + assert(x.resultset.nil?) |
| 44 | + assert(x.stats[:nodes_deleted] == 1) |
| 45 | + query_str = """MATCH (t:node) WHERE t.name = 'src' RETURN t""" |
| 46 | + assert(x.resultset.nil?) |
| 47 | + puts "Delete node - PASSED" |
| 48 | +end |
| 49 | + |
| 50 | +def validate_edge_creation |
| 51 | + query_str = """CREATE (p:node {name: 'src1'})-[:edge]->(:node {name: 'dest1'}), (:node {name: 'src2'})-[:edge]->(q:node_type_2 {name: 'dest2'})""" |
| 52 | + plan = @r.explain(query_str) |
| 53 | + assert(plan =~ /Create/) |
| 54 | + x = @r.query(query_str) |
| 55 | + assert(x.resultset.nil?) |
| 56 | + assert(x.stats[:nodes_created] == 4) |
| 57 | + assert(x.stats[:properties_set] == 4) |
| 58 | + assert(x.stats[:relationships_created] == 2) |
| 59 | + puts "Add edges - PASSED" |
| 60 | +end |
| 61 | + |
| 62 | +def validate_edge_traversal |
| 63 | + query_str = """MATCH (a)-[:edge]->(b:node) RETURN a, b""" |
| 64 | + plan = @r.explain(query_str) |
| 65 | + assert(plan.include?("Traverse")) |
| 66 | + x = @r.query(query_str) |
| 67 | + assert(x.resultset) |
| 68 | + assert(x.columns.length == 2) |
| 69 | + assert(x.resultset.length == 1) |
| 70 | + assert(x.resultset[0] == ["src1", "dest1"]) |
| 71 | + puts "Traverse edge - PASSED" |
| 72 | +end |
| 73 | + |
| 74 | +def test_suite |
| 75 | + puts "Running RedisGraph tests..." |
| 76 | + connect_test |
| 77 | + delete_graph # Clear the graph |
| 78 | + |
| 79 | + # Test basic functionalities |
| 80 | + validate_node_creation |
| 81 | + validate_node_deletion |
| 82 | + validate_edge_creation |
| 83 | + validate_edge_traversal |
| 84 | + |
| 85 | + delete_graph # Clear the graph again |
| 86 | + puts "RedisGraph tests passed!" |
| 87 | +end |
| 88 | + |
| 89 | +test_suite |
0 commit comments