Skip to content

Introduce tox, add more tests & fix bugs #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
build:
docker:
- image: circleci/python:3.6.1

- image: redislabs/redisgraph:edge

working_directory: ~/repo
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
name: run tests
command: |
. venv/bin/activate
coverage run test.py
coverage run tests/functional/test_all.py

- early_return_for_forked_pull_requests

Expand All @@ -69,7 +69,7 @@ jobs:
# - store_artifacts:
# path: test-reports
# destination: test-reports

workflows:
version: 2
commit:
Expand All @@ -84,4 +84,4 @@ workflows:
only:
- master
jobs:
- build
- build
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ for record in result.result_set:
person_age = record[1]
visit_purpose = record[2]
country_name = record[3]

query = """MATCH p = (:person)-[:visited {purpose:"pleasure"}]->(:country) RETURN p"""

result = redis_graph.query(query)
Expand All @@ -88,3 +88,10 @@ pip install redisgraph
```
pip install git+https://github.com/RedisGraph/redisgraph-py.git@master
```

### Install for development in env

```
tox -e env
source ./tox/env/bin/activate
```
8 changes: 4 additions & 4 deletions redisgraph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .node import Node
from .edge import Edge
from .graph import Graph
from .path import Path
from .node import Node # noqa
from .edge import Edge # noqa
from .graph import Graph # noqa
from .path import Path # noqa
6 changes: 4 additions & 2 deletions redisgraph/edge.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from redisgraph import Node

from .util import *
from .util import quote_string


class Edge:
"""
An edge connecting two nodes.
"""
def __init__(self, src_node, relation, dest_node, edge_id=None, properties=None):
def __init__(self, src_node, relation, dest_node, edge_id=None,
properties=None):
"""
Create a new edge.
"""
Expand Down
2 changes: 1 addition & 1 deletion redisgraph/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

class VersionMismatchException(Exception):
def __init__(self, version):
self.version = version

10 changes: 6 additions & 4 deletions redisgraph/graph.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from .util import *
import redis
from .query_result import QueryResult
from .exceptions import VersionMismatchException

from redisgraph.util import random_string, quote_string
from redisgraph.query_result import QueryResult
from redisgraph.exceptions import VersionMismatchException


class Graph:
"""
Expand Down Expand Up @@ -173,7 +175,7 @@ def query(self, q, params=None, timeout=None, read_only=False):
return QueryResult(self, response)
except redis.exceptions.ResponseError as e:
if "wrong number of arguments" in str(e):
print ("Note: RedisGraph Python requires server version 2.2.8 or above")
print("Note: RedisGraph Python requires server version 2.2.8 or above")
raise e
except VersionMismatchException as e:
# client view over the graph schema is out of sync
Expand Down
7 changes: 4 additions & 3 deletions redisgraph/node.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .util import *
from .util import quote_string


class Node:
"""
Expand Down Expand Up @@ -36,8 +37,8 @@ def __str__(self):

def __eq__(self, rhs):
# Quick positive check, if both IDs are set.
if self.id is not None and rhs.id is not None and self.id == rhs.id:
return True
if self.id is not None and rhs.id is not None and self.id != rhs.id:
return False

# Label should match.
if self.label != rhs.label:
Expand Down
12 changes: 8 additions & 4 deletions redisgraph/path.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from .node import Node
from .edge import Edge

class Path:

class Path:
def __init__(self, nodes, edges):
assert(isinstance(nodes, list) and isinstance(edges, list))
if not (isinstance(nodes, list) and isinstance(edges, list)):
raise TypeError("nodes and edges must be list")

self._nodes = nodes
self._edges = edges
self.append_type = Node
Expand Down Expand Up @@ -38,13 +40,15 @@ def nodes_count(self):
return len(self._nodes)

def add_node(self, node):
assert(type(node) == self.append_type)
if not isinstance(node, self.append_type):
raise AssertionError("Add Edge before adding Node")
self._nodes.append(node)
self.append_type = Edge
return self

def add_edge(self, edge):
assert(type(edge) == self.append_type)
if not isinstance(edge, self.append_type):
raise AssertionError("Add Node before adding Edge")
self._edges.append(edge)
self.append_type = Node
return self
Expand Down
8 changes: 5 additions & 3 deletions redisgraph/query_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
INTERNAL_EXECUTION_TIME = 'internal execution time'

STATS = [LABELS_ADDED, NODES_CREATED, PROPERTIES_SET, RELATIONSHIPS_CREATED,
NODES_DELETED, RELATIONSHIPS_DELETED, INDICES_CREATED, INDICES_DELETED,
CACHED_EXECUTION, INTERNAL_EXECUTION_TIME]
NODES_DELETED, RELATIONSHIPS_DELETED, INDICES_CREATED, INDICES_DELETED,
CACHED_EXECUTION, INTERNAL_EXECUTION_TIME]


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


class ResultSetScalarTypes:
VALUE_UNKNOWN = 0
VALUE_NULL = 1
Expand All @@ -38,6 +40,7 @@ class ResultSetScalarTypes:
VALUE_NODE = 8
VALUE_PATH = 9


class QueryResult:

def __init__(self, graph, response):
Expand Down Expand Up @@ -288,4 +291,3 @@ def cached_execution(self):
@property
def run_time_ms(self):
return self._get_stat(INTERNAL_EXECUTION_TIME)

4 changes: 4 additions & 0 deletions redisgraph/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

__all__ = ['random_string', 'quote_string']


def random_string(length=10):
"""
Returns a random N character long string.
"""
return ''.join(random.choice(string.ascii_lowercase) for x in range(length))


def quote_string(v):
"""
RedisGraph strings must be quoted,
Expand All @@ -23,6 +25,8 @@ def quote_string(v):
if len(v) == 0:
return '""'

v = v.replace('"', '\\"')

if v[0] != '"':
v = '"' + v

Expand Down
8 changes: 8 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
flake8

pytest
pytest-cov
pytest-html

testtools>=1.4.0
mock
51 changes: 51 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# All Redis Graph client tests

To keep code working properly and looking good it's critical to cover
all functionality with unit and functional tests and use automated code style
checks.

Unit tests are tests that focus is to test line by line all code of library
where any of external dependencies need in runtime (like redisgraph) are mocked

Functional tests on other hand act like black box testing, where we make sure
that client when used with real redisgraph is working properly

Code style checks are analyzing code style, to make sure code looks like
written by one person.

Combination of these 3 approaches will ensure high quality of the code.


To launch test please use: ```tox -e func```

## Requirements to run tests locally

Redisgraph is using tox, highly popular tool that aims to automate and
standardize testing in Python.

For more details https://tox.readthedocs.io/en/latest/

To install it locally run ```pip install tox```


## Running tests locally

```bash

# To run all tests
tox

# To run py3 tests
tox -e py3

# To run code style checks
tox -e pep8

# To run unittests with coverage
tox -e cover

# To run functional test use
# Make sure that redis is running locally on standard port 6379
tox -e func

```
Empty file added tests/__init__.py
Empty file.
Empty file added tests/functional/__init__.py
Empty file.
Loading