Skip to content

Commit

Permalink
Flake8 and black formats checked
Browse files Browse the repository at this point in the history
  • Loading branch information
Josipmrden authored and Jure Bajic committed May 21, 2021
1 parent d5e990c commit 870b386
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[flake8]
ignore = E203, E266, E501, W503
ignore = E203, E266, E501, W503, F541
max-line-length = 120
max-complexity = 18
46 changes: 26 additions & 20 deletions memgraph/query_builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from abc import abstractmethod, ABC
from dataclasses import dataclass
from typing import Any, Dict, Iterator, List, Optional, Union

import re
Expand Down Expand Up @@ -45,16 +44,15 @@ def __init__(self):
super().__init__(message)


@dataclass
class PartialQuery(ABC):
type: str
def __init__(self, type: str):
self.type = type

@abstractmethod
def construct_query(self) -> str:
pass


@dataclass
class MatchPartialQuery(PartialQuery):
def __init__(self, optional: bool):
super().__init__(MatchTypes.MATCH)
Expand All @@ -80,31 +78,31 @@ def construct_query(self) -> str:


class NodePartialQuery(PartialQuery):
def __init__(self, variable: str, labels: str, properties: str):
def __init__(self, variable: Optional[str], labels: Optional[str], properties: Optional[str]):
super().__init__(MatchTypes.NODE)

self._variable = variable
self._labels = labels
self._properties = properties

@property
def variable(self):
def variable(self) -> str:
return self._variable if self._variable is not None else ""

@property
def labels(self):
def labels(self) -> str:
return self._labels if self._labels is not None else ""

@property
def properties(self):
def properties(self) -> str:
return self._properties if self._properties is not None else ""

def construct_query(self) -> str:
return f"({self.variable}{self.labels}{self.properties})"


class EdgePartialQuery(PartialQuery):
def __init__(self, variable: str, labels: str, properties: str, directed: bool):
def __init__(self, variable: Optional[str], labels: Optional[str], properties: Optional[str], directed: bool):
super().__init__(MatchTypes.EDGE)

self.directed = directed
Expand All @@ -113,15 +111,15 @@ def __init__(self, variable: str, labels: str, properties: str, directed: bool):
self._properties = properties

@property
def variable(self):
def variable(self) -> str:
return self._variable if self._variable is not None else ""

@property
def labels(self):
def labels(self) -> str:
return self._labels if self._labels is not None else ""

@property
def properties(self):
def properties(self) -> str:
return self._properties if self._properties is not None else ""

def construct_query(self) -> str:
Expand All @@ -135,16 +133,21 @@ def construct_query(self) -> str:


class Match:
def __init__(self, connection: Connection = None):
self._query: List[PartialQuery] = []
def __init__(self, connection: Optional[Union[Connection, Memgraph]] = None):
self._query: List[Any] = []
self._connection = connection if connection is not None else Memgraph()

def match(self, optional: bool = False) -> "Match":
self._query.append(MatchPartialQuery(optional))

return self

def node(self, labels: Union[str, List[str], None] = "", variable: Optional[str] = None, **kwargs,) -> "Match":
def node(
self,
labels: Union[str, List[str], None] = "",
variable: Optional[str] = None,
**kwargs,
) -> "Match":
labels_str = to_cypher_labels(labels)
properties_str = to_cypher_properties(kwargs)

Expand All @@ -156,7 +159,11 @@ def node(self, labels: Union[str, List[str], None] = "", variable: Optional[str]
return self

def to(
self, edge_label: Optional[str] = "", directed: Optional[bool] = True, variable: Optional[str] = None, **kwargs,
self,
edge_label: Optional[str] = "",
directed: Optional[bool] = True,
variable: Optional[str] = None,
**kwargs,
) -> "Match":

if not self._is_linking_valid_with_query(MatchTypes.EDGE):
Expand All @@ -165,7 +172,7 @@ def to(
labels_str = to_cypher_labels(edge_label)
properties_str = to_cypher_properties(kwargs)

self._query.append(EdgePartialQuery(variable, labels_str, properties_str, directed))
self._query.append(EdgePartialQuery(variable, labels_str, properties_str, bool(directed)))

return self

Expand Down Expand Up @@ -218,13 +225,12 @@ def _construct_query(self) -> str:
for partial_query in self._query:
query.append(partial_query.construct_query())

joined_query = ''.join(query)
joined_query = re.sub("\s\s+", " ", joined_query)
joined_query = "".join(query)
joined_query = re.sub("\\s\\s+", " ", joined_query)
return joined_query

def _any_variables_matched(self) -> bool:
return any(q.type in [MatchTypes.EDGE, MatchTypes.NODE] and q.variable not in [None, ""] for q in self._query)

def _is_linking_valid_with_query(self, match_type: str):
return len(self._query) == 0 or self._query[-1].type != match_type

5 changes: 2 additions & 3 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
[pytest]
addopts =
-vv --quiet --maxfail=10
#--black
#--flake8
#--mypy
--black
--flake8
timeout = 300
python_files = test_*.py
testpaths = tests memgraph
2 changes: 1 addition & 1 deletion tests/memgraph/test_query_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import MagicMock, patch
from unittest.mock import patch

import pytest

Expand Down

0 comments on commit 870b386

Please sign in to comment.