Skip to content

Commit 7f6bc89

Browse files
committed
additional endpoint test
1 parent 03d648d commit 7f6bc89

File tree

6 files changed

+112
-10
lines changed

6 files changed

+112
-10
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
flask --app code_graph run --debug
2+
13
Process local git repository, ignoring specific folder(s)
24

35
curl -X POST http://127.0.0.1:5000/process_local_repo -H "Content-Type: application/json" -d '{"repo": "/Users/roilipman/Dev/FalkorDB", "ignore": ["./.github", "./sbin", "./.git","./deps", "./bin", "./build"]}'
46

5-
67
Process code coverage
78
curl -X POST http://127.0.0.1:5000/process_code_coverage -H "Content-Type: application/json" -d '{"lcov": "/Users/roilipman/Dev/code_graph/code_graph/code_coverage/lcov/falkordb.lcov", "repo": "FalkorDB"}'
89

code_graph/app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,19 @@ def find_paths():
420420
src = data.get('src')
421421
if src is None:
422422
return jsonify({'status': f'Missing mandatory parameter "src"'}), 400
423+
if not isinstance(src, int):
424+
return jsonify({'status': "src node id must be int"}), 400
423425

424426
# Validate 'dest' parameter
425427
dest = data.get('dest')
426428
if dest is None:
427429
return jsonify({'status': f'Missing mandatory parameter "dest"'}), 400
430+
if not isinstance(dest, int):
431+
return jsonify({'status': "dest node id must be int"}), 400
432+
433+
if not graph_exists(repo):
434+
logging.error(f"Missing project {repo}")
435+
return jsonify({"status": f"Missing project {repo}"}), 400
428436

429437
# Initialize graph with provided repo and credentials
430438
g = Graph(repo)

code_graph/graph.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,18 @@ def find_paths(self, src: int, dest: int) -> List[Path]:
722722

723723
# Extract paths from the query result set.
724724
for row in result_set:
725-
paths.append(row[0])
725+
path = []
726+
p = row[0]
727+
nodes = p.nodes()
728+
edges = p.edges()
729+
730+
for n, e in zip(nodes, edges):
731+
path.append(encode_node(n))
732+
path.append(encode_edge(e))
733+
734+
# encode last node on path
735+
path.append(encode_node(nodes[-1]))
736+
paths.append(path)
726737

727738
return paths
728739

tests/endpoints/test_find_paths.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import os
2+
import redis
3+
import pytest
4+
from pathlib import Path
5+
from code_graph import create_app, Project
6+
from falkordb import FalkorDB, Path, Node, QueryResult
7+
8+
@pytest.fixture()
9+
def app():
10+
app = create_app()
11+
app.config.update({
12+
"TESTING": True,
13+
})
14+
15+
# other setup can go here
16+
redis.Redis().flushall()
17+
18+
yield app
19+
20+
# clean up / reset resources here
21+
22+
@pytest.fixture()
23+
def client(app):
24+
return app.test_client()
25+
26+
@pytest.fixture()
27+
def runner(app):
28+
return app.test_cli_runner()
29+
30+
def test_find_paths(client):
31+
# Start with an empty DB
32+
response = client.post("/find_paths", json={"repo": "GraphRAG-SDK", "src": 0, "dest": 0}).json
33+
status = response["status"]
34+
35+
# Expecting an error
36+
assert status == "Missing project GraphRAG-SDK"
37+
38+
# Process Git repository
39+
proj = Project.from_git_repository("https://github.com/FalkorDB/GraphRAG-SDK")
40+
proj.analyze_sources()
41+
42+
# Re-issue with invalid src node id
43+
response = client.post("/find_paths", json={"repo": "GraphRAG-SDK", "src": 'invalid', "dest": 0}).json
44+
status = response["status"]
45+
assert status == "src node id must be int"
46+
47+
# Re-issue with invalid dest node id
48+
response = client.post("/find_paths", json={"repo": "GraphRAG-SDK", "src": 0, "dest": 'invalid'}).json
49+
status = response["status"]
50+
assert status == "dest node id must be int"
51+
52+
# Find src and dest nodes that are at least 3 hops apart
53+
db = FalkorDB(host=os.getenv('FALKORDB_HOST', 'localhost'),
54+
port=os.getenv('FALKORDB_PORT', 6379),
55+
username=os.getenv('FALKORDB_USERNAME', None),
56+
password=os.getenv('FALKORDB_PASSWORD', None))
57+
g = db.select_graph("GraphRAG-SDK")
58+
q = """MATCH (a:Function)-[:CALLS*3..5]->(b:Function)
59+
RETURN ID(a), ID(b)
60+
LIMIT 1"""
61+
62+
result_set = g.query(q).result_set
63+
src_id = result_set[0][0]
64+
dest_id = result_set[0][1]
65+
66+
# Re-issue with none existing node id
67+
response = client.post("/find_paths", json={
68+
"repo": "GraphRAG-SDK",
69+
"src": src_id,
70+
"dest": dest_id}).json
71+
72+
status = response["status"]
73+
paths = response["paths"]
74+
75+
for p in paths:
76+
assert p[0]['id'] == src_id
77+
assert p[-1]['id'] == dest_id
78+
assert len(p) % 2 == 1
79+
assert len(p) >= 3
80+

tests/test_c_analyzer.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import unittest
33
from pathlib import Path
44

5-
from code_graph import SourceAnalyzer, File, Struct, Function
5+
from code_graph import SourceAnalyzer, File, Struct, Function, Graph
66

77
class Test_C_Analyzer(unittest.TestCase):
88
def test_analyzer(self):
@@ -20,10 +20,11 @@ def test_analyzer(self):
2020
path = os.path.join(path, 'c')
2121
path = str(path)
2222

23-
g = analyzer.analyze_local_folder(path)
23+
g = Graph("c")
24+
analyzer.analyze(path, g)
2425

25-
f = g.get_file('.', 'src.c', '.c')
26-
self.assertEqual(File('.', 'src.c', '.c'), f)
26+
f = g.get_file('', 'src.c', '.c')
27+
self.assertEqual(File('', 'src.c', '.c'), f)
2728

2829
s = g.get_struct_by_name('exp')
2930
expected_s = Struct('src.c', 'exp', '', 9, 13)

tests/test_py_analyzer.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import unittest
33
from pathlib import Path
44

5-
from code_graph import SourceAnalyzer, File, Class, Function
5+
from code_graph import SourceAnalyzer, File, Class, Function, Graph
66

77
class Test_PY_Analyzer(unittest.TestCase):
88
def test_analyzer(self):
@@ -20,10 +20,11 @@ def test_analyzer(self):
2020
path = os.path.join(path, 'py')
2121
path = str(path)
2222

23-
g = analyzer.analyze_local_folder(str(path))
23+
g = Graph("py")
24+
analyzer.analyze(path, g)
2425

25-
f = g.get_file('.', 'src.py', '.py')
26-
self.assertEqual(File('.', 'src.py', '.py'), f)
26+
f = g.get_file('', 'src.py', '.py')
27+
self.assertEqual(File('', 'src.py', '.py'), f)
2728

2829
log = g.get_function_by_name('log')
2930
expected_log = Function('src.py', 'log', None, 'None', '', 0, 1)

0 commit comments

Comments
 (0)