DBIO-PostgreSQL-Age

 view release on metacpan or  search on metacpan

t/10-age-live.t  view on Meta::CPAN


# --- create_graph ---
lives_ok { $schema->storage->create_graph($graph) } 'create_graph lives';

my ($exists) = $schema->storage->dbh->selectrow_array(
  'SELECT 1 FROM ag_catalog.ag_graph WHERE name = ?',
  undef, $graph,
);
ok($exists, 'graph appears in ag_catalog.ag_graph');

# --- cypher: insert a few vertices and edges ---
# AGE requires at least one return column from cypher(), so each CREATE
# returns a literal value we can ignore.
lives_ok {
  $schema->storage->cypher(
    $graph,
    q{
      CREATE (alice:Person {name: 'Alice', age: 30}),
             (bob:Person   {name: 'Bob',   age: 25}),
             (carol:Person {name: 'Carol', age: 28}),
             (alice)-[:KNOWS {since: 2020}]->(bob),
             (bob)-[:KNOWS {since: 2021}]->(carol),
             (alice)-[:KNOWS {since: 2019}]->(carol)
      RETURN 1
    },
    ['ok'],
  );
} 'cypher CREATE lives';

# --- cypher: query vertices ---
my $rows = $schema->storage->cypher(
  $graph,
  q{ MATCH (p:Person) RETURN p.name },
  ['name'],
);

is(ref $rows, 'ARRAY', 'cypher returns arrayref');
is(scalar @$rows, 3, 'three persons matched');

my @names = sort map { my $n = $_->{name}; $n =~ s/^"|"$//g; $n } @$rows;
is_deeply(\@names, [qw(Alice Bob Carol)], 'all three names returned');

# --- cypher: traverse relationship ---
my $knows = $schema->storage->cypher(
  $graph,
  q{ MATCH (a:Person)-[:KNOWS]->(b:Person) RETURN a.name, b.name },
  [qw(a b)],
);
is(scalar @$knows, 3, 'three KNOWS edges traversed');

# --- cypher with parameters ---
my $alice = $schema->storage->cypher(
  $graph,
  q{ MATCH (p:Person {name: $name}) RETURN p.age },
  ['age'],
  { name => 'Alice' },
);
is(scalar @$alice, 1, 'parameterized query returns one row for Alice');
like($alice->[0]{age}, qr/30/, 'Alice age is 30');

# --- drop_graph (without cascade should fail if non-empty, in newer AGE) ---
# Skip the non-cascade case — depends on AGE version. Just drop with cascade.
lives_ok { $schema->storage->drop_graph($graph, 1) } 'drop_graph cascade lives';

my ($still_there) = $schema->storage->dbh->selectrow_array(
  'SELECT 1 FROM ag_catalog.ag_graph WHERE name = ?',
  undef, $graph,
);
ok(!$still_there, 'graph removed from ag_catalog.ag_graph');

done_testing;



( run in 1.163 second using v1.01-cache-2.11-cpan-7fcb06a456a )