|
| 1 | +require 'helper.rb' |
| 2 | + |
| 3 | +require_relative '../lib/redisgraph.rb' |
| 4 | + |
| 5 | +describe RedisGraph do |
| 6 | + # TODO it would be nice to have something like DisposableRedis |
| 7 | + # Connect to a Redis server on localhost:6379 |
| 8 | + before(:all) do |
| 9 | + begin |
| 10 | + @r = RedisGraph.new("rubytest") |
| 11 | + rescue Redis::BaseError => e |
| 12 | + puts e |
| 13 | + puts "RedisGraph tests require that a Redis server with the graph module loaded be running on localhost:6379" |
| 14 | + exit 1 |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + # Ensure that the graph "rubytest" does not exist |
| 19 | + after(:all) do |
| 20 | + @r.delete |
| 21 | + end |
| 22 | + |
| 23 | + # Test functions - each validates one or more EXPLAIN and QUERY calls |
| 24 | + |
| 25 | + context "nodes" do |
| 26 | + it "should create nodes properly" do |
| 27 | + query_str = """CREATE (t:node {name: 'src'})""" |
| 28 | + x = @r.query(query_str) |
| 29 | + plan = @r.explain(query_str) |
| 30 | + expect(plan).to include("Create") |
| 31 | + expect(x.resultset).to be_nil |
| 32 | + expect(x.stats[:labels_added]).to eq(1) |
| 33 | + expect(x.stats[:nodes_created]).to eq(1) |
| 34 | + expect(x.stats[:properties_set]).to eq(1) |
| 35 | + end |
| 36 | + |
| 37 | + it "should delete nodes properly" do |
| 38 | + query_str = """MATCH (t:node) WHERE t.name = 'src' DELETE t""" |
| 39 | + plan = @r.explain(query_str) |
| 40 | + expect(plan).to include("Delete") |
| 41 | + x = @r.query(query_str) |
| 42 | + expect(x.resultset).to be_nil |
| 43 | + expect(x.stats[:nodes_deleted]).to eq(1) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + context "edges" do |
| 48 | + it "should create edges properly" do |
| 49 | + query_str = """CREATE (p:node {name: 'src1'})-[:edge]->(:node {name: 'dest1'}), (:node {name: 'src2'})-[:edge]->(q:node_type_2 {name: 'dest2'})""" |
| 50 | + plan = @r.explain(query_str) |
| 51 | + expect(plan).to include("Create") |
| 52 | + x = @r.query(query_str) |
| 53 | + expect(x.resultset).to be_nil |
| 54 | + expect(x.stats[:nodes_created]).to eq(4) |
| 55 | + expect(x.stats[:properties_set]).to eq(4) |
| 56 | + expect(x.stats[:relationships_created]).to eq(2) |
| 57 | + end |
| 58 | + |
| 59 | + it "should traverse edges properly" do |
| 60 | + query_str = """MATCH (a)-[:edge]->(b:node) RETURN a, b""" |
| 61 | + plan = @r.explain(query_str) |
| 62 | + expect(plan).to include("Traverse") |
| 63 | + x = @r.query(query_str) |
| 64 | + expect(x.resultset).to be_instance_of(Array) |
| 65 | + expect(x.columns.length).to eq(2) |
| 66 | + expect(x.resultset.length).to eq(1) |
| 67 | + expect(x.resultset[0]).to eq(["src1", "dest1"]) |
| 68 | + end |
| 69 | + end |
| 70 | +end |
0 commit comments