Skip to content

Commit

Permalink
Remove to_dict and make Q object support the dict constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiHaghverdi committed Oct 27, 2023
1 parent c9246ee commit 97b77d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions aggify/aggify.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __getitem__(self, index):
def filter(self, arg=None, **kwargs):
if arg:
if isinstance(arg, Q):
self.pipelines.append(arg.to_dict())
self.pipelines.append(dict(arg))
else:
raise ValueError(f"Invalid Q object")
else:
Expand Down Expand Up @@ -277,23 +277,23 @@ class Q:
def __init__(self, **conditions):
self.conditions = Aggify(None).match(matches=conditions.items()).get('$match')

def to_dict(self):
return {"$match": self.conditions}
def __iter__(self):
yield '$match', self.conditions

def __or__(self, other):
if self.conditions.get("$or", None):
self.conditions["$or"].append(other.to_dict()["$match"])
self.conditions["$or"].append(dict(other)["$match"])
combined_conditions = self.conditions
else:
combined_conditions = {"$or": [self.conditions, other.to_dict()["$match"]]}
combined_conditions = {"$or": [self.conditions, dict(other)["$match"]]}
return Q(**combined_conditions)

def __and__(self, other):
if self.conditions.get("$and", None):
self.conditions["$and"].append(other.to_dict()["$match"])
self.conditions["$and"].append(dict(other)["$match"])
combined_conditions = self.conditions
else:
combined_conditions = {"$and": [self.conditions, other.to_dict()["$match"]]}
combined_conditions = {"$and": [self.conditions, dict(other)["$match"]]}
return Q(**combined_conditions)

def __invert__(self):
Expand Down
16 changes: 8 additions & 8 deletions tests/test_q.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ def test_or_operator_with_multiple_conditions(self):
q1 = Q(name="John")
q2 = Q(name="Alice")
q_combined = q1 | q2
assert q_combined.to_dict() == {
"$match": {"$or": [q1.to_dict()["$match"], q2.to_dict()["$match"]]}}
assert dict(q_combined) == {
"$match": {"$or": [dict(q1)["$match"], dict(q2)["$match"]]}}

def test_or_operator_with_multiple_conditions_more_than_rwo(self):
q1 = Q(name="John")
q2 = Q(name="Alice")
q3 = Q(name="Bob")
q_combined = q1 | q2 | q3
assert q_combined.to_dict() == {
"$match": {"$or": [q1.to_dict()["$match"], q2.to_dict()["$match"], q3.to_dict()["$match"]]}}
assert dict(q_combined) == {
"$match": {"$or": [dict(q1)["$match"], dict(q2)["$match"], dict(q3)["$match"]]}}

# Test combining NOT operators with AND
def test_combine_not_operators_with_and(self):
q1 = Q(name="John")
q2 = Q(age__lt=30)
q_combined = ~q1 & ~q2
assert q_combined.to_dict() == {
"$match": {"$and": [{"$not": [q1.to_dict()["$match"]]}, {"$not": [q2.to_dict()["$match"]]}]}}
assert dict(q_combined) == {
"$match": {"$and": [{"$not": [dict(q1)["$match"]]}, {"$not": [dict(q2)["$match"]]}]}}

# Test combining NOT operators with OR
def test_combine_not_operators_with_or(self):
q1 = Q(name="John")
q2 = Q(age__lt=30)
q_combined = ~q1 | ~q2 # Changed | to combine OR
assert q_combined.to_dict() == {
"$match": {"$or": [{"$not": [q1.to_dict()["$match"]]}, {"$not": [q2.to_dict()["$match"]]}]}}
assert dict(q_combined) == {
"$match": {"$or": [{"$not": [dict(q1)["$match"]]}, {"$not": [dict(q2)["$match"]]}]}}

0 comments on commit 97b77d8

Please sign in to comment.