Skip to content

Commit

Permalink
testing-delete one and delete bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
CrispenGari committed Jan 31, 2024
1 parent fb7abe4 commit 64b6f3a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
10 changes: 6 additions & 4 deletions orm/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,12 @@ def delete_bulk(self, instance: Model, filters: dict = {}):
return affected_rows

def delete_one(self, instance: Model, filters: dict = {}):
sql, params = instance._get_delete_where_stm(filters)
affected_rows = self._execute_sql(
sql, args=params, affected_rows=True, fetchall=True
)
pk = None
for name, field in inspect.getmembers(instance):
if isinstance(field, PrimaryKeyColumn):
pk = name
sql, params = instance._get_delete_where_stm(pk=pk, args=filters)
affected_rows = self._execute_sql(sql, args=params, affected_rows=True)
return affected_rows

def delete_by_pk(self, instance: Model, pk):
Expand Down
5 changes: 2 additions & 3 deletions orm/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def _get_select_where_stm(cls, fields: list = [], args: dict = {}):
return sql, fields, params

@classmethod
def _get_delete_where_stm(cls, args: dict = {}):
def _get_delete_where_stm(cls, pk: str = "id", args: dict = {}):
params = []
filters = []
for key, value in args.items():
Expand All @@ -276,8 +276,7 @@ def _get_delete_where_stm(cls, args: dict = {}):
)
else:
sql = Statements.DELETE_ONE_WHERE_COMMAND.format(
table_name=cls._get_name(),
filters=" AND ".join(filters),
table_name=cls._get_name(), filters=" AND ".join(filters), pk=pk
)
return sql, params

Expand Down
6 changes: 5 additions & 1 deletion orm/model/statements.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
class Statements:
# delete
DELETE_BY_PK = "DELETE FROM {table_name} WHERE {pk_name} = {pk};"
DELETE_ONE_WHERE_COMMAND = "DELETE FROM {table_name} WHERE {filters};"
DELETE_ONE_WHERE_COMMAND = """
DELETE FROM {table_name} WHERE {pk} = (
SELECT {pk} FROM {table_name} WHERE {filters} LIMIT 1
);
"""
DELETE_BULK_WHERE_COMMAND = "DELETE FROM {table_name} WHERE {filters};"
DELETE_ALL_COMMAND = "DELETE FROM {table_name};"
# select
Expand Down
39 changes: 25 additions & 14 deletions orm/tests/test_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ class User(Model):
User(name="Crispen", username="heyy"),
]
)
affected_rows_1 = db.delete_one(User, {"name": "Crispen"})
affected_rows_2 = db.delete_one(User, {"name": "Crispen", "id": 9})
affected_rows_3 = db.delete_one(User, {"name": "Crispen", "id": 2})

assert affected_rows_1 == 3
assert affected_rows_3 == 0
assert affected_rows_2 == 0
db.delete_one(User, {"name": "Crispen"})
rows_1 = db.find_many(User, {"name": "Crispen"})
db.delete_one(User, {"name": "Crispen", "id": 9})
rows_2 = db.find_many(User, {"name": "Crispen"})
db.delete_one(User, {"name": "Crispen", "id": 2})
rows_3 = db.find_many(User, {"name": "Crispen"})
assert len(rows_1) == 2
assert len(rows_2) == 2
assert len(rows_3) == 1
conn.close()

def test_delete_bulk_fn(self):
Expand All @@ -81,11 +83,20 @@ class User(Model):
User(name="Crispen", username="heyy"),
]
)
affected_rows_1 = db.delete_one(User, {"name": "Crispen"})
affected_rows_2 = db.delete_one(User, {"name": "Crispen", "id": 9})
affected_rows_3 = db.delete_one(User, {"name": "Crispen", "id": 2})

assert affected_rows_1 == 3
assert affected_rows_3 == 0
assert affected_rows_2 == 0
db.delete_bulk(User, {"name": "Crispen"})
rows_1 = db.find_many(User, {"name": "Crispen"})
db.commit_bulk(
[
User(name="Crispen", username="heyy"),
User(name="Crispen", username="heyy"),
User(name="Crispen", username="heyy"),
]
)
db.delete_bulk(User, {"name": "Crispen", "id": 99})
rows_2 = db.find_many(User, {"name": "Crispen"})
db.delete_bulk(User, {"name": "Crispen", "id": 5})
rows_3 = db.find_many(User, {"name": "Crispen"})
assert len(rows_1) == 0
assert len(rows_2) == 3
assert len(rows_3) == 2
conn.close()
3 changes: 2 additions & 1 deletion playground.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def to_dict(self):
# Post(userId=1, title="What are you thinking"),
# )

now = db.delete_bulk(User, {"name": "Crispen"})
now = db.delete_one(User, {"name": "Crispen"})
print(now)


# print(f"now: {now}")
Expand Down

0 comments on commit 64b6f3a

Please sign in to comment.