diff --git a/src/masoniteorm/collection/Collection.py b/src/masoniteorm/collection/Collection.py index 44d84f349..5a064acc5 100644 --- a/src/masoniteorm/collection/Collection.py +++ b/src/masoniteorm/collection/Collection.py @@ -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 @@ -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. diff --git a/src/masoniteorm/connections/MySQLConnection.py b/src/masoniteorm/connections/MySQLConnection.py index 386145db6..39419650e 100644 --- a/src/masoniteorm/connections/MySQLConnection.py +++ b/src/masoniteorm/connections/MySQLConnection.py @@ -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""" diff --git a/tests/collection/test_collection.py b/tests/collection/test_collection.py index 706666542..02bab4cf1 100644 --- a/tests/collection/test_collection.py +++ b/tests/collection/test_collection.py @@ -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)