From 03f58e4ec0c776a66883c9e6a68774e357155624 Mon Sep 17 00:00:00 2001 From: Tigo-cmd Date: Wed, 7 Aug 2024 16:55:44 +0100 Subject: [PATCH 1/3] now a contributor of the repo --- AUTHORS | 3 ++- README.md | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 64b26acdc14..e676f6f7507 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,4 +3,5 @@ Jennifer Huang <133@holbertonschool.com> Alexa Orrico <210@holbertonschool.com> -Joann Vuong <130@holbertonschool.com> +Joann Vuong <130@holbertonschool.com> +Nwali Ugonna Emmanuel diff --git a/README.md b/README.md index f1d72de6355..94d890ceb6e 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,8 @@ No known bugs at this time. Alexa Orrico - [Github](https://github.com/alexaorrico) / [Twitter](https://twitter.com/alexa_orrico) Jennifer Huang - [Github](https://github.com/jhuang10123) / [Twitter](https://twitter.com/earthtojhuang) +Nwali Ugonna Emmanuel - [Github](https://github.com/Tigo-cmd) + Second part of Airbnb: Joann Vuong ## License Public Domain. No copy write protection. From 5caa83fac18d6980908d7d5d46c857eb6c89a7e2 Mon Sep 17 00:00:00 2001 From: Tigo-cmd Date: Wed, 7 Aug 2024 17:09:37 +0100 Subject: [PATCH 2/3] added get and count methods [test included] --- models/engine/db_storage.py | 18 ++++++++++ models/engine/file_storage.py | 18 ++++++++++ .../test_engine/test_db_storage.py | 33 +++++++++++++++++++ .../test_engine/test_file_storage.py | 33 +++++++++++++++++++ 4 files changed, 102 insertions(+) diff --git a/models/engine/db_storage.py b/models/engine/db_storage.py index b8e7d291e6f..f7ff64c669c 100755 --- a/models/engine/db_storage.py +++ b/models/engine/db_storage.py @@ -74,3 +74,21 @@ def reload(self): def close(self): """call remove() method on the private session attribute""" self.__session.remove() + + def get(self, cls, id) -> __class__ | int | None: + """returns the object based on it's class and ID, of None if not found + Args: + cls: class to be returned + id: string representing the object ID class to be returned and none if not exists + """ + if cls in classes.values(): + return self.__session.query(cls).filter(cls.id == id).first() + return None + + def count(self, cls=None): + """Counts the number of objects in storage andReturns the number of objects, + in storage matching the given class. If no class is passed, returns the count of all objects in storage. + Args: + cls: class (optional) + """ + return len(self.all(cls)) diff --git a/models/engine/file_storage.py b/models/engine/file_storage.py index c8cb8c1764d..ed19cbcbad8 100755 --- a/models/engine/file_storage.py +++ b/models/engine/file_storage.py @@ -68,3 +68,21 @@ def delete(self, obj=None): def close(self): """call reload() method for deserializing the JSON file to objects""" self.reload() + + def get(self, cls, id) -> __class__ | int | None: + """returns the object based on it's class and ID, of None if not found + Args: + cls: class to be returned + id: string representing the object ID class to be returned and none if not exists + """ + if cls in classes.values(): + return self.__session.query(cls).filter(cls.id == id).first() + return None + + def count(self, cls=None): + """Counts the number of objects in storage andReturns the number of objects, + in storage matching the given class. If no class is passed, returns the count of all objects in storage. + Args: + cls: class (optional) + """ + return len(self.all(cls)) diff --git a/tests/test_models/test_engine/test_db_storage.py b/tests/test_models/test_engine/test_db_storage.py index 766e625b5af..b8bb7c69d68 100755 --- a/tests/test_models/test_engine/test_db_storage.py +++ b/tests/test_models/test_engine/test_db_storage.py @@ -86,3 +86,36 @@ def test_new(self): @unittest.skipIf(models.storage_t != 'db', "not testing db storage") def test_save(self): """Test that save properly saves objects to file.json""" + + @unittest.skipIf(models.storage_t != 'db', "not testing db storage") + def test_get(self, cls, id): + """tests the get method properly fro all functionalities""" + models.storage.reload() + user = User(email="emmanwaliugo@gmail.com", password="123") + models.storage.new(user) + models.storage.save() + user_id = user.id + bad_id = "666" + self.assertTrue(models.storage.get(User, user_id) is user) + self.assertIsNone(models.storage.get(User, bad_id)) + models.storage.delete(user) + state = State(name="Abia") + models.storage.new(state) + models.storage.save() + state_id = State.id + bad_id = "sharwarma" + self.assertTrue(models.storage.get(State, state_id) is state) + self.assertIsNone(models.storage.get(State, bad_id)) + + @unittest.skipIf(models.storage_t != 'db', "not testing db storage") + def test_count(self): + """test the count id it returns accurate number of objects""" + models.storage.reload() + state = State(name="Abakaliki") + user = User(email="ja161612040@gmail.com", password="234") + models.storage.new(state) + models.storage.new(user) + models.storage.save() + self.assertEqual(models.storage.count(), 2) + models.storage.delete(user) + models.storage.delete(state) diff --git a/tests/test_models/test_engine/test_file_storage.py b/tests/test_models/test_engine/test_file_storage.py index 1474a34fec0..05f996a7445 100755 --- a/tests/test_models/test_engine/test_file_storage.py +++ b/tests/test_models/test_engine/test_file_storage.py @@ -113,3 +113,36 @@ def test_save(self): with open("file.json", "r") as f: js = f.read() self.assertEqual(json.loads(string), json.loads(js)) + + @unittest.skipIf(models.storage_t != 'db', "not testing db storage") + def test_get(self, cls, id): + """tests the get method properly fro all functionalities""" + models.storage.reload() + user = User(email="emmanwaliugo@gmail.com", password="123") + models.storage.new(user) + models.storage.save() + user_id = user.id + bad_id = "666" + self.assertTrue(models.storage.get(User, user_id) is user) + self.assertIsNone(models.storage.get(User, bad_id)) + models.storage.delete(user) + state = State(name="Abia") + models.storage.new(state) + models.storage.save() + state_id = State.id + bad_id = "sharwarma" + self.assertTrue(models.storage.get(State, state_id) is state) + self.assertIsNone(models.storage.get(State, bad_id)) + + @unittest.skipIf(models.storage_t != 'db', "not testing db storage") + def test_count(self): + """test the count id it returns accurate number of objects""" + models.storage.reload() + state = State(name="Abakaliki") + user = User(email="ja161612040@gmail.com", password="234") + models.storage.new(state) + models.storage.new(user) + models.storage.save() + self.assertEqual(models.storage.count(), 2) + models.storage.delete(user) + models.storage.delete(state) From 55cc8c03127f94a893f357b342248cc2be2f24ef Mon Sep 17 00:00:00 2001 From: Tigo-cmd Date: Wed, 7 Aug 2024 17:42:42 +0100 Subject: [PATCH 3/3] added module doc --- models/engine/db_storage.py | 2 +- models/engine/file_storage.py | 2 +- tests/test_models/test_engine/test_db_storage.py | 6 ++++-- tests/test_models/test_engine/test_file_storage.py | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/models/engine/db_storage.py b/models/engine/db_storage.py index f7ff64c669c..b191d51a759 100755 --- a/models/engine/db_storage.py +++ b/models/engine/db_storage.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 """ -Contains the class DBStorage +This Module Contains the class DBStorage """ import models diff --git a/models/engine/file_storage.py b/models/engine/file_storage.py index ed19cbcbad8..94e7c3b1595 100755 --- a/models/engine/file_storage.py +++ b/models/engine/file_storage.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 """ -Contains the FileStorage class +This Module Contains the FileStorage class for all functionality """ import json diff --git a/tests/test_models/test_engine/test_db_storage.py b/tests/test_models/test_engine/test_db_storage.py index b8bb7c69d68..0dc66ad3f34 100755 --- a/tests/test_models/test_engine/test_db_storage.py +++ b/tests/test_models/test_engine/test_db_storage.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 """ -Contains the TestDBStorageDocs and TestDBStorage classes +This TestModule Contains the TestDBStorageDocs and TestDBStorage classes """ from datetime import datetime @@ -89,7 +89,8 @@ def test_save(self): @unittest.skipIf(models.storage_t != 'db', "not testing db storage") def test_get(self, cls, id): - """tests the get method properly fro all functionalities""" + """tests the get method properly for all functionalities""" + models.storage.reload() user = User(email="emmanwaliugo@gmail.com", password="123") models.storage.new(user) @@ -110,6 +111,7 @@ def test_get(self, cls, id): @unittest.skipIf(models.storage_t != 'db', "not testing db storage") def test_count(self): """test the count id it returns accurate number of objects""" + models.storage.reload() state = State(name="Abakaliki") user = User(email="ja161612040@gmail.com", password="234") diff --git a/tests/test_models/test_engine/test_file_storage.py b/tests/test_models/test_engine/test_file_storage.py index 05f996a7445..19468d5407b 100755 --- a/tests/test_models/test_engine/test_file_storage.py +++ b/tests/test_models/test_engine/test_file_storage.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 """ -Contains the TestFileStorageDocs classes +This testModule Contains the TestFileStorageDocs classes for functionalities """ from datetime import datetime