Skip to content

Commit

Permalink
Operator database connection improvements (#53)
Browse files Browse the repository at this point in the history
* added not-equal operator to DataBaseModelAttributes. Corrected insert_many response on exception

* corrected connection cleanup within __aexit__

* added tests for not equal operator

* added tests for not equal operator

---------

Co-authored-by: Joshua (codemation) <[email protected]>
  • Loading branch information
codemation and Joshua (codemation) authored Apr 3, 2023
1 parent 658e9f8 commit 6b7d19e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
10 changes: 9 additions & 1 deletion pydbantic/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@ def __eq__(self, value) -> DataBaseModelCondition:
(values,),
)

def __ne__(self, value) -> DataBaseModelCondition:
values = self.process_value(value)
return DataBaseModelCondition(
f"{self.name} != {values}",
self.column != self.process_value(value),
(values,),
)

def inside(self, choices: List[Any]) -> DataBaseModelCondition:
choices = [self.process_value(value) for value in choices]
return DataBaseModelCondition(
Expand Down Expand Up @@ -1936,7 +1944,7 @@ async def insert_many(cls: Type[T], rows: List[T]) -> Optional[int]:
except Exception:
database.log.exception(f"chain link insertion error")

return result
return None

async def _insert(
self, return_links=False
Expand Down
2 changes: 1 addition & 1 deletion pydbantic/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ async def __aexit__(self, exc_type, exc, tb):
if not self.connection_map[conn_id]["conn"].ag_running:
if time.time() - self.connection_map[conn_id]["last"] > 120:
try:
await self.connection_map[conn_id].asend("finished")
await self.connection_map[conn_id]["conn"].asend("finished")
except StopAsyncIteration:
pass
del self.connection_map[conn_id]
7 changes: 6 additions & 1 deletion tests/test_querying.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def test_querying(loaded_database_and_model_with_cache):
assert emp_with_salary[0].salary == 40000

manager_position = emp_with_salary[0].position
# breakpoint()

# filter on manager positions
managers = await Employee.filter(
position=manager_position[0],
Expand Down Expand Up @@ -56,3 +56,8 @@ async def test_querying(loaded_database_and_model_with_cache):

ranged_salary = await Employee.filter(Employee.salary.inside([40000, 10000, 30000]))
assert len(ranged_salary) == 1

# filter using not equal operator

employees = await Employee.filter(Employee.employee_id != managers[0].employee_id)
assert len(employees) == 19

0 comments on commit 6b7d19e

Please sign in to comment.