Skip to content
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

feat: add Collection.min method #874

Merged
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
28 changes: 24 additions & 4 deletions src/masoniteorm/collection/Collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ def avg(self, key=None):
return result

def max(self, key=None):
"""Returns the average of the items.
"""Returns the max of the items.

If a key is given it will return the average of all the values of the key.
If a key is given it will return the max of all the values of the key.

Keyword Arguments:
key {string} -- The key to use to find the average of all the values of that key. (default: {None})
key {string} -- The key to use to find the max of all the values of that key. (default: {None})

Returns:
int -- Returns the average.
int -- Returns the max.
"""
result = 0
items = self._get_value(key) or self._items
Expand All @@ -109,6 +109,26 @@ def max(self, key=None):
except (TypeError, ValueError):
pass
return result

def min(self, key=None):
"""Returns the min of the items.

If a key is given it will return the min of all the values of the key.

Keyword Arguments:
key {string} -- The key to use to find the min of all the values of that key. (default: {None})

Returns:
int -- Returns the min.
"""
result = 0
items = self._get_value(key) or self._items

try:
return min(items)
except (TypeError, ValueError):
pass
return result

def chunk(self, size: int):
"""Chunks the items.
Expand Down
3 changes: 3 additions & 0 deletions src/masoniteorm/connections/MySQLConnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def rollback(self):
"""Transaction"""
self._connection.rollback()
self.transaction_level -= 1
if self.get_transaction_level() <= 0:
self.open = 0
self._connection.close()

def get_transaction_level(self):
"""Transaction"""
Expand Down
18 changes: 18 additions & 0 deletions tests/collection/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ def test_max(self):
collection = Collection([{"batch": 1}, {"batch": 1}])
self.assertEqual(collection.max("batch"), 1)

def test_min(self):
collection = Collection([1, 1, 2, 4])
self.assertEqual(collection.min(), 1)

collection = Collection(
[
{"name": "Corentin All", "age": 1},
{"name": "Corentin All", "age": 2},
{"name": "Corentin All", "age": 3},
{"name": "Corentin All", "age": 4},
]
)
self.assertEqual(collection.min("age"), 1)
self.assertEqual(collection.min(), 0)

collection = Collection([{"batch": 1}, {"batch": 1}])
self.assertEqual(collection.min("batch"), 1)

def test_count(self):
collection = Collection([1, 1, 2, 4])
self.assertEqual(collection.count(), 4)
Expand Down
Loading