Skip to content

Commit

Permalink
Merge pull request #398 from bedupako12mas/centrality-week9
Browse files Browse the repository at this point in the history
Centrality Week 9
  • Loading branch information
bedupako12mas authored Jul 29, 2024
2 parents 0c31d51 + 0830bc3 commit a6d733f
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/metrics/pgr_betweennessCentrality.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Signatures
| OR EMPTY SET
.. TODO: Fix this when docqueries are made
:Example: For a directed subgraph with edges :math:`\{1, 2, 3, 4\}`.

.. literalinclude:: betweennessCentrality.queries
Expand Down
44 changes: 44 additions & 0 deletions pgtap/metrics/betweennessCentrality/edge_cases.pg
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*PGR-GNU*****************************************************************

Copyright (c) 2024 pgRouting developers
Mail: project@pgrouting.org

------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/

BEGIN;

UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
SELECT plan(15);

SELECT test_agg_cost('pgr_floydWarshall', true);
SELECT test_agg_cost('pgr_floydWarshall', false);
SELECT allPairs_test_flags('pgr_floydwarshall');

PREPARE q2 AS
SELECT * FROM pgr_floydWarshall(
'SELECT source, target, cost, reverse_cost FROM edges'
);

PREPARE q3 AS
SELECT * FROM pgr_floydWarshall(
'SELECT source, target, cost FROM edges',
true
);

SELECT lives_ok('q2', 'SHOULD WORK: without id with flag');
SELECT lives_ok('q3', 'SHOULD WORK: without id, with flag');

SELECT finish();
ROLLBACK;
46 changes: 46 additions & 0 deletions pgtap/metrics/betweennessCentrality/inner_query.pg
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

/*PGR-GNU*****************************************************************

Copyright (c) 2024 pgRouting developers
Mail: project@pgrouting.org

------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
BEGIN;

UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
SELECT CASE WHEN NOT min_version('3.7.0') THEN plan(1) ELSE plan(54) END;

CREATE OR REPLACE FUNCTION inner_query()
RETURNS SETOF TEXT AS
$BODY$
BEGIN

IF NOT min_version('3.7.0') THEN
RETURN QUERY
SELECT skip(1, 'Function is new on 3.7.0');
RETURN;
END IF;

RETURN QUERY
SELECT style_dijkstra('pgr_betweennessCentrality(', ')');

END;
$BODY$
LANGUAGE plpgsql;

SELECT inner_query();

SELECT finish();
ROLLBACK;
72 changes: 72 additions & 0 deletions pgtap/metrics/betweennessCentrality/no_crash_test.pg
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

/*PGR-GNU*****************************************************************

Copyright (c) 2024 pgRouting developers
Mail: project@pgrouting.org

------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
BEGIN;

UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
SELECT CASE WHEN NOT min_version('3.7.0') THEN plan(1) ELSE plan(7) END;

PREPARE edges_q AS
SELECT id, source, target, cost, reverse_cost FROM edges;

PREPARE null_ret AS
SELECT id FROM vertices WHERE id IN (-1);

PREPARE null_ret_arr AS
SELECT array_agg(id) FROM vertices WHERE id IN (-1);


CREATE OR REPLACE FUNCTION no_crash()
RETURNS SETOF TEXT AS
$BODY$
DECLARE
params TEXT[];
subs TEXT[];
BEGIN
IF NOT min_version('3.7.0') THEN
RETURN QUERY
SELECT skip(1, 'Function is new on 3.7.0');
RETURN;
END IF;

RETURN QUERY
SELECT isnt_empty('edges_q', 'Should be not empty to tests be meaningful');
RETURN QUERY
SELECT is_empty('null_ret', 'Should be empty to tests be meaningful');
RETURN QUERY
SELECT set_eq('null_ret_arr', 'SELECT NULL::BIGINT[]', 'Should be empty to tests be meaningful');

params = ARRAY[
'$$SELECT id, source, target, cost, reverse_cost FROM edges$$'
]::TEXT[];
subs = ARRAY[
'NULL'
]::TEXT[];

RETURN query SELECT * FROM no_crash_test('pgr_betweennessCentrality', params, subs);

END
$BODY$
LANGUAGE plpgsql VOLATILE;


SELECT * FROM no_crash();

SELECT finish();
ROLLBACK;
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ BEGIN;

SELECT plan(5);

SELECT has_function('pgr_floydwarshall');
SELECT has_function('pgr_floydwarshall', ARRAY['text','boolean']);
SELECT function_returns('pgr_floydwarshall', ARRAY['text','boolean'],'setof record');
SELECT has_function('pgr_betweennesscentrality');
SELECT has_function('pgr_betweennesscentrality', ARRAY['text','boolean']);
SELECT function_returns('pgr_betweennesscentrality', ARRAY['text','boolean'],'setof record');

SELECT set_eq(
$$SELECT proargnames from pg_proc where proname = 'pgr_floydwarshall'$$,
$$SELECT proargnames from pg_proc where proname = 'pgr_betweennesscentrality'$$,
$$VALUES
('{"","directed","start_vid","end_vid","agg_cost"}'::TEXT[])
('{"","directed","vid","centrality"}'::TEXT[])
$$);

SELECT set_eq(
$$SELECT proallargtypes from pg_proc where proname = 'pgr_floydwarshall'$$,
$$SELECT proallargtypes from pg_proc where proname = 'pgr_betweennesscentrality'$$,
$$VALUES
('{25,16,20,20,701}'::OID[])
('{25,16,20,701}'::OID[])
$$);

SELECT finish();
Expand Down

0 comments on commit a6d733f

Please sign in to comment.