From 378b8c37c1b8768350795cbfa59f6c29ba6d0861 Mon Sep 17 00:00:00 2001 From: Felipe Hertzer Date: Fri, 23 Feb 2024 19:36:51 +1100 Subject: [PATCH 1/4] Mysql close the connection on rollback --- src/masoniteorm/connections/MySQLConnection.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/masoniteorm/connections/MySQLConnection.py b/src/masoniteorm/connections/MySQLConnection.py index 386145db..39419650 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""" From 6f44e9bdde0e762825c52d4a384c945ccf25adaf Mon Sep 17 00:00:00 2001 From: Amir Hosein Salimi Date: Sun, 3 Mar 2024 23:22:43 +0330 Subject: [PATCH 2/4] feat: add `Collection.min` method --- src/masoniteorm/collection/Collection.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/masoniteorm/collection/Collection.py b/src/masoniteorm/collection/Collection.py index 44d84f34..6516c629 100644 --- a/src/masoniteorm/collection/Collection.py +++ b/src/masoniteorm/collection/Collection.py @@ -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. From 61749ccb8be348e44cc62c70c386ec427e09460d Mon Sep 17 00:00:00 2001 From: Amir Hosein Salimi Date: Sun, 3 Mar 2024 23:16:30 +0330 Subject: [PATCH 3/4] fix: fix a typo in docs of `Collection.max` --- src/masoniteorm/collection/Collection.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/masoniteorm/collection/Collection.py b/src/masoniteorm/collection/Collection.py index 44d84f34..8b408327 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 From 3bc3406378a82dd22d92e0864dd6dd860e97e194 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Mon, 11 Mar 2024 21:09:11 -0400 Subject: [PATCH 4/4] added min test --- tests/collection/test_collection.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/collection/test_collection.py b/tests/collection/test_collection.py index 70666654..02bab4cf 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)