11import unittest
2+ from pathlib import Path
23from falkordb import FalkorDB
3- from typing import List , Optional
4- from api import *
4+ from api import Graph
5+ from api . entities import File
56
67
78class TestGraphOps (unittest .TestCase ):
@@ -11,50 +12,60 @@ def setUp(self):
1112 self .graph = Graph (name = 'test' )
1213
1314 def test_add_function (self ):
14- # Create function
15- func = Function ('/path/to/function' , 'func' , '' , 'int' , '' , 1 , 10 )
16- func .add_argument ('x' , 'int' )
17- func .add_argument ('y' , 'float' )
18-
19- self .graph .add_function (func )
20- self .assertEqual (func , self .graph .get_function (func .id ))
15+ func_id = self .graph .add_entity (
16+ 'Function' , 'func' , '' , '/path/to/function' , 1 , 10 ,
17+ {'ret_type' : 'int' , 'src' : '' , 'args' : [['x' , 'int' ], ['y' , 'float' ]]}
18+ )
19+ result = self .graph .get_function (func_id )
20+ self .assertIsNotNone (result )
21+ self .assertEqual (result .properties ['name' ], 'func' )
22+ self .assertEqual (result .properties ['ret_type' ], 'int' )
23+ self .assertEqual (result .properties ['args' ], [['x' , 'int' ], ['y' , 'float' ]])
2124
2225 def test_add_file (self ):
23- file = File ('/path/to/file' , 'file' , 'txt' )
24-
26+ file = File (Path ('/path/to/file.txt' ), None )
2527 self .graph .add_file (file )
26- self .assertEqual (file , self .graph .get_file ('/path/to/file' , 'file' , 'txt' ))
28+ result = self .graph .get_file ('/path/to/file.txt' , 'file.txt' , '.txt' )
29+ self .assertIsNotNone (result )
30+ self .assertEqual (result .properties ['name' ], 'file.txt' )
31+ self .assertEqual (result .properties ['ext' ], '.txt' )
2732
2833 def test_file_add_function (self ):
29- file = File ('/path/to/file' , 'file' , 'txt' )
30- func = Function ('/path/to/function' , 'func' , '' , 'int' , '' , 1 , 10 )
31-
34+ file = File (Path ('/path/to/file.txt' ), None )
3235 self .graph .add_file (file )
33- self .graph .add_function (func )
3436
35- self .graph .connect_entities ("CONTAINS" , file .id , func .id )
37+ func_id = self .graph .add_entity (
38+ 'Function' , 'func' , '' , '/path/to/function' , 1 , 10 ,
39+ {'ret_type' : 'int' , 'src' : '' , 'args' : []}
40+ )
41+
42+ self .graph .connect_entities ("CONTAINS" , file .id , func_id )
3643
3744 query = """MATCH (file:File)-[:CONTAINS]->(func:Function)
3845 WHERE ID(func) = $func_id AND ID(file) = $file_id
3946 RETURN true"""
4047
41- params = {'file_id' : file .id , 'func_id' : func . id }
48+ params = {'file_id' : file .id , 'func_id' : func_id }
4249 res = self .g .query (query , params ).result_set
4350 self .assertTrue (res [0 ][0 ])
4451
4552 def test_function_calls_function (self ):
46- caller = Function ('/path/to/function' , 'func_A' , '' , 'int' , '' , 1 , 10 )
47- callee = Function ('/path/to/function' , 'func_B' , '' , 'int' , '' , 11 , 21 )
53+ caller_id = self .graph .add_entity (
54+ 'Function' , 'func_A' , '' , '/path/to/function' , 1 , 10 ,
55+ {'ret_type' : 'int' , 'src' : '' , 'args' : []}
56+ )
57+ callee_id = self .graph .add_entity (
58+ 'Function' , 'func_B' , '' , '/path/to/function' , 11 , 21 ,
59+ {'ret_type' : 'int' , 'src' : '' , 'args' : []}
60+ )
4861
49- self .graph .add_function (caller )
50- self .graph .add_function (callee )
51- self .graph .function_calls_function (caller .id , callee .id , 10 )
62+ self .graph .function_calls_function (caller_id , callee_id , 10 )
5263
5364 query = """MATCH (caller:Function)-[:CALLS]->(callee:Function)
5465 WHERE ID(caller) = $caller_id AND ID(callee) = $callee_id
5566 RETURN true"""
5667
57- params = {'caller_id' : caller . id , 'callee_id' : callee . id }
68+ params = {'caller_id' : caller_id , 'callee_id' : callee_id }
5869 res = self .g .query (query , params ).result_set
5970 self .assertTrue (res [0 ][0 ])
6071
0 commit comments