Skip to content

Commit 2bd540a

Browse files
authored
Introduce tox, add more tests & fix bugs (#114)
* Move functional tests to own directory * Fix code style (PEP8) issues * Introduce tox and implement tests for Graph, Node & Path Plaese refer to tests/README.md This patch as well: - Improves readme regarding to testing - Fixes found bugs - Replaces asserts
1 parent 3a1355c commit 2bd540a

24 files changed

+449
-53
lines changed

.circleci/config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
build:
2121
docker:
2222
- image: circleci/python:3.6.1
23-
23+
2424
- image: redislabs/redisgraph:edge
2525

2626
working_directory: ~/repo
@@ -56,7 +56,7 @@ jobs:
5656
name: run tests
5757
command: |
5858
. venv/bin/activate
59-
coverage run test.py
59+
coverage run tests/functional/test_all.py
6060
6161
- early_return_for_forked_pull_requests
6262

@@ -69,7 +69,7 @@ jobs:
6969
# - store_artifacts:
7070
# path: test-reports
7171
# destination: test-reports
72-
72+
7373
workflows:
7474
version: 2
7575
commit:
@@ -84,4 +84,4 @@ workflows:
8484
only:
8585
- master
8686
jobs:
87-
- build
87+
- build

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ for record in result.result_set:
6161
person_age = record[1]
6262
visit_purpose = record[2]
6363
country_name = record[3]
64-
64+
6565
query = """MATCH p = (:person)-[:visited {purpose:"pleasure"}]->(:country) RETURN p"""
6666

6767
result = redis_graph.query(query)
@@ -88,3 +88,10 @@ pip install redisgraph
8888
```
8989
pip install git+https://github.com/RedisGraph/redisgraph-py.git@master
9090
```
91+
92+
### Install for development in env
93+
94+
```
95+
tox -e env
96+
source ./tox/env/bin/activate
97+
```

redisgraph/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .node import Node
2-
from .edge import Edge
3-
from .graph import Graph
4-
from .path import Path
1+
from .node import Node # noqa
2+
from .edge import Edge # noqa
3+
from .graph import Graph # noqa
4+
from .path import Path # noqa

redisgraph/edge.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from redisgraph import Node
22

3-
from .util import *
3+
from .util import quote_string
4+
45

56
class Edge:
67
"""
78
An edge connecting two nodes.
89
"""
9-
def __init__(self, src_node, relation, dest_node, edge_id=None, properties=None):
10+
def __init__(self, src_node, relation, dest_node, edge_id=None,
11+
properties=None):
1012
"""
1113
Create a new edge.
1214
"""

redisgraph/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1+
12
class VersionMismatchException(Exception):
23
def __init__(self, version):
34
self.version = version
4-

redisgraph/graph.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from .util import *
21
import redis
3-
from .query_result import QueryResult
4-
from .exceptions import VersionMismatchException
2+
3+
from redisgraph.util import random_string, quote_string
4+
from redisgraph.query_result import QueryResult
5+
from redisgraph.exceptions import VersionMismatchException
6+
57

68
class Graph:
79
"""
@@ -173,7 +175,7 @@ def query(self, q, params=None, timeout=None, read_only=False):
173175
return QueryResult(self, response)
174176
except redis.exceptions.ResponseError as e:
175177
if "wrong number of arguments" in str(e):
176-
print ("Note: RedisGraph Python requires server version 2.2.8 or above")
178+
print("Note: RedisGraph Python requires server version 2.2.8 or above")
177179
raise e
178180
except VersionMismatchException as e:
179181
# client view over the graph schema is out of sync

redisgraph/node.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from .util import *
1+
from .util import quote_string
2+
23

34
class Node:
45
"""
@@ -36,8 +37,8 @@ def __str__(self):
3637

3738
def __eq__(self, rhs):
3839
# Quick positive check, if both IDs are set.
39-
if self.id is not None and rhs.id is not None and self.id == rhs.id:
40-
return True
40+
if self.id is not None and rhs.id is not None and self.id != rhs.id:
41+
return False
4142

4243
# Label should match.
4344
if self.label != rhs.label:

redisgraph/path.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from .node import Node
22
from .edge import Edge
33

4-
class Path:
54

5+
class Path:
66
def __init__(self, nodes, edges):
7-
assert(isinstance(nodes, list) and isinstance(edges, list))
7+
if not (isinstance(nodes, list) and isinstance(edges, list)):
8+
raise TypeError("nodes and edges must be list")
9+
810
self._nodes = nodes
911
self._edges = edges
1012
self.append_type = Node
@@ -38,13 +40,15 @@ def nodes_count(self):
3840
return len(self._nodes)
3941

4042
def add_node(self, node):
41-
assert(type(node) == self.append_type)
43+
if not isinstance(node, self.append_type):
44+
raise AssertionError("Add Edge before adding Node")
4245
self._nodes.append(node)
4346
self.append_type = Edge
4447
return self
4548

4649
def add_edge(self, edge):
47-
assert(type(edge) == self.append_type)
50+
if not isinstance(edge, self.append_type):
51+
raise AssertionError("Add Node before adding Edge")
4852
self._edges.append(edge)
4953
self.append_type = Node
5054
return self

redisgraph/query_result.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717
INTERNAL_EXECUTION_TIME = 'internal execution time'
1818

1919
STATS = [LABELS_ADDED, NODES_CREATED, PROPERTIES_SET, RELATIONSHIPS_CREATED,
20-
NODES_DELETED, RELATIONSHIPS_DELETED, INDICES_CREATED, INDICES_DELETED,
21-
CACHED_EXECUTION, INTERNAL_EXECUTION_TIME]
20+
NODES_DELETED, RELATIONSHIPS_DELETED, INDICES_CREATED, INDICES_DELETED,
21+
CACHED_EXECUTION, INTERNAL_EXECUTION_TIME]
22+
2223

2324
class ResultSetColumnTypes:
2425
COLUMN_UNKNOWN = 0
2526
COLUMN_SCALAR = 1
2627
COLUMN_NODE = 2 # Unused as of RedisGraph v2.1.0, retained for backwards compatibility.
2728
COLUMN_RELATION = 3 # Unused as of RedisGraph v2.1.0, retained for backwards compatibility.
2829

30+
2931
class ResultSetScalarTypes:
3032
VALUE_UNKNOWN = 0
3133
VALUE_NULL = 1
@@ -38,6 +40,7 @@ class ResultSetScalarTypes:
3840
VALUE_NODE = 8
3941
VALUE_PATH = 9
4042

43+
4144
class QueryResult:
4245

4346
def __init__(self, graph, response):
@@ -288,4 +291,3 @@ def cached_execution(self):
288291
@property
289292
def run_time_ms(self):
290293
return self._get_stat(INTERNAL_EXECUTION_TIME)
291-

redisgraph/util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
__all__ = ['random_string', 'quote_string']
55

6+
67
def random_string(length=10):
78
"""
89
Returns a random N character long string.
910
"""
1011
return ''.join(random.choice(string.ascii_lowercase) for x in range(length))
1112

13+
1214
def quote_string(v):
1315
"""
1416
RedisGraph strings must be quoted,
@@ -23,6 +25,8 @@ def quote_string(v):
2325
if len(v) == 0:
2426
return '""'
2527

28+
v = v.replace('"', '\\"')
29+
2630
if v[0] != '"':
2731
v = '"' + v
2832

0 commit comments

Comments
 (0)